Skip to main content

asterust_junos/protocols/isis/
backup_spf_options.rs

1//! `backup-spf-options`
2//! `protocols { isis { backup-spf-options { ... } } }`.
3//!
4//! # See also
5//!
6//! - [backup-spf-options (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/backup-spf-options-edit-protocols-isis.html)
7//! - [use-post-convergence-lfa](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/protocols-isis-backup-spf-options-use-post-convergence-lfa.html)
8
9use crate::{child_block, statement_words, value_after};
10use asterust::config::ConfigLine;
11
12/// The IS-IS `backup-spf-options`.
13#[derive(Clone, Debug, Default, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct IsisBackupSpfOptions {
16    pub node_link_degradation: bool,
17    pub per_prefix_calculation: bool,
18    pub remote_backup_calculation: bool,
19    pub use_source_packet_routing: bool,
20    /// A `use-post-convergence-lfa` statement is configured.
21    pub use_post_convergence_lfa: bool,
22    pub maximum_backup_paths: Option<String>,
23    pub maximum_labels: Option<String>,
24}
25
26pub(super) fn parse_backup_spf_options(isis: &ConfigLine) -> Option<IsisBackupSpfOptions> {
27    let words = statement_words(isis, "backup-spf-options")?;
28    let lfa =
29        child_block(isis, "backup-spf-options").and_then(|backup| statement_words(backup, "use-post-convergence-lfa"));
30    Some(IsisBackupSpfOptions {
31        node_link_degradation: words.contains(&"node-link-degradation"),
32        per_prefix_calculation: words.contains(&"per-prefix-calculation"),
33        remote_backup_calculation: words.contains(&"remote-backup-calculation"),
34        use_source_packet_routing: words.contains(&"use-source-packet-routing"),
35        use_post_convergence_lfa: lfa.is_some(),
36        maximum_backup_paths: lfa.as_ref().and_then(|lfa| value_after(lfa, "maximum-backup-paths")),
37        maximum_labels: lfa.as_ref().and_then(|lfa| value_after(lfa, "maximum-labels")),
38    })
39}
40
41#[cfg(test)]
42mod tests {
43    use crate::protocols::isis::isis;
44    use asterust::config::juniper;
45
46    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
47    const CONFIG: &str = r#"
48protocols {
49    isis {
50        backup-spf-options {
51            node-link-degradation;
52            per-prefix-calculation;
53            remote-backup-calculation;
54            use-source-packet-routing;
55            use-post-convergence-lfa {
56                maximum-backup-paths 8;
57                maximum-labels 5;
58            }
59        }
60    }
61}
62"#;
63
64    #[test]
65    fn reads_the_flags_and_post_convergence_lfa() {
66        let backup = isis(&juniper::parse_braces(CONFIG))
67            .unwrap()
68            .backup_spf_options
69            .unwrap();
70        assert!(backup.node_link_degradation);
71        assert!(backup.per_prefix_calculation);
72        assert!(backup.remote_backup_calculation);
73        assert!(backup.use_source_packet_routing);
74        assert!(backup.use_post_convergence_lfa);
75        assert_eq!(backup.maximum_backup_paths.as_deref(), Some("8"));
76        assert_eq!(backup.maximum_labels.as_deref(), Some("5"));
77    }
78
79    #[test]
80    fn reads_the_inline_lfa_form_and_absence() {
81        let config = juniper::parse_braces(
82            "protocols {\n    isis {\n        backup-spf-options {\n            use-post-convergence-lfa maximum-labels 4;\n        }\n    }\n}\n",
83        );
84        let backup = isis(&config).unwrap().backup_spf_options.unwrap();
85        assert!(backup.use_post_convergence_lfa);
86        assert_eq!(backup.maximum_labels.as_deref(), Some("4"));
87        assert_eq!(backup.maximum_backup_paths, None);
88        assert!(!backup.node_link_degradation);
89    }
90}