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