asterust_junos/interfaces/unit/family/address/vrrp_group/
mod.rs1mod 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#[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#[derive(Clone, Debug, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct VrrpGroup {
35 pub group: String,
36 pub inet6: bool,
38 pub accept_data: Option<bool>,
40 pub advertise_interval: Option<String>,
42 pub inet6_advertise_interval: Option<String>,
44 pub advertisements_threshold: Option<String>,
45 pub fast_interval: Option<String>,
47 pub priority: Option<String>,
48 pub preferred: bool,
49 pub authentication_type: Option<String>,
51 pub has_authentication_key: bool,
53 pub authentication_key: Option<crate::Secret>,
55 pub preempt: Option<bool>,
57 pub preempt_hold_time: Option<String>,
59 pub virtual_addresses: Vec<String>,
61 pub virtual_link_local_address: Option<String>,
62 pub track: Option<VrrpTrack>,
63 pub inherit_from: Option<VrrpInheritFrom>,
64}
65
66pub(super) fn parse_vrrp_groups(address: &ConfigLine) -> Vec<VrrpGroup> {
68 let groups = |keyword: &'static str, inet6: bool| {
69 named_children(address, keyword).map(move |(id, group)| parse_group(id, group, inet6))
70 };
71 groups("vrrp-group", false)
72 .chain(groups("vrrp-inet6-group", true))
73 .collect()
74}
75
76fn parse_group(id: &str, group: &ConfigLine, inet6: bool) -> VrrpGroup {
77 let field = |keyword: &str| arg_of(group, keyword).map(str::to_string);
78 VrrpGroup {
79 group: id.to_string(),
80 inet6,
81 accept_data: toggle(group, "accept-data"),
82 advertise_interval: field("advertise-interval"),
83 inet6_advertise_interval: field("inet6-advertise-interval"),
84 advertisements_threshold: field("advertisements-threshold"),
85 fast_interval: field("fast-interval"),
86 priority: field("priority"),
87 preferred: has_leaf(group, "preferred"),
88 authentication_type: field("authentication-type"),
89 has_authentication_key: arg_of(group, "authentication-key").is_some(),
90 authentication_key: crate::secret_value(arg_of(group, "authentication-key")),
91 preempt: toggle(group, "preempt"),
92 preempt_hold_time: child_block(group, "preempt")
93 .and_then(|preempt| arg_of(preempt, "hold-time"))
94 .map(str::to_string),
95 virtual_addresses: ["virtual-address", "virtual-inet6-address"]
96 .iter()
97 .flat_map(|keyword| args_of(group, keyword))
98 .flat_map(list_values)
99 .collect(),
100 virtual_link_local_address: field("virtual-link-local-address"),
101 track: child_block(group, "track").map(parse_track),
102 inherit_from: child_block(group, "vrrp-inherit-from").map(|inherit| VrrpInheritFrom {
103 active_group: arg_of(inherit, "active-group").map(str::to_string),
104 active_interface: arg_of(inherit, "active-interface").map(str::to_string),
105 }),
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use crate::interfaces::interfaces;
112 use asterust::config::juniper;
113
114 const CONFIG: &str = r#"
116interfaces {
117 ge-0/0/0 {
118 unit 0 {
119 family inet {
120 address 192.0.2.2/24 {
121 vrrp-group 10 {
122 virtual-address [ 192.0.2.1 192.0.2.254 ];
123 priority 200;
124 accept-data;
125 advertise-interval 2;
126 advertisements-threshold 3;
127 fast-interval 500;
128 preferred;
129 authentication-type md5;
130 authentication-key "$9$secret"; ## SECRET-DATA
131 preempt {
132 hold-time 60;
133 }
134 track {
135 interface ge-0/0/1 {
136 priority-cost 20;
137 bandwidth-threshold 1000000 priority-cost 5;
138 bandwidth-threshold 100000 priority-cost 10;
139 }
140 route 10.0.0.0/8 routing-instance default priority-cost 30;
141 priority-hold-time 15;
142 }
143 vrrp-inherit-from {
144 active-group 1;
145 active-interface ge-0/0/2.0;
146 }
147 }
148 vrrp-group 11 {
149 virtual-address 192.0.2.3;
150 no-accept-data;
151 no-preempt;
152 }
153 }
154 }
155 family inet6 {
156 address 2001:db8::2/64 {
157 vrrp-inet6-group 20 {
158 virtual-inet6-address [ 2001:db8::1 ];
159 virtual-link-local-address fe80::1;
160 inet6-advertise-interval 1000;
161 priority 150;
162 preempt;
163 }
164 }
165 }
166 }
167 }
168}
169"#;
170
171 #[test]
172 fn extracts_vrrp_group_without_the_key() {
173 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
174 let groups = &ifaces[0].units[0].family_settings[0].addresses[0].vrrp_groups;
175 assert_eq!(groups.len(), 2);
176
177 let group = &groups[0];
178 assert_eq!(group.group, "10");
179 assert!(!group.inet6);
180 assert_eq!(group.virtual_addresses, ["192.0.2.1", "192.0.2.254"]);
181 assert_eq!(group.priority.as_deref(), Some("200"));
182 assert_eq!(group.accept_data, Some(true));
183 assert_eq!(group.advertise_interval.as_deref(), Some("2"));
184 assert_eq!(group.advertisements_threshold.as_deref(), Some("3"));
185 assert_eq!(group.fast_interval.as_deref(), Some("500"));
186 assert!(group.preferred);
187 assert_eq!(group.authentication_type.as_deref(), Some("md5"));
188 assert!(group.has_authentication_key);
189 assert_eq!(
190 group.authentication_key.as_ref().map(crate::Secret::expose),
191 crate::secret::expected("$9$secret")
192 );
193 assert_eq!(group.preempt, Some(true));
194 assert_eq!(group.preempt_hold_time.as_deref(), Some("60"));
195
196 let inherit = group.inherit_from.as_ref().unwrap();
197 assert_eq!(inherit.active_group.as_deref(), Some("1"));
198 assert_eq!(inherit.active_interface.as_deref(), Some("ge-0/0/2.0"));
199 }
200
201 #[test]
202 fn extracts_vrrp_track_interfaces_and_routes() {
203 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
204 let track = ifaces[0].units[0].family_settings[0].addresses[0].vrrp_groups[0]
205 .track
206 .as_ref()
207 .unwrap();
208 assert_eq!(track.priority_hold_time.as_deref(), Some("15"));
209
210 let interface = &track.interfaces[0];
211 assert_eq!(interface.name, "ge-0/0/1");
212 assert_eq!(interface.priority_cost.as_deref(), Some("20"));
213 assert_eq!(interface.bandwidth_thresholds.len(), 2);
214 assert_eq!(interface.bandwidth_thresholds[0].bandwidth, "1000000");
215 assert_eq!(interface.bandwidth_thresholds[0].priority_cost.as_deref(), Some("5"));
216
217 let route = &track.routes[0];
218 assert_eq!(route.prefix, "10.0.0.0/8");
219 assert_eq!(route.routing_instance.as_deref(), Some("default"));
220 assert_eq!(route.priority_cost.as_deref(), Some("30"));
221 }
222
223 #[test]
224 fn minimal_vrrp_group_has_defaults() {
225 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
226 let group = &ifaces[0].units[0].family_settings[0].addresses[0].vrrp_groups[1];
227 assert_eq!(group.virtual_addresses, ["192.0.2.3"]);
228 assert_eq!(group.accept_data, Some(false));
229 assert_eq!(group.preempt, Some(false));
230 assert_eq!(group.preempt_hold_time, None);
231 assert!(!group.has_authentication_key);
232 assert!(group.track.is_none());
233 assert!(group.inherit_from.is_none());
234 }
235
236 #[test]
237 fn extracts_vrrp_inet6_group() {
238 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
239 let group = &ifaces[0].units[0].family_settings[1].addresses[0].vrrp_groups[0];
240 assert_eq!(group.group, "20");
241 assert!(group.inet6);
242 assert_eq!(group.virtual_addresses, ["2001:db8::1"]);
243 assert_eq!(group.virtual_link_local_address.as_deref(), Some("fe80::1"));
244 assert_eq!(group.inet6_advertise_interval.as_deref(), Some("1000"));
245 assert_eq!(group.preempt, Some(true));
246 }
247
248 #[test]
249 fn address_without_vrrp_has_no_groups() {
250 let ifaces = interfaces(&juniper::parse_braces(
251 "interfaces {\n ge-0/0/0 {\n unit 0 {\n family inet {\n address 192.0.2.1/24;\n }\n }\n }\n}\n",
252 ));
253 assert!(ifaces[0].units[0].family_settings[0].addresses[0]
254 .vrrp_groups
255 .is_empty());
256 }
257}