asterust-junos 0.5.5

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `damping`
//! `policy-options { damping <name> { ... } }`.
//!
//! # See also
//!
//! - [damping (Policy Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/damping-edit-policy-options.html)

use crate::{active_children, split_keyword, split_name};
use asterust::config::ConfigLine;

/// A `damping <name>`, a set of BGP route flap damping parameters that `then damping` applies.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Damping {
    pub name: String,
    /// `disable`: routes it is applied to are not damped.
    pub disable: bool,
    pub half_life: Option<String>,
    pub max_suppress: Option<String>,
    pub reuse: Option<String>,
    pub suppress: Option<String>,
}

pub(super) fn parse_damping(line: &ConfigLine) -> Option<Damping> {
    let (name, inline) = split_name(line.arg("damping")?);
    let mut damping = Damping {
        name: name.to_string(),
        disable: false,
        half_life: None,
        max_suppress: None,
        reuse: None,
        suppress: None,
    };
    let statements = active_children(line)
        .map(|c| c.text.as_str())
        .chain(Some(inline).filter(|s| !s.is_empty()));
    for statement in statements {
        let (keyword, value) = split_keyword(statement);
        let value = Some(value.to_string()).filter(|v| !v.is_empty());
        match keyword {
            "disable" => damping.disable = true,
            "half-life" => damping.half_life = value,
            "max-suppress" => damping.max_suppress = value,
            "reuse" => damping.reuse = value,
            "suppress" => damping.suppress = value,
            _ => {}
        }
    }
    Some(damping)
}

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

    // Adapted from the BGP User Guide, Understanding BGP Session Flaps: the `damping` entries of the `show
    // policy-options` output; see the README "Sources".
    #[test]
    fn reads_the_guide_damping_profiles() {
        let config = juniper::parse_braces(
            "policy-options {\n    damping aggressive {\n        half-life 30;\n        suppress 2500;\n    }\n    damping timid {\n        half-life 5;\n    }\n    damping dry {\n        disable;\n    }\n    damping no-damp {\n        disable;\n    }\n}\n",
        );
        let dampings = policy_options(&config).unwrap().dampings;
        assert_eq!(dampings.len(), 4);
        assert_eq!(dampings[0].name, "aggressive");
        assert_eq!(dampings[0].half_life.as_deref(), Some("30"));
        assert_eq!(dampings[0].suppress.as_deref(), Some("2500"));
        assert!(!dampings[0].disable);
        assert_eq!(dampings[1].half_life.as_deref(), Some("5"));
        assert_eq!(dampings[1].suppress, None);
        assert!(dampings[2].disable);
        assert!(dampings[3].disable);
    }

    // Not from documentation: the remaining options and the one-line form, from the damping CLI Reference page.
    #[test]
    fn reads_the_other_options_and_the_one_line_form() {
        let config = juniper::parse_braces(
            "policy-options {\n    damping custom {\n        reuse 800;\n        max-suppress 40;\n    }\n    damping quick half-life 3;\n}\n",
        );
        let dampings = policy_options(&config).unwrap().dampings;
        assert_eq!(dampings[0].reuse.as_deref(), Some("800"));
        assert_eq!(dampings[0].max_suppress.as_deref(), Some("40"));
        assert_eq!(dampings[1].name, "quick");
        assert_eq!(dampings[1].half_life.as_deref(), Some("3"));
    }
}