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