asterust_junos/protocols/isis/
spf_options.rs1use crate::{child_block, statement_words, value_after};
12use asterust::config::ConfigLine;
13
14#[derive(Clone, Debug, Default, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct IsisSpfOptions {
18 pub delay: Option<String>,
20 pub holddown: Option<String>,
22 pub rapid_runs: Option<String>,
23 pub microloop_avoidance: bool,
25 pub microloop_avoidance_delay: Option<String>,
27 pub microloop_avoidance_maximum_labels: Option<String>,
28 pub microloop_avoidance_maximum_srv6_sids: Option<String>,
29 pub multipath: bool,
31 pub multipath_weighted_one_hop: bool,
33}
34
35pub(super) fn parse_spf_options(isis: &ConfigLine) -> Option<IsisSpfOptions> {
36 let words = statement_words(isis, "spf-options")?;
37 let microloop = child_block(isis, "spf-options").and_then(|spf| child_block(spf, "microloop-avoidance"));
38 let path = microloop.and_then(|microloop| statement_words(microloop, "post-convergence-path"));
39 let multipath = child_block(isis, "spf-options").and_then(|spf| child_block(spf, "multipath"));
40 let weighted = multipath.and_then(|multipath| statement_words(multipath, "weighted"));
41 Some(IsisSpfOptions {
42 delay: value_after(&words, "delay"),
43 holddown: value_after(&words, "holddown"),
44 rapid_runs: value_after(&words, "rapid-runs"),
45 microloop_avoidance: microloop.is_some(),
46 microloop_avoidance_delay: path.as_ref().and_then(|path| value_after(path, "delay")),
47 microloop_avoidance_maximum_labels: path.as_ref().and_then(|path| value_after(path, "maximum-labels")),
48 microloop_avoidance_maximum_srv6_sids: path.as_ref().and_then(|path| value_after(path, "maximum-srv6-sids")),
49 multipath: multipath.is_some(),
50 multipath_weighted_one_hop: weighted.is_some_and(|weighted| weighted.contains(&"one-hop")),
51 })
52}
53
54#[cfg(test)]
55mod tests {
56 use crate::protocols::isis::isis;
57 use asterust::config::juniper;
58
59 const CONFIG: &str = r#"
61protocols {
62 isis {
63 spf-options {
64 delay 50;
65 holddown 5000;
66 rapid-runs 3;
67 microloop-avoidance {
68 post-convergence-path {
69 delay 60000;
70 maximum-labels 5;
71 maximum-srv6-sids 4;
72 }
73 }
74 }
75 }
76}
77"#;
78
79 #[test]
80 fn reads_the_throttle_timers_and_microloop_avoidance() {
81 let spf = isis(&juniper::parse_braces(CONFIG)).unwrap().spf_options.unwrap();
82 assert_eq!(spf.delay.as_deref(), Some("50"));
83 assert_eq!(spf.holddown.as_deref(), Some("5000"));
84 assert_eq!(spf.rapid_runs.as_deref(), Some("3"));
85 assert!(spf.microloop_avoidance);
86 assert_eq!(spf.microloop_avoidance_delay.as_deref(), Some("60000"));
87 assert_eq!(spf.microloop_avoidance_maximum_labels.as_deref(), Some("5"));
88 assert_eq!(spf.microloop_avoidance_maximum_srv6_sids.as_deref(), Some("4"));
89 }
90
91 #[test]
92 fn leaves_unwritten_timers_unset() {
93 let config = juniper::parse_braces(
94 "protocols {\n isis {\n spf-options {\n delay 100;\n }\n }\n}\n",
95 );
96 let spf = isis(&config).unwrap().spf_options.unwrap();
97 assert_eq!(spf.delay.as_deref(), Some("100"));
98 assert_eq!(spf.holddown, None);
99 assert!(!spf.microloop_avoidance);
100 assert!(!spf.multipath);
101 }
102
103 const MULTIPATH: &str = r#"
105protocols {
106 isis {
107 spf-options {
108 multipath {
109 weighted {
110 one-hop;
111 }
112 }
113 }
114 }
115}
116"#;
117
118 #[test]
119 fn reads_weighted_one_hop_multipath() {
120 let spf = isis(&juniper::parse_braces(MULTIPATH)).unwrap().spf_options.unwrap();
121 assert!(spf.multipath);
122 assert!(spf.multipath_weighted_one_hop);
123 assert_eq!(spf.delay, None);
124 }
125}