asterust-junos 0.5.7

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `ethernet-switching`
//! `interfaces <name> { unit <n> { family ethernet-switching { ... } } }`.
//!
//! The Layer 2 switching port settings of EX and QFX Series switches.
//!
//! # See also
//!
//! - [family ethernet-switching (EX Series and QFX Series)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/family-ethernet-switching-edit-interfaces-qfx-series-els.html)

use crate::{arg_of, child_block, has_leaf, list_values, statements};
use asterust::config::ConfigLine;

/// The settings of a `family ethernet-switching`. The firewall filters are in [`UnitFamily::filter`].
///
/// [`UnitFamily::filter`]: super::UnitFamily::filter
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EthernetSwitching {
    /// `"access"` or `"trunk"`.
    pub interface_mode: Option<String>,
    /// The older `port-mode`, e.g. `"trunk"` or `"tagged-access"`.
    pub port_mode: Option<String>,
    pub native_vlan_id: Option<String>,
    /// The `vlan members`: names, IDs and ranges, or `all`.
    pub vlan_members: Vec<String>,
    /// The `storm-control` profile.
    pub storm_control: Option<String>,
    pub recovery_timeout: Option<String>,
    pub reflective_relay: bool,
    pub block_non_ip_all: bool,
    pub bpdu_vlan_flooding: bool,
    pub bypass_non_ip_unicast: bool,
    pub no_packet_flooding: bool,
    /// `no-trace-route` inside `no-packet-flooding`.
    pub no_trace_route: bool,
}

pub(super) fn parse_ethernet_switching(family: &ConfigLine) -> EthernetSwitching {
    let field = |keyword: &str| arg_of(family, keyword).map(str::to_string);
    EthernetSwitching {
        interface_mode: field("interface-mode"),
        port_mode: field("port-mode"),
        native_vlan_id: field("native-vlan-id"),
        vlan_members: statements(family, "vlan")
            .into_iter()
            .filter_map(|s| s.strip_prefix("members "))
            .flat_map(list_values)
            .collect(),
        storm_control: field("storm-control"),
        recovery_timeout: field("recovery-timeout"),
        reflective_relay: has_leaf(family, "reflective-relay"),
        block_non_ip_all: has_leaf(family, "block-non-ip-all"),
        bpdu_vlan_flooding: has_leaf(family, "bpdu-vlan-flooding"),
        bypass_non_ip_unicast: has_leaf(family, "bypass-non-ip-unicast"),
        no_packet_flooding: has_leaf(family, "no-packet-flooding")
            || child_block(family, "no-packet-flooding").is_some(),
        no_trace_route: child_block(family, "no-packet-flooding").is_some_and(|b| has_leaf(b, "no-trace-route")),
    }
}

#[cfg(test)]
mod tests {
    use crate::interfaces::interfaces;
    use asterust::config::juniper;

    // Adapted from the QFX Series guides: the `show configuration interfaces` output of Example: Configuring CoS PFC
    // across Layer 3 Interfaces, the FCoE MC-LAG example and the automatically generated ESI page (unit 0 of ae0); see
    // the README "Sources".
    const DOC_SWITCHING: &str = r#"
interfaces {
    xe-0/0/20 {
        unit 0 {
            family ethernet-switching {
                interface-mode trunk;
                vlan {
                    members vlan105;
                }
            }
        }
    }
    ae0 {
        mtu 2180;
        unit 0 {
            family ethernet-switching {
                port-mode trunk;
                vlan {
                    members fcoe_vlan;
                }
            }
        }
    }
    xe-0/0/30 {
        mtu 2180;
        unit 0 {
            family ethernet-switching {
                port-mode tagged-access;
                vlan {
                    members fcoe_vlan;
                }
            }
        }
    }
    ae5 {
        unit 0 {
            family ethernet-switching {
                interface-mode trunk;
                vlan {
                    members [ v100 v200 ];
                }
            }
        }
    }
}
"#;

    fn switching(ifaces: &[crate::interfaces::Interface], index: usize) -> &super::EthernetSwitching {
        ifaces[index].units[0].family_settings[0]
            .ethernet_switching
            .as_ref()
            .unwrap()
    }

    #[test]
    fn reads_the_guide_ports() {
        let ifaces = interfaces(&juniper::parse_braces(DOC_SWITCHING));
        let trunk = switching(&ifaces, 0);
        assert_eq!(trunk.interface_mode.as_deref(), Some("trunk"));
        assert_eq!(trunk.port_mode, None);
        assert_eq!(trunk.vlan_members, ["vlan105"]);

        let legacy = switching(&ifaces, 1);
        assert_eq!(legacy.port_mode.as_deref(), Some("trunk"));
        assert_eq!(legacy.interface_mode, None);
        assert_eq!(legacy.vlan_members, ["fcoe_vlan"]);
        assert_eq!(switching(&ifaces, 2).port_mode.as_deref(), Some("tagged-access"));
        assert_eq!(switching(&ifaces, 3).vlan_members, ["v100", "v200"]);
        assert_eq!(ifaces[0].units[0].family_settings[0].name, "ethernet-switching");
    }

    // Not from documentation: written from the family ethernet-switching CLI Reference pages. See the README
    // "Sources".
    #[test]
    fn reads_the_other_statements() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    xe-0/0/1 {
        unit 0 {
            family ethernet-switching {
                filter {
                    input FILTER-IN;
                    output-list [ A B ];
                }
                interface-mode access;
                native-vlan-id 10;
                recovery-timeout 600;
                storm-control STORM;
                reflective-relay;
                block-non-ip-all;
                bpdu-vlan-flooding;
                bypass-non-ip-unicast;
                no-packet-flooding {
                    no-trace-route;
                }
                vlan {
                    members all;
                }
            }
            family inet;
        }
    }
    xe-0/0/2 {
        unit 0 {
            family ethernet-switching {
                filter input OLD-IN;
                filter output OLD-OUT;
                no-packet-flooding;
            }
        }
    }
}
"#,
        ));
        let family = &ifaces[0].units[0].family_settings[0];
        let switching = family.ethernet_switching.as_ref().unwrap();
        assert_eq!(switching.interface_mode.as_deref(), Some("access"));
        assert_eq!(switching.native_vlan_id.as_deref(), Some("10"));
        assert_eq!(switching.recovery_timeout.as_deref(), Some("600"));
        assert_eq!(switching.storm_control.as_deref(), Some("STORM"));
        assert!(switching.reflective_relay && switching.block_non_ip_all);
        assert!(switching.bpdu_vlan_flooding && switching.bypass_non_ip_unicast);
        assert!(switching.no_packet_flooding && switching.no_trace_route);
        assert_eq!(switching.vlan_members, ["all"]);
        let filter = family.filter.as_ref().unwrap();
        assert_eq!(filter.input.as_deref(), Some("FILTER-IN"));
        assert_eq!(filter.output_list, ["A", "B"]);
        assert!(ifaces[0].units[0].family_settings[1].ethernet_switching.is_none());

        let old = &ifaces[1].units[0].family_settings[0];
        let filter = old.filter.as_ref().unwrap();
        assert_eq!(filter.input.as_deref(), Some("OLD-IN"));
        assert_eq!(filter.output.as_deref(), Some("OLD-OUT"));
        let switching = old.ethernet_switching.as_ref().unwrap();
        assert!(switching.no_packet_flooding && !switching.no_trace_route);
    }
}