use crate::{arg_of, child_block, has_leaf, named_children, split_name, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsPollingProfile {
pub name: String,
pub interval: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsStatistics {
pub auto_bandwidth: bool,
pub auto_bandwidth_polling_profiles: Vec<MplsPollingProfile>,
pub file: Option<String>,
pub files: Option<String>,
pub size: Option<String>,
pub world_readable: Option<bool>,
pub interval: Option<String>,
pub no_transit_statistics: bool,
pub traffic_class_statistics: bool,
pub transit_statistics_polling: bool,
pub statistics_query_batch_size: Option<String>,
}
pub(super) fn parse_statistics(mpls: &ConfigLine) -> Option<MplsStatistics> {
let statistics = child_block(mpls, "statistics")?;
let (file, options) = arg_of(statistics, "file")
.map(|arg| {
let (name, rest) = split_name(arg);
(Some(name.to_string()), rest)
})
.unwrap_or((None, ""));
let words: Vec<&str> = options.split_whitespace().collect();
let auto_bandwidth = child_block(statistics, "auto-bandwidth");
Some(MplsStatistics {
auto_bandwidth: auto_bandwidth.is_some(),
auto_bandwidth_polling_profiles: auto_bandwidth
.map(|block| {
named_children(block, "polling-profile")
.map(|(name, profile)| MplsPollingProfile {
name: name.to_string(),
interval: arg_of(profile, "interval").map(str::to_string),
})
.collect()
})
.unwrap_or_default(),
file,
files: value_after(&words, "files"),
size: value_after(&words, "size"),
world_readable: if words.contains(&"world-readable") {
Some(true)
} else if words.contains(&"no-world-readable") {
Some(false)
} else {
None
},
interval: arg_of(statistics, "interval").map(str::to_string),
no_transit_statistics: has_leaf(statistics, "no-transit-statistics"),
traffic_class_statistics: has_leaf(statistics, "traffic-class-statistics"),
transit_statistics_polling: has_leaf(statistics, "transit-statistics-polling"),
statistics_query_batch_size: arg_of(statistics, "statistics-query-batch-size").map(str::to_string),
})
}
#[cfg(test)]
mod tests {
use crate::protocols::mpls::mpls;
use asterust::config::juniper;
const CONFIG: &str = r#"
protocols {
mpls {
statistics {
auto-bandwidth;
file mpls.stats size 1m files 5 world-readable;
interval 60;
no-transit-statistics;
traffic-class-statistics;
transit-statistics-polling;
statistics-query-batch-size 100;
}
}
}
"#;
#[test]
fn reads_the_statistics_block() {
let statistics = mpls(&juniper::parse_braces(CONFIG)).unwrap().statistics.unwrap();
assert!(statistics.auto_bandwidth);
assert_eq!(statistics.file.as_deref(), Some("mpls.stats"));
assert_eq!(statistics.files.as_deref(), Some("5"));
assert_eq!(statistics.size.as_deref(), Some("1m"));
assert_eq!(statistics.world_readable, Some(true));
assert_eq!(statistics.interval.as_deref(), Some("60"));
assert!(statistics.no_transit_statistics);
assert!(statistics.traffic_class_statistics);
assert!(statistics.transit_statistics_polling);
assert_eq!(statistics.statistics_query_batch_size.as_deref(), Some("100"));
}
#[test]
fn a_bare_statistics_block_has_nothing_set() {
let statistics = mpls(&juniper::parse_braces(
"protocols {\n mpls {\n statistics;\n }\n}\n",
))
.unwrap()
.statistics
.unwrap();
assert_eq!(statistics, Default::default());
}
#[test]
fn no_world_readable_is_read() {
let statistics = mpls(&juniper::parse_braces(
"protocols {\n mpls {\n statistics {\n file s no-world-readable;\n }\n }\n}\n",
))
.unwrap()
.statistics
.unwrap();
assert_eq!(statistics.world_readable, Some(false));
}
const DOC_TRAFFIC_CLASS: &str = r#"
protocols {
mpls {
statistics {
traffic-class-statistics;
}
label-switched-path R1-R2 {
to 10.20.0.1;
oam mpls-tp-mode;
ultimate-hop-popping;
associate-lsp R2-R1;
}
interface ge-0/0/0.0;
interface lo0.0;
interface fxp0.0 {
disable;
}
}
}
"#;
const DOC_AUTO_BANDWIDTH: &str = r#"
protocols {
mpls {
statistics {
interval 60;
auto-bandwidth {
polling-profile poll-prof-1 {
interval 30;
}
}
}
label-switched-path lsp1 {
to 192.168.4.4;
auto-bandwidth {
adjust-threshold-options {
overflow-limit 3;
overflow-threshold 10;
threshold 20;
underflow-limit 6;
underflow-threshold 30;
}
maximum-bandwidth 10g;
polling-profile poll-prof-1;
}
}
}
}
"#;
#[test]
fn reads_the_guide_traffic_class_statistics() {
let mpls = mpls(&juniper::parse_braces(DOC_TRAFFIC_CLASS)).unwrap();
let statistics = mpls.statistics.unwrap();
assert!(statistics.traffic_class_statistics);
assert!(!statistics.auto_bandwidth);
assert_eq!(mpls.label_switched_paths[0].name, "R1-R2");
assert_eq!(mpls.label_switched_paths[0].to.as_deref(), Some("10.20.0.1"));
assert_eq!(mpls.interfaces.len(), 3);
assert!(mpls.interfaces[2].disable);
}
#[test]
fn reads_the_guide_auto_bandwidth_statistics() {
let mpls = mpls(&juniper::parse_braces(DOC_AUTO_BANDWIDTH)).unwrap();
let statistics = mpls.statistics.unwrap();
assert_eq!(statistics.interval.as_deref(), Some("60"));
assert!(statistics.auto_bandwidth);
assert_eq!(statistics.auto_bandwidth_polling_profiles.len(), 1);
assert_eq!(statistics.auto_bandwidth_polling_profiles[0].name, "poll-prof-1");
assert_eq!(
statistics.auto_bandwidth_polling_profiles[0].interval.as_deref(),
Some("30")
);
assert_eq!(mpls.label_switched_paths[0].to.as_deref(), Some("192.168.4.4"));
}
}