Skip to main content

asterust_junos/class_of_service/
drop_profile.rs

1//! `drop-profiles`
2//! `class-of-service { drop-profiles { <name> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [drop-profiles](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/drop-profiles-edit-cos.html)
7//! - [interpolate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interpolate-edit-cos.html)
8//! - [Example: Configuring RED Drop Profiles](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/topics/example/cos-red-drop-profile-security-configuring.html)
9//! - [Example: Configuring Segmented and Interpolated Style Profiles](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cos-security-devices/topics/example/cos-drop-profile-segmented-interpolated-security-configuring.html)
10
11use crate::{active_children, args_of, child_block, list_values, value_after};
12use asterust::config::ConfigLine;
13
14/// A `fill-level <percent> drop-probability <percent>` point of a segmented drop profile.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct DropPoint {
18    pub fill_level: String,
19    pub drop_probability: String,
20}
21
22/// The `interpolate` block of a drop profile.
23#[derive(Clone, Debug, Default, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct DropProfileInterpolation {
26    pub fill_levels: Vec<String>,
27    pub drop_probabilities: Vec<String>,
28}
29
30/// A `drop-profiles` entry.
31#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct DropProfile {
34    pub name: String,
35    /// The `fill-level` and `drop-probability` pairs of a segmented profile.
36    pub points: Vec<DropPoint>,
37    pub interpolate: Option<DropProfileInterpolation>,
38}
39
40fn parse_point(arg: &str) -> Option<DropPoint> {
41    let words: Vec<&str> = arg.split_whitespace().collect();
42    Some(DropPoint {
43        fill_level: words.first()?.to_string(),
44        drop_probability: value_after(&words, "drop-probability")?,
45    })
46}
47
48fn parse_profile(profile: &ConfigLine) -> Option<DropProfile> {
49    let name = profile.text.split_whitespace().next()?;
50    // The guide writes the lists of an interpolated profile directly in the profile, without `interpolate`.
51    let interpolate = child_block(profile, "interpolate")
52        .or_else(|| (!args_of(profile, "drop-probability").is_empty()).then_some(profile));
53    Some(DropProfile {
54        name: name.to_string(),
55        points: args_of(profile, "fill-level")
56            .into_iter()
57            .filter_map(parse_point)
58            .collect(),
59        interpolate: interpolate.map(|block| DropProfileInterpolation {
60            fill_levels: args_of(block, "fill-level").into_iter().flat_map(list_values).collect(),
61            drop_probabilities: args_of(block, "drop-probability")
62                .into_iter()
63                .flat_map(list_values)
64                .collect(),
65        }),
66    })
67}
68
69pub(super) fn parse_drop_profiles(cos: &ConfigLine) -> Vec<DropProfile> {
70    let Some(block) = child_block(cos, "drop-profiles") else {
71        return Vec::new();
72    };
73    active_children(block).filter_map(parse_profile).collect()
74}
75
76#[cfg(test)]
77mod tests {
78    use crate::class_of_service::class_of_service;
79    use asterust::config::juniper;
80
81    // The `drop-profiles` of Example: Configuring RED Drop Profiles and Example: Configuring Segmented and Interpolated
82    // Style Profiles (Class of Service User Guide for Security Devices), wrapped in `class-of-service`; see the README
83    // "Sources".
84    const DOC_RED: &str = r#"
85class-of-service {
86    drop-profiles {
87        af-normal {
88            interpolate {
89                fill-level [ 95 100 ];
90                drop-probability [ 0 100 ];
91            }
92        }
93        af-with-PLP {
94            interpolate {
95                fill-level [ 80 95 ];
96                drop-probability [ 95 100 ];
97            }
98        }
99    }
100}
101"#;
102
103    const DOC_SEGMENTED: &str = r#"
104class-of-service {
105    drop-profiles {
106        segmented-style-profile {
107            fill-level 25 drop-probability 25;
108            fill-level 50 drop-probability 50;
109            fill-level 75 drop-probability 75;
110            fill-level 95 drop-probability 100;
111        }
112        interpolated-style-profile {
113            fill-level [ 50 75 ];
114            drop-probability [ 25 50 ];
115        }
116    }
117}
118"#;
119
120    #[test]
121    fn reads_the_guide_red_profiles() {
122        let profiles = class_of_service(&juniper::parse_braces(DOC_RED)).unwrap().drop_profiles;
123        assert_eq!(profiles.len(), 2);
124        assert_eq!(profiles[0].name, "af-normal");
125        assert!(profiles[0].points.is_empty());
126        let interpolate = profiles[0].interpolate.as_ref().unwrap();
127        assert_eq!(interpolate.fill_levels, ["95", "100"]);
128        assert_eq!(interpolate.drop_probabilities, ["0", "100"]);
129        assert_eq!(profiles[1].name, "af-with-PLP");
130        assert_eq!(profiles[1].interpolate.as_ref().unwrap().fill_levels, ["80", "95"]);
131    }
132
133    #[test]
134    fn reads_the_guide_segmented_and_interpolated_profiles() {
135        let profiles = class_of_service(&juniper::parse_braces(DOC_SEGMENTED))
136            .unwrap()
137            .drop_profiles;
138        assert_eq!(profiles.len(), 2);
139        let segmented = &profiles[0];
140        assert_eq!(segmented.name, "segmented-style-profile");
141        assert_eq!(segmented.points.len(), 4);
142        assert_eq!(segmented.points[3].fill_level, "95");
143        assert_eq!(segmented.points[3].drop_probability, "100");
144        assert_eq!(segmented.interpolate, None);
145        let interpolated = &profiles[1];
146        assert!(interpolated.points.is_empty());
147        assert_eq!(
148            interpolated.interpolate.as_ref().unwrap().drop_probabilities,
149            ["25", "50"]
150        );
151    }
152}