Skip to main content

asterust_junos/interfaces/otn_options/
mod.rs

1//! `interfaces { <name> { otn-options { ... } } }`.
2
3mod rate;
4mod signal_degrade;
5
6pub use rate::OtnRate;
7pub use signal_degrade::SignalDegrade;
8
9use crate::{active_children, arg_of, child_block, has_leaf, statement_words, toggle, value_after};
10use asterust::config::ConfigLine;
11use rate::parse_rate;
12use signal_degrade::parse_signal_degrade;
13
14/// The OTN settings of an interface. `odu-delay-management`, `odu-signal-degrade`, `preemptive-fast-reroute`, `tca`,
15/// `trigger` and the loopback statements are not extracted.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct OtnOptions {
19    /// e.g. `"gfec"`, `"efec"`, `"ufec"` or `"none"`.
20    pub fec: Option<String>,
21    /// From `bytes transmit-payload-type`.
22    pub transmit_payload_type: Option<String>,
23    /// Each `tti` statement as written, e.g. `"otu-sapi \"text\""`.
24    pub tti: Vec<String>,
25    pub rate: Option<OtnRate>,
26    pub signal_degrade: Option<SignalDegrade>,
27    pub transport_monitoring: bool,
28    /// `Some(true)` for `is-ma`, `Some(false)` for `no-is-ma`.
29    pub is_ma: Option<bool>,
30    /// `Some(true)` for `laser-enable`, `Some(false)` for `no-laser-enable`.
31    pub laser_enable: Option<bool>,
32    /// `Some(true)` for `prbs`, `Some(false)` for `no-prbs`.
33    pub prbs: Option<bool>,
34    pub odu_ttim_action_enable: Option<bool>,
35    pub otu_ttim_action_enable: Option<bool>,
36}
37
38pub(super) fn parse_otn_options(otn: &ConfigLine) -> OtnOptions {
39    OtnOptions {
40        fec: arg_of(otn, "fec").map(str::to_string),
41        transmit_payload_type: statement_words(otn, "bytes")
42            .and_then(|words| value_after(&words, "transmit-payload-type")),
43        tti: active_children(otn)
44            .filter_map(|c| c.arg("tti"))
45            .map(str::to_string)
46            .collect(),
47        rate: child_block(otn, "rate").map(parse_rate),
48        signal_degrade: child_block(otn, "signal-degrade").map(parse_signal_degrade),
49        transport_monitoring: has_leaf(otn, "transport-monitoring"),
50        is_ma: toggle(otn, "is-ma"),
51        laser_enable: toggle(otn, "laser-enable"),
52        prbs: toggle(otn, "prbs"),
53        odu_ttim_action_enable: toggle(otn, "odu-ttim-action-enable"),
54        otu_ttim_action_enable: toggle(otn, "otu-ttim-action-enable"),
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use crate::interfaces::interfaces;
61    use asterust::config::juniper;
62
63    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
64    #[test]
65    fn extracts_otn_options() {
66        let ifaces = interfaces(&juniper::parse_braces(
67            r#"
68interfaces {
69    et-1/0/0 {
70        otn-options {
71            fec gfec;
72            bytes {
73                transmit-payload-type 7;
74            }
75            tti otu-sapi "LAB1";
76            tti odu-dapi "LAB2";
77            transport-monitoring;
78            laser-enable;
79            no-prbs;
80            odu-ttim-action-enable;
81            rate {
82                otu4;
83                no-pass-through;
84            }
85            signal-degrade {
86                ber-threshold-signal-degrade 1.0E-5;
87                ber-threshold-clear 1.0E-6;
88                interval 100;
89            }
90        }
91    }
92}
93"#,
94        ));
95        let otn = ifaces[0].otn_options.as_ref().unwrap();
96        assert_eq!(otn.fec.as_deref(), Some("gfec"));
97        assert_eq!(otn.transmit_payload_type.as_deref(), Some("7"));
98        assert_eq!(otn.tti, ["otu-sapi \"LAB1\"", "odu-dapi \"LAB2\""]);
99        assert!(otn.transport_monitoring);
100        assert_eq!(otn.laser_enable, Some(true));
101        assert_eq!(otn.prbs, Some(false));
102        assert_eq!(otn.odu_ttim_action_enable, Some(true));
103        assert_eq!(otn.otu_ttim_action_enable, None);
104        assert_eq!(otn.is_ma, None);
105
106        let rate = otn.rate.as_ref().unwrap();
107        assert!(rate.otu4);
108        assert!(!rate.oc192);
109        assert_eq!(rate.pass_through, Some(false));
110        assert_eq!(rate.fixed_stuff_bytes, None);
111
112        let degrade = otn.signal_degrade.as_ref().unwrap();
113        assert_eq!(degrade.ber_threshold_signal_degrade.as_deref(), Some("1.0E-5"));
114        assert_eq!(degrade.ber_threshold_clear.as_deref(), Some("1.0E-6"));
115        assert_eq!(degrade.interval.as_deref(), Some("100"));
116    }
117
118    #[test]
119    fn bare_otn_options_have_no_settings() {
120        let ifaces = interfaces(&juniper::parse_braces(
121            "interfaces {\n    et-1/0/0 {\n        otn-options {\n            fec none;\n        }\n    }\n    et-1/0/1 {\n        disable;\n    }\n}\n",
122        ));
123        let otn = ifaces[0].otn_options.as_ref().unwrap();
124        assert_eq!(otn.fec.as_deref(), Some("none"));
125        assert!(otn.tti.is_empty());
126        assert!(otn.rate.is_none());
127        assert!(otn.signal_degrade.is_none());
128        assert!(!otn.transport_monitoring);
129        assert!(ifaces[1].otn_options.is_none());
130    }
131}