asterust-junos 0.5.6

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
//! `rp`
//! `protocols { pim { rp { ... } } }`.
//!
//! # See also
//!
//! - [rp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rp-edit-protocols-pim.html)
//! - [local](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/local-edit-protocols-pim.html)
//! - [static (Protocols PIM RP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-edit-protocols-pim.html)
//! - [auto-rp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/auto-rp-edit-protocols-pim.html)
//! - [bootstrap](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bootstrap-edit-protocols-pim.html)
//! - [bootstrap-import](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bootstrap-import-edit-protocols-pim.html)
//! - [bootstrap-export](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bootstrap-export-edit-protocols-pim.html)
//! - [bootstrap-priority](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bootstrap-priority-edit-protocols-pim.html)
//! - [dr-register-policy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dr-register-policy-edit-protocols-pim.html)
//! - [rp-register-policy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rp-register-policy-edit-protocols-pim.html)
//! - [embedded-rp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/embedded-rp-edit-protocols-pim.html)
//! - [register-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/register-limit-edit-protocols-pim.html)
//! - [register-probe-time](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/register-probe-time-edit-protocols-pim.html)
//! - [anycast-pim](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/anycast-pim-edit-protocols-pim.html)
//! - [group-ranges](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-ranges-edit-protocols-pim.html)
//! - [hold-time (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/hold-time-edit-protocols-pim.html)
//! - [override](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/override-edit-protocols-pim.html)
//! - [Configuring Static RP](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-static-rp.html)
//! - [Configuring PIM Bootstrap Router](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-bootstrap-router.html)
//! - [Example: Configuring Anycast RP](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-anycast-rp.html)
//! - [Example: Configuring Bidirectional PIM](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-bidirectional.html)

use super::limits::{parse_limits, PimLimits};
use crate::{
    arg_of, args_of, child_block, has_leaf, list_values, named_children, split_name, statement_words, statements,
    toggle, value_after,
};
use asterust::config::ConfigLine;

/// The `auto-rp` statement.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimAutoRp {
    /// `announce`, `discovery` or `mapping`.
    pub mode: Option<String>,
    /// `Some(true)` for `mapping-agent-election`, `Some(false)` for `no-mapping-agent-election`.
    pub mapping_agent_election: Option<bool>,
}

/// An `address` of `rp bidirectional`, with the groups it is the bidirectional RP for.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimBidirectionalRp {
    pub address: String,
    pub group_ranges: Vec<String>,
    /// Seconds.
    pub hold_time: Option<String>,
    pub priority: Option<String>,
}

/// A `family (inet | inet6)` of `rp bootstrap`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimBootstrapFamily {
    pub family: String,
    pub priority: Option<String>,
    pub import_policies: Vec<String>,
    pub export_policies: Vec<String>,
}

/// The `embedded-rp` statement.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimEmbeddedRp {
    pub group_ranges: Vec<String>,
    pub maximum_rps: Option<String>,
}

/// An `address` of an `anycast-pim` `rp-set`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimAnycastRp {
    pub address: String,
    pub forward_msdp_sa: bool,
}

/// The `anycast-pim` statement of a local RP.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimAnycast {
    pub disable: bool,
    pub local_address: Option<String>,
    pub rp_set: Vec<PimAnycastRp>,
}

/// The options of a local RP, written directly under `local` or under one of its families.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimLocalRpOptions {
    pub address: Option<String>,
    pub anycast_pim: Option<PimAnycast>,
    pub group_ranges: Vec<String>,
    /// Seconds.
    pub hold_time: Option<String>,
    /// `override`: this RP wins over dynamically learned ones.
    pub override_rp: bool,
    pub priority: Option<String>,
    pub process_non_null_as_null_register: bool,
}

/// A `family (inet | inet6)` of `rp local`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimLocalRpFamily {
    pub family: String,
    pub disable: bool,
    pub options: PimLocalRpOptions,
}

/// The `local` statement.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimLocalRp {
    pub disable: bool,
    pub options: PimLocalRpOptions,
    pub families: Vec<PimLocalRpFamily>,
}

/// An `address` of `rp static`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimStaticRp {
    pub address: String,
    pub override_rp: bool,
    pub version: Option<String>,
    pub group_ranges: Vec<String>,
}

/// The `protocols pim rp` configuration. The `group-rp-mapping` statement is not extracted.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimRp {
    pub auto_rp: Option<PimAutoRp>,
    pub bidirectional: Vec<PimBidirectionalRp>,
    pub bootstrap: Vec<PimBootstrapFamily>,
    pub bootstrap_import_policies: Vec<String>,
    pub bootstrap_export_policies: Vec<String>,
    pub bootstrap_priority: Option<String>,
    pub dr_register_policies: Vec<String>,
    pub embedded_rp: Option<PimEmbeddedRp>,
    pub local: Option<PimLocalRp>,
    pub register_limit: Option<PimLimits>,
    pub register_probe_time: Option<String>,
    pub rp_register_policies: Vec<String>,
    pub static_rps: Vec<PimStaticRp>,
}

fn policies(line: &ConfigLine, keyword: &str) -> Vec<String> {
    args_of(line, keyword).into_iter().flat_map(list_values).collect()
}

fn group_ranges(line: &ConfigLine) -> Vec<String> {
    statements(line, "group-ranges")
        .into_iter()
        .map(str::to_string)
        .collect()
}

fn parse_auto_rp(rp: &ConfigLine) -> Option<PimAutoRp> {
    let words = statement_words(rp, "auto-rp")?;
    Some(PimAutoRp {
        mode: ["announce", "discovery", "mapping"]
            .into_iter()
            .find(|mode| words.contains(mode))
            .map(str::to_string),
        mapping_agent_election: child_block(rp, "auto-rp").and_then(|block| toggle(block, "mapping-agent-election")),
    })
}

fn parse_anycast(anycast: &ConfigLine) -> PimAnycast {
    PimAnycast {
        disable: has_leaf(anycast, "disable"),
        local_address: arg_of(anycast, "local-address").map(str::to_string),
        rp_set: child_block(anycast, "rp-set")
            .map(|set| {
                named_children(set, "address")
                    .map(|(arg, _)| {
                        let (address, rest) = split_name(arg);
                        PimAnycastRp {
                            address: address.to_string(),
                            forward_msdp_sa: rest.split_whitespace().any(|word| word == "forward-msdp-sa"),
                        }
                    })
                    .collect()
            })
            .unwrap_or_default(),
    }
}

fn parse_local_options(line: &ConfigLine) -> PimLocalRpOptions {
    PimLocalRpOptions {
        address: arg_of(line, "address").map(str::to_string),
        anycast_pim: child_block(line, "anycast-pim").map(parse_anycast),
        group_ranges: group_ranges(line),
        hold_time: arg_of(line, "hold-time").map(str::to_string),
        override_rp: has_leaf(line, "override"),
        priority: arg_of(line, "priority").map(str::to_string),
        process_non_null_as_null_register: has_leaf(line, "process-non-null-as-null-register"),
    }
}

fn parse_static(arg: &str, entry: &ConfigLine) -> PimStaticRp {
    let (address, rest) = split_name(arg);
    let words: Vec<&str> = rest.split_whitespace().collect();
    PimStaticRp {
        address: address.to_string(),
        override_rp: has_leaf(entry, "override") || words.contains(&"override"),
        version: arg_of(entry, "version")
            .map(str::to_string)
            .or_else(|| value_after(&words, "version")),
        group_ranges: group_ranges(entry),
    }
}

pub(super) fn parse_rp(rp: &ConfigLine) -> PimRp {
    PimRp {
        auto_rp: parse_auto_rp(rp),
        bidirectional: child_block(rp, "bidirectional")
            .map(|bidirectional| {
                named_children(bidirectional, "address")
                    .map(|(arg, entry)| PimBidirectionalRp {
                        address: split_name(arg).0.to_string(),
                        group_ranges: group_ranges(entry),
                        hold_time: arg_of(entry, "hold-time").map(str::to_string),
                        priority: arg_of(entry, "priority").map(str::to_string),
                    })
                    .collect()
            })
            .unwrap_or_default(),
        bootstrap: child_block(rp, "bootstrap")
            .map(|bootstrap| {
                named_children(bootstrap, "family")
                    .map(|(family, entry)| PimBootstrapFamily {
                        family: family.to_string(),
                        priority: arg_of(entry, "priority").map(str::to_string),
                        import_policies: policies(entry, "import"),
                        export_policies: policies(entry, "export"),
                    })
                    .collect()
            })
            .unwrap_or_default(),
        bootstrap_import_policies: policies(rp, "bootstrap-import"),
        bootstrap_export_policies: policies(rp, "bootstrap-export"),
        bootstrap_priority: arg_of(rp, "bootstrap-priority").map(str::to_string),
        dr_register_policies: policies(rp, "dr-register-policy"),
        embedded_rp: child_block(rp, "embedded-rp").map(|embedded| PimEmbeddedRp {
            group_ranges: group_ranges(embedded),
            maximum_rps: arg_of(embedded, "maximum-rps").map(str::to_string),
        }),
        local: child_block(rp, "local").map(|local| PimLocalRp {
            disable: has_leaf(local, "disable"),
            options: parse_local_options(local),
            families: named_children(local, "family")
                .map(|(family, entry)| PimLocalRpFamily {
                    family: family.to_string(),
                    disable: has_leaf(entry, "disable"),
                    options: parse_local_options(entry),
                })
                .collect(),
        }),
        register_limit: child_block(rp, "register-limit").map(parse_limits),
        register_probe_time: arg_of(rp, "register-probe-time").map(str::to_string),
        rp_register_policies: policies(rp, "rp-register-policy"),
        static_rps: child_block(rp, "static")
            .map(|block| {
                named_children(block, "address")
                    .map(|(arg, entry)| parse_static(arg, entry))
                    .collect()
            })
            .unwrap_or_default(),
    }
}