asterust-junos 0.5.12

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
//! `ethernet-switch-profile`
//! `interfaces { <name> { ether-options | gigether-options | aggregated-ether-options { ethernet-switch-profile { ... } } } }`.
//!
//! # See also
//!
//! - [ethernet-switch-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ethernet-switch-profile-edit-interfaces.html)

use crate::{active_children, arg_of, child_block, list_values, named_children, toggle};
use asterust::config::ConfigLine;

/// A `policer <name>` of an Ethernet policer profile.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EthernetSwitchPolicer {
    pub name: String,
    /// Bits per second, from `aggregate { bandwidth-limit }`.
    pub aggregate_bandwidth_limit: Option<String>,
    /// Bytes, from `aggregate { burst-size-limit }`.
    pub aggregate_burst_size_limit: Option<String>,
    pub premium_bandwidth_limit: Option<String>,
    pub premium_burst_size_limit: Option<String>,
}

/// A `forwarding-class <name>` of the premium output priority map.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EthernetSwitchForwardingClass {
    pub name: String,
    /// `high` or `low`.
    pub loss_priority: Option<String>,
}

/// The `ethernet-switch-profile` of an Ethernet interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EthernetSwitchProfile {
    /// `Some(true)` for `mac-learn-enable`, `Some(false)` for `no-mac-learn-enable`.
    pub mac_learn_enable: Option<bool>,
    pub tag_protocol_id: Option<String>,
    pub storm_control: Option<String>,
    /// The 802.1p values of `input-priority-map { ieee802.1p premium [ ... ] }`.
    pub input_premium_ieee_802_1p: Vec<String>,
    /// The classes of `output-priority-map { classifier { premium { ... } } }`.
    pub output_premium_classes: Vec<EthernetSwitchForwardingClass>,
    pub policers: Vec<EthernetSwitchPolicer>,
}

pub(in crate::interfaces) fn parse_ethernet_switch_profile(profile: &ConfigLine) -> EthernetSwitchProfile {
    let policer_profile = child_block(profile, "ethernet-policer-profile");
    let limits = |block: Option<&ConfigLine>, keyword: &str| block.and_then(|b| arg_of(b, keyword)).map(str::to_string);
    EthernetSwitchProfile {
        mac_learn_enable: toggle(profile, "mac-learn-enable"),
        tag_protocol_id: policer_profile
            .and_then(|p| arg_of(p, "tag-protocol-id"))
            .or_else(|| arg_of(profile, "tag-protocol-id"))
            .map(str::to_string),
        storm_control: policer_profile
            .and_then(|p| arg_of(p, "storm-control"))
            .or_else(|| arg_of(profile, "storm-control"))
            .map(str::to_string),
        input_premium_ieee_802_1p: policer_profile
            .and_then(|p| child_block(p, "input-priority-map"))
            .map(|map| {
                active_children(map)
                    .filter_map(|c| c.arg("ieee802.1p"))
                    .flat_map(|arg| list_values(arg.trim_start_matches("premium")))
                    .collect()
            })
            .unwrap_or_default(),
        output_premium_classes: policer_profile
            .and_then(|p| child_block(p, "output-priority-map"))
            .and_then(|map| child_block(map, "classifier"))
            .and_then(|classifier| child_block(classifier, "premium"))
            .map(|premium| {
                named_children(premium, "forwarding-class")
                    .map(|(name, class)| EthernetSwitchForwardingClass {
                        name: name.to_string(),
                        loss_priority: arg_of(class, "loss-priority").map(str::to_string),
                    })
                    .collect()
            })
            .unwrap_or_default(),
        policers: policer_profile
            .map(|p| {
                named_children(p, "policer")
                    .map(|(name, policer)| {
                        let aggregate = child_block(policer, "aggregate");
                        let premium = child_block(policer, "premium");
                        EthernetSwitchPolicer {
                            name: name.to_string(),
                            aggregate_bandwidth_limit: limits(aggregate, "bandwidth-limit"),
                            aggregate_burst_size_limit: limits(aggregate, "burst-size-limit"),
                            premium_bandwidth_limit: limits(premium, "bandwidth-limit"),
                            premium_burst_size_limit: limits(premium, "burst-size-limit"),
                        }
                    })
                    .collect()
            })
            .unwrap_or_default(),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_the_ethernet_switch_profile() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ge-0/0/0 {
        gigether-options {
            ethernet-switch-profile {
                mac-learn-enable;
                ethernet-policer-profile {
                    input-priority-map {
                        ieee802.1p premium [ 5 6 ];
                    }
                    output-priority-map {
                        classifier {
                            premium {
                                forwarding-class expedited-forwarding {
                                    loss-priority low;
                                }
                            }
                        }
                    }
                    policer cos-policer {
                        aggregate {
                            bandwidth-limit 100m;
                            burst-size-limit 15k;
                        }
                        premium {
                            bandwidth-limit 50m;
                            burst-size-limit 10k;
                        }
                    }
                    storm-control sc1;
                    tag-protocol-id 0x8100;
                }
            }
        }
    }
}
"#,
        ));
        let profile = ifaces[0]
            .ether_options
            .as_ref()
            .unwrap()
            .ethernet_switch_profile
            .as_ref()
            .unwrap();
        assert_eq!(profile.mac_learn_enable, Some(true));
        assert_eq!(profile.tag_protocol_id.as_deref(), Some("0x8100"));
        assert_eq!(profile.storm_control.as_deref(), Some("sc1"));
        assert_eq!(profile.input_premium_ieee_802_1p, ["5", "6"]);
        assert_eq!(profile.output_premium_classes[0].name, "expedited-forwarding");
        assert_eq!(profile.output_premium_classes[0].loss_priority.as_deref(), Some("low"));
        let policer = &profile.policers[0];
        assert_eq!(policer.name, "cos-policer");
        assert_eq!(policer.aggregate_bandwidth_limit.as_deref(), Some("100m"));
        assert_eq!(policer.aggregate_burst_size_limit.as_deref(), Some("15k"));
        assert_eq!(policer.premium_bandwidth_limit.as_deref(), Some("50m"));
        assert_eq!(policer.premium_burst_size_limit.as_deref(), Some("10k"));
    }
}