asterust_junos/system/
arp.rs1use crate::{active_children, arg_of, child_block, has_leaf, unquote};
12use asterust::config::ConfigLine;
13
14#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct ArpInterface {
18 pub name: String,
19 pub aging_timer: Option<String>,
21}
22
23#[derive(Clone, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct Arp {
27 pub aging_timer: Option<String>,
29 pub arp_retries: Option<String>,
30 pub gratuitous_arp_delay: Option<String>,
32 pub gratuitous_arp_on_ifup: bool,
33 pub interfaces: Vec<ArpInterface>,
35 pub non_subscriber_no_reply: bool,
37 pub passive_learning: bool,
38 pub proactive_arp_detection: bool,
39 pub proxy_arp_reply_for_default: bool,
40 pub purging: bool,
42 pub respond_out_of_subnet: bool,
43 pub unicast_mode_on_expire: bool,
44}
45
46pub(super) fn parse_arp(arp: &ConfigLine) -> Arp {
47 Arp {
48 aging_timer: arg_of(arp, "aging-timer").map(str::to_string),
49 arp_retries: arg_of(arp, "arp-retries").map(str::to_string),
50 gratuitous_arp_delay: arg_of(arp, "gratuitous-arp-delay").map(str::to_string),
51 gratuitous_arp_on_ifup: has_leaf(arp, "gratuitous-arp-on-ifup"),
52 interfaces: child_block(arp, "interfaces")
53 .map(|interfaces| {
54 active_children(interfaces)
55 .map(|interface| ArpInterface {
56 name: unquote(&interface.text).to_string(),
57 aging_timer: arg_of(interface, "aging-timer").map(str::to_string),
58 })
59 .collect()
60 })
61 .unwrap_or_default(),
62 non_subscriber_no_reply: has_leaf(arp, "non-subscriber-no-reply"),
63 passive_learning: has_leaf(arp, "passive-learning"),
64 proactive_arp_detection: has_leaf(arp, "proactive-arp-detection"),
65 proxy_arp_reply_for_default: has_leaf(arp, "proxy-arp-reply-for-default"),
66 purging: has_leaf(arp, "purging"),
67 respond_out_of_subnet: has_leaf(arp, "respond-out-of-subnet"),
68 unicast_mode_on_expire: has_leaf(arp, "unicast-mode-on-expire"),
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use crate::system::system;
75 use asterust::config::juniper;
76
77 #[test]
79 fn extracts_arp_options() {
80 let arp = system(&juniper::parse_braces(
81 r#"
82system {
83 arp {
84 aging-timer 10;
85 arp-retries 6;
86 gratuitous-arp-delay 4;
87 gratuitous-arp-on-ifup;
88 interfaces {
89 ge-0/0/0.0 {
90 aging-timer 30;
91 }
92 ge-0/0/1.0;
93 }
94 non-subscriber-no-reply;
95 passive-learning;
96 proactive-arp-detection;
97 proxy-arp-reply-for-default;
98 purging;
99 respond-out-of-subnet;
100 unicast-mode-on-expire;
101 }
102}
103"#,
104 ))
105 .unwrap()
106 .arp
107 .unwrap();
108 assert_eq!(arp.aging_timer.as_deref(), Some("10"));
109 assert_eq!(arp.arp_retries.as_deref(), Some("6"));
110 assert_eq!(arp.gratuitous_arp_delay.as_deref(), Some("4"));
111 assert!(arp.gratuitous_arp_on_ifup);
112 assert_eq!(arp.interfaces.len(), 2);
113 assert_eq!(arp.interfaces[0].name, "ge-0/0/0.0");
114 assert_eq!(arp.interfaces[0].aging_timer.as_deref(), Some("30"));
115 assert_eq!(arp.interfaces[1].aging_timer, None);
116 assert!(arp.non_subscriber_no_reply);
117 assert!(arp.passive_learning);
118 assert!(arp.proactive_arp_detection);
119 assert!(arp.proxy_arp_reply_for_default);
120 assert!(arp.purging);
121 assert!(arp.respond_out_of_subnet);
122 assert!(arp.unicast_mode_on_expire);
123 }
124
125 #[test]
126 fn arp_options_default_to_off() {
127 let arp = system(&juniper::parse_braces(
128 "system {\n arp {\n purging;\n }\n}\n",
129 ))
130 .unwrap()
131 .arp
132 .unwrap();
133 assert!(!arp.non_subscriber_no_reply);
134 assert!(!arp.passive_learning);
135 assert_eq!(arp.aging_timer, None);
136 assert!(arp.interfaces.is_empty());
137 }
138
139 #[test]
140 fn no_arp_is_none() {
141 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
142 assert!(system.arp.is_none());
143 }
144}