use super::interface::{parse_mappings, AppliedMapping};
use super::scheduler::{parse_rate, SchedulerRate};
use super::words;
use crate::{active_children, arg_of, args_of, child_block, has_leaf, statement_words, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct CosOptions {
pub hierarchical_scheduler_disable: bool,
pub latency_ecn_threshold: Option<String>,
pub latency_tracking: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FabricScheduler {
pub priority: String,
pub scheduler: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AdaptiveShaperTrigger {
pub kind: String,
pub shaping_rate: Option<SchedulerRate>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AdaptiveShaper {
pub name: String,
pub triggers: Vec<AdaptiveShaperTrigger>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AdjustmentApplication {
pub kind: String,
pub priority: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AdjustmentControlProfile {
pub name: String,
pub applications: Vec<AdjustmentApplication>,
pub algorithm: Option<String>,
}
pub(super) fn parse_options(cos: &ConfigLine) -> Option<CosOptions> {
let options = child_block(cos, "options")?;
Some(CosOptions {
hierarchical_scheduler_disable: has_leaf(options, "hierarchical-scheduler-disable"),
latency_ecn_threshold: statement_words(options, "latency-ecn")
.and_then(|words| value_after(&words, "threshold")),
latency_tracking: has_leaf(options, "latency-tracking"),
})
}
pub(super) fn parse_multi_destination(line: &ConfigLine) -> Vec<AppliedMapping> {
child_block(line, "multi-destination")
.map(|block| parse_mappings(block, "classifiers"))
.unwrap_or_default()
}
pub(super) fn parse_fabric(cos: &ConfigLine) -> Vec<FabricScheduler> {
let Some(map) = child_block(cos, "fabric").and_then(|fabric| child_block(fabric, "scheduler-map")) else {
return Vec::new();
};
args_of(map, "priority")
.into_iter()
.filter_map(|arg| {
let words: Vec<&str> = arg.split_whitespace().collect();
Some(FabricScheduler {
priority: words.first()?.to_string(),
scheduler: value_after(&words, "scheduler"),
})
})
.collect()
}
pub(super) fn parse_adaptive_shapers(cos: &ConfigLine) -> Vec<AdaptiveShaper> {
let Some(block) = child_block(cos, "adaptive-shapers") else {
return Vec::new();
};
active_children(block)
.filter_map(|shaper| {
Some(AdaptiveShaper {
name: shaper.text.split_whitespace().next()?.to_string(),
triggers: active_children(shaper)
.filter(|child| child.arg("trigger").is_some())
.filter_map(|trigger| {
let words = words(trigger, 1);
let rate = words
.iter()
.position(|word| *word == "shaping-rate")
.map(|at| &words[at + 1..]);
Some(AdaptiveShaperTrigger {
kind: words.first()?.to_string(),
shaping_rate: rate.map(parse_rate),
})
})
.collect(),
})
})
.collect()
}
pub(super) fn parse_adjustment_control_profiles(cos: &ConfigLine) -> Vec<AdjustmentControlProfile> {
let Some(block) = child_block(cos, "adjustment-control-profiles") else {
return Vec::new();
};
active_children(block)
.filter_map(|profile| {
let application = child_block(profile, "application");
Some(AdjustmentControlProfile {
name: profile.text.split_whitespace().next()?.to_string(),
applications: application
.map(|block| {
active_children(block)
.filter(|child| child.text != "algorithm" && child.arg("algorithm").is_none())
.filter_map(|child| {
let words: Vec<&str> = child.text.split_whitespace().collect();
Some(AdjustmentApplication {
kind: words.first()?.to_string(),
priority: value_after(&words, "priority"),
})
})
.collect()
})
.unwrap_or_default(),
algorithm: application
.and_then(|block| arg_of(block, "algorithm"))
.map(str::to_string),
})
})
.collect()
}
#[cfg(test)]
mod tests {
use crate::class_of_service::class_of_service;
use asterust::config::juniper;
const EXTRA: &str = r#"
class-of-service {
options {
hierarchical-scheduler-disable;
latency-ecn threshold 20;
latency-tracking;
}
multi-destination {
classifiers {
dscp mc-dscp;
ieee-802.1 mc-dot1p;
}
}
interfaces {
xe-0/0/1 {
multi-destination {
classifiers {
inet-precedence mc-prec;
}
}
}
}
fabric {
scheduler-map {
priority high scheduler fabric-high;
priority low scheduler fabric-low;
}
}
adaptive-shapers {
FR {
trigger becn shaping-rate 64k;
trigger other shaping-rate percent 50;
}
EMPTY;
}
adjustment-control-profiles {
ACP1 {
application {
ancp priority 1;
pppoe-tags priority 2;
algorithm highest-rate;
}
}
ACP2;
}
}
"#;
#[test]
fn reads_the_options() {
let cos = class_of_service(&juniper::parse_braces(EXTRA)).unwrap();
let options = cos.options.unwrap();
assert!(options.hierarchical_scheduler_disable && options.latency_tracking);
assert_eq!(options.latency_ecn_threshold.as_deref(), Some("20"));
}
#[test]
fn reads_multi_destination_classifiers() {
let cos = class_of_service(&juniper::parse_braces(EXTRA)).unwrap();
assert_eq!(cos.multi_destination_classifiers.len(), 2);
assert_eq!(cos.multi_destination_classifiers[1].kind, "ieee-802.1");
assert_eq!(cos.multi_destination_classifiers[1].name, "mc-dot1p");
let interface = &cos.interfaces[0].settings.multi_destination_classifiers;
assert_eq!(interface.len(), 1);
assert_eq!(interface[0].name, "mc-prec");
}
#[test]
fn reads_the_fabric_scheduler_map() {
let fabric = class_of_service(&juniper::parse_braces(EXTRA)).unwrap().fabric;
assert_eq!(fabric.len(), 2);
assert_eq!(fabric[0].priority, "high");
assert_eq!(fabric[0].scheduler.as_deref(), Some("fabric-high"));
assert_eq!(fabric[1].priority, "low");
}
#[test]
fn reads_the_adaptive_shapers() {
let shapers = class_of_service(&juniper::parse_braces(EXTRA))
.unwrap()
.adaptive_shapers;
assert_eq!(shapers.len(), 2);
assert_eq!(shapers[0].name, "FR");
assert_eq!(shapers[0].triggers.len(), 2);
assert_eq!(shapers[0].triggers[0].kind, "becn");
assert_eq!(
shapers[0].triggers[0].shaping_rate.as_ref().unwrap().value.as_deref(),
Some("64k")
);
let percent = shapers[0].triggers[1].shaping_rate.as_ref().unwrap();
assert_eq!(percent.mode.as_deref(), Some("percent"));
assert!(shapers[1].triggers.is_empty());
}
#[test]
fn reads_the_adjustment_control_profiles() {
let profiles = class_of_service(&juniper::parse_braces(EXTRA))
.unwrap()
.adjustment_control_profiles;
assert_eq!(profiles.len(), 2);
assert_eq!(profiles[0].name, "ACP1");
assert_eq!(profiles[0].applications.len(), 2);
assert_eq!(profiles[0].applications[1].kind, "pppoe-tags");
assert_eq!(profiles[0].applications[1].priority.as_deref(), Some("2"));
assert_eq!(profiles[0].algorithm.as_deref(), Some("highest-rate"));
assert!(profiles[1].applications.is_empty() && profiles[1].algorithm.is_none());
}
}