Skip to main content

asterust_junos/routing_options/
interface_routes.rs

1//! `interface-routes`
2//! `routing-options { interface-routes { family <family> { ... } rib-group <name>; } }`.
3//!
4//! # See also
5//!
6//! - [interface-routes](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-routes-edit-routing-options.html)
7
8use crate::{arg_of, child_block, has_leaf, named_children};
9use asterust::config::ConfigLine;
10
11/// A `family` of `interface-routes`.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct InterfaceRoutesFamily {
15    /// `inet` or `inet6`.
16    pub name: String,
17    /// `export { lan; }`.
18    pub export_lan: bool,
19    /// `export { point-to-point; }`.
20    pub export_point_to_point: bool,
21    pub import_primary: Option<String>,
22}
23
24/// The `interface-routes` settings that control which interface routes are installed in which tables.
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct InterfaceRoutes {
28    /// The `rib-group` argument as written, e.g. `"MY-GROUP"` or `"inet MY-GROUP"`.
29    pub rib_group: Option<String>,
30    pub families: Vec<InterfaceRoutesFamily>,
31}
32
33pub(super) fn parse_interface_routes(routes: &ConfigLine) -> InterfaceRoutes {
34    InterfaceRoutes {
35        rib_group: arg_of(routes, "rib-group").map(str::to_string),
36        families: named_children(routes, "family")
37            .map(|(name, family)| {
38                let export = child_block(family, "export");
39                InterfaceRoutesFamily {
40                    name: name.to_string(),
41                    export_lan: export.is_some_and(|export| has_leaf(export, "lan")),
42                    export_point_to_point: export.is_some_and(|export| has_leaf(export, "point-to-point")),
43                    import_primary: arg_of(family, "import-primary").map(str::to_string),
44                }
45            })
46            .collect(),
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use crate::routing_options::routing_options;
53    use asterust::config::juniper;
54
55    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
56    #[test]
57    fn extracts_interface_routes() {
58        let options = routing_options(&juniper::parse_braces(
59            r#"
60routing-options {
61    interface-routes {
62        rib-group inet IFL-GROUP;
63        family inet {
64            export {
65                lan;
66                point-to-point;
67            }
68            import-primary PRIMARY-ONLY;
69        }
70        family inet6 {
71            export {
72                lan;
73            }
74        }
75    }
76}
77"#,
78        ))
79        .unwrap();
80        let routes = options.interface_routes.unwrap();
81        assert_eq!(routes.rib_group.as_deref(), Some("inet IFL-GROUP"));
82        assert_eq!(routes.families.len(), 2);
83
84        let inet = &routes.families[0];
85        assert_eq!(inet.name, "inet");
86        assert!(inet.export_lan);
87        assert!(inet.export_point_to_point);
88        assert_eq!(inet.import_primary.as_deref(), Some("PRIMARY-ONLY"));
89
90        let inet6 = &routes.families[1];
91        assert_eq!(inet6.name, "inet6");
92        assert!(inet6.export_lan);
93        assert!(!inet6.export_point_to_point);
94        assert_eq!(inet6.import_primary, None);
95    }
96
97    #[test]
98    fn no_interface_routes_is_none() {
99        let options = routing_options(&juniper::parse_braces(
100            "routing-options {\n    router-id 192.0.2.1;\n}\n",
101        ))
102        .unwrap();
103        assert!(options.interface_routes.is_none());
104    }
105}