use crate::{active_children, arg_of, child_block, split_name};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AcceptedMac {
pub address: String,
pub policer_input: Option<String>,
pub policer_output: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PolicerOverhead {
pub egress: Option<String>,
pub ingress: Option<String>,
}
pub(super) fn parse_accept_source_mac(unit: &ConfigLine) -> Vec<AcceptedMac> {
let Some(accept) = child_block(unit, "accept-source-mac") else {
return Vec::new();
};
active_children(accept)
.filter_map(|entry| {
let (address, _) = split_name(entry.arg("mac-address")?);
let policer = child_block(entry, "policer");
Some(AcceptedMac {
address: address.to_string(),
policer_input: policer.and_then(|p| arg_of(p, "input")).map(str::to_string),
policer_output: policer.and_then(|p| arg_of(p, "output")).map(str::to_string),
})
})
.collect()
}
pub(super) fn parse_policer_overhead(overhead: &ConfigLine) -> PolicerOverhead {
PolicerOverhead {
egress: arg_of(overhead, "egress").map(str::to_string),
ingress: arg_of(overhead, "ingress").map(str::to_string),
}
}
#[cfg(test)]
mod tests {
use crate::interfaces::interfaces;
use asterust::config::juniper;
#[test]
fn extracts_accept_source_mac_and_policer_overhead() {
let ifaces = interfaces(&juniper::parse_braces(
r#"
interfaces {
ge-0/0/0 {
unit 0 {
accept-source-mac {
mac-address 00:11:22:33:44:55 {
policer {
input pin;
output pout;
}
}
mac-address 00:11:22:33:44:66;
}
policer-overhead {
egress 20;
ingress 8;
}
}
unit 1 {
description plain;
}
}
}
"#,
));
let units = &ifaces[0].units;
let macs = &units[0].accept_source_mac;
assert_eq!(macs.len(), 2);
assert_eq!(macs[0].address, "00:11:22:33:44:55");
assert_eq!(macs[0].policer_input.as_deref(), Some("pin"));
assert_eq!(macs[0].policer_output.as_deref(), Some("pout"));
assert_eq!(macs[1].address, "00:11:22:33:44:66");
assert!(macs[1].policer_input.is_none());
let overhead = units[0].policer_overhead.as_ref().unwrap();
assert_eq!(overhead.egress.as_deref(), Some("20"));
assert_eq!(overhead.ingress.as_deref(), Some("8"));
assert!(units[1].accept_source_mac.is_empty() && units[1].policer_overhead.is_none());
}
}