use crate::{active_children, split_name, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct IsisLevel {
pub level: String,
pub disable: bool,
pub wide_metrics_only: bool,
pub preference: Option<String>,
pub external_preference: Option<String>,
pub prefix_export_limit: Option<String>,
pub max_lsp_size: Option<String>,
pub authentication_type: Option<String>,
pub has_authentication_key: bool,
pub authentication_key_chain: Option<String>,
pub no_authentication_check: bool,
pub no_csnp_authentication: bool,
pub no_hello_authentication: bool,
pub no_psnp_authentication: bool,
pub purge_originator: Option<String>,
}
pub(super) fn parse_level(arg: &str, level: &ConfigLine) -> IsisLevel {
let (number, rest) = split_name(arg);
let words: Vec<&str> = rest
.split_whitespace()
.chain(active_children(level).flat_map(|child| child.text.split_whitespace()))
.collect();
IsisLevel {
level: number.to_string(),
disable: words.contains(&"disable"),
wide_metrics_only: words.contains(&"wide-metrics-only"),
preference: value_after(&words, "preference"),
external_preference: value_after(&words, "external-preference"),
prefix_export_limit: value_after(&words, "prefix-export-limit"),
max_lsp_size: value_after(&words, "max-lsp-size"),
authentication_type: value_after(&words, "authentication-type"),
has_authentication_key: words.contains(&"authentication-key"),
authentication_key_chain: value_after(&words, "authentication-key-chain"),
no_authentication_check: words.contains(&"no-authentication-check"),
no_csnp_authentication: words.contains(&"no-csnp-authentication"),
no_hello_authentication: words.contains(&"no-hello-authentication"),
no_psnp_authentication: words.contains(&"no-psnp-authentication"),
purge_originator: value_after(&words, "purge-originator"),
}
}
#[cfg(test)]
mod tests {
use crate::protocols::isis::isis;
use asterust::config::juniper;
const CONFIG: &str = r#"
protocols {
isis {
level 1 disable;
level 2 wide-metrics-only;
level 2 {
preference 20;
external-preference 170;
prefix-export-limit 500;
max-lsp-size 1492;
authentication-key "$9$secret"; ## SECRET-DATA
authentication-type md5;
no-hello-authentication;
purge-originator self;
}
}
}
"#;
#[test]
fn reads_the_one_line_forms() {
let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
assert!(isis.levels[0].disable);
assert_eq!(isis.levels[0].level, "1");
assert!(isis.levels[1].wide_metrics_only);
assert!(!isis.levels[1].disable);
}
#[test]
fn reads_the_block_form_without_the_key() {
let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
let level = &isis.levels[2];
assert_eq!(level.preference.as_deref(), Some("20"));
assert_eq!(level.external_preference.as_deref(), Some("170"));
assert_eq!(level.prefix_export_limit.as_deref(), Some("500"));
assert_eq!(level.max_lsp_size.as_deref(), Some("1492"));
assert_eq!(level.authentication_type.as_deref(), Some("md5"));
assert!(level.has_authentication_key);
assert_eq!(level.authentication_key_chain, None);
assert!(level.no_hello_authentication);
assert!(!level.no_csnp_authentication);
assert_eq!(level.purge_originator.as_deref(), Some("self"));
}
}