Skip to main content

asterust_junos/class_of_service/
fragmentation_map.rs

1//! `fragmentation-maps`
2//! `class-of-service { fragmentation-maps { <name> { forwarding-class <class> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [fragmentation-maps](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/fragmentation-maps-edit-cos-cs.html)
7
8use crate::{active_children, arg_of, child_block, has_leaf, named_children};
9use asterust::config::ConfigLine;
10
11/// A `forwarding-class <name>` entry of a fragmentation map.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct FragmentationClass {
15    pub forwarding_class: String,
16    /// Milliseconds.
17    pub drop_timeout: Option<String>,
18    /// Bytes.
19    pub fragment_threshold: Option<String>,
20    pub multilink_class: Option<String>,
21    pub no_fragmentation: bool,
22}
23
24/// A `fragmentation-maps` entry.
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct FragmentationMap {
28    pub name: String,
29    pub classes: Vec<FragmentationClass>,
30}
31
32pub(super) fn parse_fragmentation_maps(cos: &ConfigLine) -> Vec<FragmentationMap> {
33    let Some(block) = child_block(cos, "fragmentation-maps") else {
34        return Vec::new();
35    };
36    active_children(block)
37        .filter_map(|map| {
38            Some(FragmentationMap {
39                name: map.text.split_whitespace().next()?.to_string(),
40                classes: named_children(map, "forwarding-class")
41                    .map(|(class, line)| {
42                        let field = |keyword: &str| arg_of(line, keyword).map(str::to_string);
43                        FragmentationClass {
44                            forwarding_class: class.to_string(),
45                            drop_timeout: field("drop-timeout"),
46                            fragment_threshold: field("fragment-threshold"),
47                            multilink_class: field("multilink-class"),
48                            no_fragmentation: has_leaf(line, "no-fragmentation"),
49                        }
50                    })
51                    .collect(),
52            })
53        })
54        .collect()
55}
56
57#[cfg(test)]
58mod tests {
59    use crate::class_of_service::class_of_service;
60    use asterust::config::juniper;
61
62    // Not from documentation: written from the CLI Reference. See the README "Sources".
63    const EXTRA: &str = r#"
64class-of-service {
65    fragmentation-maps {
66        FRAG {
67            forwarding-class best-effort {
68                fragment-threshold 128;
69                drop-timeout 50;
70            }
71            forwarding-class expedited-forwarding {
72                no-fragmentation;
73            }
74            forwarding-class assured-forwarding {
75                multilink-class 2;
76            }
77        }
78    }
79}
80"#;
81
82    #[test]
83    fn reads_the_fragmentation_maps() {
84        let maps = class_of_service(&juniper::parse_braces(EXTRA))
85            .unwrap()
86            .fragmentation_maps;
87        assert_eq!(maps.len(), 1);
88        assert_eq!(maps[0].name, "FRAG");
89        let classes = &maps[0].classes;
90        assert_eq!(classes.len(), 3);
91        assert_eq!(classes[0].forwarding_class, "best-effort");
92        assert_eq!(classes[0].fragment_threshold.as_deref(), Some("128"));
93        assert_eq!(classes[0].drop_timeout.as_deref(), Some("50"));
94        assert!(!classes[0].no_fragmentation);
95        assert!(classes[1].no_fragmentation);
96        assert_eq!(classes[2].multilink_class.as_deref(), Some("2"));
97    }
98}