Skip to main content

asterust_junos/routing_options/
resolution.rs

1//! `resolution`
2//! `routing-options { resolution { rib <name> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [resolution](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resolution-edit-routing-options.html)
7//! - [rib (Route Resolution)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rib-edit-routing-options-resolution.html)
8
9use crate::{args_of, has_leaf, list_values, named_children, toggle};
10use asterust::config::ConfigLine;
11
12/// A `rib <name>` of `resolution`: where next-hop resolution looks for routes to this table.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct ResolutionRib {
16    pub name: String,
17    pub import: Vec<String>,
18    pub resolution_ribs: Vec<String>,
19}
20
21/// The `resolution` settings. `scheme`, `tracefilter` and `traceoptions` are not extracted.
22#[derive(Clone, Debug, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct Resolution {
25    pub ribs: Vec<ResolutionRib>,
26    /// `Some(true)` for `allow-expanded-over-compressed`, `Some(false)` for the `no-` form.
27    pub allow_expanded_over_compressed: Option<bool>,
28    pub prefer_recursive_primary_path: Option<bool>,
29    pub preserve_nexthop_hierarchy: Option<bool>,
30    pub disable_auto_resolve_on_scheme_changes: bool,
31    pub no_policy_metric_for_non_recursive_routes: bool,
32}
33
34pub(super) fn parse_resolution(resolution: &ConfigLine) -> Resolution {
35    Resolution {
36        ribs: named_children(resolution, "rib")
37            .map(|(name, rib)| ResolutionRib {
38                name: name.to_string(),
39                import: args_of(rib, "import").into_iter().flat_map(list_values).collect(),
40                resolution_ribs: args_of(rib, "resolution-ribs")
41                    .into_iter()
42                    .flat_map(list_values)
43                    .collect(),
44            })
45            .collect(),
46        allow_expanded_over_compressed: toggle(resolution, "allow-expanded-over-compressed"),
47        prefer_recursive_primary_path: toggle(resolution, "prefer-recursive-primary-path"),
48        preserve_nexthop_hierarchy: toggle(resolution, "preserve-nexthop-hierarchy"),
49        disable_auto_resolve_on_scheme_changes: has_leaf(resolution, "disable-auto-resolve-on-scheme-changes"),
50        no_policy_metric_for_non_recursive_routes: has_leaf(resolution, "no-policy-metric-for-non-recursive-routes"),
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use crate::routing_options::routing_options;
57    use asterust::config::juniper;
58
59    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
60    #[test]
61    fn extracts_resolution_ribs_and_flags() {
62        let options = routing_options(&juniper::parse_braces(
63            r#"
64routing-options {
65    resolution {
66        rib inet.3 {
67            resolution-ribs [ inet.0 inet.3 ];
68            import RESOLVE-POLICY;
69        }
70        rib inet6.3 {
71            resolution-ribs inet6.0;
72        }
73        allow-expanded-over-compressed;
74        no-prefer-recursive-primary-path;
75        disable-auto-resolve-on-scheme-changes;
76    }
77}
78"#,
79        ))
80        .unwrap();
81        let resolution = options.resolution.unwrap();
82        assert_eq!(resolution.ribs.len(), 2);
83        assert_eq!(resolution.ribs[0].name, "inet.3");
84        assert_eq!(resolution.ribs[0].resolution_ribs, ["inet.0", "inet.3"]);
85        assert_eq!(resolution.ribs[0].import, ["RESOLVE-POLICY"]);
86        assert_eq!(resolution.ribs[1].resolution_ribs, ["inet6.0"]);
87        assert!(resolution.ribs[1].import.is_empty());
88        assert_eq!(resolution.allow_expanded_over_compressed, Some(true));
89        assert_eq!(resolution.prefer_recursive_primary_path, Some(false));
90        assert_eq!(resolution.preserve_nexthop_hierarchy, None);
91        assert!(resolution.disable_auto_resolve_on_scheme_changes);
92        assert!(!resolution.no_policy_metric_for_non_recursive_routes);
93    }
94
95    #[test]
96    fn no_resolution_is_none() {
97        let options = routing_options(&juniper::parse_braces(
98            "routing-options {\n    router-id 192.0.2.1;\n}\n",
99        ))
100        .unwrap();
101        assert!(options.resolution.is_none());
102    }
103}