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