Skip to main content

asterust_junos/protocols/isis/
traffic_engineering.rs

1//! `traffic-engineering`
2//! `protocols { isis { traffic-engineering { ... } } }`.
3//!
4//! # See also
5//!
6//! - [traffic-engineering (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traffic-engineering-edit-protocols-isis.html)
7//! - [family (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/family-edit-protocols-isis.html)
8//! - [advertisement](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/protocols-isis-traffic-engineering-advertisement.html)
9
10use crate::{named_children, split_name, statement_words};
11use asterust::config::ConfigLine;
12
13/// The IS-IS `traffic-engineering` statement. A bare `traffic-engineering;` leaves every field unset.
14/// `tunnel-source-protocol` and the application-specific advertisement settings are not extracted.
15#[derive(Clone, Debug, Default, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct IsisTrafficEngineering {
18    pub disable: bool,
19    pub credibility_protocol_preference: bool,
20    pub ignore_lsp_metrics: bool,
21    pub ipv6: bool,
22    pub l3_unicast_topology: bool,
23    /// An `advertisement` block is configured.
24    pub advertisement: bool,
25    /// `advertisement { always; }`.
26    pub advertisement_always: bool,
27    /// A `multipath` block is configured.
28    pub multipath: bool,
29    /// `multipath { lsp-equal-cost; }`.
30    pub multipath_lsp_equal_cost: bool,
31    pub families: Vec<IsisTeFamily>,
32}
33
34/// A `family <inet | inet-mpls | inet6 | inet6-mpls>` of `traffic-engineering`.
35#[derive(Clone, Debug, PartialEq, Eq)]
36#[non_exhaustive]
37pub struct IsisTeFamily {
38    pub name: String,
39    pub shortcuts: bool,
40    /// `shortcuts { multicast-rpf-routes; }`, valid for `inet`.
41    pub multicast_rpf_routes: bool,
42}
43
44pub(super) fn parse_traffic_engineering(isis: &ConfigLine) -> Option<IsisTrafficEngineering> {
45    let words = statement_words(isis, "traffic-engineering")?;
46    let te = crate::child_block(isis, "traffic-engineering");
47    let advertisement = te.and_then(|te| statement_words(te, "advertisement"));
48    let multipath = te.and_then(|te| statement_words(te, "multipath"));
49    Some(IsisTrafficEngineering {
50        disable: words.contains(&"disable"),
51        credibility_protocol_preference: words.contains(&"credibility-protocol-preference"),
52        ignore_lsp_metrics: words.contains(&"ignore-lsp-metrics"),
53        ipv6: words.contains(&"ipv6"),
54        l3_unicast_topology: words.contains(&"l3-unicast-topology"),
55        advertisement: advertisement.is_some(),
56        advertisement_always: advertisement.is_some_and(|words| words.contains(&"always")),
57        multipath: multipath.is_some(),
58        multipath_lsp_equal_cost: multipath.is_some_and(|words| words.contains(&"lsp-equal-cost")),
59        families: te.map(parse_families).unwrap_or_default(),
60    })
61}
62
63fn parse_families(te: &ConfigLine) -> Vec<IsisTeFamily> {
64    named_children(te, "family")
65        .map(|(arg, family)| {
66            let (name, rest) = split_name(arg);
67            let inline = rest.split_whitespace().collect::<Vec<_>>();
68            let shortcuts = statement_words(family, "shortcuts");
69            let has =
70                |flag: &str| inline.contains(&flag) || shortcuts.as_ref().is_some_and(|words| words.contains(&flag));
71            IsisTeFamily {
72                name: name.to_string(),
73                shortcuts: has("shortcuts") || shortcuts.is_some(),
74                multicast_rpf_routes: has("multicast-rpf-routes"),
75            }
76        })
77        .collect()
78}
79
80#[cfg(test)]
81mod tests {
82    use crate::protocols::isis::isis;
83    use asterust::config::juniper;
84
85    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
86    const CONFIG: &str = r#"
87protocols {
88    isis {
89        traffic-engineering {
90            credibility-protocol-preference;
91            ignore-lsp-metrics;
92            ipv6;
93            l3-unicast-topology;
94            advertisement {
95                always;
96            }
97            multipath {
98                lsp-equal-cost;
99            }
100            family inet {
101                shortcuts {
102                    multicast-rpf-routes;
103                }
104            }
105            family inet6 {
106                shortcuts;
107            }
108            family inet6-mpls;
109        }
110    }
111}
112"#;
113
114    #[test]
115    fn reads_the_flags_and_blocks() {
116        let te = isis(&juniper::parse_braces(CONFIG))
117            .unwrap()
118            .traffic_engineering
119            .unwrap();
120        assert!(te.credibility_protocol_preference);
121        assert!(te.ignore_lsp_metrics);
122        assert!(te.ipv6);
123        assert!(te.l3_unicast_topology);
124        assert!(te.advertisement);
125        assert!(te.advertisement_always);
126        assert!(te.multipath);
127        assert!(te.multipath_lsp_equal_cost);
128        assert!(!te.disable);
129    }
130
131    #[test]
132    fn reads_the_families() {
133        let te = isis(&juniper::parse_braces(CONFIG))
134            .unwrap()
135            .traffic_engineering
136            .unwrap();
137        let families: Vec<_> = te
138            .families
139            .iter()
140            .map(|family| (family.name.as_str(), family.shortcuts, family.multicast_rpf_routes))
141            .collect();
142        assert_eq!(
143            families,
144            [
145                ("inet", true, true),
146                ("inet6", true, false),
147                ("inet6-mpls", false, false)
148            ]
149        );
150    }
151
152    #[test]
153    fn reads_the_bare_form_and_absence() {
154        let bare = juniper::parse_braces("protocols {\n    isis {\n        traffic-engineering;\n    }\n}\n");
155        let te = isis(&bare).unwrap().traffic_engineering.unwrap();
156        assert!(te.families.is_empty());
157        assert!(!te.multipath);
158
159        let none = juniper::parse_braces("protocols {\n    isis {\n        interface ge-0/0/0.0;\n    }\n}\n");
160        assert_eq!(isis(&none).unwrap().traffic_engineering, None);
161    }
162}