Skip to main content

asterust_junos/interfaces/unit/
mod.rs

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