Skip to main content

asterust_junos/interfaces/
link_degrade_monitor.rs

1//! `interfaces { <name> { link-degrade-monitor { ... } } }`.
2
3use crate::{arg_of, child_block, has_leaf};
4use asterust::config::ConfigLine;
5
6/// The BER `thresholds` of a link degrade monitor, written `1E-<n>`.
7#[derive(Clone, Debug, Default, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct LinkDegradeThresholds {
10    pub set: Option<String>,
11    pub clear: Option<String>,
12    pub warning_set: Option<String>,
13    pub warning_clear: Option<String>,
14    /// Consecutive degrade events before the action is taken.
15    pub interval: Option<String>,
16}
17
18/// The `link-degrade-monitor` settings of an interface.
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct LinkDegradeMonitor {
22    pub thresholds: Option<LinkDegradeThresholds>,
23    /// `auto` or `manual`, from `recovery`.
24    pub recovery_mode: Option<String>,
25    /// Seconds, from `recovery { timer ...; }`.
26    pub recovery_timer: Option<String>,
27}
28
29pub(super) fn parse_link_degrade_monitor(monitor: &ConfigLine) -> LinkDegradeMonitor {
30    let recovery = child_block(monitor, "recovery");
31    LinkDegradeMonitor {
32        thresholds: child_block(monitor, "thresholds").map(|thresholds| {
33            let field = |keyword: &str| arg_of(thresholds, keyword).map(str::to_string);
34            LinkDegradeThresholds {
35                set: field("set"),
36                clear: field("clear"),
37                warning_set: field("warning-set"),
38                warning_clear: field("warning-clear"),
39                interval: field("interval"),
40            }
41        }),
42        recovery_mode: recovery.and_then(|recovery| {
43            ["auto", "manual"]
44                .iter()
45                .find(|mode| has_leaf(recovery, mode))
46                .map(|mode| mode.to_string())
47        }),
48        recovery_timer: recovery
49            .and_then(|recovery| arg_of(recovery, "timer"))
50            .map(str::to_string),
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use crate::interfaces::interfaces;
57    use asterust::config::juniper;
58
59    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
60    #[test]
61    fn extracts_link_degrade_monitor() {
62        let ifaces = interfaces(&juniper::parse_braces(
63            r#"
64interfaces {
65    et-0/0/0 {
66        link-degrade-monitor {
67            thresholds {
68                set 1E-7;
69                clear 1E-12;
70                warning-set 1E-9;
71                warning-clear 1E-11;
72                interval 10;
73            }
74            recovery {
75                auto;
76                timer 1800;
77            }
78        }
79    }
80    et-0/0/1 {
81        link-degrade-monitor {
82            recovery {
83                manual;
84            }
85        }
86    }
87}
88"#,
89        ));
90        let monitor = ifaces[0].link_degrade_monitor.as_ref().unwrap();
91        let thresholds = monitor.thresholds.as_ref().unwrap();
92        assert_eq!(thresholds.set.as_deref(), Some("1E-7"));
93        assert_eq!(thresholds.clear.as_deref(), Some("1E-12"));
94        assert_eq!(thresholds.warning_set.as_deref(), Some("1E-9"));
95        assert_eq!(thresholds.warning_clear.as_deref(), Some("1E-11"));
96        assert_eq!(thresholds.interval.as_deref(), Some("10"));
97        assert_eq!(monitor.recovery_mode.as_deref(), Some("auto"));
98        assert_eq!(monitor.recovery_timer.as_deref(), Some("1800"));
99
100        let manual = ifaces[1].link_degrade_monitor.as_ref().unwrap();
101        assert!(manual.thresholds.is_none());
102        assert_eq!(manual.recovery_mode.as_deref(), Some("manual"));
103        assert_eq!(manual.recovery_timer, None);
104    }
105
106    #[test]
107    fn link_degrade_monitor_is_optional() {
108        let ifaces = interfaces(&juniper::parse_braces(
109            "interfaces {\n    et-0/0/0 {\n        disable;\n    }\n}\n",
110        ));
111        assert!(ifaces[0].link_degrade_monitor.is_none());
112    }
113}