Skip to main content

asterust_junos/interfaces/unit/family/
mod.rs

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