Skip to main content

asterust_junos/
interface.rs

1//! `interfaces { ... }`: physical interfaces and their logical units.
2
3use crate::{active_children, arg_of, args_of, has_leaf, named_children, top_level};
4use asterust::config::{Config, ConfigLine};
5
6/// A logical `unit <n>` of an interface. Addresses are the raw `address` values (e.g. `"10.0.0.1/24"`;
7/// IS-IS NETs for `family iso`).
8#[derive(Clone, Debug, PartialEq, Eq)]
9#[non_exhaustive]
10pub struct InterfaceUnit {
11    pub number: String,
12    pub description: Option<String>,
13    pub vlan_id: Option<u32>,
14    /// Every `family <name>` present, e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
15    pub families: Vec<String>,
16    pub inet_addresses: Vec<String>,
17    pub inet6_addresses: Vec<String>,
18    pub iso_addresses: Vec<String>,
19}
20
21/// A physical or loopback interface such as `ge-0/0/0` or `lo0`.
22#[derive(Clone, Debug, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct Interface {
25    pub name: String,
26    pub description: Option<String>,
27    /// Set by the `disable` statement.
28    pub disabled: bool,
29    pub units: Vec<InterfaceUnit>,
30}
31
32/// Every interface under the top-level `interfaces` block.
33pub fn interfaces(config: &Config) -> Vec<Interface> {
34    let Some(root) = top_level(config, "interfaces") else {
35        return Vec::new();
36    };
37    // Interface names have no spaces; this skips `apply-groups`, `interface-range <x>` and similar.
38    active_children(root)
39        .filter(|c| !c.text.contains(' '))
40        .map(parse_interface)
41        .collect()
42}
43
44fn parse_interface(line: &ConfigLine) -> Interface {
45    Interface {
46        name: line.text.clone(),
47        description: arg_of(line, "description").map(str::to_string),
48        disabled: has_leaf(line, "disable"),
49        units: named_children(line, "unit")
50            .map(|(number, unit)| parse_unit(number, unit))
51            .collect(),
52    }
53}
54
55fn parse_unit(number: &str, unit: &ConfigLine) -> InterfaceUnit {
56    let families: Vec<_> = named_children(unit, "family").collect();
57    let addresses = |family: &str| -> Vec<String> {
58        families
59            .iter()
60            .filter(|(name, _)| *name == family)
61            .flat_map(|(_, f)| args_of(f, "address"))
62            .map(str::to_string)
63            .collect()
64    };
65    InterfaceUnit {
66        number: number.to_string(),
67        description: arg_of(unit, "description").map(str::to_string),
68        vlan_id: arg_of(unit, "vlan-id").and_then(|v| v.parse().ok()),
69        inet_addresses: addresses("inet"),
70        inet6_addresses: addresses("inet6"),
71        iso_addresses: addresses("iso"),
72        families: families.iter().map(|(name, _)| name.to_string()).collect(),
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79    use asterust::config::juniper;
80
81    // Not from documentation: covers statements the Juniper examples do not use. See the README "Sources".
82    const CONFIG: &str = r#"
83interfaces {
84    ge-0/0/0 {
85        description "to p1";
86        unit 0 {
87            description "core link";
88            family inet {
89                address 10.0.1.1/30;
90                address 10.0.2.1/30 {
91                    primary;
92                }
93            }
94            family inet6 {
95                address 2001:db8:1::1/64;
96            }
97            family iso;
98            family mpls;
99        }
100    }
101    ge-0/0/1 {
102        disable;
103        vlan-tagging;
104        unit 100 {
105            vlan-id 100;
106            family inet {
107                address 192.0.2.1/24;
108            }
109        }
110    }
111    inactive: ge-0/0/2 {
112        unit 0;
113    }
114    lo0 {
115        unit 0 {
116            family inet {
117                address 10.255.0.1/32;
118            }
119            family iso {
120                address 49.0001.0100.2550.0001.00;
121            }
122        }
123    }
124    apply-groups GLOBAL;
125}
126"#;
127
128    #[test]
129    fn extracts_units_families_and_addresses() {
130        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
131        let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
132        assert_eq!(names, ["ge-0/0/0", "ge-0/0/1", "lo0"]);
133
134        let core = &ifaces[0];
135        assert_eq!(core.description.as_deref(), Some("to p1"));
136        assert!(!core.disabled);
137        let unit = &core.units[0];
138        assert_eq!(unit.number, "0");
139        assert_eq!(unit.description.as_deref(), Some("core link"));
140        assert_eq!(unit.families, ["inet", "inet6", "iso", "mpls"]);
141        assert_eq!(unit.inet_addresses, ["10.0.1.1/30", "10.0.2.1/30"]);
142        assert_eq!(unit.inet6_addresses, ["2001:db8:1::1/64"]);
143        assert!(unit.iso_addresses.is_empty());
144    }
145
146    #[test]
147    fn disabled_vlan_units_and_loopback_net() {
148        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
149        assert!(ifaces[1].disabled);
150        assert_eq!(ifaces[1].units[0].number, "100");
151        assert_eq!(ifaces[1].units[0].vlan_id, Some(100));
152        assert_eq!(ifaces[2].units[0].iso_addresses, ["49.0001.0100.2550.0001.00"]);
153    }
154
155    #[test]
156    fn missing_interfaces_block_is_empty() {
157        assert!(interfaces(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_empty());
158    }
159
160    // The DOC_* fixtures are adapted from Juniper's BGP User Guide, "BGP Peering Sessions"; see the README "Sources".
161
162    // "Example: Configuring External BGP Point-to-Point Peer Sessions", Device E `show interfaces` (two of four interfaces).
163    const DOC_POINT_TO_POINT: &str = r#"
164interfaces {
165    ge-1/2/0 {
166        unit 0 {
167            description to-A;
168            family inet {
169                address 10.10.10.1/30;
170            }
171        }
172    }
173    ge-0/0/1 {
174        unit 5 {
175            description to-B;
176            family inet {
177                address 10.10.10.5/30;
178            }
179        }
180    }
181}
182"#;
183
184    // "Example: Configure Multiple Single-Hop EBGP Sessions on Different Links Using the Same IPv6 Link-Local
185    // Address", R1 `show interfaces`.
186    const DOC_LINK_LOCAL: &str = r#"
187interfaces {
188    ge-0/0/1 {
189        description R1-to-R2-Link;
190        vlan-tagging;
191        unit 1 {
192            vlan-id 1;
193            family inet6 {
194                address fe80::10/64;
195            }
196        }
197        unit 2 {
198            vlan-id 2;
199            family inet6 {
200                address fe80::10/64;
201            }
202        }
203    }
204    lo0 {
205        unit 0 {
206            family inet {
207                address 198.51.100.1/24 {
208                    primary;
209                }
210            }
211        }
212    }
213}
214"#;
215
216    #[test]
217    fn doc_point_to_point_interfaces() {
218        let ifaces = interfaces(&juniper::parse_braces(DOC_POINT_TO_POINT));
219        let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
220        assert_eq!(names, ["ge-1/2/0", "ge-0/0/1"]);
221        assert_eq!(ifaces[0].units[0].number, "0");
222        assert_eq!(ifaces[0].units[0].description.as_deref(), Some("to-A"));
223        assert_eq!(ifaces[0].units[0].inet_addresses, ["10.10.10.1/30"]);
224        assert_eq!(ifaces[1].units[0].number, "5");
225        assert_eq!(ifaces[1].units[0].inet_addresses, ["10.10.10.5/30"]);
226    }
227
228    #[test]
229    fn doc_link_local_units_vlans_and_primary_address() {
230        let ifaces = interfaces(&juniper::parse_braces(DOC_LINK_LOCAL));
231        let ge = &ifaces[0];
232        assert_eq!(ge.description.as_deref(), Some("R1-to-R2-Link"));
233        assert_eq!(ge.units.len(), 2);
234        assert_eq!(ge.units[0].vlan_id, Some(1));
235        assert_eq!(ge.units[1].vlan_id, Some(2));
236        assert_eq!(ge.units[0].inet6_addresses, ["fe80::10/64"]);
237
238        let lo0 = &ifaces[1];
239        assert_eq!(lo0.units[0].inet_addresses, ["198.51.100.1/24"]);
240    }
241}