use crate::{statement_words, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct OspfDatabaseProtection {
pub maximum_lsa: Option<String>,
pub ignore_count: Option<String>,
pub ignore_time: Option<String>,
pub reset_time: Option<String>,
pub warning_threshold: Option<String>,
pub warning_only: bool,
}
pub(super) fn parse_database_protection(ospf: &ConfigLine) -> Option<OspfDatabaseProtection> {
let words = statement_words(ospf, "database-protection")?;
Some(OspfDatabaseProtection {
maximum_lsa: value_after(&words, "maximum-lsa"),
ignore_count: value_after(&words, "ignore-count"),
ignore_time: value_after(&words, "ignore-time"),
reset_time: value_after(&words, "reset-time"),
warning_threshold: value_after(&words, "warning-threshold"),
warning_only: words.contains(&"warning-only"),
})
}
#[cfg(test)]
mod tests {
use crate::protocols::ospf::ospf;
use asterust::config::juniper;
const CONFIG: &str = r#"
protocols {
ospf {
database-protection {
maximum-lsa 100000;
ignore-count 3;
ignore-time 120;
reset-time 900;
warning-threshold 80;
warning-only;
}
}
}
"#;
#[test]
fn reads_the_limits() {
let protection = ospf(&juniper::parse_braces(CONFIG))
.unwrap()
.database_protection
.unwrap();
assert_eq!(protection.maximum_lsa.as_deref(), Some("100000"));
assert_eq!(protection.ignore_count.as_deref(), Some("3"));
assert_eq!(protection.ignore_time.as_deref(), Some("120"));
assert_eq!(protection.reset_time.as_deref(), Some("900"));
assert_eq!(protection.warning_threshold.as_deref(), Some("80"));
assert!(protection.warning_only);
}
#[test]
fn leaves_unwritten_limits_unset() {
let config = juniper::parse_braces("protocols {\n ospf {\n database-protection {\n maximum-lsa 500;\n }\n }\n}\n");
let protection = ospf(&config).unwrap().database_protection.unwrap();
assert_eq!(protection.maximum_lsa.as_deref(), Some("500"));
assert_eq!(protection.reset_time, None);
assert!(!protection.warning_only);
}
}