use super::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{arg_of, args_of, child_block, has_leaf, list_values, named_children, statement_words, top_level};
use asterust::config::{Config, ConfigLine};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LldpChassisId {
pub id_type: Option<String>,
pub id_value: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LldpInterface {
pub name: String,
pub enabled: Option<bool>,
pub dest_mac_type: Option<String>,
pub power_negotiation: Option<bool>,
pub trap_notification: Option<bool>,
pub tlv_filter: Vec<String>,
pub tlv_select: Vec<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Lldp {
pub enabled: Option<bool>,
pub advertisement_interval: Option<String>,
pub hold_multiplier: Option<String>,
pub transmit_delay: Option<String>,
pub chassis_id: Option<LldpChassisId>,
pub dest_mac_type: Option<String>,
pub lldp_configuration_notification_interval: Option<String>,
pub lldp_tx_fast_init: bool,
pub management_address: Option<String>,
pub management_interface: Option<String>,
pub mau_type: bool,
pub netbios_snooping: bool,
pub no_tagging: bool,
pub neighbour_port_info_display: Option<String>,
pub port_description_type: Option<String>,
pub port_id_subtype: Option<String>,
pub ptopo_configuration_maximum_hold_time: Option<String>,
pub ptopo_configuration_trap_interval: Option<String>,
pub tlv_filter: Vec<String>,
pub tlv_select: Vec<String>,
pub vlan_name_tlv_option: Option<String>,
pub traceoptions: Option<Traceoptions>,
pub interfaces: Vec<LldpInterface>,
}
pub(super) fn enabled(line: &ConfigLine) -> Option<bool> {
if has_leaf(line, "disable") {
Some(false)
} else if has_leaf(line, "enable") {
Some(true)
} else {
None
}
}
pub(super) fn names(line: &ConfigLine, keyword: &str) -> Vec<String> {
args_of(line, keyword).into_iter().flat_map(list_values).collect()
}
fn toggle_word(line: &ConfigLine, keyword: &str) -> Option<bool> {
statement_words(line, keyword).map(|words| !words.contains(&"disable"))
}
fn parse_interface(name: &str, interface: &ConfigLine) -> LldpInterface {
LldpInterface {
name: name.to_string(),
enabled: enabled(interface),
dest_mac_type: arg_of(interface, "dest-mac-type").map(str::to_string),
power_negotiation: toggle_word(interface, "power-negotiation"),
trap_notification: toggle_word(interface, "trap-notification"),
tlv_filter: names(interface, "tlv-filter"),
tlv_select: names(interface, "tlv-select"),
}
}
pub fn lldp(config: &Config) -> Option<Lldp> {
let lldp = child_block(top_level(config, "protocols")?, "lldp")?;
let field = |keyword: &str| arg_of(lldp, keyword).map(str::to_string);
Some(Lldp {
enabled: enabled(lldp),
advertisement_interval: field("advertisement-interval"),
hold_multiplier: field("hold-multiplier"),
transmit_delay: field("transmit-delay"),
chassis_id: child_block(lldp, "chassis-id").map(|block| LldpChassisId {
id_type: arg_of(block, "chassis-id-type").map(str::to_string),
id_value: arg_of(block, "chassis-id-value").map(str::to_string),
}),
dest_mac_type: field("dest-mac-type"),
lldp_configuration_notification_interval: field("lldp-configuration-notification-interval"),
lldp_tx_fast_init: has_leaf(lldp, "lldp-tx-fast-init"),
management_address: field("management-address"),
management_interface: field("management-interface"),
mau_type: has_leaf(lldp, "mau-type"),
netbios_snooping: has_leaf(lldp, "netbios-snooping"),
no_tagging: has_leaf(lldp, "no-tagging"),
neighbour_port_info_display: field("neighbour-port-info-display"),
port_description_type: field("port-description-type"),
port_id_subtype: field("port-id-subtype"),
ptopo_configuration_maximum_hold_time: field("ptopo-configuration-maximum-hold-time"),
ptopo_configuration_trap_interval: field("ptopo-configuration-trap-interval"),
tlv_filter: names(lldp, "tlv-filter"),
tlv_select: names(lldp, "tlv-select"),
vlan_name_tlv_option: field("vlan-name-tlv-option"),
traceoptions: parse_traceoptions(lldp),
interfaces: named_children(lldp, "interface")
.map(|(name, interface)| parse_interface(name, interface))
.collect(),
})
}
#[cfg(test)]
mod tests {
use super::*;
use asterust::config::juniper;
const DOC_EXAMPLE: &str = r#"
protocols {
lldp {
advertisement-interval 30;
hold-multiplier 4;
interface all {
disable;
}
interface ge-1/1/1;
lldp-configuration-notification-interval 30;
ptopo-configuration-maximum-hold-time 300;
ptopo-configuration-trap-interval 30;
transmit-delay 2;
}
}
"#;
const EXTRA: &str = r#"
protocols {
lldp {
enable;
chassis-id {
chassis-id-type locally-assigned;
chassis-id-value core1;
}
dest-mac-type nearest-customer-bridge;
lldp-tx-fast-init;
management-interface fxp0;
mau-type;
netbios-snooping;
no-tagging;
neighbour-port-info-display port-description;
port-description-type interface-description;
port-id-subtype locally-assigned;
tlv-select [ system-name system-description ];
vlan-name-tlv-option vlan-id;
traceoptions {
file lldp.log size 1m;
flag config;
flag protocol;
}
interface ge-0/0/0 {
enable;
dest-mac-type nearest-bridge;
power-negotiation disable;
trap-notification enable;
tlv-filter port-vlan-id;
}
interface ge-0/0/1 {
power-negotiation;
trap-notification disable;
}
}
}
"#;
fn lldp_of(text: &str) -> Lldp {
lldp(&juniper::parse_braces(text)).unwrap()
}
#[test]
fn reads_the_guide_example() {
let lldp = lldp_of(DOC_EXAMPLE);
assert_eq!(lldp.advertisement_interval.as_deref(), Some("30"));
assert_eq!(lldp.hold_multiplier.as_deref(), Some("4"));
assert_eq!(lldp.lldp_configuration_notification_interval.as_deref(), Some("30"));
assert_eq!(lldp.ptopo_configuration_maximum_hold_time.as_deref(), Some("300"));
assert_eq!(lldp.ptopo_configuration_trap_interval.as_deref(), Some("30"));
assert_eq!(lldp.transmit_delay.as_deref(), Some("2"));
assert_eq!(lldp.enabled, None);
assert_eq!(lldp.interfaces.len(), 2);
assert_eq!(lldp.interfaces[0].name, "all");
assert_eq!(lldp.interfaces[0].enabled, Some(false));
assert_eq!(lldp.interfaces[1].name, "ge-1/1/1");
assert_eq!(lldp.interfaces[1].enabled, None);
}
#[test]
fn reads_the_global_options() {
let lldp = lldp_of(EXTRA);
assert_eq!(lldp.enabled, Some(true));
let chassis = lldp.chassis_id.unwrap();
assert_eq!(chassis.id_type.as_deref(), Some("locally-assigned"));
assert_eq!(chassis.id_value.as_deref(), Some("core1"));
assert_eq!(lldp.dest_mac_type.as_deref(), Some("nearest-customer-bridge"));
assert!(lldp.lldp_tx_fast_init);
assert_eq!(lldp.management_interface.as_deref(), Some("fxp0"));
assert_eq!(lldp.management_address, None);
assert!(lldp.mau_type);
assert!(lldp.netbios_snooping);
assert!(lldp.no_tagging);
assert_eq!(lldp.neighbour_port_info_display.as_deref(), Some("port-description"));
assert_eq!(lldp.port_description_type.as_deref(), Some("interface-description"));
assert_eq!(lldp.port_id_subtype.as_deref(), Some("locally-assigned"));
assert_eq!(lldp.tlv_select, ["system-name", "system-description"]);
assert!(lldp.tlv_filter.is_empty());
assert_eq!(lldp.vlan_name_tlv_option.as_deref(), Some("vlan-id"));
let trace = lldp.traceoptions.unwrap();
assert_eq!(trace.file.as_deref(), Some("lldp.log"));
assert_eq!(trace.flags[1].name, "protocol");
}
#[test]
fn reads_the_interface_options() {
let lldp = lldp_of(EXTRA);
let first = &lldp.interfaces[0];
assert_eq!(first.enabled, Some(true));
assert_eq!(first.dest_mac_type.as_deref(), Some("nearest-bridge"));
assert_eq!(first.power_negotiation, Some(false));
assert_eq!(first.trap_notification, Some(true));
assert_eq!(first.tlv_filter, ["port-vlan-id"]);
assert!(first.tlv_select.is_empty());
let second = &lldp.interfaces[1];
assert_eq!(second.enabled, None);
assert_eq!(second.power_negotiation, Some(true));
assert_eq!(second.trap_notification, Some(false));
}
#[test]
fn a_bare_lldp_block_has_nothing_set() {
let lldp = lldp_of("protocols {\n lldp;\n}\n");
assert_eq!(lldp, Lldp::default());
}
#[test]
fn disabled_lldp_is_read() {
assert_eq!(
lldp_of("protocols {\n lldp {\n disable;\n }\n}\n").enabled,
Some(false)
);
}
#[test]
fn no_lldp_is_none() {
let config = juniper::parse_braces("protocols {\n ospf {\n area 0.0.0.0;\n }\n}\n");
assert_eq!(lldp(&config), None);
}
}