Skip to main content

asterust_junos/interfaces/unit/family/
mod.rs

1//! `family`
2//! `interfaces { <name> { unit <n> { family <name> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [family (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/family-edit-interfaces.html)
7//! - [mtu (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mtu-edit-interfaces-ni.html)
8//! - [vlan-id (Logical Port in Bridge Domain)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-id-edit-interfaces-family-bridge.html)
9//! - [vlan-id-list (Interface in Bridge Domain)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-id-list-edit-interfaces.html)
10
11mod address;
12mod filter;
13mod policer;
14mod rpf_check;
15mod sampling;
16
17pub use address::{
18    AddressArp, BandwidthThreshold, TrackedInterface, TrackedRoute, UnitAddress, VrrpGroup, VrrpInheritFrom, VrrpTrack,
19};
20pub use filter::FamilyFilter;
21pub use policer::FamilyPolicer;
22pub use rpf_check::RpfCheck;
23pub use sampling::FamilySampling;
24
25use crate::{arg_of, args_of, list_values};
26use address::parse_addresses;
27use asterust::config::ConfigLine;
28use filter::parse_filter;
29use policer::parse_policer;
30use rpf_check::parse_rpf_check;
31use sampling::parse_sampling;
32
33/// A `family <name>` of a logical unit, with the settings that apply to it.
34#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct UnitFamily {
37    /// e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
38    pub name: String,
39    pub mtu: Option<String>,
40    pub filter: Option<FamilyFilter>,
41    pub rpf_check: Option<RpfCheck>,
42    pub policer: Option<FamilyPolicer>,
43    pub sampling: Option<FamilySampling>,
44    /// `vlan-id` of `family bridge`.
45    pub vlan_id: Option<String>,
46    /// `vlan-id-list` of `family bridge`: IDs and `<from>-<to>` ranges.
47    pub vlan_id_list: Vec<String>,
48    pub addresses: Vec<UnitAddress>,
49}
50
51pub(super) fn parse_family(name: &str, family: &ConfigLine) -> UnitFamily {
52    UnitFamily {
53        name: name.to_string(),
54        mtu: arg_of(family, "mtu").map(str::to_string),
55        filter: parse_filter(family),
56        rpf_check: parse_rpf_check(family),
57        policer: parse_policer(family),
58        sampling: parse_sampling(family),
59        vlan_id: arg_of(family, "vlan-id").map(str::to_string),
60        vlan_id_list: args_of(family, "vlan-id-list")
61            .into_iter()
62            .flat_map(list_values)
63            .collect(),
64        addresses: parse_addresses(family),
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use crate::interfaces::interfaces;
71    use asterust::config::juniper;
72
73    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
74    const CONFIG: &str = r#"
75interfaces {
76    ge-0/0/0 {
77        unit 0 {
78            family inet {
79                mtu 1500;
80                filter {
81                    group 5;
82                    input PROTECT-IN;
83                    output PROTECT-OUT;
84                    input-list [ A B ];
85                    output-list C;
86                }
87                rpf-check {
88                    fail-filter RPF-FAIL;
89                    mode loose;
90                }
91                policer {
92                    arp ARP-POLICER;
93                    input IN-POLICER;
94                    output OUT-POLICER;
95                }
96                sampling {
97                    input;
98                    output;
99                }
100                address 192.0.2.1/24 {
101                    primary;
102                    preferred;
103                    destination 192.0.2.2;
104                    broadcast 192.0.2.255;
105                    arp 192.0.2.50 mac 00:11:22:33:44:55 publish;
106                    arp 192.0.2.51 multicast-mac 01:00:5e:00:00:01;
107                }
108                address 192.0.2.9/24;
109            }
110            family inet6 {
111                rpf-check;
112                sampling input;
113                address 2001:db8::1/64 {
114                    eui-64;
115                }
116            }
117            family iso;
118        }
119    }
120}
121"#;
122
123    #[test]
124    fn extracts_family_filters_policers_and_sampling() {
125        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
126        let inet = &ifaces[0].units[0].family_settings[0];
127        assert_eq!(inet.name, "inet");
128        assert_eq!(inet.mtu.as_deref(), Some("1500"));
129
130        let filter = inet.filter.as_ref().unwrap();
131        assert_eq!(filter.group.as_deref(), Some("5"));
132        assert_eq!(filter.input.as_deref(), Some("PROTECT-IN"));
133        assert_eq!(filter.output.as_deref(), Some("PROTECT-OUT"));
134        assert_eq!(filter.input_list, ["A", "B"]);
135        assert_eq!(filter.output_list, ["C"]);
136
137        let policer = inet.policer.as_ref().unwrap();
138        assert_eq!(policer.arp.as_deref(), Some("ARP-POLICER"));
139        assert_eq!(policer.input.as_deref(), Some("IN-POLICER"));
140        assert_eq!(policer.output.as_deref(), Some("OUT-POLICER"));
141
142        let sampling = inet.sampling.as_ref().unwrap();
143        assert!(sampling.input);
144        assert!(sampling.output);
145    }
146
147    #[test]
148    fn extracts_rpf_check_in_block_and_flag_form() {
149        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
150        let families = &ifaces[0].units[0].family_settings;
151
152        let rpf = families[0].rpf_check.as_ref().unwrap();
153        assert_eq!(rpf.fail_filter.as_deref(), Some("RPF-FAIL"));
154        assert_eq!(rpf.mode.as_deref(), Some("loose"));
155
156        let bare = families[1].rpf_check.as_ref().unwrap();
157        assert_eq!(bare.fail_filter, None);
158        assert_eq!(bare.mode, None);
159    }
160
161    #[test]
162    fn extracts_address_options_and_arp_entries() {
163        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
164        let addresses = &ifaces[0].units[0].family_settings[0].addresses;
165        assert_eq!(addresses.len(), 2);
166
167        let first = &addresses[0];
168        assert_eq!(first.address, "192.0.2.1/24");
169        assert!(first.primary);
170        assert!(first.preferred);
171        assert!(!first.master_only);
172        assert_eq!(first.destination.as_deref(), Some("192.0.2.2"));
173        assert_eq!(first.broadcast.as_deref(), Some("192.0.2.255"));
174        assert_eq!(first.arps.len(), 2);
175        assert_eq!(first.arps[0].address, "192.0.2.50");
176        assert_eq!(first.arps[0].mac.as_deref(), Some("00:11:22:33:44:55"));
177        assert!(first.arps[0].publish);
178        assert!(!first.arps[0].multicast_mac);
179        assert!(first.arps[1].multicast_mac);
180        assert!(!first.arps[1].publish);
181
182        let second = &addresses[1];
183        assert_eq!(second.address, "192.0.2.9/24");
184        assert!(!second.primary);
185        assert!(second.arps.is_empty());
186    }
187
188    #[test]
189    fn extracts_inline_sampling_and_eui_64() {
190        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
191        let inet6 = &ifaces[0].units[0].family_settings[1];
192        let sampling = inet6.sampling.as_ref().unwrap();
193        assert!(sampling.input);
194        assert!(!sampling.output);
195        assert!(inet6.addresses[0].eui_64);
196    }
197
198    #[test]
199    fn family_without_settings_has_none() {
200        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
201        let iso = &ifaces[0].units[0].family_settings[2];
202        assert_eq!(iso.name, "iso");
203        assert!(iso.mtu.is_none());
204        assert!(iso.filter.is_none());
205        assert!(iso.rpf_check.is_none());
206        assert!(iso.policer.is_none());
207        assert!(iso.sampling.is_none());
208        assert!(iso.addresses.is_empty());
209    }
210}