Skip to main content

asterust_junos/protocols/ospf/
backup_spf_options.rs

1//! `backup-spf-options`
2//! `protocols { ospf { [topology <name> {] backup-spf-options { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [backup-spf-options (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/backup-spf-options-edit-protocols-ospf.html)
7//! - [use-post-convergence-lfa (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/use-post-convergence-editprotocols-ospf-backup-spf-options.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 `backup-spf-options` of an OSPF instance or topology.
14#[derive(Clone, Debug, Default, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct OspfBackupSpfOptions {
17    pub disable: bool,
18    pub downstream_paths_only: bool,
19    pub no_install: bool,
20    pub node_link_degradation: bool,
21    pub remote_backup_calculation: bool,
22    pub use_source_packet_routing: bool,
23    pub no_source_packet_routing: bool,
24    /// `None` without `per-prefix-calculation`; otherwise the written members of `all`, `externals`, `stubs` and
25    /// `summary`.
26    pub per_prefix_calculation: Option<Vec<String>>,
27    /// A `use-post-convergence-lfa` statement is configured.
28    pub use_post_convergence_lfa: bool,
29    pub maximum_backup_paths: Option<String>,
30    pub maximum_labels: Option<String>,
31}
32
33pub(super) fn parse_backup_spf_options(parent: &ConfigLine) -> Option<OspfBackupSpfOptions> {
34    let words = statement_words(parent, "backup-spf-options")?;
35    let backup = child_block(parent, "backup-spf-options");
36    let per_prefix = backup.and_then(|backup| statement_words(backup, "per-prefix-calculation"));
37    let lfa = backup.and_then(|backup| statement_words(backup, "use-post-convergence-lfa"));
38    Some(OspfBackupSpfOptions {
39        disable: words.contains(&"disable"),
40        downstream_paths_only: words.contains(&"downstream-paths-only"),
41        no_install: words.contains(&"no-install"),
42        node_link_degradation: words.contains(&"node-link-degradation"),
43        remote_backup_calculation: words.contains(&"remote-backup-calculation"),
44        use_source_packet_routing: words.contains(&"use-source-packet-routing"),
45        no_source_packet_routing: words.contains(&"no-source-packet-routing"),
46        per_prefix_calculation: per_prefix.map(|words| {
47            words
48                .into_iter()
49                .filter(|word| matches!(*word, "all" | "externals" | "stubs" | "summary"))
50                .map(str::to_string)
51                .collect()
52        }),
53        use_post_convergence_lfa: lfa.is_some(),
54        maximum_backup_paths: lfa.as_ref().and_then(|lfa| value_after(lfa, "maximum-backup-paths")),
55        maximum_labels: lfa.as_ref().and_then(|lfa| value_after(lfa, "maximum-labels")),
56    })
57}
58
59#[cfg(test)]
60mod tests {
61    use crate::protocols::ospf::ospf;
62    use asterust::config::juniper;
63
64    // Adapted from "Configuring Segment Routing Microloop Avoidance in OSPFv2 Networks" (Segment Routing User
65    // Guide), Device R0 `show protocols` (OSPF part), plus a second block written from the CLI Reference; see the
66    // README "Sources".
67    const CONFIG: &str = r#"
68protocols {
69    ospf {
70        backup-spf-options {
71            use-post-convergence-lfa {
72                maximum-labels 5;
73                maximum-backup-paths 8;
74            }
75            use-source-packet-routing;
76        }
77        topology ipv4-multicast {
78            backup-spf-options {
79                disable;
80                downstream-paths-only;
81                no-install;
82                node-link-degradation;
83                remote-backup-calculation;
84                per-prefix-calculation {
85                    all;
86                    stubs;
87                }
88            }
89        }
90    }
91}
92"#;
93
94    #[test]
95    fn reads_post_convergence_lfa_and_source_packet_routing() {
96        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
97        let backup = ospf.backup_spf_options.unwrap();
98        assert!(backup.use_post_convergence_lfa);
99        assert_eq!(backup.maximum_labels.as_deref(), Some("5"));
100        assert_eq!(backup.maximum_backup_paths.as_deref(), Some("8"));
101        assert!(backup.use_source_packet_routing);
102        assert!(!backup.disable);
103        assert_eq!(backup.per_prefix_calculation, None);
104    }
105
106    #[test]
107    fn reads_the_flags_and_per_prefix_calculation_of_a_topology() {
108        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
109        let backup = ospf.topologies[0].backup_spf_options.as_ref().unwrap();
110        assert!(backup.disable);
111        assert!(backup.downstream_paths_only);
112        assert!(backup.no_install);
113        assert!(backup.node_link_degradation);
114        assert!(backup.remote_backup_calculation);
115        assert_eq!(
116            backup.per_prefix_calculation,
117            Some(vec!["all".to_string(), "stubs".to_string()])
118        );
119        assert!(!backup.use_post_convergence_lfa);
120    }
121}