asterust_junos/protocols/isis/
traffic_engineering.rs1use crate::{named_children, split_name, statement_words};
11use asterust::config::ConfigLine;
12
13#[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 pub advertisement: bool,
25 pub advertisement_always: bool,
27 pub multipath: bool,
29 pub multipath_lsp_equal_cost: bool,
31 pub families: Vec<IsisTeFamily>,
32}
33
34#[derive(Clone, Debug, PartialEq, Eq)]
36#[non_exhaustive]
37pub struct IsisTeFamily {
38 pub name: String,
39 pub shortcuts: bool,
40 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 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}