Skip to main content

asterust_junos/protocols/
lldp.rs

1//! `lldp`
2//! `protocols { lldp { ... } }`.
3//!
4//! The `lldp-med` statement is not extracted.
5//!
6//! # See also
7//!
8//! - [lldp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/lldp-edit-protocols.html)
9//! - [tlv-filter](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tlv-filter-edit-protocols-lldp.html)
10//! - [tlv-select](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tlv-select-edit-protocols-lldp.html)
11//! - [traceoptions (LLDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-protocols-lldp.html)
12//! - [Configuring LLDP](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast-l2/topics/task/layer-2-services-lldp-configuring.html), for the statements that have no CLI Reference page of their own
13//! - [Example: Configuring LLDP](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast-l2/topics/example/layer-2-services-lldp-example.html)
14//! - [Tracing LLDP Operations](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast-l2/topics/task/layer-2-services-lldp-tracing-operation.html)
15
16use 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/// The `chassis-id` block.
21#[derive(Clone, Debug, Default, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct LldpChassisId {
24    /// `chassis-component`, `interface-alias`, `interface-name`, `locally-assigned`, `mac-address`, `network-address`
25    /// or `port-component`.
26    pub id_type: Option<String>,
27    pub id_value: Option<String>,
28}
29
30/// An `interface <name>` entry of LLDP; the name can be `all`.
31#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct LldpInterface {
34    pub name: String,
35    /// `Some(true)` for `enable`, `Some(false)` for `disable`.
36    pub enabled: Option<bool>,
37    pub dest_mac_type: Option<String>,
38    /// `Some(false)` for `power-negotiation disable`, `Some(true)` for `enable` or a bare statement.
39    pub power_negotiation: Option<bool>,
40    /// `Some(true)` for `trap-notification enable`, `Some(false)` for `disable`.
41    pub trap_notification: Option<bool>,
42    pub tlv_filter: Vec<String>,
43    pub tlv_select: Vec<String>,
44}
45
46/// The `protocols lldp` configuration.
47#[derive(Clone, Debug, Default, PartialEq, Eq)]
48#[non_exhaustive]
49pub struct Lldp {
50    /// `Some(true)` for `enable`, `Some(false)` for `disable`.
51    pub enabled: Option<bool>,
52    /// Seconds.
53    pub advertisement_interval: Option<String>,
54    pub hold_multiplier: Option<String>,
55    /// Seconds.
56    pub transmit_delay: Option<String>,
57    pub chassis_id: Option<LldpChassisId>,
58    /// `nearest-bridge`, `nearest-customer-bridge` or `nearest-non-tpmr-bridge`.
59    pub dest_mac_type: Option<String>,
60    /// Seconds.
61    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    /// `port-description` or `port-id`.
69    pub neighbour_port_info_display: Option<String>,
70    /// `interface-alias` or `interface-description`.
71    pub port_description_type: Option<String>,
72    /// `interface-name` or `locally-assigned`.
73    pub port_id_subtype: Option<String>,
74    /// Seconds.
75    pub ptopo_configuration_maximum_hold_time: Option<String>,
76    /// Seconds.
77    pub ptopo_configuration_trap_interval: Option<String>,
78    pub tlv_filter: Vec<String>,
79    pub tlv_select: Vec<String>,
80    /// `name` or `vlan-id`.
81    pub vlan_name_tlv_option: Option<String>,
82    pub traceoptions: Option<Traceoptions>,
83    pub interfaces: Vec<LldpInterface>,
84}
85
86fn 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
96fn names(line: &ConfigLine, keyword: &str) -> Vec<String> {
97    args_of(line, keyword).into_iter().flat_map(list_values).collect()
98}
99
100/// `Some(false)` for `<keyword> disable`, `Some(true)` for `<keyword> enable` or a bare `<keyword>`.
101fn toggle_word(line: &ConfigLine, keyword: &str) -> Option<bool> {
102    statement_words(line, keyword).map(|words| !words.contains(&"disable"))
103}
104
105fn parse_interface(name: &str, interface: &ConfigLine) -> LldpInterface {
106    LldpInterface {
107        name: name.to_string(),
108        enabled: enabled(interface),
109        dest_mac_type: arg_of(interface, "dest-mac-type").map(str::to_string),
110        power_negotiation: toggle_word(interface, "power-negotiation"),
111        trap_notification: toggle_word(interface, "trap-notification"),
112        tlv_filter: names(interface, "tlv-filter"),
113        tlv_select: names(interface, "tlv-select"),
114    }
115}
116
117/// Returns the `protocols lldp` block, or `None` if LLDP is not configured.
118///
119/// # Examples
120///
121/// ```
122/// use asterust::config::juniper;
123/// use asterust_junos::protocols::lldp::lldp;
124///
125/// let config = juniper::parse_braces("protocols {\n    lldp {\n        interface all {\n            disable;\n        }\n        interface ge-1/1/1;\n    }\n}\n");
126/// let lldp = lldp(&config).unwrap();
127/// assert_eq!(lldp.interfaces[0].enabled, Some(false));
128/// assert_eq!(lldp.interfaces[1].name, "ge-1/1/1");
129/// ```
130pub fn lldp(config: &Config) -> Option<Lldp> {
131    let lldp = child_block(top_level(config, "protocols")?, "lldp")?;
132    let field = |keyword: &str| arg_of(lldp, keyword).map(str::to_string);
133    Some(Lldp {
134        enabled: enabled(lldp),
135        advertisement_interval: field("advertisement-interval"),
136        hold_multiplier: field("hold-multiplier"),
137        transmit_delay: field("transmit-delay"),
138        chassis_id: child_block(lldp, "chassis-id").map(|block| LldpChassisId {
139            id_type: arg_of(block, "chassis-id-type").map(str::to_string),
140            id_value: arg_of(block, "chassis-id-value").map(str::to_string),
141        }),
142        dest_mac_type: field("dest-mac-type"),
143        lldp_configuration_notification_interval: field("lldp-configuration-notification-interval"),
144        lldp_tx_fast_init: has_leaf(lldp, "lldp-tx-fast-init"),
145        management_address: field("management-address"),
146        management_interface: field("management-interface"),
147        mau_type: has_leaf(lldp, "mau-type"),
148        netbios_snooping: has_leaf(lldp, "netbios-snooping"),
149        no_tagging: has_leaf(lldp, "no-tagging"),
150        neighbour_port_info_display: field("neighbour-port-info-display"),
151        port_description_type: field("port-description-type"),
152        port_id_subtype: field("port-id-subtype"),
153        ptopo_configuration_maximum_hold_time: field("ptopo-configuration-maximum-hold-time"),
154        ptopo_configuration_trap_interval: field("ptopo-configuration-trap-interval"),
155        tlv_filter: names(lldp, "tlv-filter"),
156        tlv_select: names(lldp, "tlv-select"),
157        vlan_name_tlv_option: field("vlan-name-tlv-option"),
158        traceoptions: parse_traceoptions(lldp),
159        interfaces: named_children(lldp, "interface")
160            .map(|(name, interface)| parse_interface(name, interface))
161            .collect(),
162    })
163}
164
165#[cfg(test)]
166mod tests {
167    use super::*;
168    use asterust::config::juniper;
169
170    // The block of Example: Configuring LLDP (Layer 2 Services User Guide); see the README "Sources".
171    const DOC_EXAMPLE: &str = r#"
172protocols {
173    lldp {
174        advertisement-interval 30;
175        hold-multiplier 4;
176        interface all {
177            disable;
178        }
179        interface ge-1/1/1;
180        lldp-configuration-notification-interval 30;
181        ptopo-configuration-maximum-hold-time 300;
182        ptopo-configuration-trap-interval 30;
183        transmit-delay 2;
184    }
185}
186"#;
187
188    // Not from documentation: written from the CLI Reference and the Configuring LLDP statement summary. See the
189    // README "Sources".
190    const EXTRA: &str = r#"
191protocols {
192    lldp {
193        enable;
194        chassis-id {
195            chassis-id-type locally-assigned;
196            chassis-id-value core1;
197        }
198        dest-mac-type nearest-customer-bridge;
199        lldp-tx-fast-init;
200        management-interface fxp0;
201        mau-type;
202        netbios-snooping;
203        no-tagging;
204        neighbour-port-info-display port-description;
205        port-description-type interface-description;
206        port-id-subtype locally-assigned;
207        tlv-select [ system-name system-description ];
208        vlan-name-tlv-option vlan-id;
209        traceoptions {
210            file lldp.log size 1m;
211            flag config;
212            flag protocol;
213        }
214        interface ge-0/0/0 {
215            enable;
216            dest-mac-type nearest-bridge;
217            power-negotiation disable;
218            trap-notification enable;
219            tlv-filter port-vlan-id;
220        }
221        interface ge-0/0/1 {
222            power-negotiation;
223            trap-notification disable;
224        }
225    }
226}
227"#;
228
229    fn lldp_of(text: &str) -> Lldp {
230        lldp(&juniper::parse_braces(text)).unwrap()
231    }
232
233    #[test]
234    fn reads_the_guide_example() {
235        let lldp = lldp_of(DOC_EXAMPLE);
236        assert_eq!(lldp.advertisement_interval.as_deref(), Some("30"));
237        assert_eq!(lldp.hold_multiplier.as_deref(), Some("4"));
238        assert_eq!(lldp.lldp_configuration_notification_interval.as_deref(), Some("30"));
239        assert_eq!(lldp.ptopo_configuration_maximum_hold_time.as_deref(), Some("300"));
240        assert_eq!(lldp.ptopo_configuration_trap_interval.as_deref(), Some("30"));
241        assert_eq!(lldp.transmit_delay.as_deref(), Some("2"));
242        assert_eq!(lldp.enabled, None);
243        assert_eq!(lldp.interfaces.len(), 2);
244        assert_eq!(lldp.interfaces[0].name, "all");
245        assert_eq!(lldp.interfaces[0].enabled, Some(false));
246        assert_eq!(lldp.interfaces[1].name, "ge-1/1/1");
247        assert_eq!(lldp.interfaces[1].enabled, None);
248    }
249
250    #[test]
251    fn reads_the_global_options() {
252        let lldp = lldp_of(EXTRA);
253        assert_eq!(lldp.enabled, Some(true));
254        let chassis = lldp.chassis_id.unwrap();
255        assert_eq!(chassis.id_type.as_deref(), Some("locally-assigned"));
256        assert_eq!(chassis.id_value.as_deref(), Some("core1"));
257        assert_eq!(lldp.dest_mac_type.as_deref(), Some("nearest-customer-bridge"));
258        assert!(lldp.lldp_tx_fast_init);
259        assert_eq!(lldp.management_interface.as_deref(), Some("fxp0"));
260        assert_eq!(lldp.management_address, None);
261        assert!(lldp.mau_type);
262        assert!(lldp.netbios_snooping);
263        assert!(lldp.no_tagging);
264        assert_eq!(lldp.neighbour_port_info_display.as_deref(), Some("port-description"));
265        assert_eq!(lldp.port_description_type.as_deref(), Some("interface-description"));
266        assert_eq!(lldp.port_id_subtype.as_deref(), Some("locally-assigned"));
267        assert_eq!(lldp.tlv_select, ["system-name", "system-description"]);
268        assert!(lldp.tlv_filter.is_empty());
269        assert_eq!(lldp.vlan_name_tlv_option.as_deref(), Some("vlan-id"));
270        let trace = lldp.traceoptions.unwrap();
271        assert_eq!(trace.file.as_deref(), Some("lldp.log"));
272        assert_eq!(trace.flags[1].name, "protocol");
273    }
274
275    #[test]
276    fn reads_the_interface_options() {
277        let lldp = lldp_of(EXTRA);
278        let first = &lldp.interfaces[0];
279        assert_eq!(first.enabled, Some(true));
280        assert_eq!(first.dest_mac_type.as_deref(), Some("nearest-bridge"));
281        assert_eq!(first.power_negotiation, Some(false));
282        assert_eq!(first.trap_notification, Some(true));
283        assert_eq!(first.tlv_filter, ["port-vlan-id"]);
284        assert!(first.tlv_select.is_empty());
285
286        let second = &lldp.interfaces[1];
287        assert_eq!(second.enabled, None);
288        assert_eq!(second.power_negotiation, Some(true));
289        assert_eq!(second.trap_notification, Some(false));
290    }
291
292    #[test]
293    fn a_bare_lldp_block_has_nothing_set() {
294        let lldp = lldp_of("protocols {\n    lldp;\n}\n");
295        assert_eq!(lldp, Lldp::default());
296    }
297
298    #[test]
299    fn disabled_lldp_is_read() {
300        assert_eq!(
301            lldp_of("protocols {\n    lldp {\n        disable;\n    }\n}\n").enabled,
302            Some(false)
303        );
304    }
305
306    #[test]
307    fn no_lldp_is_none() {
308        let config = juniper::parse_braces("protocols {\n    ospf {\n        area 0.0.0.0;\n    }\n}\n");
309        assert_eq!(lldp(&config), None);
310    }
311}