asterust-junos 0.5.14

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
//! `redundancy-options`
//! `interfaces { <name> { redundancy-options { ... } } }`.
//!
//! # See also
//!
//! - [redundancy-options (Stateful Synchronization)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/nat-redundancy-options-msmic-msmpc-edit-interfaces.html)
//! - [redundancy-options (Adaptive Services Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/redundancy-options-edit-interfaces-rsp.html)
//! - [redundancy-options (Link Services IQ PIC Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/redundancy-options-edit-interfaces-rlsq.html)
//! - [redundancy-options (Aggregated Multiservices)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/redundancy-option-edit-interfaces-ams.html)

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

/// The `redundancy-options` of a services interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RedundancyOptions {
    pub primary: Option<String>,
    pub secondary: Option<String>,
    pub hot_standby: bool,
    pub warm_standby: bool,
    /// From `redundancy-local { data-address }`.
    pub redundancy_local_data_address: Option<String>,
    /// From `redundancy-peer { ipaddress }`.
    pub redundancy_peer_ipaddress: Option<String>,
    pub replication_threshold: Option<String>,
    pub routing_instance: Option<String>,
}

pub(super) fn parse_redundancy_options(options: &ConfigLine) -> RedundancyOptions {
    let value = |keyword| arg_of(options, keyword).map(str::to_string);
    RedundancyOptions {
        primary: value("primary"),
        secondary: value("secondary"),
        hot_standby: has_leaf(options, "hot-standby"),
        warm_standby: has_leaf(options, "warm-standby"),
        redundancy_local_data_address: child_block(options, "redundancy-local")
            .and_then(|local| arg_of(local, "data-address"))
            .map(str::to_string),
        redundancy_peer_ipaddress: child_block(options, "redundancy-peer")
            .and_then(|peer| arg_of(peer, "ipaddress"))
            .map(str::to_string),
        replication_threshold: value("replication-threshold"),
        routing_instance: value("routing-instance"),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_redundancy_options() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    rsp0 {
        redundancy-options {
            primary sp-1/0/0;
            secondary sp-2/0/0;
            hot-standby;
        }
    }
    rlsq0 {
        redundancy-options {
            primary lsq-1/0/0;
            secondary lsq-2/0/0;
            warm-standby;
        }
    }
    ms-1/0/0 {
        redundancy-options {
            redundancy-local {
                data-address 10.0.0.1;
            }
            redundancy-peer {
                ipaddress 10.0.0.2;
            }
            replication-threshold 30;
            routing-instance sync;
        }
    }
}
"#,
        ));
        let rsp = ifaces[0].redundancy_options.as_ref().unwrap();
        assert_eq!(rsp.primary.as_deref(), Some("sp-1/0/0"));
        assert_eq!(rsp.secondary.as_deref(), Some("sp-2/0/0"));
        assert!(rsp.hot_standby && !rsp.warm_standby);
        let rlsq = ifaces[1].redundancy_options.as_ref().unwrap();
        assert!(rlsq.warm_standby && !rlsq.hot_standby);
        let nat = ifaces[2].redundancy_options.as_ref().unwrap();
        assert_eq!(nat.redundancy_local_data_address.as_deref(), Some("10.0.0.1"));
        assert_eq!(nat.redundancy_peer_ipaddress.as_deref(), Some("10.0.0.2"));
        assert_eq!(nat.replication_threshold.as_deref(), Some("30"));
        assert_eq!(nat.routing_instance.as_deref(), Some("sync"));
        assert!(nat.primary.is_none());
    }
}