asterust-junos 0.4.0

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `interface`
//! `protocols { pim { interface (<name> | all) { ... } } }`.
//!
//! # See also
//!
//! - [interface (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-protocols-pim.html)
//! - [family (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/family-edit-protocols-pim.html)
//! - [mode (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mode-edit-protocols-pim.html)
//! - [version (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/version-edit-protocols-pim.html)
//! - [hello-interval](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/hello-interval-edit-protocols-pim.html)
//! - [priority (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/priority-edit-protocols-pim-interface.html)
//! - [override-interval](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/override-interval-edit-protocols-pim.html)
//! - [propagation-delay](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/propagation-delay-edit-protocols-pim.html)
//! - [reset-tracking-bit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/reset-tracking-bit-edit-protocols-pim.html)
//! - [neighbor-policy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/neighbor-policy-edit-protocols-pim.html)
//! - [accept-remote-source](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accept-remote-source-edit-protocols-pim.html)
//! - [accept-join-always-from](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accept-join-always-from-protocols-pim-interface.html)
//! - [df-election](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/df-election-edit-protocols-pim-interface.html)
//! - [bfd-liveness-detection (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bfd-liveness-detection-edit-protocols-pim.html)
//! - [Configuring PIM and the Bidirectional Forwarding Detection (BFD) Protocol](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-bfd.html)

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

/// The BFD session of a PIM interface or interface family. The key chain is a name; keys are never read.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimBfd {
    /// Milliseconds.
    pub minimum_interval: Option<String>,
    pub minimum_receive_interval: Option<String>,
    pub multiplier: Option<String>,
    /// `0`, `1` or `automatic`.
    pub version: Option<String>,
    pub no_adaptation: bool,
    pub pdu_size: Option<String>,
    /// From `detection-time { threshold ...; }`.
    pub detection_time_threshold: Option<String>,
    /// From `transmit-interval { minimum-interval ...; }`.
    pub transmit_interval_minimum: Option<String>,
    /// From `transmit-interval { threshold ...; }`.
    pub transmit_interval_threshold: Option<String>,
    pub authentication_algorithm: Option<String>,
    pub authentication_key_chain: Option<String>,
    pub authentication_loose_check: bool,
}

/// A `family (inet | inet6)` statement of PIM or of one of its interfaces.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimFamily {
    /// `inet` or `inet6`.
    pub name: String,
    pub disable: bool,
    pub bfd_liveness_detection: Option<PimBfd>,
}

/// The `bidirectional { df-election { ... } }` settings of an interface, all but the robustness count in milliseconds.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimDfElection {
    pub backoff_period: Option<String>,
    pub offer_period: Option<String>,
    pub robustness_count: Option<String>,
}

/// The `multiple-triggered-joins` settings of an interface.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimMultipleTriggeredJoins {
    pub count: Option<String>,
    /// Milliseconds.
    pub interval: Option<String>,
}

/// An `interface <name>` entry of PIM; the name can be `all`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimInterface {
    pub name: String,
    pub disable: bool,
    /// `bidirectional-sparse`, `bidirectional-sparse-dense`, `dense`, `sparse` or `sparse-dense`.
    pub mode: Option<String>,
    pub version: Option<String>,
    /// Seconds.
    pub hello_interval: Option<String>,
    /// The designated router priority.
    pub priority: Option<String>,
    /// Milliseconds.
    pub override_interval: Option<String>,
    /// Milliseconds.
    pub propagation_delay: Option<String>,
    pub reset_tracking_bit: bool,
    pub accept_remote_source: bool,
    /// `dual-dr { enhanced; }`.
    pub dual_dr_enhanced: bool,
    pub neighbor_policies: Vec<String>,
    pub accept_join_always_from: Vec<String>,
    pub families: Vec<PimFamily>,
    pub bfd_liveness_detection: Option<PimBfd>,
    pub df_election: Option<PimDfElection>,
    pub multiple_triggered_joins: Option<PimMultipleTriggeredJoins>,
}

fn parse_bfd(bfd: &ConfigLine) -> PimBfd {
    let field = |keyword: &str| arg_of(bfd, keyword).map(str::to_string);
    let nested = |block: &str, keyword: &str| {
        child_block(bfd, block)
            .and_then(|block| arg_of(block, keyword))
            .map(str::to_string)
    };
    PimBfd {
        minimum_interval: field("minimum-interval"),
        minimum_receive_interval: field("minimum-receive-interval"),
        multiplier: field("multiplier"),
        version: field("version"),
        no_adaptation: has_leaf(bfd, "no-adaptation"),
        pdu_size: field("pdu-size"),
        detection_time_threshold: nested("detection-time", "threshold"),
        transmit_interval_minimum: nested("transmit-interval", "minimum-interval"),
        transmit_interval_threshold: nested("transmit-interval", "threshold"),
        authentication_algorithm: nested("authentication", "algorithm"),
        authentication_key_chain: nested("authentication", "key-chain"),
        authentication_loose_check: child_block(bfd, "authentication")
            .is_some_and(|auth| has_leaf(auth, "loose-check")),
    }
}

/// The `family` statements of `parent`.
pub(super) fn parse_families(parent: &ConfigLine) -> Vec<PimFamily> {
    named_children(parent, "family")
        .map(|(name, family)| PimFamily {
            name: name.to_string(),
            disable: has_leaf(family, "disable"),
            bfd_liveness_detection: child_block(family, "bfd-liveness-detection").map(parse_bfd),
        })
        .collect()
}

pub(super) fn parse_interface(name: &str, interface: &ConfigLine) -> PimInterface {
    let field = |keyword: &str| arg_of(interface, keyword).map(str::to_string);
    let policies =
        |keyword: &str| -> Vec<String> { args_of(interface, keyword).into_iter().flat_map(list_values).collect() };
    PimInterface {
        name: name.to_string(),
        disable: has_leaf(interface, "disable"),
        mode: field("mode"),
        version: field("version"),
        hello_interval: field("hello-interval"),
        priority: field("priority"),
        override_interval: field("override-interval"),
        propagation_delay: field("propagation-delay"),
        reset_tracking_bit: has_leaf(interface, "reset-tracking-bit"),
        accept_remote_source: has_leaf(interface, "accept-remote-source"),
        dual_dr_enhanced: child_block(interface, "dual-dr").is_some_and(|dual| has_leaf(dual, "enhanced")),
        neighbor_policies: policies("neighbor-policy"),
        accept_join_always_from: policies("accept-join-always-from"),
        families: parse_families(interface),
        bfd_liveness_detection: child_block(interface, "bfd-liveness-detection").map(parse_bfd),
        df_election: child_block(interface, "bidirectional")
            .and_then(|bidirectional| child_block(bidirectional, "df-election"))
            .map(|df| PimDfElection {
                backoff_period: arg_of(df, "backoff-period").map(str::to_string),
                offer_period: arg_of(df, "offer-period").map(str::to_string),
                robustness_count: arg_of(df, "robustness-count").map(str::to_string),
            }),
        multiple_triggered_joins: child_block(interface, "multiple-triggered-joins").map(|joins| {
            PimMultipleTriggeredJoins {
                count: arg_of(joins, "count").map(str::to_string),
                interval: arg_of(joins, "interval").map(str::to_string),
            }
        }),
    }
}