asterust_junos/interfaces/
mod.rs1mod aggregated_ether_options;
17mod auto_configure;
18mod ether_options;
19mod hold_time;
20mod link_degrade_monitor;
21mod optics_options;
22mod otn_options;
23mod tca;
24mod unit;
25
26pub use aggregated_ether_options::{AggregatedEtherOptions, BfdLivenessDetection, Lacp};
27pub use auto_configure::{AutoConfigure, AutoConfigureProfile, AutoConfigureRanges};
28pub use ether_options::{EtherOptions, Ieee8023ad};
29pub use hold_time::HoldTime;
30pub use link_degrade_monitor::{LinkDegradeMonitor, LinkDegradeThresholds};
31pub use optics_options::{OpticsApplication, OpticsLowLight, OpticsOptions};
32pub use otn_options::{OduDelayManagement, OtnOptions, OtnRate, OtnTrigger, PreemptiveFastReroute, SignalDegrade};
33pub use tca::Tca;
34pub use unit::{
35 AddressArp, BandwidthThreshold, FamilyFilter, FamilyPolicer, FamilySampling, InterfaceUnit, RpfCheck,
36 TrackedInterface, TrackedRoute, UnitAddress, UnitFamily, VlanMap, VlanTags, VrrpGroup, VrrpInheritFrom, VrrpTrack,
37};
38
39use crate::{active_children, arg_of, child_block, has_leaf, named_children, top_level};
40use aggregated_ether_options::parse_aggregated_ether_options;
41use asterust::config::{Config, ConfigLine};
42use auto_configure::parse_auto_configure;
43use ether_options::parse_ether_options;
44use hold_time::parse_hold_time;
45use link_degrade_monitor::parse_link_degrade_monitor;
46use optics_options::parse_optics_options;
47use otn_options::parse_otn_options;
48use unit::parse_unit;
49
50#[derive(Clone, Debug, PartialEq, Eq)]
52#[non_exhaustive]
53pub struct Interface {
54 pub name: String,
55 pub description: Option<String>,
56 pub disabled: bool,
58 pub mtu: Option<String>,
59 pub speed: Option<String>,
61 pub encapsulation: Option<String>,
63 pub vlan_tagging: bool,
64 pub flexible_vlan_tagging: bool,
65 pub native_vlan_id: Option<String>,
66 pub per_unit_scheduler: bool,
67 pub no_gratuitous_arp_request: bool,
68 pub hold_time: Option<HoldTime>,
69 pub ether_options: Option<EtherOptions>,
70 pub aggregated_ether_options: Option<AggregatedEtherOptions>,
71 pub auto_configure: Option<AutoConfigure>,
72 pub optics_options: Option<OpticsOptions>,
73 pub otn_options: Option<OtnOptions>,
74 pub link_degrade_monitor: Option<LinkDegradeMonitor>,
75 pub units: Vec<InterfaceUnit>,
76}
77
78pub fn interfaces(config: &Config) -> Vec<Interface> {
92 let Some(root) = top_level(config, "interfaces") else {
93 return Vec::new();
94 };
95 active_children(root)
97 .filter(|c| !c.text.contains(' '))
98 .map(parse_interface)
99 .collect()
100}
101
102fn parse_interface(line: &ConfigLine) -> Interface {
103 Interface {
104 name: line.text.clone(),
105 description: arg_of(line, "description").map(str::to_string),
106 disabled: has_leaf(line, "disable"),
107 mtu: arg_of(line, "mtu").map(str::to_string),
108 speed: arg_of(line, "speed").map(str::to_string),
109 encapsulation: arg_of(line, "encapsulation").map(str::to_string),
110 vlan_tagging: has_leaf(line, "vlan-tagging"),
111 flexible_vlan_tagging: has_leaf(line, "flexible-vlan-tagging"),
112 native_vlan_id: arg_of(line, "native-vlan-id").map(str::to_string),
113 per_unit_scheduler: has_leaf(line, "per-unit-scheduler"),
114 no_gratuitous_arp_request: has_leaf(line, "no-gratuitous-arp-request"),
115 hold_time: parse_hold_time(line),
116 ether_options: parse_ether_options(line),
117 aggregated_ether_options: child_block(line, "aggregated-ether-options").map(parse_aggregated_ether_options),
118 auto_configure: child_block(line, "auto-configure").map(parse_auto_configure),
119 optics_options: child_block(line, "optics-options").map(parse_optics_options),
120 otn_options: child_block(line, "otn-options").map(parse_otn_options),
121 link_degrade_monitor: child_block(line, "link-degrade-monitor").map(parse_link_degrade_monitor),
122 units: named_children(line, "unit")
123 .map(|(number, unit)| parse_unit(number, unit))
124 .collect(),
125 }
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131 use asterust::config::juniper;
132
133 const CONFIG: &str = r#"
135interfaces {
136 ge-0/0/0 {
137 description "to p1";
138 unit 0 {
139 description "core link";
140 family inet {
141 address 10.0.1.1/30;
142 address 10.0.2.1/30 {
143 primary;
144 }
145 }
146 family inet6 {
147 address 2001:db8:1::1/64;
148 }
149 family iso;
150 family mpls;
151 }
152 }
153 ge-0/0/1 {
154 disable;
155 vlan-tagging;
156 unit 100 {
157 vlan-id 100;
158 family inet {
159 address 192.0.2.1/24;
160 }
161 }
162 }
163 inactive: ge-0/0/2 {
164 unit 0;
165 }
166 lo0 {
167 unit 0 {
168 family inet {
169 address 10.255.0.1/32;
170 }
171 family iso {
172 address 49.0001.0100.2550.0001.00;
173 }
174 }
175 }
176 apply-groups GLOBAL;
177}
178"#;
179
180 #[test]
181 fn extracts_units_families_and_addresses() {
182 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
183 let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
184 assert_eq!(names, ["ge-0/0/0", "ge-0/0/1", "lo0"]);
185
186 let core = &ifaces[0];
187 assert_eq!(core.description.as_deref(), Some("to p1"));
188 assert!(!core.disabled);
189 let unit = &core.units[0];
190 assert_eq!(unit.number, "0");
191 assert_eq!(unit.description.as_deref(), Some("core link"));
192 assert_eq!(unit.families, ["inet", "inet6", "iso", "mpls"]);
193 assert_eq!(unit.inet_addresses, ["10.0.1.1/30", "10.0.2.1/30"]);
194 assert_eq!(unit.inet6_addresses, ["2001:db8:1::1/64"]);
195 assert!(unit.iso_addresses.is_empty());
196 }
197
198 #[test]
199 fn disabled_vlan_units_and_loopback_net() {
200 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
201 assert!(ifaces[1].disabled);
202 assert_eq!(ifaces[1].units[0].number, "100");
203 assert_eq!(ifaces[1].units[0].vlan_id, Some(100));
204 assert_eq!(ifaces[2].units[0].iso_addresses, ["49.0001.0100.2550.0001.00"]);
205 }
206
207 #[test]
208 fn missing_interfaces_block_is_empty() {
209 assert!(interfaces(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_empty());
210 }
211
212 #[test]
214 fn extracts_physical_interface_settings() {
215 let ifaces = interfaces(&juniper::parse_braces(
216 r#"
217interfaces {
218 xe-0/0/0 {
219 mtu 9192;
220 speed 10g;
221 encapsulation flexible-ethernet-services;
222 vlan-tagging;
223 flexible-vlan-tagging;
224 native-vlan-id 10;
225 per-unit-scheduler;
226 no-gratuitous-arp-request;
227 }
228 inactive: xe-0/0/1 {
229 mtu 1500;
230 }
231 xe-0/0/2 {
232 inactive: mtu 1500;
233 }
234}
235"#,
236 ));
237 assert_eq!(ifaces.len(), 2);
238
239 let xe0 = &ifaces[0];
240 assert_eq!(xe0.mtu.as_deref(), Some("9192"));
241 assert_eq!(xe0.speed.as_deref(), Some("10g"));
242 assert_eq!(xe0.encapsulation.as_deref(), Some("flexible-ethernet-services"));
243 assert!(xe0.vlan_tagging);
244 assert!(xe0.flexible_vlan_tagging);
245 assert_eq!(xe0.native_vlan_id.as_deref(), Some("10"));
246 assert!(xe0.per_unit_scheduler);
247 assert!(xe0.no_gratuitous_arp_request);
248
249 assert_eq!(ifaces[1].mtu, None);
250 }
251
252 #[test]
253 fn physical_interface_settings_default_to_off() {
254 let ifaces = interfaces(&juniper::parse_braces(
255 "interfaces {\n ge-0/0/0 {\n disable;\n }\n}\n",
256 ));
257 let ge = &ifaces[0];
258 assert_eq!(ge.mtu, None);
259 assert_eq!(ge.speed, None);
260 assert_eq!(ge.encapsulation, None);
261 assert!(!ge.vlan_tagging);
262 assert!(!ge.flexible_vlan_tagging);
263 assert_eq!(ge.native_vlan_id, None);
264 assert!(!ge.per_unit_scheduler);
265 assert!(!ge.no_gratuitous_arp_request);
266 }
267
268 const DOC_POINT_TO_POINT: &str = r#"
272interfaces {
273 ge-1/2/0 {
274 unit 0 {
275 description to-A;
276 family inet {
277 address 10.10.10.1/30;
278 }
279 }
280 }
281 ge-0/0/1 {
282 unit 5 {
283 description to-B;
284 family inet {
285 address 10.10.10.5/30;
286 }
287 }
288 }
289}
290"#;
291
292 const DOC_LINK_LOCAL: &str = r#"
295interfaces {
296 ge-0/0/1 {
297 description R1-to-R2-Link;
298 vlan-tagging;
299 unit 1 {
300 vlan-id 1;
301 family inet6 {
302 address fe80::10/64;
303 }
304 }
305 unit 2 {
306 vlan-id 2;
307 family inet6 {
308 address fe80::10/64;
309 }
310 }
311 }
312 lo0 {
313 unit 0 {
314 family inet {
315 address 198.51.100.1/24 {
316 primary;
317 }
318 }
319 }
320 }
321}
322"#;
323
324 #[test]
325 fn doc_point_to_point_interfaces() {
326 let ifaces = interfaces(&juniper::parse_braces(DOC_POINT_TO_POINT));
327 let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
328 assert_eq!(names, ["ge-1/2/0", "ge-0/0/1"]);
329 assert_eq!(ifaces[0].units[0].number, "0");
330 assert_eq!(ifaces[0].units[0].description.as_deref(), Some("to-A"));
331 assert_eq!(ifaces[0].units[0].inet_addresses, ["10.10.10.1/30"]);
332 assert_eq!(ifaces[1].units[0].number, "5");
333 assert_eq!(ifaces[1].units[0].inet_addresses, ["10.10.10.5/30"]);
334 }
335
336 #[test]
337 fn doc_link_local_units_vlans_and_primary_address() {
338 let ifaces = interfaces(&juniper::parse_braces(DOC_LINK_LOCAL));
339 let ge = &ifaces[0];
340 assert_eq!(ge.description.as_deref(), Some("R1-to-R2-Link"));
341 assert_eq!(ge.units.len(), 2);
342 assert_eq!(ge.units[0].vlan_id, Some(1));
343 assert_eq!(ge.units[1].vlan_id, Some(2));
344 assert_eq!(ge.units[0].inet6_addresses, ["fe80::10/64"]);
345
346 let lo0 = &ifaces[1];
347 assert_eq!(lo0.units[0].inet_addresses, ["198.51.100.1/24"]);
348 }
349}