asterust-junos 0.5.12

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
//! `ether-options`, `gigether-options` and `fastether-options`
//! `interfaces { <name> { ether-options { ... } } }`.
//!
//! The options are written under `ether-options`, `gigether-options` or `fastether-options`,
//! depending on the platform.
//!
//! # See also
//!
//! - [gigether-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/gigether-options-edit-interfaces.html)
//! - [fastether-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/fastether-options-edit-interfaces.html)
//! - [ether-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ether-options-interfaces-qfx-series.html)
//! - [auto-negotiation](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/auto-negotiation-edit-interfaces.html)
//! - [flow-control (ACX Series)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/flow-control-edit-interfaces.html)
//! - [loopback (Aggregated Ethernet, Fast Ethernet, and Gigabit Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/loopback-edit-interfaces-ethernet.html)
//! - [source-filtering](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/source-filtering-edit-interfaces.html)
//! - [speed (MX Series DPC)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/speed-edit-interfaces-gigether-options.html)
//! - [link-mode](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/link-mode-edit-interfaces-qfx-series.html)
//! - [ieee-802-3az-eee](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ieee-802-3az-eee-interfaces.html)
//! - [autostate-exclude](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/autostate-exclude-edit-interfaces-ether-options.html)
//! - [configured-flow-control](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/configured-flow-control-edit-interfaces-qfx-series.html)
//! - [redundant-parent (Interfaces Gigabit Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-edit-redundant-parent-gigabit-ethernet.html)
//! - [redundant-parent (Interfaces Fast Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-edit-redundant-parent-fast-ethernet.html)
//! - [mru (Gigabit Ethernet Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-gigether-options-mru.html)
//! - [loopback-remote](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-gigether-options-loopback-remote.html)
//! - [asynchronous-notification](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/asynchronous-notification-edit-interfaces.html)
//! - [ignore-l3-incompletes](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ignore-l3-incompletes-edit-interfaces.html)
//! - [ingress-rate-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ingress-rate-limit-edit-interfaces.html)
//! - [source-address-filter](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/source-address-filter-edit-interfaces.html)
//! - [fec (ether)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-ether-options-fec.html)

mod ethernet_switch_profile;
mod ieee_802_3ad;

pub(in crate::interfaces) use ethernet_switch_profile::parse_ethernet_switch_profile;
pub use ethernet_switch_profile::{EthernetSwitchForwardingClass, EthernetSwitchPolicer, EthernetSwitchProfile};
pub use ieee_802_3ad::Ieee8023ad;

use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, toggle, value_after};
use asterust::config::ConfigLine;
use ieee_802_3ad::parse_ieee_802_3ad;

/// The Ethernet options of a physical interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EtherOptions {
    /// `Some(true)` for `auto-negotiation`, `Some(false)` for `no-auto-negotiation`.
    pub auto_negotiation: Option<bool>,
    /// `local-interface-online` or `local-interface-offline`.
    pub remote_fault: Option<String>,
    /// `Some(true)` for `flow-control`, `Some(false)` for `no-flow-control`.
    pub flow_control: Option<bool>,
    /// `Some(true)` for `loopback`, `Some(false)` for `no-loopback`.
    pub loopback: Option<bool>,
    /// `Some(true)` for `source-filtering`, `Some(false)` for `no-source-filtering`.
    pub source_filtering: Option<bool>,
    pub speed: Option<String>,
    /// `full-duplex`, `half-duplex` or `automatic`.
    pub link_mode: Option<String>,
    /// e.g. `"fec91"`, `"fec74"`, `"fec108"` or `"none"`.
    pub fec: Option<String>,
    /// The bundle this interface is a member of; the statement is `802.3ad`.
    pub ieee_802_3ad: Option<Ieee8023ad>,
    pub mdi_mode: Option<String>,
    pub no_auto_mdix: bool,
    pub autostate_exclude: bool,
    pub ieee_802_3az_eee: bool,
    /// `Some(true)` for `on`, from `configured-flow-control { rx-buffers }`.
    pub configured_flow_control_rx_buffers: Option<bool>,
    /// `Some(true)` for `on`, from `configured-flow-control { tx-buffers }`.
    pub configured_flow_control_tx_buffers: Option<bool>,
    pub ethernet_switch_profile: Option<EthernetSwitchProfile>,
    /// The redundant Ethernet interface the interface is a child of, from `redundant-parent`.
    pub redundant_parent: Option<String>,
    pub mru: Option<String>,
    pub loopback_remote: bool,
    pub ignore_l3_incompletes: bool,
    /// `Some(true)` for `asynchronous-notification`, `Some(false)` for `no-asynchronous-notification`.
    pub asynchronous_notification: Option<bool>,
    /// From `fastether-options { ingress-rate-limit }`.
    pub ingress_rate_limit: Option<String>,
    /// The MAC addresses of `source-address-filter`.
    pub source_address_filter: Vec<String>,
}

/// The MAC addresses of `source-address-filter { mac-address; }` or `source-address-filter <mac>;`.
pub(super) fn source_address_filter(options: &ConfigLine) -> Vec<String> {
    let mut found: Vec<String> = args_of(options, "source-address-filter")
        .into_iter()
        .flat_map(list_values)
        .collect();
    if let Some(filter) = child_block(options, "source-address-filter") {
        found.extend(crate::active_children(filter).map(|c| c.text.clone()));
    }
    found
}

pub(super) fn parse_ether_options(interface: &ConfigLine) -> Option<EtherOptions> {
    let options = ["ether-options", "gigether-options", "fastether-options"]
        .iter()
        .find_map(|name| child_block(interface, name))?;
    let negotiation =
        statement_words(options, "auto-negotiation").or_else(|| statement_words(options, "no-auto-negotiation"));
    let flow_control = child_block(options, "configured-flow-control");
    Some(EtherOptions {
        auto_negotiation: toggle(options, "auto-negotiation"),
        remote_fault: negotiation.and_then(|words| value_after(&words, "remote-fault")),
        flow_control: toggle(options, "flow-control"),
        loopback: toggle(options, "loopback"),
        source_filtering: toggle(options, "source-filtering"),
        speed: arg_of(options, "speed").map(str::to_string),
        link_mode: arg_of(options, "link-mode").map(str::to_string),
        fec: arg_of(options, "fec").map(str::to_string),
        ieee_802_3ad: parse_ieee_802_3ad(options),
        mdi_mode: arg_of(options, "mdi-mode").map(str::to_string),
        no_auto_mdix: has_leaf(options, "no-auto-mdix"),
        autostate_exclude: has_leaf(options, "autostate-exclude"),
        ieee_802_3az_eee: has_leaf(options, "ieee-802-3az-eee"),
        configured_flow_control_rx_buffers: flow_control.and_then(|f| arg_of(f, "rx-buffers")).map(|v| v == "on"),
        configured_flow_control_tx_buffers: flow_control.and_then(|f| arg_of(f, "tx-buffers")).map(|v| v == "on"),
        ethernet_switch_profile: child_block(options, "ethernet-switch-profile").map(parse_ethernet_switch_profile),
        redundant_parent: arg_of(options, "redundant-parent").map(str::to_string),
        mru: arg_of(options, "mru").map(str::to_string),
        loopback_remote: has_leaf(options, "loopback-remote"),
        ignore_l3_incompletes: has_leaf(options, "ignore-l3-incompletes"),
        asynchronous_notification: toggle(options, "asynchronous-notification"),
        ingress_rate_limit: arg_of(options, "ingress-rate-limit").map(str::to_string),
        source_address_filter: source_address_filter(options),
    })
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    const CONFIG: &str = r#"
interfaces {
    ge-0/0/0 {
        gigether-options {
            802.3ad ae0;
            no-auto-negotiation;
            no-flow-control;
            loopback;
            source-filtering;
            speed 1g;
        }
    }
    ge-0/0/1 {
        gigether-options {
            802.3ad ae1 {
                primary;
                link-index 2;
                distribution-list DL1;
                lacp {
                    port-priority 64;
                }
            }
            auto-negotiation remote-fault local-interface-offline;
        }
    }
    xe-0/0/0 {
        ether-options {
            802.3ad {
                ae2;
                backup;
            }
            link-mode full-duplex;
            fec fec91;
            no-loopback;
            no-source-filtering;
            flow-control;
        }
    }
}
"#;

    #[test]
    fn extracts_ether_options_and_bundle_membership() {
        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
        let options = ifaces[0].ether_options.as_ref().unwrap();
        assert_eq!(options.auto_negotiation, Some(false));
        assert_eq!(options.remote_fault, None);
        assert_eq!(options.flow_control, Some(false));
        assert_eq!(options.loopback, Some(true));
        assert_eq!(options.source_filtering, Some(true));
        assert_eq!(options.speed.as_deref(), Some("1g"));
        assert_eq!(options.link_mode, None);

        let bundle = options.ieee_802_3ad.as_ref().unwrap();
        assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae0"));
        assert_eq!(bundle.role, None);
    }

    #[test]
    fn extracts_802_3ad_block_and_remote_fault() {
        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
        let options = ifaces[1].ether_options.as_ref().unwrap();
        assert_eq!(options.auto_negotiation, Some(true));
        assert_eq!(options.remote_fault.as_deref(), Some("local-interface-offline"));

        let bundle = options.ieee_802_3ad.as_ref().unwrap();
        assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae1"));
        assert_eq!(bundle.role.as_deref(), Some("primary"));
        assert_eq!(bundle.link_index.as_deref(), Some("2"));
        assert_eq!(bundle.distribution_list.as_deref(), Some("DL1"));
        assert_eq!(bundle.lacp_port_priority.as_deref(), Some("64"));
    }

    #[test]
    fn extracts_ether_options_with_the_bundle_named_inside_the_block() {
        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
        let options = ifaces[2].ether_options.as_ref().unwrap();
        assert_eq!(options.link_mode.as_deref(), Some("full-duplex"));
        assert_eq!(options.fec.as_deref(), Some("fec91"));
        assert_eq!(options.loopback, Some(false));
        assert_eq!(options.source_filtering, Some(false));
        assert_eq!(options.flow_control, Some(true));
        assert_eq!(options.auto_negotiation, None);

        let bundle = options.ieee_802_3ad.as_ref().unwrap();
        assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae2"));
        assert_eq!(bundle.role.as_deref(), Some("backup"));
    }

    #[test]
    fn extracts_the_other_ethernet_options() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ge-0/0/5 {
        gigether-options {
            redundant-parent reth0;
            mru 9216;
            loopback-remote;
            no-asynchronous-notification;
            ignore-l3-incompletes;
            source-address-filter {
                00:11:22:33:44:55;
                00:11:22:33:44:66;
            }
        }
    }
    fe-0/0/6 {
        fastether-options {
            ingress-rate-limit 100;
            redundant-parent reth1;
        }
    }
    xe-0/0/7 {
        ether-options {
            ieee-802-3az-eee;
            autostate-exclude;
            configured-flow-control {
                rx-buffers on;
                tx-buffers off;
            }
            802.3ad {
                ae3;
                link-protection-sub-group grp1;
            }
        }
    }
}
"#,
        ));
        let gig = ifaces[0].ether_options.as_ref().unwrap();
        assert_eq!(gig.redundant_parent.as_deref(), Some("reth0"));
        assert_eq!(gig.mru.as_deref(), Some("9216"));
        assert!(gig.loopback_remote);
        assert_eq!(gig.asynchronous_notification, Some(false));
        assert!(gig.ignore_l3_incompletes);
        assert_eq!(gig.source_address_filter, ["00:11:22:33:44:55", "00:11:22:33:44:66"]);
        let fast = ifaces[1].ether_options.as_ref().unwrap();
        assert_eq!(fast.ingress_rate_limit.as_deref(), Some("100"));
        assert_eq!(fast.redundant_parent.as_deref(), Some("reth1"));
        let xe = ifaces[2].ether_options.as_ref().unwrap();
        assert!(xe.ieee_802_3az_eee);
        assert!(xe.autostate_exclude);
        assert_eq!(xe.configured_flow_control_rx_buffers, Some(true));
        assert_eq!(xe.configured_flow_control_tx_buffers, Some(false));
        assert_eq!(
            xe.ieee_802_3ad.as_ref().unwrap().link_protection_sub_group.as_deref(),
            Some("grp1")
        );
        assert!(!gig.autostate_exclude);
        assert_eq!(gig.configured_flow_control_rx_buffers, None);
        assert!(gig.ethernet_switch_profile.is_none());
    }

    #[test]
    fn ether_options_are_optional() {
        let ifaces = interfaces(&juniper::parse_braces(
            "interfaces {\n    ge-0/0/0 {\n        disable;\n    }\n}\n",
        ));
        assert!(ifaces[0].ether_options.is_none());
    }
}