Skip to main content

asterust_junos/interfaces/unit/
mod.rs

1//! `interfaces { <name> { unit <n> { ... } } }`.
2
3mod family;
4mod vlan_map;
5mod vlan_tags;
6
7pub use family::{
8    AddressArp, BandwidthThreshold, FamilyFilter, FamilyPolicer, FamilySampling, RpfCheck, TrackedInterface,
9    TrackedRoute, UnitAddress, UnitFamily, VrrpGroup, VrrpInheritFrom, VrrpTrack,
10};
11pub use vlan_map::VlanMap;
12pub use vlan_tags::VlanTags;
13
14use crate::{arg_of, args_of, named_children};
15use asterust::config::ConfigLine;
16use family::parse_family;
17use vlan_map::parse_vlan_map;
18use vlan_tags::parse_vlan_tags;
19
20/// A logical `unit <n>` of an interface. Addresses are the raw `address` values (e.g. `"10.0.0.1/24"`;
21/// IS-IS NETs for `family iso`).
22#[derive(Clone, Debug, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct InterfaceUnit {
25    pub number: String,
26    pub description: Option<String>,
27    pub vlan_id: Option<u32>,
28    /// `vlan-id unmatched`.
29    pub vlan_id_unmatched: bool,
30    /// e.g. `"100-200"`.
31    pub vlan_id_range: Option<String>,
32    pub vlan_tags: Option<VlanTags>,
33    pub input_vlan_map: Option<VlanMap>,
34    pub output_vlan_map: Option<VlanMap>,
35    /// Logical encapsulation, e.g. `"vlan-bridge"` or `"vlan-ccc"`.
36    pub encapsulation: Option<String>,
37    /// Every `family <name>` present, e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
38    pub families: Vec<String>,
39    pub inet_addresses: Vec<String>,
40    pub inet6_addresses: Vec<String>,
41    pub iso_addresses: Vec<String>,
42    /// The settings of each `family`, in the order written.
43    pub family_settings: Vec<UnitFamily>,
44}
45
46pub(super) fn parse_unit(number: &str, unit: &ConfigLine) -> InterfaceUnit {
47    let families: Vec<_> = named_children(unit, "family").collect();
48    let addresses = |family: &str| -> Vec<String> {
49        families
50            .iter()
51            .filter(|(name, _)| *name == family)
52            .flat_map(|(_, f)| args_of(f, "address"))
53            .map(str::to_string)
54            .collect()
55    };
56    InterfaceUnit {
57        number: number.to_string(),
58        description: arg_of(unit, "description").map(str::to_string),
59        vlan_id: arg_of(unit, "vlan-id").and_then(|v| v.parse().ok()),
60        vlan_id_unmatched: arg_of(unit, "vlan-id") == Some("unmatched"),
61        vlan_id_range: arg_of(unit, "vlan-id-range").map(str::to_string),
62        vlan_tags: parse_vlan_tags(unit),
63        input_vlan_map: parse_vlan_map(unit, "input-vlan-map"),
64        output_vlan_map: parse_vlan_map(unit, "output-vlan-map"),
65        encapsulation: arg_of(unit, "encapsulation").map(str::to_string),
66        inet_addresses: addresses("inet"),
67        inet6_addresses: addresses("inet6"),
68        iso_addresses: addresses("iso"),
69        families: families.iter().map(|(name, _)| name.to_string()).collect(),
70        family_settings: families
71            .iter()
72            .map(|(name, family)| parse_family(name, family))
73            .collect(),
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use crate::interfaces::interfaces;
80    use asterust::config::juniper;
81
82    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
83    const CONFIG: &str = r#"
84interfaces {
85    ge-0/0/0 {
86        flexible-vlan-tagging;
87        encapsulation flexible-ethernet-services;
88        unit 100 {
89            encapsulation vlan-bridge;
90            vlan-id 100;
91            input-vlan-map {
92                swap;
93                tag-protocol-id 0x8100;
94                vlan-id 200;
95            }
96            output-vlan-map swap;
97        }
98        unit 200 {
99            vlan-tags outer 0x8100.200 inner 0x8100.300;
100            input-vlan-map {
101                push-push;
102                tag-protocol-id 0x9100;
103                vlan-id 10;
104                inner-tag-protocol-id 0x8100;
105                inner-vlan-id 20;
106            }
107            output-vlan-map {
108                pop-pop;
109            }
110        }
111        unit 300 {
112            vlan-id unmatched;
113        }
114        unit 400 {
115            vlan-id-range 400-499;
116            vlan-tags outer 500 inner-range 1-4094;
117        }
118    }
119}
120"#;
121
122    #[test]
123    fn extracts_unit_vlan_id_forms_and_encapsulation() {
124        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
125        assert_eq!(units[0].vlan_id, Some(100));
126        assert!(!units[0].vlan_id_unmatched);
127        assert_eq!(units[0].encapsulation.as_deref(), Some("vlan-bridge"));
128
129        assert_eq!(units[2].vlan_id, None);
130        assert!(units[2].vlan_id_unmatched);
131
132        assert_eq!(units[3].vlan_id_range.as_deref(), Some("400-499"));
133        assert_eq!(units[1].encapsulation, None);
134    }
135
136    #[test]
137    fn extracts_vlan_tags() {
138        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
139        let tags = units[1].vlan_tags.as_ref().unwrap();
140        assert_eq!(tags.outer.as_deref(), Some("0x8100.200"));
141        assert_eq!(tags.inner.as_deref(), Some("0x8100.300"));
142        assert_eq!(tags.inner_range, None);
143
144        let ranged = units[3].vlan_tags.as_ref().unwrap();
145        assert_eq!(ranged.outer.as_deref(), Some("500"));
146        assert_eq!(ranged.inner, None);
147        assert_eq!(ranged.inner_range.as_deref(), Some("1-4094"));
148
149        assert!(units[0].vlan_tags.is_none());
150    }
151
152    #[test]
153    fn extracts_vlan_maps_in_block_and_one_line_form() {
154        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
155
156        let input = units[0].input_vlan_map.as_ref().unwrap();
157        assert_eq!(input.operation.as_deref(), Some("swap"));
158        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x8100"));
159        assert_eq!(input.vlan_id.as_deref(), Some("200"));
160        assert_eq!(input.inner_vlan_id, None);
161        assert_eq!(
162            units[0].output_vlan_map.as_ref().unwrap().operation.as_deref(),
163            Some("swap")
164        );
165
166        let input = units[1].input_vlan_map.as_ref().unwrap();
167        assert_eq!(input.operation.as_deref(), Some("push-push"));
168        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x9100"));
169        assert_eq!(input.vlan_id.as_deref(), Some("10"));
170        assert_eq!(input.inner_tag_protocol_id.as_deref(), Some("0x8100"));
171        assert_eq!(input.inner_vlan_id.as_deref(), Some("20"));
172        assert_eq!(
173            units[1].output_vlan_map.as_ref().unwrap().operation.as_deref(),
174            Some("pop-pop")
175        );
176
177        assert!(units[2].input_vlan_map.is_none());
178        assert!(units[2].output_vlan_map.is_none());
179    }
180
181    #[test]
182    fn extracts_bridge_family_vlan_ids() {
183        let ifaces = interfaces(&juniper::parse_braces(
184            "interfaces {\n    ge-0/0/1 {\n        unit 0 {\n            family bridge {\n                vlan-id-list [ 100-200 300 ];\n                vlan-id 50;\n            }\n        }\n    }\n}\n",
185        ));
186        let bridge = &ifaces[0].units[0].family_settings[0];
187        assert_eq!(bridge.name, "bridge");
188        assert_eq!(bridge.vlan_id_list, ["100-200", "300"]);
189        assert_eq!(bridge.vlan_id.as_deref(), Some("50"));
190    }
191}