Skip to main content

asterust_junos/protocols/ospf/
database_protection.rs

1//! `database-protection`
2//! `protocols { ospf { database-protection { ... } } }`.
3//!
4//! # See also
5//!
6//! - [database-protection](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/database-protection-edit-protocols-ospf.html)
7
8use crate::{statement_words, value_after};
9use asterust::config::ConfigLine;
10
11/// The OSPF `database-protection` limits on the LSAs a router accepts. Values are kept as written.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct OspfDatabaseProtection {
15    pub maximum_lsa: Option<String>,
16    pub ignore_count: Option<String>,
17    /// Seconds.
18    pub ignore_time: Option<String>,
19    /// Seconds.
20    pub reset_time: Option<String>,
21    /// Percent.
22    pub warning_threshold: Option<String>,
23    pub warning_only: bool,
24}
25
26pub(super) fn parse_database_protection(ospf: &ConfigLine) -> Option<OspfDatabaseProtection> {
27    let words = statement_words(ospf, "database-protection")?;
28    Some(OspfDatabaseProtection {
29        maximum_lsa: value_after(&words, "maximum-lsa"),
30        ignore_count: value_after(&words, "ignore-count"),
31        ignore_time: value_after(&words, "ignore-time"),
32        reset_time: value_after(&words, "reset-time"),
33        warning_threshold: value_after(&words, "warning-threshold"),
34        warning_only: words.contains(&"warning-only"),
35    })
36}
37
38#[cfg(test)]
39mod tests {
40    use crate::protocols::ospf::ospf;
41    use asterust::config::juniper;
42
43    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
44    const CONFIG: &str = r#"
45protocols {
46    ospf {
47        database-protection {
48            maximum-lsa 100000;
49            ignore-count 3;
50            ignore-time 120;
51            reset-time 900;
52            warning-threshold 80;
53            warning-only;
54        }
55    }
56}
57"#;
58
59    #[test]
60    fn reads_the_limits() {
61        let protection = ospf(&juniper::parse_braces(CONFIG))
62            .unwrap()
63            .database_protection
64            .unwrap();
65        assert_eq!(protection.maximum_lsa.as_deref(), Some("100000"));
66        assert_eq!(protection.ignore_count.as_deref(), Some("3"));
67        assert_eq!(protection.ignore_time.as_deref(), Some("120"));
68        assert_eq!(protection.reset_time.as_deref(), Some("900"));
69        assert_eq!(protection.warning_threshold.as_deref(), Some("80"));
70        assert!(protection.warning_only);
71    }
72
73    #[test]
74    fn leaves_unwritten_limits_unset() {
75        let config = juniper::parse_braces("protocols {\n    ospf {\n        database-protection {\n            maximum-lsa 500;\n        }\n    }\n}\n");
76        let protection = ospf(&config).unwrap().database_protection.unwrap();
77        assert_eq!(protection.maximum_lsa.as_deref(), Some("500"));
78        assert_eq!(protection.reset_time, None);
79        assert!(!protection.warning_only);
80    }
81}