Skip to main content

asterust_junos/firewall/
prefix_action.rs

1//! `prefix-action`
2//! `firewall { family inet { prefix-action <name> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [prefix-action (Configuring)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/prefix-action-edit-firewall.html)
7//! - [prefix-action (Firewall Filter Action)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/prefix-action-edit-firewall-family-filter-term-then.html)
8
9use crate::{arg_of, has_leaf, named_children};
10use asterust::config::ConfigLine;
11
12/// A `prefix-action <name>`, which polices or counts per source or destination prefix.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct PrefixAction {
16    /// The `family` it is under, `"inet"`.
17    pub family: String,
18    pub name: String,
19    pub count: bool,
20    pub destination_prefix_length: Option<String>,
21    pub filter_specific: bool,
22    pub policer: Option<String>,
23    pub source_prefix_length: Option<String>,
24    pub subnet_prefix_length: Option<String>,
25}
26
27/// The `prefix-action` entries under each `family` of `firewall`.
28pub(super) fn prefix_actions(firewall: &ConfigLine) -> Vec<PrefixAction> {
29    named_children(firewall, "family")
30        .flat_map(|(family, family_line)| {
31            named_children(family_line, "prefix-action")
32                .map(move |(name, line)| parse_prefix_action(family, name, line))
33        })
34        .collect()
35}
36
37fn parse_prefix_action(family: &str, name: &str, line: &ConfigLine) -> PrefixAction {
38    let field = |keyword: &str| arg_of(line, keyword).map(str::to_string);
39    PrefixAction {
40        family: family.to_string(),
41        name: name.to_string(),
42        count: has_leaf(line, "count"),
43        destination_prefix_length: field("destination-prefix-length"),
44        filter_specific: has_leaf(line, "filter-specific"),
45        policer: field("policer"),
46        source_prefix_length: field("source-prefix-length"),
47        subnet_prefix_length: field("subnet-prefix-length"),
48    }
49}