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
//! `host-outbound-traffic`
//! `class-of-service { host-outbound-traffic { ... } }`.
//!
//! # See also
//!
//! - [host-outbound-traffic](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/host-outbound-traffic-edit-cos.html)

use crate::{arg_of, child_block, has_leaf};
use asterust::config::ConfigLine;

/// The `host-outbound-traffic` block.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct HostOutboundTraffic {
    pub dscp_code_point: Option<String>,
    pub forwarding_class: Option<String>,
    /// The `default` of the `ieee-802.1` block.
    pub ieee_802_1_default: Option<String>,
    pub ieee_802_1_rewrite_rules: bool,
    pub ieee_802_1_override_firewall: bool,
    /// The `dscp-code-point` of `protocol isis-over-gre`.
    pub isis_over_gre_dscp_code_point: Option<String>,
}

pub(super) fn parse_host_outbound_traffic(cos: &ConfigLine) -> Option<HostOutboundTraffic> {
    let host = child_block(cos, "host-outbound-traffic")?;
    let field = |line: &ConfigLine, keyword: &str| arg_of(line, keyword).map(str::to_string);
    let ieee = child_block(host, "ieee-802.1");
    Some(HostOutboundTraffic {
        dscp_code_point: field(host, "dscp-code-point"),
        forwarding_class: field(host, "forwarding-class"),
        ieee_802_1_default: ieee.and_then(|block| field(block, "default")),
        ieee_802_1_rewrite_rules: ieee.is_some_and(|block| has_leaf(block, "rewrite-rules")),
        ieee_802_1_override_firewall: ieee.is_some_and(|block| has_leaf(block, "override-firewall")),
        isis_over_gre_dscp_code_point: child_block(host, "protocol")
            .and_then(|protocol| child_block(protocol, "isis-over-gre"))
            .and_then(|gre| field(gre, "dscp-code-point")),
    })
}

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

    // Not from documentation: written from the CLI Reference. See the README "Sources".
    const EXTRA: &str = r#"
class-of-service {
    host-outbound-traffic {
        dscp-code-point 110000;
        forwarding-class network-control;
        ieee-802.1 {
            default 6;
            rewrite-rules;
            override-firewall;
        }
        protocol {
            isis-over-gre {
                dscp-code-point 111000;
            }
        }
    }
}
"#;

    #[test]
    fn reads_the_host_outbound_traffic_options() {
        let host = class_of_service(&juniper::parse_braces(EXTRA))
            .unwrap()
            .host_outbound_traffic
            .unwrap();
        assert_eq!(host.dscp_code_point.as_deref(), Some("110000"));
        assert_eq!(host.forwarding_class.as_deref(), Some("network-control"));
        assert_eq!(host.ieee_802_1_default.as_deref(), Some("6"));
        assert!(host.ieee_802_1_rewrite_rules && host.ieee_802_1_override_firewall);
        assert_eq!(host.isis_over_gre_dscp_code_point.as_deref(), Some("111000"));
    }

    #[test]
    fn a_bare_block_has_nothing_set() {
        let cos = class_of_service(&juniper::parse_braces(
            "class-of-service {\n    host-outbound-traffic;\n}\n",
        ))
        .unwrap();
        assert_eq!(cos.host_outbound_traffic, Some(Default::default()));
    }
}