use crate::{child_block, statement_words};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct OspfTrafficEngineering {
pub advertise_unnumbered_interfaces: bool,
pub credibility_protocol_preference: bool,
pub multicast_rpf_routes: bool,
pub no_topology: bool,
pub l3_unicast_topology: bool,
pub shortcuts: bool,
pub lsp_metric_into_summary: bool,
pub ignore_lsp_metrics: bool,
pub advertisement: bool,
pub advertisement_always: bool,
}
pub(super) fn parse_traffic_engineering(ospf: &ConfigLine) -> Option<OspfTrafficEngineering> {
let words = statement_words(ospf, "traffic-engineering")?;
let shortcuts = child_block(ospf, "traffic-engineering").and_then(|te| statement_words(te, "shortcuts"));
let in_shortcuts = |flag: &str| shortcuts.as_ref().is_some_and(|words| words.contains(&flag));
let advertisement = child_block(ospf, "traffic-engineering").and_then(|te| statement_words(te, "advertisement"));
Some(OspfTrafficEngineering {
advertise_unnumbered_interfaces: words.contains(&"advertise-unnumbered-interfaces"),
credibility_protocol_preference: words.contains(&"credibility-protocol-preference"),
multicast_rpf_routes: words.contains(&"multicast-rpf-routes"),
no_topology: words.contains(&"no-topology"),
l3_unicast_topology: words.contains(&"l3-unicast-topology"),
shortcuts: shortcuts.is_some(),
lsp_metric_into_summary: in_shortcuts("lsp-metric-into-summary"),
ignore_lsp_metrics: words.contains(&"ignore-lsp-metrics") || in_shortcuts("ignore-lsp-metrics"),
advertisement: advertisement.is_some(),
advertisement_always: advertisement.is_some_and(|words| words.contains(&"always")),
})
}
#[cfg(test)]
mod tests {
use crate::protocols::ospf::ospf;
use asterust::config::juniper;
const CONFIG: &str = r#"
protocols {
ospf {
traffic-engineering {
advertise-unnumbered-interfaces;
credibility-protocol-preference;
no-topology;
shortcuts {
lsp-metric-into-summary;
ignore-lsp-metrics;
}
}
}
}
"#;
#[test]
fn reads_the_flags_and_shortcuts() {
let te = ospf(&juniper::parse_braces(CONFIG))
.unwrap()
.traffic_engineering
.unwrap();
assert!(te.advertise_unnumbered_interfaces);
assert!(te.credibility_protocol_preference);
assert!(te.no_topology);
assert!(te.shortcuts);
assert!(te.lsp_metric_into_summary);
assert!(te.ignore_lsp_metrics);
assert!(!te.multicast_rpf_routes);
}
const ADVERTISEMENT: &str = r#"
protocols {
ospf {
traffic-engineering {
advertisement always;
}
}
}
"#;
#[test]
fn reads_advertisement_always() {
let te = ospf(&juniper::parse_braces(ADVERTISEMENT))
.unwrap()
.traffic_engineering
.unwrap();
assert!(te.advertisement);
assert!(te.advertisement_always);
assert!(!te.shortcuts);
}
#[test]
fn reads_ignore_lsp_metrics_directly_under_the_statement() {
let config = juniper::parse_braces("protocols {\n ospf {\n traffic-engineering {\n ignore-lsp-metrics;\n }\n }\n}\n");
let te = ospf(&config).unwrap().traffic_engineering.unwrap();
assert!(te.ignore_lsp_metrics);
assert!(!te.shortcuts);
}
}