Skip to main content

asterust_junos/routing_options/
auto_export.rs

1//! `auto-export`
2//! `routing-options { auto-export { family <family> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [auto-export](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/auto-export-edit-routing-options.html)
7
8use crate::{arg_of, child_block, has_leaf, named_children};
9use asterust::config::ConfigLine;
10
11/// The `unicast`, `multicast` or `flow` setting of an `auto-export` family.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct AutoExportScope {
15    pub disable: bool,
16    pub rib_group: Option<String>,
17}
18
19/// A `family` of `auto-export`. `traceoptions` is not extracted.
20#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct AutoExportFamily {
23    /// `inet`, `inet6` or `iso`.
24    pub name: String,
25    pub disable: bool,
26    pub unicast: Option<AutoExportScope>,
27    pub multicast: Option<AutoExportScope>,
28    pub flow: Option<AutoExportScope>,
29}
30
31/// The `auto-export` settings for sharing routes between routing instances.
32#[derive(Clone, Debug, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct AutoExport {
35    pub disable: bool,
36    pub families: Vec<AutoExportFamily>,
37}
38
39pub(super) fn parse_auto_export(auto_export: &ConfigLine) -> AutoExport {
40    AutoExport {
41        disable: has_leaf(auto_export, "disable"),
42        families: named_children(auto_export, "family")
43            .map(|(name, family)| {
44                let scope = |keyword: &str| {
45                    child_block(family, keyword).map(|scope| AutoExportScope {
46                        disable: has_leaf(scope, "disable"),
47                        rib_group: arg_of(scope, "rib-group").map(str::to_string),
48                    })
49                };
50                AutoExportFamily {
51                    name: name.to_string(),
52                    disable: has_leaf(family, "disable"),
53                    unicast: scope("unicast"),
54                    multicast: scope("multicast"),
55                    flow: scope("flow"),
56                }
57            })
58            .collect(),
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use crate::routing_options::routing_options;
65    use asterust::config::juniper;
66
67    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
68    #[test]
69    fn extracts_auto_export_families() {
70        let options = routing_options(&juniper::parse_braces(
71            r#"
72routing-options {
73    auto-export {
74        family inet {
75            unicast {
76                rib-group EXPORT-GROUP;
77            }
78            multicast {
79                disable;
80            }
81        }
82        family inet6 {
83            disable;
84        }
85    }
86}
87"#,
88        ))
89        .unwrap();
90        let auto_export = options.auto_export.unwrap();
91        assert!(!auto_export.disable);
92        assert_eq!(auto_export.families.len(), 2);
93
94        let inet = &auto_export.families[0];
95        assert_eq!(inet.name, "inet");
96        assert!(!inet.disable);
97        assert_eq!(
98            inet.unicast.as_ref().unwrap().rib_group.as_deref(),
99            Some("EXPORT-GROUP")
100        );
101        assert!(inet.multicast.as_ref().unwrap().disable);
102        assert!(inet.flow.is_none());
103
104        assert!(auto_export.families[1].disable);
105    }
106
107    #[test]
108    fn bare_auto_export_enables_with_defaults() {
109        let options = routing_options(&juniper::parse_braces("routing-options {\n    auto-export;\n}\n")).unwrap();
110        let auto_export = options.auto_export.unwrap();
111        assert!(!auto_export.disable);
112        assert!(auto_export.families.is_empty());
113    }
114
115    #[test]
116    fn no_auto_export_is_none() {
117        let options = routing_options(&juniper::parse_braces(
118            "routing-options {\n    router-id 192.0.2.1;\n}\n",
119        ))
120        .unwrap();
121        assert!(options.auto_export.is_none());
122    }
123}