Skip to main content

asterust_junos/class_of_service/
scheduler_map.rs

1//! `scheduler-maps`
2//! `class-of-service { scheduler-maps { <name> { forwarding-class <class> scheduler <scheduler>; } } }`.
3//!
4//! # See also
5//!
6//! - [scheduler-maps](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/scheduler-maps-edit-cos.html)
7//! - [Example: Configuring and Applying Scheduler Maps](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/cos/topics/example/cos-scheduler-map-security-applying-security-configuring.html)
8
9use crate::{active_children, args_of, child_block, split_name, value_after};
10use asterust::config::ConfigLine;
11
12/// A `forwarding-class <class> scheduler <scheduler>` entry of a scheduler map.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct SchedulerMapEntry {
16    pub forwarding_class: String,
17    pub scheduler: Option<String>,
18}
19
20/// A `scheduler-maps` entry.
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct SchedulerMap {
24    pub name: String,
25    pub entries: Vec<SchedulerMapEntry>,
26}
27
28fn parse_entry(arg: &str) -> SchedulerMapEntry {
29    let (class, rest) = split_name(arg);
30    let words: Vec<&str> = rest.split_whitespace().collect();
31    SchedulerMapEntry {
32        forwarding_class: class.to_string(),
33        scheduler: value_after(&words, "scheduler"),
34    }
35}
36
37pub(super) fn parse_scheduler_maps(cos: &ConfigLine) -> Vec<SchedulerMap> {
38    let Some(block) = child_block(cos, "scheduler-maps") else {
39        return Vec::new();
40    };
41    active_children(block)
42        .filter_map(|map| {
43            Some(SchedulerMap {
44                name: map.text.split_whitespace().next()?.to_string(),
45                entries: args_of(map, "forwarding-class").into_iter().map(parse_entry).collect(),
46            })
47        })
48        .collect()
49}
50
51#[cfg(test)]
52mod tests {
53    use crate::class_of_service::class_of_service;
54    use asterust::config::juniper;
55
56    // The `scheduler-maps` of Example: Configuring and Applying Scheduler Maps and Example: Configuring Priority
57    // Scheduling (Class of Service User Guide for Security Devices), wrapped in `class-of-service`; see the README
58    // "Sources".
59    const DOC_MAPS: &str = r#"
60class-of-service {
61    scheduler-maps {
62        diffserv-cos-map {
63            forwarding-class be-class scheduler be-scheduler;
64            forwarding-class ef-class scheduler ef-scheduler;
65            forwarding-class af-class scheduler af-scheduler;
66            forwarding-class nc-class scheduler nc-scheduler;
67        }
68        be-map {
69            forwarding-class best-effort scheduler be-sched;
70        }
71    }
72}
73"#;
74
75    #[test]
76    fn reads_the_guide_scheduler_maps() {
77        let maps = class_of_service(&juniper::parse_braces(DOC_MAPS))
78            .unwrap()
79            .scheduler_maps;
80        assert_eq!(maps.len(), 2);
81        assert_eq!(maps[0].name, "diffserv-cos-map");
82        assert_eq!(maps[0].entries.len(), 4);
83        assert_eq!(maps[0].entries[2].forwarding_class, "af-class");
84        assert_eq!(maps[0].entries[2].scheduler.as_deref(), Some("af-scheduler"));
85        assert_eq!(maps[1].name, "be-map");
86        assert_eq!(maps[1].entries[0].forwarding_class, "best-effort");
87        assert_eq!(maps[1].entries[0].scheduler.as_deref(), Some("be-sched"));
88    }
89
90    #[test]
91    fn a_map_without_a_scheduler_has_none() {
92        let cos = class_of_service(&juniper::parse_braces(
93            "class-of-service {\n    scheduler-maps {\n        m {\n            forwarding-class be;\n        }\n    }\n}\n",
94        ))
95        .unwrap();
96        assert_eq!(cos.scheduler_maps[0].entries[0].forwarding_class, "be");
97        assert_eq!(cos.scheduler_maps[0].entries[0].scheduler, None);
98    }
99}