Skip to main content

asterust_junos/interfaces/
mod.rs

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