Skip to main content

asterust_junos/class_of_service/
classifier.rs

1//! `classifiers`
2//! `class-of-service { classifiers { <type> <name> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [classifiers](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/classifiers-edit-cos.html)
7//! - [forwarding-class (classifiers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/forwarding-class-edit-cos-classifiers.html)
8//! - [Example: Configure and Apply Custom BA Classifiers](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/cos/topics/example/cos-behavior-aggregate-classifier-configuring-security.html)
9//! - [Example: Configuring Forwarding Classes](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/topics/example/cos-forwarding-class-configuring-security.html)
10
11use super::{code_points, typed_children};
12use crate::{arg_of, args_of, child_block, named_children};
13use asterust::config::ConfigLine;
14
15/// A `loss-priority <level> code-points [ ... ]` statement of a classifier.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct ClassifierEntry {
19    pub loss_priority: String,
20    /// Bit patterns or code-point aliases.
21    pub code_points: Vec<String>,
22}
23
24/// A `forwarding-class <name>` entry of a classifier.
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct ClassifierClass {
28    pub name: String,
29    pub entries: Vec<ClassifierEntry>,
30}
31
32/// A `<type> <name>` classifier.
33#[derive(Clone, Debug, PartialEq, Eq)]
34#[non_exhaustive]
35pub struct Classifier {
36    /// `dscp`, `dscp-ipv6`, `exp`, `ieee-802.1`, `ieee-802.1ad`, `inet-precedence` or `inet6-precedence`.
37    pub kind: String,
38    pub name: String,
39    /// The classifier imported, or `default`.
40    pub import: Option<String>,
41    pub forwarding_classes: Vec<ClassifierClass>,
42}
43
44fn parse_entry(arg: &str) -> ClassifierEntry {
45    let (level, points) = arg.split_once("code-points").unwrap_or((arg, ""));
46    ClassifierEntry {
47        loss_priority: level.trim().to_string(),
48        code_points: code_points(points),
49    }
50}
51
52pub(super) fn parse_classifiers(cos: &ConfigLine) -> Vec<Classifier> {
53    let Some(block) = child_block(cos, "classifiers") else {
54        return Vec::new();
55    };
56    typed_children(block)
57        .map(|(kind, name, classifier)| Classifier {
58            kind: kind.to_string(),
59            name: name.to_string(),
60            import: arg_of(classifier, "import").map(str::to_string),
61            forwarding_classes: named_children(classifier, "forwarding-class")
62                .map(|(class, line)| ClassifierClass {
63                    name: class.to_string(),
64                    entries: args_of(line, "loss-priority").into_iter().map(parse_entry).collect(),
65                })
66                .collect(),
67        })
68        .collect()
69}
70
71#[cfg(test)]
72mod tests {
73    use crate::class_of_service::class_of_service;
74    use asterust::config::juniper;
75
76    // The `classifiers` of Example: Configure and Apply Custom BA Classifiers and of Example: Configuring Forwarding
77    // Classes (Class of Service User Guide for Security Devices), wrapped in `class-of-service`; see the README
78    // "Sources".
79    const DOC_BA_CLASSIFIER: &str = r#"
80class-of-service {
81    classifiers {
82        dscp ba-classifier {
83            import default;
84            forwarding-class be-class {
85                loss-priority high code-points 000001;
86            }
87            forwarding-class ef-class {
88                loss-priority high code-points 101111;
89            }
90            forwarding-class af-class {
91                loss-priority high code-points 001100;
92            }
93            forwarding-class nc-class {
94                loss-priority high code-points 110001;
95            }
96        }
97    }
98}
99"#;
100
101    // The guide writes the list with commas.
102    const DOC_DSCP_TABLE: &str = r#"
103class-of-service {
104    classifiers {
105        dscp dscp-table {
106            forwarding-class ef {
107                loss-priority low code-points [101000, 101001];
108                loss-priority high code-points [101010, 101011];
109            }
110            forwarding-class be {
111                loss-priority low code-points [000000];
112            }
113        }
114    }
115}
116"#;
117
118    const DOC_INET_PRECEDENCE: &str = r#"
119class-of-service {
120    classifiers {
121        inet-precedence corp-traffic {
122            forwarding-class voice-class {
123                loss-priority low code-points 101;
124            }
125            forwarding-class data-class {
126                loss-priority high code-points 000;
127            }
128        }
129    }
130}
131"#;
132
133    // Not from documentation: written from the CLI Reference. See the README "Sources".
134    const EXTRA: &str = r#"
135class-of-service {
136    classifiers {
137        exp mpls-in {
138            import mpls-base;
139            forwarding-class expedited-forwarding {
140                loss-priority low code-points [ ef af11 ];
141            }
142        }
143        ieee-802.1 dot1p;
144    }
145}
146"#;
147
148    fn classifiers(text: &str) -> Vec<super::Classifier> {
149        class_of_service(&juniper::parse_braces(text)).unwrap().classifiers
150    }
151
152    #[test]
153    fn reads_the_guide_ba_classifier() {
154        let classifiers = classifiers(DOC_BA_CLASSIFIER);
155        assert_eq!(classifiers.len(), 1);
156        let classifier = &classifiers[0];
157        assert_eq!(classifier.kind, "dscp");
158        assert_eq!(classifier.name, "ba-classifier");
159        assert_eq!(classifier.import.as_deref(), Some("default"));
160        assert_eq!(classifier.forwarding_classes.len(), 4);
161        assert_eq!(classifier.forwarding_classes[1].name, "ef-class");
162        let entry = &classifier.forwarding_classes[1].entries[0];
163        assert_eq!(entry.loss_priority, "high");
164        assert_eq!(entry.code_points, ["101111"]);
165    }
166
167    #[test]
168    fn reads_the_guide_code_point_lists() {
169        let classifier = &classifiers(DOC_DSCP_TABLE)[0];
170        let ef = &classifier.forwarding_classes[0];
171        assert_eq!(ef.entries.len(), 2);
172        assert_eq!(ef.entries[0].loss_priority, "low");
173        assert_eq!(ef.entries[0].code_points, ["101000", "101001"]);
174        assert_eq!(ef.entries[1].code_points, ["101010", "101011"]);
175        assert_eq!(classifier.forwarding_classes[1].entries[0].code_points, ["000000"]);
176        assert_eq!(classifier.import, None);
177    }
178
179    #[test]
180    fn reads_the_guide_inet_precedence_classifier() {
181        let classifier = &classifiers(DOC_INET_PRECEDENCE)[0];
182        assert_eq!(classifier.kind, "inet-precedence");
183        assert_eq!(classifier.name, "corp-traffic");
184        assert_eq!(classifier.forwarding_classes[0].name, "voice-class");
185        assert_eq!(classifier.forwarding_classes[0].entries[0].code_points, ["101"]);
186        assert_eq!(classifier.forwarding_classes[1].entries[0].loss_priority, "high");
187    }
188
189    #[test]
190    fn reads_aliases_and_a_classifier_without_entries() {
191        let classifiers = classifiers(EXTRA);
192        assert_eq!(classifiers.len(), 2);
193        assert_eq!(classifiers[0].kind, "exp");
194        assert_eq!(classifiers[0].import.as_deref(), Some("mpls-base"));
195        assert_eq!(
196            classifiers[0].forwarding_classes[0].entries[0].code_points,
197            ["ef", "af11"]
198        );
199        assert_eq!(classifiers[1].kind, "ieee-802.1");
200        assert_eq!(classifiers[1].name, "dot1p");
201        assert!(classifiers[1].forwarding_classes.is_empty());
202    }
203}