asterust-junos 0.5.14

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
//! `aggregated-ether-options`
//! `interfaces { ae<n> { aggregated-ether-options { ... } } }`.
//!
//! # See also
//!
//! - [aggregated-ether-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/aggregated-ether-options-edit-interfaces.html)
//! - [link-speed (Aggregated Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/link-speed-edit-interfaces-ae.html)
//! - [local-bias](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/local-bias-edit-interface-ae.html)
//! - [local-minimum-links-threshold](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/local-minimum-links-threshold-edit-interfaces-ae.html)
//! - [minimum-bandwidth](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/minimum-bandwidth-edit-interfaces.html)
//! - [resilient-hash](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resilient-hash-edit-interfaces.html)
//! - [fcoe-lag](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/fcoe-lag-edit-interfaces.html)
//! - [no-fcoe-lag](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-fcoe-lag-edit-interfaces.html)
//! - [logical-interface-chassis-redundancy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/logical-interface-chassis-redundancy-edit-interfaces.html)
//! - [minimum-links](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/minimum-links-edit-interfaces.html)

mod balancing;
mod bfd_liveness_detection;
mod lacp;
mod mc_ae;

pub use balancing::{Dlb, LinkProtection, LinkProtectionSubGroup, LoadBalance, RebalancePeriodic};
pub use bfd_liveness_detection::{BfdDamping, BfdLivenessDetection};
pub use lacp::Lacp;
pub use mc_ae::{McAe, McAeEvents};

use super::ether_options::{parse_ethernet_switch_profile, source_address_filter, EthernetSwitchProfile};
use super::targeted_options::{parse_targeted_options, TargetedOptions};
use crate::{arg_of, child_block, has_leaf, toggle, value_after};
use asterust::config::ConfigLine;
use balancing::{parse_dlb, parse_link_protection_sub_groups};
pub(in crate::interfaces) use balancing::{parse_link_protection, parse_load_balance, parse_rebalance_periodic};
use bfd_liveness_detection::parse_bfd_liveness_detection;
pub(in crate::interfaces) use lacp::parse_lacp;
use mc_ae::parse_mc_ae;

/// The `aggregated-ether-options` of an aggregated Ethernet interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AggregatedEtherOptions {
    pub lacp: Option<Lacp>,
    pub mc_ae: Option<McAe>,
    pub bfd_liveness_detection: Option<BfdLivenessDetection>,
    /// e.g. `"10g"` or `"mixed"`.
    pub link_speed: Option<String>,
    pub minimum_links: 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 link_protection: Option<LinkProtection>,
    pub link_protection_sub_groups: Vec<LinkProtectionSubGroup>,
    pub local_bias: bool,
    pub local_minimum_links_threshold: Option<String>,
    /// From `minimum-bandwidth bw-unit <unit> bw-value <value>`, e.g. `"gbps"`.
    pub minimum_bandwidth_unit: Option<String>,
    pub minimum_bandwidth_value: Option<String>,
    pub resilient_hash: bool,
    pub load_balance: Option<LoadBalance>,
    pub rebalance_periodic: Option<RebalancePeriodic>,
    pub dlb: Option<Dlb>,
    /// `Some(true)` for `fcoe-lag`, `Some(false)` for `no-fcoe-lag`.
    pub fcoe_lag: Option<bool>,
    pub logical_interface_chassis_redundancy: bool,
    pub logical_interface_fpc_redundancy: bool,
    pub targeted_options: Option<TargetedOptions>,
    pub ethernet_switch_profile: Option<EthernetSwitchProfile>,
    /// The MAC addresses of `source-address-filter`.
    pub source_address_filter: Vec<String>,
}

pub(super) fn parse_aggregated_ether_options(options: &ConfigLine) -> AggregatedEtherOptions {
    let minimum_bandwidth = crate::statement_words(options, "minimum-bandwidth");
    AggregatedEtherOptions {
        lacp: child_block(options, "lacp").map(parse_lacp),
        mc_ae: child_block(options, "mc-ae").map(parse_mc_ae),
        bfd_liveness_detection: child_block(options, "bfd-liveness-detection").map(parse_bfd_liveness_detection),
        link_speed: arg_of(options, "link-speed").map(str::to_string),
        minimum_links: arg_of(options, "minimum-links").map(str::to_string),
        flow_control: toggle(options, "flow-control"),
        loopback: toggle(options, "loopback"),
        source_filtering: toggle(options, "source-filtering"),
        link_protection: child_block(options, "link-protection").map(parse_link_protection),
        link_protection_sub_groups: parse_link_protection_sub_groups(options),
        local_bias: has_leaf(options, "local-bias"),
        local_minimum_links_threshold: arg_of(options, "local-minimum-links-threshold").map(str::to_string),
        minimum_bandwidth_unit: minimum_bandwidth.as_ref().and_then(|w| value_after(w, "bw-unit")),
        minimum_bandwidth_value: minimum_bandwidth.as_ref().and_then(|w| value_after(w, "bw-value")),
        resilient_hash: has_leaf(options, "resilient-hash"),
        load_balance: child_block(options, "load-balance").map(parse_load_balance),
        rebalance_periodic: crate::active_children(options)
            .find(|c| c.arg("rebalance-periodic").is_some())
            .map(parse_rebalance_periodic),
        dlb: child_block(options, "dlb").map(parse_dlb),
        fcoe_lag: toggle(options, "fcoe-lag"),
        logical_interface_chassis_redundancy: has_leaf(options, "logical-interface-chassis-redundancy"),
        logical_interface_fpc_redundancy: has_leaf(options, "logical-interface-fpc-redundancy"),
        targeted_options: child_block(options, "targeted-options").map(parse_targeted_options),
        ethernet_switch_profile: child_block(options, "ethernet-switch-profile").map(parse_ethernet_switch_profile),
        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".
    #[test]
    fn extracts_aggregated_ether_options_and_lacp() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ae0 {
        aggregated-ether-options {
            link-speed 10g;
            minimum-links 2;
            no-flow-control;
            lacp {
                active;
                periodic fast;
                system-priority 100;
                system-id 00:11:22:33:44:55;
                admin-key 7;
                force-up;
                fast-failover;
                accept-data;
            }
        }
    }
}
"#,
        ));
        let options = ifaces[0].aggregated_ether_options.as_ref().unwrap();
        assert_eq!(options.link_speed.as_deref(), Some("10g"));
        assert_eq!(options.minimum_links.as_deref(), Some("2"));
        assert_eq!(options.flow_control, Some(false));
        assert_eq!(options.loopback, None);
        assert_eq!(options.source_filtering, None);

        let lacp = options.lacp.as_ref().unwrap();
        assert_eq!(lacp.mode.as_deref(), Some("active"));
        assert_eq!(lacp.periodic.as_deref(), Some("fast"));
        assert_eq!(lacp.system_priority.as_deref(), Some("100"));
        assert_eq!(lacp.system_id.as_deref(), Some("00:11:22:33:44:55"));
        assert_eq!(lacp.admin_key.as_deref(), Some("7"));
        assert!(lacp.force_up);
        assert!(lacp.fast_failover);
        assert!(lacp.accept_data);
    }

    #[test]
    fn extracts_load_balancing_and_link_protection() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ae2 {
        aggregated-ether-options {
            local-bias;
            local-minimum-links-threshold 2;
            minimum-bandwidth bw-unit gbps bw-value 20;
            resilient-hash;
            fcoe-lag;
            logical-interface-chassis-redundancy;
            rebalance-periodic time 03:30 interval 12;
            link-protection {
                revertive;
            }
            link-protection-sub-group g1 {
                primary;
            }
            link-protection-sub-group g2 {
                backup;
            }
            load-balance {
                adaptive {
                    pps;
                    scan-interval 30;
                    tolerance 20;
                }
                per-packet;
                local-bias percent 60;
            }
            dlb {
                assigned-flow;
                flowlet inactivity-interval 64;
                ether-type ipv4;
            }
            source-address-filter {
                00:aa:bb:cc:dd:ee;
            }
            lacp {
                active;
                aggregate-wait-time 5;
                sync-reset enable;
                link-protection {
                    non-revertive;
                    disable;
                }
            }
        }
    }
    ae3 {
        aggregated-ether-options {
            no-fcoe-lag;
            link-protection {
                disable;
            }
        }
    }
}
"#,
        ));
        let options = ifaces[0].aggregated_ether_options.as_ref().unwrap();
        assert!(options.local_bias);
        assert_eq!(options.local_minimum_links_threshold.as_deref(), Some("2"));
        assert_eq!(options.minimum_bandwidth_unit.as_deref(), Some("gbps"));
        assert_eq!(options.minimum_bandwidth_value.as_deref(), Some("20"));
        assert!(options.resilient_hash);
        assert_eq!(options.fcoe_lag, Some(true));
        assert!(options.logical_interface_chassis_redundancy);
        let rebalance = options.rebalance_periodic.as_ref().unwrap();
        assert_eq!(rebalance.time.as_deref(), Some("03:30"));
        assert_eq!(rebalance.interval.as_deref(), Some("12"));
        let protection = options.link_protection.as_ref().unwrap();
        assert!(!protection.disable);
        assert_eq!(protection.revertive, Some(true));
        assert_eq!(options.link_protection_sub_groups.len(), 2);
        assert_eq!(options.link_protection_sub_groups[0].name, "g1");
        assert_eq!(options.link_protection_sub_groups[0].role.as_deref(), Some("primary"));
        assert_eq!(options.link_protection_sub_groups[1].role.as_deref(), Some("backup"));
        let balance = options.load_balance.as_ref().unwrap();
        assert!(balance.adaptive && balance.adaptive_pps && balance.per_packet && !balance.no_adaptive);
        assert_eq!(balance.adaptive_scan_interval.as_deref(), Some("30"));
        assert_eq!(balance.adaptive_tolerance.as_deref(), Some("20"));
        assert_eq!(balance.local_bias_percent.as_deref(), Some("60"));
        let dlb = options.dlb.as_ref().unwrap();
        assert!(dlb.assigned_flow && !dlb.per_packet);
        assert_eq!(dlb.flowlet_inactivity_interval.as_deref(), Some("64"));
        assert_eq!(dlb.ether_type.as_deref(), Some("ipv4"));
        assert_eq!(options.source_address_filter, ["00:aa:bb:cc:dd:ee"]);
        let lacp = options.lacp.as_ref().unwrap();
        assert_eq!(lacp.aggregate_wait_time.as_deref(), Some("5"));
        assert_eq!(lacp.sync_reset.as_deref(), Some("enable"));
        let lacp_protection = lacp.link_protection.as_ref().unwrap();
        assert!(lacp_protection.disable);
        assert_eq!(lacp_protection.revertive, Some(false));

        let other = ifaces[1].aggregated_ether_options.as_ref().unwrap();
        assert_eq!(other.fcoe_lag, Some(false));
        assert!(other.link_protection.as_ref().unwrap().disable);
        assert!(other.load_balance.is_none() && other.dlb.is_none() && other.rebalance_periodic.is_none());
        assert!(!other.local_bias && !other.resilient_hash);
    }

    #[test]
    fn bare_lacp_has_no_settings() {
        let ifaces = interfaces(&juniper::parse_braces(
            "interfaces {\n    ae1 {\n        aggregated-ether-options {\n            lacp {\n                passive;\n            }\n        }\n    }\n}\n",
        ));
        let lacp = ifaces[0]
            .aggregated_ether_options
            .as_ref()
            .unwrap()
            .lacp
            .as_ref()
            .unwrap();
        assert_eq!(lacp.mode.as_deref(), Some("passive"));
        assert_eq!(lacp.periodic, None);
        assert!(!lacp.force_up);
    }

    #[test]
    fn aggregated_ether_options_are_optional() {
        let ifaces = interfaces(&juniper::parse_braces(
            "interfaces {\n    ae0 {\n        disable;\n    }\n}\n",
        ));
        assert!(ifaces[0].aggregated_ether_options.is_none());
    }
}