Skip to main content

asterust_junos/interfaces/aggregated_ether_options/
bfd_liveness_detection.rs

1//! `bfd-liveness-detection`
2//! `interfaces { ae<n> { aggregated-ether-options { bfd-liveness-detection { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [bfd-liveness-detection (LAG)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bfd-liveness-detection-edit-interfaces-aex-aggregated-ether-options.html)
7
8use crate::{arg_of, child_block, has_leaf};
9use asterust::config::ConfigLine;
10
11/// The BFD `damping` of an aggregated Ethernet bundle's session.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct BfdDamping {
15    /// Seconds.
16    pub half_life_interval: Option<String>,
17    pub increment: Option<String>,
18    /// Seconds.
19    pub max_suppress_time: Option<String>,
20    pub reuse: Option<String>,
21    pub suppress: Option<String>,
22}
23
24/// The BFD session of an aggregated Ethernet bundle.
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct BfdLivenessDetection {
28    pub local_address: Option<String>,
29    pub neighbor: Option<String>,
30    /// Milliseconds.
31    pub minimum_interval: Option<String>,
32    pub minimum_receive_interval: Option<String>,
33    pub multiplier: Option<String>,
34    /// `1` or `automatic`.
35    pub version: Option<String>,
36    pub holddown_interval: Option<String>,
37    pub no_adaptation: bool,
38    /// From `detection-time { threshold ...; }`.
39    pub detection_time_threshold: Option<String>,
40    /// From `transmit-interval { minimum-interval ...; }`.
41    pub transmit_interval_minimum: Option<String>,
42    /// From `transmit-interval { threshold ...; }`.
43    pub transmit_interval_threshold: Option<String>,
44    /// From `authentication { algorithm ...; }`, e.g. `keyed-sha-1`.
45    pub authentication_algorithm: Option<String>,
46    /// The name of the `security authentication-key-chains` entry; the keys are not extracted.
47    pub authentication_key_chain: Option<String>,
48    pub authentication_loose_check: bool,
49    pub damping: Option<BfdDamping>,
50}
51
52pub(super) fn parse_bfd_liveness_detection(bfd: &ConfigLine) -> BfdLivenessDetection {
53    let field = |keyword: &str| arg_of(bfd, keyword).map(str::to_string);
54    let nested = |block: &str, keyword: &str| {
55        child_block(bfd, block)
56            .and_then(|block| arg_of(block, keyword))
57            .map(str::to_string)
58    };
59    let authentication = child_block(bfd, "authentication");
60    BfdLivenessDetection {
61        local_address: field("local-address"),
62        neighbor: field("neighbor"),
63        minimum_interval: field("minimum-interval"),
64        minimum_receive_interval: field("minimum-receive-interval"),
65        multiplier: field("multiplier"),
66        version: field("version"),
67        holddown_interval: field("holddown-interval"),
68        no_adaptation: has_leaf(bfd, "no-adaptation"),
69        detection_time_threshold: nested("detection-time", "threshold"),
70        transmit_interval_minimum: nested("transmit-interval", "minimum-interval"),
71        transmit_interval_threshold: nested("transmit-interval", "threshold"),
72        authentication_algorithm: nested("authentication", "algorithm"),
73        authentication_key_chain: nested("authentication", "key-chain"),
74        authentication_loose_check: authentication.is_some_and(|auth| has_leaf(auth, "loose-check")),
75        damping: child_block(bfd, "damping").map(|damping| {
76            let field = |keyword: &str| arg_of(damping, keyword).map(str::to_string);
77            BfdDamping {
78                half_life_interval: field("half-life-interval"),
79                increment: field("increment"),
80                max_suppress_time: field("max-suppress-time"),
81                reuse: field("reuse"),
82                suppress: field("suppress"),
83            }
84        }),
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use crate::interfaces::interfaces;
91    use asterust::config::juniper;
92
93    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
94    #[test]
95    fn extracts_bfd_liveness_detection() {
96        let ifaces = interfaces(&juniper::parse_braces(
97            r#"
98interfaces {
99    ae0 {
100        aggregated-ether-options {
101            bfd-liveness-detection {
102                local-address 192.0.2.1;
103                neighbor 192.0.2.2;
104                minimum-interval 300;
105                minimum-receive-interval 400;
106                multiplier 3;
107                version automatic;
108                holddown-interval 1000;
109                no-adaptation;
110                detection-time {
111                    threshold 5000;
112                }
113                transmit-interval {
114                    minimum-interval 250;
115                    threshold 2000;
116                }
117                authentication {
118                    algorithm keyed-sha-1;
119                    key-chain BFD-KEYS;
120                    loose-check;
121                }
122            }
123        }
124    }
125}
126"#,
127        ));
128        let bfd = ifaces[0]
129            .aggregated_ether_options
130            .as_ref()
131            .unwrap()
132            .bfd_liveness_detection
133            .as_ref()
134            .unwrap();
135        assert_eq!(bfd.local_address.as_deref(), Some("192.0.2.1"));
136        assert_eq!(bfd.neighbor.as_deref(), Some("192.0.2.2"));
137        assert_eq!(bfd.minimum_interval.as_deref(), Some("300"));
138        assert_eq!(bfd.minimum_receive_interval.as_deref(), Some("400"));
139        assert_eq!(bfd.multiplier.as_deref(), Some("3"));
140        assert_eq!(bfd.version.as_deref(), Some("automatic"));
141        assert_eq!(bfd.holddown_interval.as_deref(), Some("1000"));
142        assert!(bfd.no_adaptation);
143        assert_eq!(bfd.detection_time_threshold.as_deref(), Some("5000"));
144        assert_eq!(bfd.transmit_interval_minimum.as_deref(), Some("250"));
145        assert_eq!(bfd.transmit_interval_threshold.as_deref(), Some("2000"));
146        assert_eq!(bfd.authentication_algorithm.as_deref(), Some("keyed-sha-1"));
147        assert_eq!(bfd.authentication_key_chain.as_deref(), Some("BFD-KEYS"));
148        assert!(bfd.authentication_loose_check);
149    }
150
151    #[test]
152    fn bare_bfd_liveness_detection_has_no_settings() {
153        let ifaces = interfaces(&juniper::parse_braces(
154            "interfaces {\n    ae0 {\n        aggregated-ether-options {\n            bfd-liveness-detection {\n                neighbor 192.0.2.2;\n            }\n        }\n    }\n}\n",
155        ));
156        let bfd = ifaces[0]
157            .aggregated_ether_options
158            .as_ref()
159            .unwrap()
160            .bfd_liveness_detection
161            .as_ref()
162            .unwrap();
163        assert_eq!(bfd.neighbor.as_deref(), Some("192.0.2.2"));
164        assert_eq!(bfd.local_address, None);
165        assert!(!bfd.no_adaptation);
166        assert_eq!(bfd.authentication_algorithm, None);
167        assert!(!bfd.authentication_loose_check);
168    }
169
170    // Not from documentation: written from the damping CLI Reference page. See the README "Sources".
171    #[test]
172    fn extracts_damping() {
173        let ifaces = interfaces(&juniper::parse_braces(
174            "interfaces {\n    ae0 {\n        aggregated-ether-options {\n            bfd-liveness-detection {\n                damping {\n                    half-life-interval 60;\n                    increment 1000;\n                    max-suppress-time 300;\n                    reuse 750;\n                    suppress 3000;\n                }\n            }\n        }\n    }\n    ae1 {\n        aggregated-ether-options {\n            bfd-liveness-detection {\n                multiplier 3;\n            }\n        }\n    }\n}\n",
175        ));
176        let damping = ifaces[0]
177            .aggregated_ether_options
178            .as_ref()
179            .unwrap()
180            .bfd_liveness_detection
181            .as_ref()
182            .unwrap()
183            .damping
184            .as_ref()
185            .unwrap();
186        assert_eq!(damping.half_life_interval.as_deref(), Some("60"));
187        assert_eq!(damping.increment.as_deref(), Some("1000"));
188        assert_eq!(damping.max_suppress_time.as_deref(), Some("300"));
189        assert_eq!(damping.reuse.as_deref(), Some("750"));
190        assert_eq!(damping.suppress.as_deref(), Some("3000"));
191
192        let bfd = ifaces[1]
193            .aggregated_ether_options
194            .as_ref()
195            .unwrap()
196            .bfd_liveness_detection
197            .as_ref()
198            .unwrap();
199        assert!(bfd.damping.is_none());
200    }
201}