asterust_junos/protocols/
lldp.rs1use super::traceoptions::{parse_traceoptions, Traceoptions};
17use crate::{arg_of, args_of, child_block, has_leaf, list_values, named_children, statement_words, top_level};
18use asterust::config::{Config, ConfigLine};
19
20#[derive(Clone, Debug, Default, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct LldpChassisId {
24 pub id_type: Option<String>,
27 pub id_value: Option<String>,
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct LldpInterface {
34 pub name: String,
35 pub enabled: Option<bool>,
37 pub dest_mac_type: Option<String>,
38 pub power_negotiation: Option<bool>,
40 pub trap_notification: Option<bool>,
42 pub tlv_filter: Vec<String>,
43 pub tlv_select: Vec<String>,
44}
45
46#[derive(Clone, Debug, Default, PartialEq, Eq)]
48#[non_exhaustive]
49pub struct Lldp {
50 pub enabled: Option<bool>,
52 pub advertisement_interval: Option<String>,
54 pub hold_multiplier: Option<String>,
55 pub transmit_delay: Option<String>,
57 pub chassis_id: Option<LldpChassisId>,
58 pub dest_mac_type: Option<String>,
60 pub lldp_configuration_notification_interval: Option<String>,
62 pub lldp_tx_fast_init: bool,
63 pub management_address: Option<String>,
64 pub management_interface: Option<String>,
65 pub mau_type: bool,
66 pub netbios_snooping: bool,
67 pub no_tagging: bool,
68 pub neighbour_port_info_display: Option<String>,
70 pub port_description_type: Option<String>,
72 pub port_id_subtype: Option<String>,
74 pub ptopo_configuration_maximum_hold_time: Option<String>,
76 pub ptopo_configuration_trap_interval: Option<String>,
78 pub tlv_filter: Vec<String>,
79 pub tlv_select: Vec<String>,
80 pub vlan_name_tlv_option: Option<String>,
82 pub traceoptions: Option<Traceoptions>,
83 pub interfaces: Vec<LldpInterface>,
84}
85
86pub(super) fn enabled(line: &ConfigLine) -> Option<bool> {
87 if has_leaf(line, "disable") {
88 Some(false)
89 } else if has_leaf(line, "enable") {
90 Some(true)
91 } else {
92 None
93 }
94}
95
96pub(super) fn interface_entries(line: &ConfigLine) -> impl Iterator<Item = (String, &ConfigLine)> {
98 named_children(line, "interface")
99 .flat_map(|(name, block)| list_values(name).into_iter().map(move |name| (name, block)))
100}
101
102pub(super) fn names(line: &ConfigLine, keyword: &str) -> Vec<String> {
103 args_of(line, keyword).into_iter().flat_map(list_values).collect()
104}
105
106fn toggle_word(line: &ConfigLine, keyword: &str) -> Option<bool> {
108 statement_words(line, keyword).map(|words| !words.contains(&"disable"))
109}
110
111fn parse_interface(name: &str, interface: &ConfigLine) -> LldpInterface {
112 LldpInterface {
113 name: name.to_string(),
114 enabled: enabled(interface),
115 dest_mac_type: arg_of(interface, "dest-mac-type").map(str::to_string),
116 power_negotiation: toggle_word(interface, "power-negotiation"),
117 trap_notification: toggle_word(interface, "trap-notification"),
118 tlv_filter: names(interface, "tlv-filter"),
119 tlv_select: names(interface, "tlv-select"),
120 }
121}
122
123pub fn lldp(config: &Config) -> Option<Lldp> {
137 child_block(top_level(config, "protocols")?, "lldp").map(parse_lldp)
138}
139
140pub(crate) fn parse_lldp(lldp: &ConfigLine) -> Lldp {
142 let field = |keyword: &str| arg_of(lldp, keyword).map(str::to_string);
143 Lldp {
144 enabled: enabled(lldp),
145 advertisement_interval: field("advertisement-interval"),
146 hold_multiplier: field("hold-multiplier"),
147 transmit_delay: field("transmit-delay"),
148 chassis_id: child_block(lldp, "chassis-id").map(|block| LldpChassisId {
149 id_type: arg_of(block, "chassis-id-type").map(str::to_string),
150 id_value: arg_of(block, "chassis-id-value").map(str::to_string),
151 }),
152 dest_mac_type: field("dest-mac-type"),
153 lldp_configuration_notification_interval: field("lldp-configuration-notification-interval"),
154 lldp_tx_fast_init: has_leaf(lldp, "lldp-tx-fast-init"),
155 management_address: field("management-address"),
156 management_interface: field("management-interface"),
157 mau_type: has_leaf(lldp, "mau-type"),
158 netbios_snooping: has_leaf(lldp, "netbios-snooping"),
159 no_tagging: has_leaf(lldp, "no-tagging"),
160 neighbour_port_info_display: field("neighbour-port-info-display"),
161 port_description_type: field("port-description-type"),
162 port_id_subtype: field("port-id-subtype"),
163 ptopo_configuration_maximum_hold_time: field("ptopo-configuration-maximum-hold-time"),
164 ptopo_configuration_trap_interval: field("ptopo-configuration-trap-interval"),
165 tlv_filter: names(lldp, "tlv-filter"),
166 tlv_select: names(lldp, "tlv-select"),
167 vlan_name_tlv_option: field("vlan-name-tlv-option"),
168 traceoptions: parse_traceoptions(lldp),
169 interfaces: interface_entries(lldp)
170 .map(|(name, interface)| parse_interface(&name, interface))
171 .collect(),
172 }
173}
174
175#[cfg(test)]
176mod tests {
177 use super::*;
178 use asterust::config::juniper;
179
180 const DOC_EXAMPLE: &str = r#"
182protocols {
183 lldp {
184 advertisement-interval 30;
185 hold-multiplier 4;
186 interface all {
187 disable;
188 }
189 interface ge-1/1/1;
190 lldp-configuration-notification-interval 30;
191 ptopo-configuration-maximum-hold-time 300;
192 ptopo-configuration-trap-interval 30;
193 transmit-delay 2;
194 }
195}
196"#;
197
198 const EXTRA: &str = r#"
201protocols {
202 lldp {
203 enable;
204 chassis-id {
205 chassis-id-type locally-assigned;
206 chassis-id-value core1;
207 }
208 dest-mac-type nearest-customer-bridge;
209 lldp-tx-fast-init;
210 management-interface fxp0;
211 mau-type;
212 netbios-snooping;
213 no-tagging;
214 neighbour-port-info-display port-description;
215 port-description-type interface-description;
216 port-id-subtype locally-assigned;
217 tlv-select [ system-name system-description ];
218 vlan-name-tlv-option vlan-id;
219 traceoptions {
220 file lldp.log size 1m;
221 flag config;
222 flag protocol;
223 }
224 interface ge-0/0/0 {
225 enable;
226 dest-mac-type nearest-bridge;
227 power-negotiation disable;
228 trap-notification enable;
229 tlv-filter port-vlan-id;
230 }
231 interface ge-0/0/1 {
232 power-negotiation;
233 trap-notification disable;
234 }
235 }
236}
237"#;
238
239 fn lldp_of(text: &str) -> Lldp {
240 lldp(&juniper::parse_braces(text)).unwrap()
241 }
242
243 #[test]
245 fn interface_lists_expand_to_one_entry_per_name() {
246 let lldp = lldp(&juniper::parse_braces(
247 "protocols {\n lldp {\n interface [ ge-0/0/0 ge-0/0/1 ];\n interface xe-0/0/0 {\n disable;\n }\n }\n}\n",
248 ))
249 .unwrap();
250 let names: Vec<_> = lldp.interfaces.iter().map(|i| i.name.as_str()).collect();
251 assert_eq!(names, ["ge-0/0/0", "ge-0/0/1", "xe-0/0/0"]);
252 assert_eq!(lldp.interfaces[1].enabled, None);
253 assert_eq!(lldp.interfaces[2].enabled, Some(false));
254 }
255
256 #[test]
257 fn reads_the_guide_example() {
258 let lldp = lldp_of(DOC_EXAMPLE);
259 assert_eq!(lldp.advertisement_interval.as_deref(), Some("30"));
260 assert_eq!(lldp.hold_multiplier.as_deref(), Some("4"));
261 assert_eq!(lldp.lldp_configuration_notification_interval.as_deref(), Some("30"));
262 assert_eq!(lldp.ptopo_configuration_maximum_hold_time.as_deref(), Some("300"));
263 assert_eq!(lldp.ptopo_configuration_trap_interval.as_deref(), Some("30"));
264 assert_eq!(lldp.transmit_delay.as_deref(), Some("2"));
265 assert_eq!(lldp.enabled, None);
266 assert_eq!(lldp.interfaces.len(), 2);
267 assert_eq!(lldp.interfaces[0].name, "all");
268 assert_eq!(lldp.interfaces[0].enabled, Some(false));
269 assert_eq!(lldp.interfaces[1].name, "ge-1/1/1");
270 assert_eq!(lldp.interfaces[1].enabled, None);
271 }
272
273 #[test]
274 fn reads_the_global_options() {
275 let lldp = lldp_of(EXTRA);
276 assert_eq!(lldp.enabled, Some(true));
277 let chassis = lldp.chassis_id.unwrap();
278 assert_eq!(chassis.id_type.as_deref(), Some("locally-assigned"));
279 assert_eq!(chassis.id_value.as_deref(), Some("core1"));
280 assert_eq!(lldp.dest_mac_type.as_deref(), Some("nearest-customer-bridge"));
281 assert!(lldp.lldp_tx_fast_init);
282 assert_eq!(lldp.management_interface.as_deref(), Some("fxp0"));
283 assert_eq!(lldp.management_address, None);
284 assert!(lldp.mau_type);
285 assert!(lldp.netbios_snooping);
286 assert!(lldp.no_tagging);
287 assert_eq!(lldp.neighbour_port_info_display.as_deref(), Some("port-description"));
288 assert_eq!(lldp.port_description_type.as_deref(), Some("interface-description"));
289 assert_eq!(lldp.port_id_subtype.as_deref(), Some("locally-assigned"));
290 assert_eq!(lldp.tlv_select, ["system-name", "system-description"]);
291 assert!(lldp.tlv_filter.is_empty());
292 assert_eq!(lldp.vlan_name_tlv_option.as_deref(), Some("vlan-id"));
293 let trace = lldp.traceoptions.unwrap();
294 assert_eq!(trace.file.as_deref(), Some("lldp.log"));
295 assert_eq!(trace.flags[1].name, "protocol");
296 }
297
298 #[test]
299 fn reads_the_interface_options() {
300 let lldp = lldp_of(EXTRA);
301 let first = &lldp.interfaces[0];
302 assert_eq!(first.enabled, Some(true));
303 assert_eq!(first.dest_mac_type.as_deref(), Some("nearest-bridge"));
304 assert_eq!(first.power_negotiation, Some(false));
305 assert_eq!(first.trap_notification, Some(true));
306 assert_eq!(first.tlv_filter, ["port-vlan-id"]);
307 assert!(first.tlv_select.is_empty());
308
309 let second = &lldp.interfaces[1];
310 assert_eq!(second.enabled, None);
311 assert_eq!(second.power_negotiation, Some(true));
312 assert_eq!(second.trap_notification, Some(false));
313 }
314
315 #[test]
316 fn a_bare_lldp_block_has_nothing_set() {
317 let lldp = lldp_of("protocols {\n lldp;\n}\n");
318 assert_eq!(lldp, Lldp::default());
319 }
320
321 #[test]
322 fn disabled_lldp_is_read() {
323 assert_eq!(
324 lldp_of("protocols {\n lldp {\n disable;\n }\n}\n").enabled,
325 Some(false)
326 );
327 }
328
329 #[test]
330 fn no_lldp_is_none() {
331 let config = juniper::parse_braces("protocols {\n ospf {\n area 0.0.0.0;\n }\n}\n");
332 assert_eq!(lldp(&config), None);
333 }
334}