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