Skip to main content

asterust_junos/interfaces/unit/family/address/vrrp_group/
mod.rs

1//! `... address <address> { vrrp-group <id> { ... } }` and `vrrp-inet6-group <id> { ... }`.
2
3mod track;
4
5pub use track::{BandwidthThreshold, TrackedInterface, TrackedRoute, VrrpTrack};
6
7use crate::{arg_of, args_of, child_block, has_leaf, list_values, named_children, toggle};
8use asterust::config::ConfigLine;
9use track::parse_track;
10
11/// The `vrrp-inherit-from` block of a VRRP group.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct VrrpInheritFrom {
15    pub active_group: Option<String>,
16    pub active_interface: Option<String>,
17}
18
19/// A `vrrp-group` (under `family inet`) or `vrrp-inet6-group` (under `family inet6`). The `authentication-key` is not
20/// extracted.
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct VrrpGroup {
24    pub group: String,
25    /// Written as `vrrp-inet6-group`.
26    pub inet6: bool,
27    /// `Some(true)` for `accept-data`, `Some(false)` for `no-accept-data`.
28    pub accept_data: Option<bool>,
29    /// Seconds, `vrrp-group` only.
30    pub advertise_interval: Option<String>,
31    /// Milliseconds, `vrrp-inet6-group` only.
32    pub inet6_advertise_interval: Option<String>,
33    pub advertisements_threshold: Option<String>,
34    /// Milliseconds.
35    pub fast_interval: Option<String>,
36    pub priority: Option<String>,
37    pub preferred: bool,
38    /// `md5` or `simple`.
39    pub authentication_type: Option<String>,
40    /// An `authentication-key` is configured.
41    pub has_authentication_key: bool,
42    /// `Some(true)` for `preempt`, `Some(false)` for `no-preempt`.
43    pub preempt: Option<bool>,
44    /// Seconds, from `preempt { hold-time ...; }`.
45    pub preempt_hold_time: Option<String>,
46    /// From `virtual-address` or `virtual-inet6-address`.
47    pub virtual_addresses: Vec<String>,
48    pub virtual_link_local_address: Option<String>,
49    pub track: Option<VrrpTrack>,
50    pub inherit_from: Option<VrrpInheritFrom>,
51}
52
53/// Every `vrrp-group` and `vrrp-inet6-group` under `address`.
54pub(super) fn parse_vrrp_groups(address: &ConfigLine) -> Vec<VrrpGroup> {
55    let groups = |keyword: &'static str, inet6: bool| {
56        named_children(address, keyword).map(move |(id, group)| parse_group(id, group, inet6))
57    };
58    groups("vrrp-group", false)
59        .chain(groups("vrrp-inet6-group", true))
60        .collect()
61}
62
63fn parse_group(id: &str, group: &ConfigLine, inet6: bool) -> VrrpGroup {
64    let field = |keyword: &str| arg_of(group, keyword).map(str::to_string);
65    VrrpGroup {
66        group: id.to_string(),
67        inet6,
68        accept_data: toggle(group, "accept-data"),
69        advertise_interval: field("advertise-interval"),
70        inet6_advertise_interval: field("inet6-advertise-interval"),
71        advertisements_threshold: field("advertisements-threshold"),
72        fast_interval: field("fast-interval"),
73        priority: field("priority"),
74        preferred: has_leaf(group, "preferred"),
75        authentication_type: field("authentication-type"),
76        has_authentication_key: arg_of(group, "authentication-key").is_some(),
77        preempt: toggle(group, "preempt"),
78        preempt_hold_time: child_block(group, "preempt")
79            .and_then(|preempt| arg_of(preempt, "hold-time"))
80            .map(str::to_string),
81        virtual_addresses: ["virtual-address", "virtual-inet6-address"]
82            .iter()
83            .flat_map(|keyword| args_of(group, keyword))
84            .flat_map(list_values)
85            .collect(),
86        virtual_link_local_address: field("virtual-link-local-address"),
87        track: child_block(group, "track").map(parse_track),
88        inherit_from: child_block(group, "vrrp-inherit-from").map(|inherit| VrrpInheritFrom {
89            active_group: arg_of(inherit, "active-group").map(str::to_string),
90            active_interface: arg_of(inherit, "active-interface").map(str::to_string),
91        }),
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use crate::interfaces::interfaces;
98    use asterust::config::juniper;
99
100    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
101    const CONFIG: &str = r#"
102interfaces {
103    ge-0/0/0 {
104        unit 0 {
105            family inet {
106                address 192.0.2.2/24 {
107                    vrrp-group 10 {
108                        virtual-address [ 192.0.2.1 192.0.2.254 ];
109                        priority 200;
110                        accept-data;
111                        advertise-interval 2;
112                        advertisements-threshold 3;
113                        fast-interval 500;
114                        preferred;
115                        authentication-type md5;
116                        authentication-key "$9$secret"; ## SECRET-DATA
117                        preempt {
118                            hold-time 60;
119                        }
120                        track {
121                            interface ge-0/0/1 {
122                                priority-cost 20;
123                                bandwidth-threshold 1000000 priority-cost 5;
124                                bandwidth-threshold 100000 priority-cost 10;
125                            }
126                            route 10.0.0.0/8 routing-instance default priority-cost 30;
127                            priority-hold-time 15;
128                        }
129                        vrrp-inherit-from {
130                            active-group 1;
131                            active-interface ge-0/0/2.0;
132                        }
133                    }
134                    vrrp-group 11 {
135                        virtual-address 192.0.2.3;
136                        no-accept-data;
137                        no-preempt;
138                    }
139                }
140            }
141            family inet6 {
142                address 2001:db8::2/64 {
143                    vrrp-inet6-group 20 {
144                        virtual-inet6-address [ 2001:db8::1 ];
145                        virtual-link-local-address fe80::1;
146                        inet6-advertise-interval 1000;
147                        priority 150;
148                        preempt;
149                    }
150                }
151            }
152        }
153    }
154}
155"#;
156
157    #[test]
158    fn extracts_vrrp_group_without_the_key() {
159        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
160        let groups = &ifaces[0].units[0].family_settings[0].addresses[0].vrrp_groups;
161        assert_eq!(groups.len(), 2);
162
163        let group = &groups[0];
164        assert_eq!(group.group, "10");
165        assert!(!group.inet6);
166        assert_eq!(group.virtual_addresses, ["192.0.2.1", "192.0.2.254"]);
167        assert_eq!(group.priority.as_deref(), Some("200"));
168        assert_eq!(group.accept_data, Some(true));
169        assert_eq!(group.advertise_interval.as_deref(), Some("2"));
170        assert_eq!(group.advertisements_threshold.as_deref(), Some("3"));
171        assert_eq!(group.fast_interval.as_deref(), Some("500"));
172        assert!(group.preferred);
173        assert_eq!(group.authentication_type.as_deref(), Some("md5"));
174        assert!(group.has_authentication_key);
175        assert_eq!(group.preempt, Some(true));
176        assert_eq!(group.preempt_hold_time.as_deref(), Some("60"));
177
178        let inherit = group.inherit_from.as_ref().unwrap();
179        assert_eq!(inherit.active_group.as_deref(), Some("1"));
180        assert_eq!(inherit.active_interface.as_deref(), Some("ge-0/0/2.0"));
181    }
182
183    #[test]
184    fn extracts_vrrp_track_interfaces_and_routes() {
185        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
186        let track = ifaces[0].units[0].family_settings[0].addresses[0].vrrp_groups[0]
187            .track
188            .as_ref()
189            .unwrap();
190        assert_eq!(track.priority_hold_time.as_deref(), Some("15"));
191
192        let interface = &track.interfaces[0];
193        assert_eq!(interface.name, "ge-0/0/1");
194        assert_eq!(interface.priority_cost.as_deref(), Some("20"));
195        assert_eq!(interface.bandwidth_thresholds.len(), 2);
196        assert_eq!(interface.bandwidth_thresholds[0].bandwidth, "1000000");
197        assert_eq!(interface.bandwidth_thresholds[0].priority_cost.as_deref(), Some("5"));
198
199        let route = &track.routes[0];
200        assert_eq!(route.prefix, "10.0.0.0/8");
201        assert_eq!(route.routing_instance.as_deref(), Some("default"));
202        assert_eq!(route.priority_cost.as_deref(), Some("30"));
203    }
204
205    #[test]
206    fn minimal_vrrp_group_has_defaults() {
207        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
208        let group = &ifaces[0].units[0].family_settings[0].addresses[0].vrrp_groups[1];
209        assert_eq!(group.virtual_addresses, ["192.0.2.3"]);
210        assert_eq!(group.accept_data, Some(false));
211        assert_eq!(group.preempt, Some(false));
212        assert_eq!(group.preempt_hold_time, None);
213        assert!(!group.has_authentication_key);
214        assert!(group.track.is_none());
215        assert!(group.inherit_from.is_none());
216    }
217
218    #[test]
219    fn extracts_vrrp_inet6_group() {
220        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
221        let group = &ifaces[0].units[0].family_settings[1].addresses[0].vrrp_groups[0];
222        assert_eq!(group.group, "20");
223        assert!(group.inet6);
224        assert_eq!(group.virtual_addresses, ["2001:db8::1"]);
225        assert_eq!(group.virtual_link_local_address.as_deref(), Some("fe80::1"));
226        assert_eq!(group.inet6_advertise_interval.as_deref(), Some("1000"));
227        assert_eq!(group.preempt, Some(true));
228    }
229
230    #[test]
231    fn address_without_vrrp_has_no_groups() {
232        let ifaces = interfaces(&juniper::parse_braces(
233            "interfaces {\n    ge-0/0/0 {\n        unit 0 {\n            family inet {\n                address 192.0.2.1/24;\n            }\n        }\n    }\n}\n",
234        ));
235        assert!(ifaces[0].units[0].family_settings[0].addresses[0]
236            .vrrp_groups
237            .is_empty());
238    }
239}