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

mod odu;
mod rate;
mod signal_degrade;

pub use odu::{OduDelayManagement, PreemptiveFastReroute};
pub use rate::OtnRate;
pub use signal_degrade::SignalDegrade;

use super::tca::{parse_tca, Tca};
use crate::{active_children, arg_of, child_block, has_leaf, split_keyword, statement_words, toggle, value_after};
use asterust::config::ConfigLine;
use odu::{parse_odu_delay_management, parse_preemptive_fast_reroute};
use rate::parse_rate;
pub(crate) use signal_degrade::parse_signal_degrade;

/// A `trigger <identifier> (hold-time <value> | ignore)`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OtnTrigger {
    /// The alarm, e.g. `"odu-ais"`.
    pub identifier: String,
    pub hold_time: Option<String>,
    pub ignore: bool,
}

/// The OTN settings of an interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OtnOptions {
    /// e.g. `"gfec"`, `"efec"`, `"ufec"` or `"none"`.
    pub fec: Option<String>,
    /// From `bytes transmit-payload-type`.
    pub transmit_payload_type: Option<String>,
    /// Each `tti` statement as written, e.g. `"otu-sapi \"text\""`.
    pub tti: Vec<String>,
    pub rate: Option<OtnRate>,
    pub signal_degrade: Option<SignalDegrade>,
    pub transport_monitoring: bool,
    /// `Some(true)` for `is-ma`, `Some(false)` for `no-is-ma`.
    pub is_ma: Option<bool>,
    /// `Some(true)` for `laser-enable`, `Some(false)` for `no-laser-enable`.
    pub laser_enable: Option<bool>,
    /// `Some(true)` for `prbs`, `Some(false)` for `no-prbs`.
    pub prbs: Option<bool>,
    pub odu_ttim_action_enable: Option<bool>,
    pub otu_ttim_action_enable: Option<bool>,
    pub odu_delay_management: Option<OduDelayManagement>,
    pub odu_signal_degrade: Option<SignalDegrade>,
    pub preemptive_fast_reroute: Option<PreemptiveFastReroute>,
    pub tca: Vec<Tca>,
    pub triggers: Vec<OtnTrigger>,
    /// `Some(true)` for `line-loopback` or `line-loopback-enable`, `Some(false)` for `no-line-loopback`.
    pub line_loopback: Option<bool>,
    /// `Some(true)` for `local-loopback` or `local-loopback-enable`, `Some(false)` for `no-local-loopback`.
    pub local_loopback: Option<bool>,
}

pub(super) fn parse_otn_options(otn: &ConfigLine) -> OtnOptions {
    OtnOptions {
        fec: arg_of(otn, "fec").map(str::to_string),
        transmit_payload_type: statement_words(otn, "bytes")
            .and_then(|words| value_after(&words, "transmit-payload-type")),
        tti: active_children(otn)
            .filter_map(|c| c.arg("tti"))
            .map(str::to_string)
            .collect(),
        rate: child_block(otn, "rate").map(parse_rate),
        signal_degrade: child_block(otn, "signal-degrade").map(parse_signal_degrade),
        transport_monitoring: has_leaf(otn, "transport-monitoring"),
        is_ma: toggle(otn, "is-ma"),
        laser_enable: toggle(otn, "laser-enable"),
        prbs: toggle(otn, "prbs"),
        odu_ttim_action_enable: toggle(otn, "odu-ttim-action-enable"),
        otu_ttim_action_enable: toggle(otn, "otu-ttim-action-enable"),
        odu_delay_management: child_block(otn, "odu-delay-management").map(parse_odu_delay_management),
        odu_signal_degrade: child_block(otn, "odu-signal-degrade").map(parse_signal_degrade),
        preemptive_fast_reroute: child_block(otn, "preemptive-fast-reroute").map(parse_preemptive_fast_reroute),
        tca: active_children(otn)
            .filter_map(|c| c.arg("tca"))
            .map(parse_tca)
            .collect(),
        triggers: active_children(otn)
            .filter_map(|c| c.arg("trigger"))
            .map(parse_trigger)
            .collect(),
        line_loopback: loopback(otn, "line"),
        local_loopback: loopback(otn, "local"),
    }
}

/// `<kind>-loopback`, `<kind>-loopback-enable` or `no-<kind>-loopback`; the CLI Reference uses both spellings.
fn loopback(otn: &ConfigLine, kind: &str) -> Option<bool> {
    toggle(otn, &format!("{kind}-loopback"))
        .or_else(|| has_leaf(otn, &format!("{kind}-loopback-enable")).then_some(true))
}

fn parse_trigger(arg: &str) -> OtnTrigger {
    let (identifier, rest) = split_keyword(arg);
    OtnTrigger {
        identifier: identifier.to_string(),
        hold_time: rest.strip_prefix("hold-time").map(|v| v.trim().to_string()),
        ignore: rest == "ignore",
    }
}

#[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_otn_options() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    et-1/0/0 {
        otn-options {
            fec gfec;
            bytes {
                transmit-payload-type 7;
            }
            tti otu-sapi "LAB1";
            tti odu-dapi "LAB2";
            transport-monitoring;
            laser-enable;
            no-prbs;
            odu-ttim-action-enable;
            rate {
                otu4;
                no-pass-through;
            }
            signal-degrade {
                ber-threshold-signal-degrade 1.0E-5;
                ber-threshold-clear 1.0E-6;
                interval 100;
            }
        }
    }
}
"#,
        ));
        let otn = ifaces[0].otn_options.as_ref().unwrap();
        assert_eq!(otn.fec.as_deref(), Some("gfec"));
        assert_eq!(otn.transmit_payload_type.as_deref(), Some("7"));
        assert_eq!(otn.tti, ["otu-sapi \"LAB1\"", "odu-dapi \"LAB2\""]);
        assert!(otn.transport_monitoring);
        assert_eq!(otn.laser_enable, Some(true));
        assert_eq!(otn.prbs, Some(false));
        assert_eq!(otn.odu_ttim_action_enable, Some(true));
        assert_eq!(otn.otu_ttim_action_enable, None);
        assert_eq!(otn.is_ma, None);

        let rate = otn.rate.as_ref().unwrap();
        assert!(rate.otu4);
        assert!(!rate.oc192);
        assert_eq!(rate.pass_through, Some(false));
        assert_eq!(rate.fixed_stuff_bytes, None);

        let degrade = otn.signal_degrade.as_ref().unwrap();
        assert_eq!(degrade.ber_threshold_signal_degrade.as_deref(), Some("1.0E-5"));
        assert_eq!(degrade.ber_threshold_clear.as_deref(), Some("1.0E-6"));
        assert_eq!(degrade.interval.as_deref(), Some("100"));
    }

    #[test]
    fn bare_otn_options_have_no_settings() {
        let ifaces = interfaces(&juniper::parse_braces(
            "interfaces {\n    et-1/0/0 {\n        otn-options {\n            fec none;\n        }\n    }\n    et-1/0/1 {\n        disable;\n    }\n}\n",
        ));
        let otn = ifaces[0].otn_options.as_ref().unwrap();
        assert_eq!(otn.fec.as_deref(), Some("none"));
        assert!(otn.tti.is_empty());
        assert!(otn.rate.is_none());
        assert!(otn.signal_degrade.is_none());
        assert!(!otn.transport_monitoring);
        assert!(ifaces[1].otn_options.is_none());
    }

    // Not from documentation: written from the otn-options CLI Reference pages. See the README "Sources".
    #[test]
    fn extracts_the_remaining_statements() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    et-1/0/0 {
        otn-options {
            odu-delay-management {
                bypass;
                no-monitor-end-point;
                number-of-frames 100;
                start-measurement;
                remoteloopenable;
            }
            odu-signal-degrade {
                ber-threshold-signal-degrade 1.0E-4;
                ber-threshold-clear 1.0E-5;
                interval 50;
            }
            preemptive-fast-reroute {
                backward-frr-enable;
                no-signal-degrade-monitor-enable;
                odu-backward-frr-enable;
            }
            tca odu-bbe enable-tca threshold 100;
            tca otu-es no-enable-tca threshold-24hrs 500;
            trigger odu-ais hold-time 20;
            trigger otu-lof ignore;
            line-loopback;
            no-local-loopback;
        }
    }
    et-1/0/1 {
        otn-options {
            line-loopback-enable;
            local-loopback-enable;
        }
    }
}
"#,
        ));
        let otn = ifaces[0].otn_options.as_ref().unwrap();
        let delay = otn.odu_delay_management.as_ref().unwrap();
        assert_eq!(delay.bypass, Some(true));
        assert_eq!(delay.monitor_end_point, Some(false));
        assert_eq!(delay.number_of_frames.as_deref(), Some("100"));
        assert_eq!(delay.start_measurement, Some(true));
        assert_eq!(delay.remote_loop_enable, Some(true));

        let degrade = otn.odu_signal_degrade.as_ref().unwrap();
        assert_eq!(degrade.ber_threshold_signal_degrade.as_deref(), Some("1.0E-4"));
        assert_eq!(degrade.interval.as_deref(), Some("50"));

        let reroute = otn.preemptive_fast_reroute.as_ref().unwrap();
        assert_eq!(reroute.backward_frr_enable, Some(true));
        assert_eq!(reroute.signal_degrade_monitor_enable, Some(false));
        assert_eq!(reroute.odu_backward_frr_enable, Some(true));
        assert_eq!(reroute.odu_signal_degrade_monitor_enable, None);

        assert_eq!(otn.tca.len(), 2);
        assert_eq!(otn.tca[0].identifier, "odu-bbe");
        assert_eq!(otn.tca[0].enabled, Some(true));
        assert_eq!(otn.tca[0].threshold.as_deref(), Some("100"));
        assert_eq!(otn.tca[0].threshold_24hrs, None);
        assert_eq!(otn.tca[1].enabled, Some(false));
        assert_eq!(otn.tca[1].threshold_24hrs.as_deref(), Some("500"));

        assert_eq!(otn.triggers.len(), 2);
        assert_eq!(otn.triggers[0].identifier, "odu-ais");
        assert_eq!(otn.triggers[0].hold_time.as_deref(), Some("20"));
        assert!(!otn.triggers[0].ignore);
        assert!(otn.triggers[1].ignore);
        assert_eq!(otn.triggers[1].hold_time, None);

        assert_eq!(otn.line_loopback, Some(true));
        assert_eq!(otn.local_loopback, Some(false));

        let enabled = ifaces[1].otn_options.as_ref().unwrap();
        assert_eq!(enabled.line_loopback, Some(true));
        assert_eq!(enabled.local_loopback, Some(true));
        assert!(enabled.odu_delay_management.is_none());
        assert!(enabled.tca.is_empty());
    }
}