Skip to main content

asterust_junos/protocols/mpls/
diffserv_te.rs

1//! `diffserv-te`
2//! `protocols { mpls { diffserv-te { ... } } }`.
3//!
4//! # See also
5//!
6//! - [diffserv-te](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/diffserv-te-edit-protocols-mpls.html)
7//! - [bandwidth-model](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bandwidth-model-edit-protocols-mpls.html)
8//! - [te-class-matrix](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/te-class-matrix-edit-protocols-mpls.html)
9//! - [DiffServ-Aware Traffic Engineering Configuration](https://networkingdocs.hpe.com/documentation/us/en/software/junos/mpls/topics/topic-map/diffserv-aware-traffic-engineering-configuraion.html)
10
11use crate::{active_children, arg_of, args_of, child_block, value_after};
12use asterust::config::ConfigLine;
13
14/// A `traffic-class` of a TE class: a class type and its priority.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct MplsTrafficClass {
18    /// The class type, e.g. `ct0`.
19    pub class_type: String,
20    pub priority: Option<String>,
21}
22
23/// A `te <number>` entry of the `te-class-matrix`.
24#[derive(Clone, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct MplsTeClass {
27    /// The number, as in `te 0` or `te0`.
28    pub te: String,
29    pub priority: Option<String>,
30    pub traffic_classes: Vec<MplsTrafficClass>,
31}
32
33/// The `diffserv-te` block.
34#[derive(Clone, Debug, Default, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct MplsDiffservTe {
37    /// `extended-mam`, `mam` or `rdm`.
38    pub bandwidth_model: Option<String>,
39    pub te_class_matrix: Vec<MplsTeClass>,
40}
41
42/// `ct <number> priority <n>` or `ct<number> priority <n>`.
43fn parse_traffic_class(statement: &str) -> Option<MplsTrafficClass> {
44    let words: Vec<&str> = statement.split_whitespace().collect();
45    let class_type = if let Some(position) = words.iter().position(|w| *w == "ct") {
46        format!("ct{}", words.get(position + 1)?)
47    } else {
48        (*words.iter().find(|w| w.starts_with("ct"))?).to_string()
49    };
50    Some(MplsTrafficClass {
51        class_type,
52        priority: value_after(&words, "priority"),
53    })
54}
55
56/// `te` is the text after `te`: `0`, or `0 traffic-class ct1 priority 4` for the inline form.
57fn parse_te_class(te: &str, entry: &ConfigLine) -> MplsTeClass {
58    let (number, inline) = te.split_once(char::is_whitespace).unwrap_or((te, ""));
59    let mut classes: Vec<MplsTrafficClass> = args_of(entry, "traffic-class")
60        .into_iter()
61        .filter_map(parse_traffic_class)
62        .collect();
63    if let Some(statement) = inline.trim().strip_prefix("traffic-class") {
64        classes.extend(parse_traffic_class(statement));
65    }
66    if let Some(block) = child_block(entry, "traffic-class") {
67        classes.extend(active_children(block).filter_map(|c| parse_traffic_class(&c.text)));
68    }
69    MplsTeClass {
70        te: number.to_string(),
71        priority: arg_of(entry, "priority").map(str::to_string),
72        traffic_classes: classes,
73    }
74}
75
76pub(super) fn parse_diffserv_te(mpls: &ConfigLine) -> Option<MplsDiffservTe> {
77    let diffserv = child_block(mpls, "diffserv-te")?;
78    let model = child_block(diffserv, "bandwidth-model");
79    Some(MplsDiffservTe {
80        bandwidth_model: model.and_then(|block| {
81            ["extended-mam", "mam", "rdm"]
82                .into_iter()
83                .find(|name| active_children(block).any(|c| c.text == *name))
84                .map(str::to_string)
85        }),
86        te_class_matrix: child_block(diffserv, "te-class-matrix")
87            .map(|matrix| {
88                active_children(matrix)
89                    .filter_map(|entry| {
90                        let te = entry.text.strip_prefix("te")?.trim();
91                        Some(parse_te_class(te, entry))
92                    })
93                    .collect()
94            })
95            .unwrap_or_default(),
96    })
97}
98
99#[cfg(test)]
100mod tests {
101    use crate::protocols::mpls::mpls;
102    use asterust::config::juniper;
103
104    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
105    const CONFIG: &str = r#"
106protocols {
107    mpls {
108        diffserv-te {
109            bandwidth-model {
110                rdm;
111            }
112            te-class-matrix {
113                te 0 {
114                    priority 7;
115                    traffic-class {
116                        ct 0 priority 7;
117                    }
118                }
119                te1 {
120                    traffic-class ct1 priority 5;
121                    traffic-class ct2 priority 4;
122                }
123            }
124        }
125    }
126}
127"#;
128
129    #[test]
130    fn reads_the_bandwidth_model_and_te_classes() {
131        let diffserv = mpls(&juniper::parse_braces(CONFIG)).unwrap().diffserv_te.unwrap();
132        assert_eq!(diffserv.bandwidth_model.as_deref(), Some("rdm"));
133        assert_eq!(diffserv.te_class_matrix.len(), 2);
134
135        let first = &diffserv.te_class_matrix[0];
136        assert_eq!(first.te, "0");
137        assert_eq!(first.priority.as_deref(), Some("7"));
138        assert_eq!(first.traffic_classes.len(), 1);
139        assert_eq!(first.traffic_classes[0].class_type, "ct0");
140        assert_eq!(first.traffic_classes[0].priority.as_deref(), Some("7"));
141
142        let second = &diffserv.te_class_matrix[1];
143        assert_eq!(second.te, "1");
144        assert_eq!(second.priority, None);
145        let types: Vec<_> = second.traffic_classes.iter().map(|c| c.class_type.as_str()).collect();
146        assert_eq!(types, ["ct1", "ct2"]);
147        assert_eq!(second.traffic_classes[1].priority.as_deref(), Some("4"));
148    }
149
150    #[test]
151    fn a_bare_diffserv_te_block_has_nothing_set() {
152        let diffserv = mpls(&juniper::parse_braces(
153            "protocols {\n    mpls {\n        diffserv-te;\n    }\n}\n",
154        ))
155        .unwrap()
156        .diffserv_te
157        .unwrap();
158        assert_eq!(diffserv, Default::default());
159    }
160
161    // The `te-class-matrix` example of DiffServ-Aware Traffic Engineering Configuration (MPLS Applications User Guide),
162    // under `protocols mpls diffserv-te`; see the README "Sources".
163    const DOC_TE_CLASS: &str = r#"
164protocols {
165    mpls {
166        diffserv-te {
167            te-class-matrix {
168                te0 traffic-class ct1 priority 4;
169            }
170        }
171    }
172}
173"#;
174
175    #[test]
176    fn reads_the_guide_inline_te_class() {
177        let diffserv = mpls(&juniper::parse_braces(DOC_TE_CLASS)).unwrap().diffserv_te.unwrap();
178        assert_eq!(diffserv.bandwidth_model, None);
179        assert_eq!(diffserv.te_class_matrix.len(), 1);
180        let class = &diffserv.te_class_matrix[0];
181        assert_eq!(class.te, "0");
182        assert_eq!(class.traffic_classes.len(), 1);
183        assert_eq!(class.traffic_classes[0].class_type, "ct1");
184        assert_eq!(class.traffic_classes[0].priority.as_deref(), Some("4"));
185    }
186}