Skip to main content

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

1//! `track`
2//! `... address <address> { vrrp-group <id> { track { ... } } }`.
3//!
4//! # See also
5//!
6//! - [track (VRRP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/track-edit-interfaces-vrrp.html)
7//! - [priority-cost (VRRP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/priority-cost-edit-interfaces.html)
8//! - [priority-hold-time](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/priority-hold-time-edit-interfaces.html)
9//! - [bandwidth-threshold](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bandwidth-threshold-edit-interfaces.html)
10
11use crate::{active_children, arg_of, args_of, named_children, value_after};
12use asterust::config::ConfigLine;
13
14/// A `bandwidth-threshold` of a tracked interface: the priority cost applied below `bandwidth` bits
15/// per second.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct BandwidthThreshold {
19    pub bandwidth: String,
20    pub priority_cost: Option<String>,
21}
22
23/// A `track interface <name>`.
24#[derive(Clone, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct TrackedInterface {
27    pub name: String,
28    pub priority_cost: Option<String>,
29    pub bandwidth_thresholds: Vec<BandwidthThreshold>,
30}
31
32/// A `track route <prefix> routing-instance <name> priority-cost <n>` line.
33#[derive(Clone, Debug, PartialEq, Eq)]
34#[non_exhaustive]
35pub struct TrackedRoute {
36    pub prefix: String,
37    pub routing_instance: Option<String>,
38    pub priority_cost: Option<String>,
39}
40
41/// The `track` block of a VRRP group.
42#[derive(Clone, Debug, PartialEq, Eq)]
43#[non_exhaustive]
44pub struct VrrpTrack {
45    pub interfaces: Vec<TrackedInterface>,
46    pub routes: Vec<TrackedRoute>,
47    pub priority_hold_time: Option<String>,
48}
49
50pub(super) fn parse_track(track: &ConfigLine) -> VrrpTrack {
51    VrrpTrack {
52        interfaces: named_children(track, "interface")
53            .map(|(name, interface)| TrackedInterface {
54                name: name.to_string(),
55                priority_cost: arg_of(interface, "priority-cost").map(str::to_string),
56                bandwidth_thresholds: active_children(interface)
57                    .filter_map(|c| c.arg("bandwidth-threshold"))
58                    .map(parse_bandwidth_threshold)
59                    .collect(),
60            })
61            .collect(),
62        routes: args_of(track, "route").into_iter().map(parse_route).collect(),
63        priority_hold_time: arg_of(track, "priority-hold-time").map(str::to_string),
64    }
65}
66
67/// `<bits-per-second> priority-cost <n>`.
68fn parse_bandwidth_threshold(arg: &str) -> BandwidthThreshold {
69    let words: Vec<&str> = arg.split_whitespace().collect();
70    BandwidthThreshold {
71        bandwidth: words.first().unwrap_or(&"").to_string(),
72        priority_cost: value_after(&words, "priority-cost"),
73    }
74}
75
76/// `<prefix> routing-instance <name> priority-cost <n>`.
77fn parse_route(arg: &str) -> TrackedRoute {
78    let words: Vec<&str> = arg.split_whitespace().collect();
79    TrackedRoute {
80        prefix: words.first().unwrap_or(&"").to_string(),
81        routing_instance: value_after(&words, "routing-instance"),
82        priority_cost: value_after(&words, "priority-cost"),
83    }
84}