Skip to main content

asterust_junos/interfaces/aggregated_ether_options/
balancing.rs

1//! `load-balance`, `dlb`, `link-protection` and `rebalance-periodic`
2//! `interfaces { ae<n> { aggregated-ether-options { ... } } }`.
3//!
4//! # See also
5//!
6//! - [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)
7//! - [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)
8//! - [dlb](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dynamic-load-balance.html)
9//! - [link-protection](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/link-protection-edit-interfaces.html)
10//! - [link-protection (non-LACP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/link-protection-static-edit-interfaces.html)
11//! - [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)
12//! - [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)
13
14use crate::{active_children, arg_of, child_block, has_leaf, named_children, value_after};
15use asterust::config::ConfigLine;
16
17/// The `load-balance` block of an aggregated Ethernet interface.
18#[derive(Clone, Debug, PartialEq, Eq)]
19#[non_exhaustive]
20pub struct LoadBalance {
21    /// `adaptive { ... }` is configured.
22    pub adaptive: bool,
23    /// `adaptive { pps; }`.
24    pub adaptive_pps: bool,
25    pub adaptive_scan_interval: Option<String>,
26    pub adaptive_tolerance: Option<String>,
27    pub no_adaptive: bool,
28    pub per_packet: bool,
29    /// Percentage, from `local-bias percent`.
30    pub local_bias_percent: Option<String>,
31}
32
33/// The `dlb` (dynamic load balancing) block.
34#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct Dlb {
37    pub assigned_flow: bool,
38    pub per_packet: bool,
39    /// Microseconds, from `flowlet inactivity-interval`.
40    pub flowlet_inactivity_interval: Option<String>,
41    /// `ipv4`, `ipv6` or `mpls`.
42    pub ether_type: Option<String>,
43}
44
45/// The `link-protection` statement: a backup link takes over when the primary fails.
46#[derive(Clone, Debug, PartialEq, Eq)]
47#[non_exhaustive]
48pub struct LinkProtection {
49    pub disable: bool,
50    /// `Some(true)` for `revertive`, `Some(false)` for `non-revertive`.
51    pub revertive: Option<bool>,
52}
53
54/// A `link-protection-sub-group <name>`.
55#[derive(Clone, Debug, PartialEq, Eq)]
56#[non_exhaustive]
57pub struct LinkProtectionSubGroup {
58    pub name: String,
59    /// `primary` or `backup`.
60    pub role: Option<String>,
61}
62
63/// The `rebalance-periodic` statement.
64#[derive(Clone, Debug, PartialEq, Eq)]
65#[non_exhaustive]
66pub struct RebalancePeriodic {
67    /// The `hour:minute` the rebalance starts, from `time` or `start-time`.
68    pub time: Option<String>,
69    /// Hours.
70    pub interval: Option<String>,
71}
72
73pub(in crate::interfaces) fn parse_load_balance(balance: &ConfigLine) -> LoadBalance {
74    let adaptive = child_block(balance, "adaptive");
75    LoadBalance {
76        adaptive: adaptive.is_some() || has_leaf(balance, "adaptive"),
77        adaptive_pps: adaptive.is_some_and(|a| has_leaf(a, "pps")),
78        adaptive_scan_interval: adaptive.and_then(|a| arg_of(a, "scan-interval")).map(str::to_string),
79        adaptive_tolerance: adaptive.and_then(|a| arg_of(a, "tolerance")).map(str::to_string),
80        no_adaptive: has_leaf(balance, "no-adaptive"),
81        per_packet: has_leaf(balance, "per-packet"),
82        local_bias_percent: arg_of(balance, "local-bias").and_then(|arg| {
83            value_after(&arg.split_whitespace().collect::<Vec<_>>(), "percent").or_else(|| Some(arg.to_string()))
84        }),
85    }
86}
87
88pub(super) fn parse_dlb(dlb: &ConfigLine) -> Dlb {
89    Dlb {
90        assigned_flow: has_leaf(dlb, "assigned-flow"),
91        per_packet: has_leaf(dlb, "per-packet"),
92        flowlet_inactivity_interval: arg_of(dlb, "flowlet").and_then(|arg| {
93            value_after(&arg.split_whitespace().collect::<Vec<_>>(), "inactivity-interval")
94                .or_else(|| arg.split_whitespace().next().map(str::to_string))
95        }),
96        ether_type: arg_of(dlb, "ether-type").map(str::to_string),
97    }
98}
99
100pub(in crate::interfaces) fn parse_link_protection(protection: &ConfigLine) -> LinkProtection {
101    LinkProtection {
102        disable: has_leaf(protection, "disable"),
103        revertive: if has_leaf(protection, "revertive") {
104            Some(true)
105        } else if has_leaf(protection, "non-revertive") {
106            Some(false)
107        } else {
108            None
109        },
110    }
111}
112
113pub(super) fn parse_link_protection_sub_groups(options: &ConfigLine) -> Vec<LinkProtectionSubGroup> {
114    named_children(options, "link-protection-sub-group")
115        .map(|(name, group)| LinkProtectionSubGroup {
116            name: name.to_string(),
117            role: ["primary", "backup"]
118                .iter()
119                .find(|role| has_leaf(group, role))
120                .map(|role| role.to_string()),
121        })
122        .collect()
123}
124
125pub(in crate::interfaces) fn parse_rebalance_periodic(periodic: &ConfigLine) -> RebalancePeriodic {
126    // Written `rebalance-periodic time <hh:mm> [interval <hours>]` or as a block.
127    let words: Vec<&str> = periodic
128        .text
129        .split_whitespace()
130        .skip(1)
131        .chain(active_children(periodic).flat_map(|c| c.text.split_whitespace()))
132        .collect();
133    RebalancePeriodic {
134        time: value_after(&words, "time").or_else(|| value_after(&words, "start-time")),
135        interval: value_after(&words, "interval"),
136    }
137}