asterust-junos 0.4.4

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `drop-profiles`
//! `class-of-service { drop-profiles { <name> { ... } } }`.
//!
//! # See also
//!
//! - [drop-profiles](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/drop-profiles-edit-cos.html)
//! - [interpolate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interpolate-edit-cos.html)
//! - [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)
//! - [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)

use crate::{active_children, args_of, child_block, list_values, value_after};
use asterust::config::ConfigLine;

/// A `fill-level <percent> drop-probability <percent>` point of a segmented drop profile.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DropPoint {
    pub fill_level: String,
    pub drop_probability: String,
}

/// The `interpolate` block of a drop profile.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct DropProfileInterpolation {
    pub fill_levels: Vec<String>,
    pub drop_probabilities: Vec<String>,
}

/// A `drop-profiles` entry.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DropProfile {
    pub name: String,
    /// The `fill-level` and `drop-probability` pairs of a segmented profile.
    pub points: Vec<DropPoint>,
    pub interpolate: Option<DropProfileInterpolation>,
}

fn parse_point(arg: &str) -> Option<DropPoint> {
    let words: Vec<&str> = arg.split_whitespace().collect();
    Some(DropPoint {
        fill_level: words.first()?.to_string(),
        drop_probability: value_after(&words, "drop-probability")?,
    })
}

fn parse_profile(profile: &ConfigLine) -> Option<DropProfile> {
    let name = profile.text.split_whitespace().next()?;
    // The guide writes the lists of an interpolated profile directly in the profile, without `interpolate`.
    let interpolate = child_block(profile, "interpolate")
        .or_else(|| (!args_of(profile, "drop-probability").is_empty()).then_some(profile));
    Some(DropProfile {
        name: name.to_string(),
        points: args_of(profile, "fill-level")
            .into_iter()
            .filter_map(parse_point)
            .collect(),
        interpolate: interpolate.map(|block| DropProfileInterpolation {
            fill_levels: args_of(block, "fill-level").into_iter().flat_map(list_values).collect(),
            drop_probabilities: args_of(block, "drop-probability")
                .into_iter()
                .flat_map(list_values)
                .collect(),
        }),
    })
}

pub(super) fn parse_drop_profiles(cos: &ConfigLine) -> Vec<DropProfile> {
    let Some(block) = child_block(cos, "drop-profiles") else {
        return Vec::new();
    };
    active_children(block).filter_map(parse_profile).collect()
}

#[cfg(test)]
mod tests {
    use crate::class_of_service::class_of_service;
    use asterust::config::juniper;

    // The `drop-profiles` of Example: Configuring RED Drop Profiles and Example: Configuring Segmented and Interpolated
    // Style Profiles (Class of Service User Guide for Security Devices), wrapped in `class-of-service`; see the README
    // "Sources".
    const DOC_RED: &str = r#"
class-of-service {
    drop-profiles {
        af-normal {
            interpolate {
                fill-level [ 95 100 ];
                drop-probability [ 0 100 ];
            }
        }
        af-with-PLP {
            interpolate {
                fill-level [ 80 95 ];
                drop-probability [ 95 100 ];
            }
        }
    }
}
"#;

    const DOC_SEGMENTED: &str = r#"
class-of-service {
    drop-profiles {
        segmented-style-profile {
            fill-level 25 drop-probability 25;
            fill-level 50 drop-probability 50;
            fill-level 75 drop-probability 75;
            fill-level 95 drop-probability 100;
        }
        interpolated-style-profile {
            fill-level [ 50 75 ];
            drop-probability [ 25 50 ];
        }
    }
}
"#;

    #[test]
    fn reads_the_guide_red_profiles() {
        let profiles = class_of_service(&juniper::parse_braces(DOC_RED)).unwrap().drop_profiles;
        assert_eq!(profiles.len(), 2);
        assert_eq!(profiles[0].name, "af-normal");
        assert!(profiles[0].points.is_empty());
        let interpolate = profiles[0].interpolate.as_ref().unwrap();
        assert_eq!(interpolate.fill_levels, ["95", "100"]);
        assert_eq!(interpolate.drop_probabilities, ["0", "100"]);
        assert_eq!(profiles[1].name, "af-with-PLP");
        assert_eq!(profiles[1].interpolate.as_ref().unwrap().fill_levels, ["80", "95"]);
    }

    #[test]
    fn reads_the_guide_segmented_and_interpolated_profiles() {
        let profiles = class_of_service(&juniper::parse_braces(DOC_SEGMENTED))
            .unwrap()
            .drop_profiles;
        assert_eq!(profiles.len(), 2);
        let segmented = &profiles[0];
        assert_eq!(segmented.name, "segmented-style-profile");
        assert_eq!(segmented.points.len(), 4);
        assert_eq!(segmented.points[3].fill_level, "95");
        assert_eq!(segmented.points[3].drop_probability, "100");
        assert_eq!(segmented.interpolate, None);
        let interpolated = &profiles[1];
        assert!(interpolated.points.is_empty());
        assert_eq!(
            interpolated.interpolate.as_ref().unwrap().drop_probabilities,
            ["25", "50"]
        );
    }
}