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
//! `load-balance`, `dlb`, `link-protection` and `rebalance-periodic`
//! `interfaces { ae<n> { aggregated-ether-options { ... } } }`.
//!
//! # See also
//!
//! - [load-balance](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/load-balance-edit-interfaces-aex-aggregated-ether-options.html)
//! - [local-bias (ae load-balance)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/local-bias-edit-interfaces-aex-aggregated-ether-options.html)
//! - [dlb](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dynamic-load-balance.html)
//! - [link-protection](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/link-protection-edit-interfaces.html)
//! - [link-protection (non-LACP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/link-protection-static-edit-interfaces.html)
//! - [link-protection-sub-group (aggregated-ether-options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/link-protection-sub-group-edit-interfaces-ae-aggregated-ether-options.html)
//! - [rebalance-periodic (Aggregated Ethernet Subscriber Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rebalance-periodic-edit-interfaces.html)

use crate::{active_children, arg_of, child_block, has_leaf, named_children, value_after};
use asterust::config::ConfigLine;

/// The `load-balance` block of an aggregated Ethernet interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LoadBalance {
    /// `adaptive { ... }` is configured.
    pub adaptive: bool,
    /// `adaptive { pps; }`.
    pub adaptive_pps: bool,
    pub adaptive_scan_interval: Option<String>,
    pub adaptive_tolerance: Option<String>,
    pub no_adaptive: bool,
    pub per_packet: bool,
    /// Percentage, from `local-bias percent`.
    pub local_bias_percent: Option<String>,
}

/// The `dlb` (dynamic load balancing) block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Dlb {
    pub assigned_flow: bool,
    pub per_packet: bool,
    /// Microseconds, from `flowlet inactivity-interval`.
    pub flowlet_inactivity_interval: Option<String>,
    /// `ipv4`, `ipv6` or `mpls`.
    pub ether_type: Option<String>,
}

/// The `link-protection` statement: a backup link takes over when the primary fails.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkProtection {
    pub disable: bool,
    /// `Some(true)` for `revertive`, `Some(false)` for `non-revertive`.
    pub revertive: Option<bool>,
}

/// A `link-protection-sub-group <name>`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkProtectionSubGroup {
    pub name: String,
    /// `primary` or `backup`.
    pub role: Option<String>,
}

/// The `rebalance-periodic` statement.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RebalancePeriodic {
    /// The `hour:minute` the rebalance starts, from `time` or `start-time`.
    pub time: Option<String>,
    /// Hours.
    pub interval: Option<String>,
}

pub(in crate::interfaces) fn parse_load_balance(balance: &ConfigLine) -> LoadBalance {
    let adaptive = child_block(balance, "adaptive");
    LoadBalance {
        adaptive: adaptive.is_some() || has_leaf(balance, "adaptive"),
        adaptive_pps: adaptive.is_some_and(|a| has_leaf(a, "pps")),
        adaptive_scan_interval: adaptive.and_then(|a| arg_of(a, "scan-interval")).map(str::to_string),
        adaptive_tolerance: adaptive.and_then(|a| arg_of(a, "tolerance")).map(str::to_string),
        no_adaptive: has_leaf(balance, "no-adaptive"),
        per_packet: has_leaf(balance, "per-packet"),
        local_bias_percent: arg_of(balance, "local-bias").and_then(|arg| {
            value_after(&arg.split_whitespace().collect::<Vec<_>>(), "percent").or_else(|| Some(arg.to_string()))
        }),
    }
}

pub(super) fn parse_dlb(dlb: &ConfigLine) -> Dlb {
    Dlb {
        assigned_flow: has_leaf(dlb, "assigned-flow"),
        per_packet: has_leaf(dlb, "per-packet"),
        flowlet_inactivity_interval: arg_of(dlb, "flowlet").and_then(|arg| {
            value_after(&arg.split_whitespace().collect::<Vec<_>>(), "inactivity-interval")
                .or_else(|| arg.split_whitespace().next().map(str::to_string))
        }),
        ether_type: arg_of(dlb, "ether-type").map(str::to_string),
    }
}

pub(in crate::interfaces) fn parse_link_protection(protection: &ConfigLine) -> LinkProtection {
    LinkProtection {
        disable: has_leaf(protection, "disable"),
        revertive: if has_leaf(protection, "revertive") {
            Some(true)
        } else if has_leaf(protection, "non-revertive") {
            Some(false)
        } else {
            None
        },
    }
}

pub(super) fn parse_link_protection_sub_groups(options: &ConfigLine) -> Vec<LinkProtectionSubGroup> {
    named_children(options, "link-protection-sub-group")
        .map(|(name, group)| LinkProtectionSubGroup {
            name: name.to_string(),
            role: ["primary", "backup"]
                .iter()
                .find(|role| has_leaf(group, role))
                .map(|role| role.to_string()),
        })
        .collect()
}

pub(in crate::interfaces) fn parse_rebalance_periodic(periodic: &ConfigLine) -> RebalancePeriodic {
    // Written `rebalance-periodic time <hh:mm> [interval <hours>]` or as a block.
    let words: Vec<&str> = periodic
        .text
        .split_whitespace()
        .skip(1)
        .chain(active_children(periodic).flat_map(|c| c.text.split_whitespace()))
        .collect();
    RebalancePeriodic {
        time: value_after(&words, "time").or_else(|| value_after(&words, "start-time")),
        interval: value_after(&words, "interval"),
    }
}