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