use crate::{args_of, child_block, has_leaf, list_values};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EntropyLabel {
pub elc_v2_compatible: bool,
pub no_next_hop_validation: bool,
pub import: Vec<String>,
}
pub(super) fn parse_entropy_label(node: &ConfigLine) -> Option<EntropyLabel> {
let label = child_block(node, "entropy-label")?;
Some(EntropyLabel {
elc_v2_compatible: has_leaf(label, "elc-v2-compatible"),
no_next_hop_validation: has_leaf(label, "no-next-hop-validation"),
import: args_of(label, "import").into_iter().flat_map(list_values).collect(),
})
}
#[cfg(test)]
mod tests {
use crate::protocols::bgp::bgp;
use asterust::config::juniper;
const CONFIG: &str = r#"
protocols {
bgp {
group LU {
family inet {
labeled-unicast {
entropy-label {
elc-v2-compatible;
import [ ENTROPY-A ENTROPY-B ];
}
}
unicast {
extended-nexthop;
}
}
}
}
}
"#;
#[test]
fn reads_the_entropy_label_options() {
let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
let label = bgp.groups[0].options.address_families[0]
.entropy_label
.as_ref()
.unwrap();
assert!(label.elc_v2_compatible);
assert!(!label.no_next_hop_validation);
assert_eq!(label.import, ["ENTROPY-A", "ENTROPY-B"]);
}
#[test]
fn reads_extended_nexthop() {
let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
let families = &bgp.groups[0].options.address_families;
assert!(families[1].extended_nexthop);
assert!(!families[0].extended_nexthop);
assert_eq!(families[1].entropy_label, None);
}
}