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