Skip to main content

asterust_junos/protocols/ospf/
spf_options.rs

1//! `spf-options`
2//! `protocols { ospf { spf-options { delay <ms>; holddown <ms>; rapid-runs <n>; } } }`.
3//!
4//! # See also
5//!
6//! - [spf-options (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/spf-options-edit-protocols-ospf.html)
7//! - [microloop-avoidance (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/post-convergence-path-edit-protocols-ospf-spf-options-microloop-avoidance.html)
8//! - [Configuring Segment Routing Microloop Avoidance in OSPFv2 Networks](https://networkingdocs.hpe.com/documentation/us/en/software/junos/segment-routing/topics/example/ospfv2-microloop-avoidance-configuring.html)
9
10use crate::{child_block, statement_words, value_after};
11use asterust::config::ConfigLine;
12
13/// The SPF throttling of `spf-options`. The `multipath` child is not extracted.
14#[derive(Clone, Debug, Default, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct OspfSpfOptions {
17    /// Milliseconds.
18    pub delay: Option<String>,
19    /// Milliseconds.
20    pub holddown: Option<String>,
21    pub rapid_runs: Option<String>,
22    /// A `microloop-avoidance` block is configured (22.1 and later).
23    pub microloop_avoidance: bool,
24    /// Milliseconds, from `microloop-avoidance { post-convergence-path { delay ...; } }`.
25    pub microloop_avoidance_delay: Option<String>,
26    /// From `microloop-avoidance { post-convergence-path { maximum-labels ...; } }`.
27    pub microloop_avoidance_maximum_labels: Option<String>,
28}
29
30pub(super) fn parse_spf_options(ospf: &ConfigLine) -> Option<OspfSpfOptions> {
31    let words = statement_words(ospf, "spf-options")?;
32    let microloop = child_block(ospf, "spf-options").and_then(|spf| child_block(spf, "microloop-avoidance"));
33    let path = microloop.and_then(|microloop| statement_words(microloop, "post-convergence-path"));
34    Some(OspfSpfOptions {
35        delay: value_after(&words, "delay"),
36        holddown: value_after(&words, "holddown"),
37        rapid_runs: value_after(&words, "rapid-runs"),
38        microloop_avoidance: microloop.is_some(),
39        microloop_avoidance_delay: path.as_ref().and_then(|path| value_after(path, "delay")),
40        microloop_avoidance_maximum_labels: path.as_ref().and_then(|path| value_after(path, "maximum-labels")),
41    })
42}
43
44#[cfg(test)]
45mod tests {
46    use crate::protocols::ospf::ospf;
47    use asterust::config::juniper;
48
49    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
50    const CONFIG: &str = r#"
51protocols {
52    ospf {
53        spf-options {
54            delay 50;
55            holddown 5000;
56            rapid-runs 3;
57        }
58    }
59}
60"#;
61
62    #[test]
63    fn reads_the_throttle_timers() {
64        let spf = ospf(&juniper::parse_braces(CONFIG)).unwrap().spf_options.unwrap();
65        assert_eq!(spf.delay.as_deref(), Some("50"));
66        assert_eq!(spf.holddown.as_deref(), Some("5000"));
67        assert_eq!(spf.rapid_runs.as_deref(), Some("3"));
68    }
69
70    #[test]
71    fn leaves_unwritten_timers_unset() {
72        let config = juniper::parse_braces(
73            "protocols {\n    ospf {\n        spf-options {\n            delay 100;\n        }\n    }\n}\n",
74        );
75        let spf = ospf(&config).unwrap().spf_options.unwrap();
76        assert_eq!(spf.delay.as_deref(), Some("100"));
77        assert_eq!(spf.holddown, None);
78        assert!(!spf.microloop_avoidance);
79    }
80
81    // Adapted from "Configuring Segment Routing Microloop Avoidance in OSPFv2 Networks" (Segment Routing User
82    // Guide), Device R0 `show protocols` (OSPF part), with `maximum-labels` added from the CLI Reference; see the
83    // README "Sources".
84    const MICROLOOP: &str = r#"
85protocols {
86    ospf {
87        spf-options {
88            microloop-avoidance {
89                post-convergence-path {
90                    delay 60000;
91                    maximum-labels 5;
92                }
93            }
94        }
95    }
96}
97"#;
98
99    #[test]
100    fn reads_microloop_avoidance() {
101        let spf = ospf(&juniper::parse_braces(MICROLOOP)).unwrap().spf_options.unwrap();
102        assert!(spf.microloop_avoidance);
103        assert_eq!(spf.microloop_avoidance_delay.as_deref(), Some("60000"));
104        assert_eq!(spf.microloop_avoidance_maximum_labels.as_deref(), Some("5"));
105        assert_eq!(spf.delay, None);
106    }
107}