use crate::{arg_of, child_block, has_leaf, toggle};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MultiserviceOptions {
pub core_dump: Option<bool>,
pub syslog: Option<bool>,
pub pic_boot_timeout: Option<String>,
pub down_on_flow_control: bool,
pub dump_on_flow_control: bool,
pub reset_on_flow_control: bool,
pub up_on_flow_control: bool,
}
pub(super) fn parse_multiservice_options(options: &ConfigLine) -> MultiserviceOptions {
let flow_control = |name| {
has_leaf(options, name)
|| child_block(options, "flow-control-options").is_some_and(|block| has_leaf(block, name))
};
MultiserviceOptions {
core_dump: toggle(options, "core-dump"),
syslog: toggle(options, "syslog"),
pic_boot_timeout: arg_of(options, "pic-boot-timeout").map(str::to_string),
down_on_flow_control: flow_control("down-on-flow-control"),
dump_on_flow_control: flow_control("dump-on-flow-control"),
reset_on_flow_control: flow_control("reset-on-flow-control"),
up_on_flow_control: flow_control("up-on-flow-control"),
}
}
#[cfg(test)]
mod tests {
use crate::interfaces::interfaces;
use asterust::config::juniper;
#[test]
fn extracts_multiservice_options() {
let ifaces = interfaces(&juniper::parse_braces(
r#"
interfaces {
mo-1/0/0 {
multiservice-options {
core-dump;
no-syslog;
pic-boot-timeout 300;
flow-control-options {
down-on-flow-control;
reset-on-flow-control;
}
}
}
mo-1/0/1 {
multiservice-options {
no-core-dump;
syslog;
dump-on-flow-control;
}
}
}
"#,
));
let first = ifaces[0].multiservice_options.as_ref().unwrap();
assert_eq!(first.core_dump, Some(true));
assert_eq!(first.syslog, Some(false));
assert_eq!(first.pic_boot_timeout.as_deref(), Some("300"));
assert!(first.down_on_flow_control && first.reset_on_flow_control);
assert!(!first.dump_on_flow_control && !first.up_on_flow_control);
let second = ifaces[1].multiservice_options.as_ref().unwrap();
assert_eq!(second.core_dump, Some(false));
assert_eq!(second.syslog, Some(true));
assert!(second.dump_on_flow_control);
}
}