use crate::{active_children, arg_of, child_block, has_leaf, unquote};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ArpInterface {
pub name: String,
pub aging_timer: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Arp {
pub aging_timer: Option<String>,
pub arp_retries: Option<String>,
pub gratuitous_arp_delay: Option<String>,
pub gratuitous_arp_on_ifup: bool,
pub interfaces: Vec<ArpInterface>,
pub non_subscriber_no_reply: bool,
pub passive_learning: bool,
pub proactive_arp_detection: bool,
pub proxy_arp_reply_for_default: bool,
pub purging: bool,
pub respond_out_of_subnet: bool,
pub unicast_mode_on_expire: bool,
}
pub(super) fn parse_arp(arp: &ConfigLine) -> Arp {
Arp {
aging_timer: arg_of(arp, "aging-timer").map(str::to_string),
arp_retries: arg_of(arp, "arp-retries").map(str::to_string),
gratuitous_arp_delay: arg_of(arp, "gratuitous-arp-delay").map(str::to_string),
gratuitous_arp_on_ifup: has_leaf(arp, "gratuitous-arp-on-ifup"),
interfaces: child_block(arp, "interfaces")
.map(|interfaces| {
active_children(interfaces)
.map(|interface| ArpInterface {
name: unquote(&interface.text).to_string(),
aging_timer: arg_of(interface, "aging-timer").map(str::to_string),
})
.collect()
})
.unwrap_or_default(),
non_subscriber_no_reply: has_leaf(arp, "non-subscriber-no-reply"),
passive_learning: has_leaf(arp, "passive-learning"),
proactive_arp_detection: has_leaf(arp, "proactive-arp-detection"),
proxy_arp_reply_for_default: has_leaf(arp, "proxy-arp-reply-for-default"),
purging: has_leaf(arp, "purging"),
respond_out_of_subnet: has_leaf(arp, "respond-out-of-subnet"),
unicast_mode_on_expire: has_leaf(arp, "unicast-mode-on-expire"),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_arp_options() {
let arp = system(&juniper::parse_braces(
r#"
system {
arp {
aging-timer 10;
arp-retries 6;
gratuitous-arp-delay 4;
gratuitous-arp-on-ifup;
interfaces {
ge-0/0/0.0 {
aging-timer 30;
}
ge-0/0/1.0;
}
non-subscriber-no-reply;
passive-learning;
proactive-arp-detection;
proxy-arp-reply-for-default;
purging;
respond-out-of-subnet;
unicast-mode-on-expire;
}
}
"#,
))
.unwrap()
.arp
.unwrap();
assert_eq!(arp.aging_timer.as_deref(), Some("10"));
assert_eq!(arp.arp_retries.as_deref(), Some("6"));
assert_eq!(arp.gratuitous_arp_delay.as_deref(), Some("4"));
assert!(arp.gratuitous_arp_on_ifup);
assert_eq!(arp.interfaces.len(), 2);
assert_eq!(arp.interfaces[0].name, "ge-0/0/0.0");
assert_eq!(arp.interfaces[0].aging_timer.as_deref(), Some("30"));
assert_eq!(arp.interfaces[1].aging_timer, None);
assert!(arp.non_subscriber_no_reply);
assert!(arp.passive_learning);
assert!(arp.proactive_arp_detection);
assert!(arp.proxy_arp_reply_for_default);
assert!(arp.purging);
assert!(arp.respond_out_of_subnet);
assert!(arp.unicast_mode_on_expire);
}
#[test]
fn arp_options_default_to_off() {
let arp = system(&juniper::parse_braces(
"system {\n arp {\n purging;\n }\n}\n",
))
.unwrap()
.arp
.unwrap();
assert!(!arp.non_subscriber_no_reply);
assert!(!arp.passive_learning);
assert_eq!(arp.aging_timer, None);
assert!(arp.interfaces.is_empty());
}
#[test]
fn no_arp_is_none() {
let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
assert!(system.arp.is_none());
}
}