Skip to main content

asterust_junos/policy_options/
defaults.rs

1//! `defaults`
2//! `policy-options { defaults { ... } }` and `policy-options { policy-statement <name> { defaults { ... } } }`.
3//!
4//! # See also
5//!
6//! - [defaults (Policy Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/defaults-edit-policy-options.html)
7
8use crate::{active_children, split_keyword, statements};
9use asterust::config::ConfigLine;
10
11/// The `defaults` of `policy-options` or of one policy statement.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct PolicyDefaults {
15    /// `"optimize"` or `"no-optimize"`.
16    pub asregex_optimize: Option<String>,
17    /// `"walkup"` or `"no-walkup"`.
18    pub route_filter: Option<String>,
19}
20
21/// The `defaults` of `line`, or `None` if it has none.
22pub(super) fn parse_defaults(line: &ConfigLine) -> Option<PolicyDefaults> {
23    if !active_children(line).any(|c| c.text == "defaults" || c.arg("defaults").is_some()) {
24        return None;
25    }
26    let mut defaults = PolicyDefaults::default();
27    for statement in statements(line, "defaults") {
28        let (keyword, value) = split_keyword(statement);
29        let value = Some(value.to_string()).filter(|v| !v.is_empty());
30        match keyword {
31            "asregex-optimize" => defaults.asregex_optimize = value,
32            "route-filter" => defaults.route_filter = value,
33            _ => {}
34        }
35    }
36    Some(defaults)
37}
38
39#[cfg(test)]
40mod tests {
41    use crate::policy_options::{policy_options, PolicyDefaults};
42    use asterust::config::juniper;
43
44    // Adapted from the Routing Policies, Firewall Filters, and Traffic Policers User Guide, Understanding the Route
45    // Filter Walkup Policy Option: the `defaults` of the `RouteFilter-A` policy; see the README "Sources".
46    #[test]
47    fn reads_the_guide_walkup_default() {
48        let config = juniper::parse_braces(
49            "policy-options {\n    policy-statement RouteFilter-A {\n        defaults {\n            route-filter walkup;\n        }\n        term RouteFilter-1 {\n            from {\n                route-filter 10.0.0.0/16 prefix-length-range /22-/24;\n            }\n            then accept;\n        }\n    }\n}\n",
50        );
51        let options = policy_options(&config).unwrap();
52        let defaults = options.policy_statements[0].defaults.as_ref().unwrap();
53        assert_eq!(defaults.route_filter.as_deref(), Some("walkup"));
54        assert_eq!(defaults.asregex_optimize, None);
55        assert_eq!(options.defaults, None);
56    }
57
58    // Not from documentation: the global form and `asregex-optimize` of the defaults CLI Reference page.
59    #[test]
60    fn reads_the_global_defaults() {
61        let config = juniper::parse_braces(
62            "policy-options {\n    defaults {\n        asregex-optimize no-optimize;\n        route-filter no-walkup;\n    }\n}\n",
63        );
64        assert_eq!(
65            policy_options(&config).unwrap().defaults,
66            Some(PolicyDefaults {
67                asregex_optimize: Some("no-optimize".to_string()),
68                route_filter: Some("no-walkup".to_string()),
69            })
70        );
71    }
72}