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 session of an aggregated Ethernet bundle. The `damping` block is not extracted.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct BfdLivenessDetection {
15    pub local_address: Option<String>,
16    pub neighbor: Option<String>,
17    /// Milliseconds.
18    pub minimum_interval: Option<String>,
19    pub minimum_receive_interval: Option<String>,
20    pub multiplier: Option<String>,
21    /// `1` or `automatic`.
22    pub version: Option<String>,
23    pub holddown_interval: Option<String>,
24    pub no_adaptation: bool,
25    /// From `detection-time { threshold ...; }`.
26    pub detection_time_threshold: Option<String>,
27    /// From `transmit-interval { minimum-interval ...; }`.
28    pub transmit_interval_minimum: Option<String>,
29    /// From `transmit-interval { threshold ...; }`.
30    pub transmit_interval_threshold: Option<String>,
31    /// From `authentication { algorithm ...; }`, e.g. `keyed-sha-1`.
32    pub authentication_algorithm: Option<String>,
33    /// The name of the `security authentication-key-chains` entry; the keys are not extracted.
34    pub authentication_key_chain: Option<String>,
35    pub authentication_loose_check: bool,
36}
37
38pub(super) fn parse_bfd_liveness_detection(bfd: &ConfigLine) -> BfdLivenessDetection {
39    let field = |keyword: &str| arg_of(bfd, keyword).map(str::to_string);
40    let nested = |block: &str, keyword: &str| {
41        child_block(bfd, block)
42            .and_then(|block| arg_of(block, keyword))
43            .map(str::to_string)
44    };
45    let authentication = child_block(bfd, "authentication");
46    BfdLivenessDetection {
47        local_address: field("local-address"),
48        neighbor: field("neighbor"),
49        minimum_interval: field("minimum-interval"),
50        minimum_receive_interval: field("minimum-receive-interval"),
51        multiplier: field("multiplier"),
52        version: field("version"),
53        holddown_interval: field("holddown-interval"),
54        no_adaptation: has_leaf(bfd, "no-adaptation"),
55        detection_time_threshold: nested("detection-time", "threshold"),
56        transmit_interval_minimum: nested("transmit-interval", "minimum-interval"),
57        transmit_interval_threshold: nested("transmit-interval", "threshold"),
58        authentication_algorithm: nested("authentication", "algorithm"),
59        authentication_key_chain: nested("authentication", "key-chain"),
60        authentication_loose_check: authentication.is_some_and(|auth| has_leaf(auth, "loose-check")),
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use crate::interfaces::interfaces;
67    use asterust::config::juniper;
68
69    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
70    #[test]
71    fn extracts_bfd_liveness_detection() {
72        let ifaces = interfaces(&juniper::parse_braces(
73            r#"
74interfaces {
75    ae0 {
76        aggregated-ether-options {
77            bfd-liveness-detection {
78                local-address 192.0.2.1;
79                neighbor 192.0.2.2;
80                minimum-interval 300;
81                minimum-receive-interval 400;
82                multiplier 3;
83                version automatic;
84                holddown-interval 1000;
85                no-adaptation;
86                detection-time {
87                    threshold 5000;
88                }
89                transmit-interval {
90                    minimum-interval 250;
91                    threshold 2000;
92                }
93                authentication {
94                    algorithm keyed-sha-1;
95                    key-chain BFD-KEYS;
96                    loose-check;
97                }
98            }
99        }
100    }
101}
102"#,
103        ));
104        let bfd = ifaces[0]
105            .aggregated_ether_options
106            .as_ref()
107            .unwrap()
108            .bfd_liveness_detection
109            .as_ref()
110            .unwrap();
111        assert_eq!(bfd.local_address.as_deref(), Some("192.0.2.1"));
112        assert_eq!(bfd.neighbor.as_deref(), Some("192.0.2.2"));
113        assert_eq!(bfd.minimum_interval.as_deref(), Some("300"));
114        assert_eq!(bfd.minimum_receive_interval.as_deref(), Some("400"));
115        assert_eq!(bfd.multiplier.as_deref(), Some("3"));
116        assert_eq!(bfd.version.as_deref(), Some("automatic"));
117        assert_eq!(bfd.holddown_interval.as_deref(), Some("1000"));
118        assert!(bfd.no_adaptation);
119        assert_eq!(bfd.detection_time_threshold.as_deref(), Some("5000"));
120        assert_eq!(bfd.transmit_interval_minimum.as_deref(), Some("250"));
121        assert_eq!(bfd.transmit_interval_threshold.as_deref(), Some("2000"));
122        assert_eq!(bfd.authentication_algorithm.as_deref(), Some("keyed-sha-1"));
123        assert_eq!(bfd.authentication_key_chain.as_deref(), Some("BFD-KEYS"));
124        assert!(bfd.authentication_loose_check);
125    }
126
127    #[test]
128    fn bare_bfd_liveness_detection_has_no_settings() {
129        let ifaces = interfaces(&juniper::parse_braces(
130            "interfaces {\n    ae0 {\n        aggregated-ether-options {\n            bfd-liveness-detection {\n                neighbor 192.0.2.2;\n            }\n        }\n    }\n}\n",
131        ));
132        let bfd = ifaces[0]
133            .aggregated_ether_options
134            .as_ref()
135            .unwrap()
136            .bfd_liveness_detection
137            .as_ref()
138            .unwrap();
139        assert_eq!(bfd.neighbor.as_deref(), Some("192.0.2.2"));
140        assert_eq!(bfd.local_address, None);
141        assert!(!bfd.no_adaptation);
142        assert_eq!(bfd.authentication_algorithm, None);
143        assert!(!bfd.authentication_loose_check);
144    }
145}