use crate::{active_children, arg_of, child_block, has_leaf, named_children, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LoadBalance {
pub adaptive: bool,
pub adaptive_pps: bool,
pub adaptive_scan_interval: Option<String>,
pub adaptive_tolerance: Option<String>,
pub no_adaptive: bool,
pub per_packet: bool,
pub local_bias_percent: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Dlb {
pub assigned_flow: bool,
pub per_packet: bool,
pub flowlet_inactivity_interval: Option<String>,
pub ether_type: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkProtection {
pub disable: bool,
pub revertive: Option<bool>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkProtectionSubGroup {
pub name: String,
pub role: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RebalancePeriodic {
pub time: Option<String>,
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 {
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"),
}
}