Skip to main content

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

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