asterust-junos 0.5.9

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
//! `packet-forwarding-options`
//! `system { packet-forwarding-options { ... } }`.
//!
//! Most of these statements are platform specific and restart the Packet Forwarding Engine.
//!
//! # See also
//!
//! - [eracl-ip6-match](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/eracl-ip6-match-edit-system.html)
//! - [flow-export-timer](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/flow-export-timer-edit-system-packet-fo.html)
//! - [hw-db-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-hw-db-profile.html)
//! - [mcast stats-enable](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mcast-stats-enable-edit-system-packet-forwarding-options.html)
//! - [mcast-repl-type ingress-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-mcast-repl-type-ingress-only.html)
//! - [mcast-repl-type ingress-egress-recycle](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-mcast-repl-type-ingress-egress-recycle.html)
//! - [mef-forwarding-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mef-forwarding-profile-edit-system-packet-forwarding-options.html)
//! - [mpls (packet-forwarding-options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-mpls.html)
//! - [overlay (Packet Forwarding Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/overlay-edit-system-packet-forwarding-options.html)
//! - [packet-format-match](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-packet-format-match.html)
//! - [recycle-bandwidth-profiles](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-recycle-bandwidth-profiles.html)
//! - [vxlan-extended](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-system-profile-vxlan-extended.html)
//! - [vxlan-stitching](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vxlan-stitching-edit-system-packet-forwarding-options-system-profile.html)
//! - [eracl-profile (packet-forwarding-options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-packet-forwarding-options-firewall-eracl-profile.html)
//! - [ethernet-switching (firewall profiles)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ethernet-switching-edit-system-packet-forwarding-options-firewall-profiles.html)
//! - [polling-interval (Network Management)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/polling-interval-edit-system-packet-forwarding-options-hw-resource-monitor.html)
//! - [monitor-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/monitor-profile-edit-system-packet-forwarding-options-hw-resource-monitor.html)
//! - [resource-list (Network Management)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resource-list-edit-system-packet-forwarding-options-hw-resource-monitor.html)

use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, statements};
use asterust::config::ConfigLine;

/// A `profile <name>` of `recycle-bandwidth-profiles`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RecycleBandwidthProfile {
    pub name: String,
    /// `(application, percentage)` pairs such as `("tunnel-services", "20")`.
    pub applications: Vec<(String, String)>,
}

/// The `system-profile` block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PfeSystemProfile {
    pub vxlan_extended: bool,
    pub vxlan_stitching: bool,
    /// `vxlan-stitching { control-word; }`.
    pub vxlan_stitching_control_word: bool,
}

/// The `firewall` block of `packet-forwarding-options`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PfeFirewall {
    /// `eracl-profile { eracl-scale; }`: egress firewall filters in scaled mode.
    pub eracl_scale: bool,
    /// The profiles of `profiles ethernet-switching`, e.g. `profile1`, `udf-profile1`.
    pub ethernet_switching_profiles: Vec<String>,
    /// The profiles of `profiles ethernet-switching egress`.
    pub ethernet_switching_egress_profiles: Vec<String>,
}

/// A `monitor-profile <name>` of `hw-resource-monitor`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HwMonitorProfile {
    pub name: String,
    pub lower_threshold: Option<String>,
    /// `alarm`, `none` or `syslog`.
    pub notification_type: Option<String>,
    pub upper_threshold: Option<String>,
}

/// A `resource-list <name>` of `hw-resource-monitor`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HwResourceList {
    pub name: String,
    /// The `resource-names`, e.g. `lpm-ipv4`.
    pub resource_names: Vec<String>,
}

/// The `hw-resource-monitor` block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HwResourceMonitor {
    /// Milliseconds.
    pub polling_interval: Option<String>,
    pub monitor_profiles: Vec<HwMonitorProfile>,
    pub resource_lists: Vec<HwResourceList>,
}

/// The `system packet-forwarding-options` settings.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PacketForwardingOptions {
    /// `srcip6-and-destip6` or `srcip6-only`.
    pub eracl_ip6_match: Option<String>,
    /// Seconds.
    pub flow_export_timer: Option<String>,
    /// e.g. `lean-edge`, `bng`, `carrier-ethernet` or `cloud-metro`.
    pub hw_db_profile: Option<String>,
    /// `mcast { stats-enable; }`.
    pub mcast_stats_enable: bool,
    /// `ingress-only` or `ingress-egress-recycle`.
    pub mcast_repl_type: Option<String>,
    pub mef_forwarding_profile: bool,
    pub mpls_lsp_ingress_stats_enable: bool,
    pub mpls_lsp_transit_stats_enable: bool,
    /// From `overlay { interface-num }`.
    pub overlay_interface_num: Option<String>,
    /// From `overlay { next-hop }`.
    pub overlay_next_hop: Option<String>,
    /// e.g. `mpls-packet-format-1`.
    pub packet_format_match: Option<String>,
    pub recycle_bandwidth_profiles: Vec<RecycleBandwidthProfile>,
    pub system_profile: Option<PfeSystemProfile>,
    pub firewall: Option<PfeFirewall>,
    pub hw_resource_monitor: Option<HwResourceMonitor>,
}

fn parse_firewall(firewall: &ConfigLine) -> PfeFirewall {
    let switching = child_block(firewall, "profiles").and_then(|profiles| child_block(profiles, "ethernet-switching"));
    PfeFirewall {
        eracl_scale: child_block(firewall, "eracl-profile").is_some_and(|profile| has_leaf(profile, "eracl-scale")),
        ethernet_switching_profiles: switching
            .map(|s| {
                active_children(s)
                    .filter(|c| c.text != "egress")
                    .map(|c| c.text.clone())
                    .collect()
            })
            .unwrap_or_default(),
        ethernet_switching_egress_profiles: switching
            .and_then(|s| child_block(s, "egress"))
            .map(|egress| active_children(egress).map(|c| c.text.clone()).collect())
            .unwrap_or_default(),
    }
}

fn parse_hw_resource_monitor(monitor: &ConfigLine) -> HwResourceMonitor {
    HwResourceMonitor {
        polling_interval: arg_of(monitor, "polling-interval").map(str::to_string),
        monitor_profiles: named_children(monitor, "monitor-profile")
            .map(|(name, profile)| HwMonitorProfile {
                name: name.to_string(),
                lower_threshold: arg_of(profile, "lower-threshold").map(str::to_string),
                notification_type: arg_of(profile, "notification-type").map(str::to_string),
                upper_threshold: arg_of(profile, "upper-threshold").map(str::to_string),
            })
            .collect(),
        resource_lists: named_children(monitor, "resource-list")
            .map(|(name, list)| HwResourceList {
                name: name.to_string(),
                resource_names: args_of(list, "resource-names")
                    .into_iter()
                    .flat_map(list_values)
                    .collect(),
            })
            .collect(),
    }
}

pub(super) fn parse_packet_forwarding_options(options: &ConfigLine) -> PacketForwardingOptions {
    let overlay = child_block(options, "overlay");
    let mpls = child_block(options, "mpls");
    let profile = child_block(options, "system-profile");
    PacketForwardingOptions {
        eracl_ip6_match: statements(options, "eracl-ip6-match").first().map(|s| s.to_string()),
        flow_export_timer: arg_of(options, "flow-export-timer").map(str::to_string),
        hw_db_profile: statements(options, "hw-db-profile").first().map(|s| s.to_string()),
        mcast_stats_enable: child_block(options, "mcast").is_some_and(|mcast| has_leaf(mcast, "stats-enable")),
        mcast_repl_type: statements(options, "mcast-repl-type").first().map(|s| s.to_string()),
        mef_forwarding_profile: has_leaf(options, "mef-forwarding-profile"),
        mpls_lsp_ingress_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-ingress-stats-enable")),
        mpls_lsp_transit_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-transit-stats-enable")),
        overlay_interface_num: overlay.and_then(|o| arg_of(o, "interface-num")).map(str::to_string),
        overlay_next_hop: overlay.and_then(|o| arg_of(o, "next-hop")).map(str::to_string),
        packet_format_match: statements(options, "packet-format-match")
            .first()
            .map(|s| s.to_string()),
        recycle_bandwidth_profiles: named_children(options, "recycle-bandwidth-profiles")
            .map(|(name, profile)| RecycleBandwidthProfile {
                name: name.to_string(),
                applications: active_children(profile)
                    .filter_map(|c| {
                        let (application, percentage) = c.text.split_once(char::is_whitespace)?;
                        Some((application.to_string(), percentage.trim().to_string()))
                    })
                    .collect(),
            })
            .collect(),
        system_profile: profile.map(|profile| PfeSystemProfile {
            vxlan_extended: has_leaf(profile, "vxlan-extended"),
            vxlan_stitching: child_block(profile, "vxlan-stitching").is_some(),
            vxlan_stitching_control_word: child_block(profile, "vxlan-stitching")
                .is_some_and(|stitching| has_leaf(stitching, "control-word")),
        }),
        firewall: child_block(options, "firewall").map(parse_firewall),
        hw_resource_monitor: child_block(options, "hw-resource-monitor").map(parse_hw_resource_monitor),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_the_packet_forwarding_options() {
        let options = system(&juniper::parse_braces(
            r#"
system {
    packet-forwarding-options {
        eracl-ip6-match {
            srcip6-only;
        }
        flow-export-timer 30;
        hw-db-profile {
            cloud-metro;
        }
        mcast {
            stats-enable;
        }
        mcast-repl-type {
            ingress-only;
        }
        mef-forwarding-profile;
        mpls {
            lsp-ingress-stats-enable;
        }
        overlay {
            interface-num 2048;
            next-hop 8192;
        }
        packet-format-match {
            mpls-packet-format-3;
        }
        recycle-bandwidth-profiles lab {
            tunnel-services 20;
            srv6 30;
        }
        system-profile {
            vxlan-extended;
            vxlan-stitching {
                control-word;
            }
        }
        firewall {
            eracl-profile {
                eracl-scale;
            }
            profiles {
                ethernet-switching {
                    profile1;
                    udf-profile1;
                    egress {
                        profile1;
                    }
                }
            }
        }
        hw-resource-monitor {
            polling-interval 5000;
            monitor-profile alarms {
                lower-threshold 60;
                notification-type syslog;
                upper-threshold 90;
            }
            resource-list lpm {
                resource-names [ lpm-ipv4 lpm-ipv6-64 ];
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .packet_forwarding_options
        .unwrap();
        assert_eq!(options.eracl_ip6_match.as_deref(), Some("srcip6-only"));
        assert_eq!(options.flow_export_timer.as_deref(), Some("30"));
        assert_eq!(options.hw_db_profile.as_deref(), Some("cloud-metro"));
        assert!(options.mcast_stats_enable);
        assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-only"));
        assert!(options.mef_forwarding_profile);
        assert!(options.mpls_lsp_ingress_stats_enable);
        assert!(!options.mpls_lsp_transit_stats_enable);
        assert_eq!(options.overlay_interface_num.as_deref(), Some("2048"));
        assert_eq!(options.overlay_next_hop.as_deref(), Some("8192"));
        assert_eq!(options.packet_format_match.as_deref(), Some("mpls-packet-format-3"));
        assert_eq!(options.recycle_bandwidth_profiles[0].name, "lab");
        assert_eq!(
            options.recycle_bandwidth_profiles[0].applications,
            [
                ("tunnel-services".to_string(), "20".to_string()),
                ("srv6".to_string(), "30".to_string())
            ]
        );
        let profile = options.system_profile.unwrap();
        assert!(profile.vxlan_extended);
        assert!(profile.vxlan_stitching);
        assert!(profile.vxlan_stitching_control_word);

        let firewall = options.firewall.unwrap();
        assert!(firewall.eracl_scale);
        assert_eq!(firewall.ethernet_switching_profiles, ["profile1", "udf-profile1"]);
        assert_eq!(firewall.ethernet_switching_egress_profiles, ["profile1"]);

        let monitor = options.hw_resource_monitor.unwrap();
        assert_eq!(monitor.polling_interval.as_deref(), Some("5000"));
        assert_eq!(monitor.monitor_profiles[0].name, "alarms");
        assert_eq!(monitor.monitor_profiles[0].lower_threshold.as_deref(), Some("60"));
        assert_eq!(monitor.monitor_profiles[0].notification_type.as_deref(), Some("syslog"));
        assert_eq!(monitor.monitor_profiles[0].upper_threshold.as_deref(), Some("90"));
        assert_eq!(monitor.resource_lists[0].name, "lpm");
        assert_eq!(monitor.resource_lists[0].resource_names, ["lpm-ipv4", "lpm-ipv6-64"]);
    }

    #[test]
    fn accepts_the_inline_forms() {
        let options = system(&juniper::parse_braces(
            "system {\n    packet-forwarding-options {\n        hw-db-profile bng;\n        mcast-repl-type ingress-egress-recycle;\n        mcast {\n            stats-enable;\n        }\n    }\n}\n",
        ))
        .unwrap()
        .packet_forwarding_options
        .unwrap();
        assert_eq!(options.hw_db_profile.as_deref(), Some("bng"));
        assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-egress-recycle"));
        assert!(options.system_profile.is_none());
        assert!(options.firewall.is_none());
    }

    #[test]
    fn no_packet_forwarding_options_is_none() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.packet_forwarding_options.is_none());
    }
}