Skip to main content

asterust_junos/interfaces/
optics_options.rs

1//! `interfaces { <name> { optics-options { ... } } }`.
2
3use crate::{arg_of, has_leaf, named_children, toggle};
4use asterust::config::ConfigLine;
5
6/// The actions of a `low-light-alarm` or `low-light-warning` statement.
7#[derive(Clone, Debug, Default, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct OpticsLowLight {
10    /// Take the link down.
11    pub link_down: bool,
12    pub syslog: bool,
13}
14
15/// The optics settings of an interface. `signal-degrade`, `tca`, `loopback`, `lane`, `host-side-fec`, `media-side-fec`
16/// and `application` are not extracted.
17#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct OpticsOptions {
20    pub low_light_alarm: Option<OpticsLowLight>,
21    pub low_light_warning: Option<OpticsLowLight>,
22    /// The legacy `alarms;` flag.
23    pub alarms: bool,
24    /// The ITU grid wavelength in nanometres, e.g. `"1550.12"`.
25    pub wavelength: Option<String>,
26    /// dBm.
27    pub tx_power: Option<String>,
28    /// e.g. `"sdfec"` or `"hgfec"`.
29    pub fec: Option<String>,
30    pub encoding: Option<String>,
31    /// e.g. `"16qam"` or `"qpsk"`.
32    pub modulation_format: Option<String>,
33    pub los_alarm_threshold: Option<String>,
34    pub los_warning_threshold: Option<String>,
35    pub high_polarization: bool,
36    pub high_power_mode: bool,
37    pub low_power_mode: bool,
38    /// `Some(true)` for `laser-enable`, `Some(false)` for `no-laser-enable`.
39    pub laser_enable: Option<bool>,
40    /// `Some(true)` for `is-ma`, `Some(false)` for `no-is-ma`.
41    pub is_ma: Option<bool>,
42}
43
44pub(super) fn parse_optics_options(optics: &ConfigLine) -> OpticsOptions {
45    let field = |keyword: &str| arg_of(optics, keyword).map(str::to_string);
46    OpticsOptions {
47        low_light_alarm: low_light(optics, "alarm", "low-light-alarm"),
48        low_light_warning: low_light(optics, "warning", "low-light-warning"),
49        alarms: has_leaf(optics, "alarms"),
50        wavelength: field("wavelength"),
51        tx_power: field("tx-power"),
52        fec: field("fec"),
53        encoding: field("encoding"),
54        modulation_format: field("modulation-format"),
55        los_alarm_threshold: field("los-alarm-threshold"),
56        los_warning_threshold: field("los-warning-threshold"),
57        high_polarization: has_leaf(optics, "high-polarization"),
58        high_power_mode: has_leaf(optics, "high-power-mode"),
59        low_power_mode: has_leaf(optics, "low-power-mode"),
60        laser_enable: toggle(optics, "laser-enable"),
61        is_ma: toggle(optics, "is-ma"),
62    }
63}
64
65/// `<keyword> <name> { link-down; syslog; }`.
66fn low_light(optics: &ConfigLine, keyword: &str, name: &str) -> Option<OpticsLowLight> {
67    let (_, block) = named_children(optics, keyword).find(|(found, _)| *found == name)?;
68    Some(OpticsLowLight {
69        link_down: has_leaf(block, "link-down"),
70        syslog: has_leaf(block, "syslog"),
71    })
72}
73
74#[cfg(test)]
75mod tests {
76    use crate::interfaces::interfaces;
77    use asterust::config::juniper;
78
79    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
80    #[test]
81    fn extracts_optics_options() {
82        let ifaces = interfaces(&juniper::parse_braces(
83            r#"
84interfaces {
85    et-0/0/0 {
86        optics-options {
87            alarm low-light-alarm {
88                link-down;
89            }
90            warning low-light-warning {
91                syslog;
92            }
93            wavelength 1550.12;
94            tx-power -3;
95            fec sdfec;
96            modulation-format 16qam;
97            encoding differential;
98            los-alarm-threshold -20;
99            los-warning-threshold -18;
100            high-polarization;
101            no-laser-enable;
102            is-ma;
103        }
104    }
105    xe-0/0/1 {
106        optics-options {
107            alarms;
108            low-power-mode;
109        }
110    }
111}
112"#,
113        ));
114        let optics = ifaces[0].optics_options.as_ref().unwrap();
115        let alarm = optics.low_light_alarm.as_ref().unwrap();
116        assert!(alarm.link_down);
117        assert!(!alarm.syslog);
118        let warning = optics.low_light_warning.as_ref().unwrap();
119        assert!(!warning.link_down);
120        assert!(warning.syslog);
121        assert!(!optics.alarms);
122        assert_eq!(optics.wavelength.as_deref(), Some("1550.12"));
123        assert_eq!(optics.tx_power.as_deref(), Some("-3"));
124        assert_eq!(optics.fec.as_deref(), Some("sdfec"));
125        assert_eq!(optics.modulation_format.as_deref(), Some("16qam"));
126        assert_eq!(optics.encoding.as_deref(), Some("differential"));
127        assert_eq!(optics.los_alarm_threshold.as_deref(), Some("-20"));
128        assert_eq!(optics.los_warning_threshold.as_deref(), Some("-18"));
129        assert!(optics.high_polarization);
130        assert!(!optics.high_power_mode);
131        assert_eq!(optics.laser_enable, Some(false));
132        assert_eq!(optics.is_ma, Some(true));
133
134        let legacy = ifaces[1].optics_options.as_ref().unwrap();
135        assert!(legacy.alarms);
136        assert!(legacy.low_power_mode);
137        assert!(legacy.low_light_alarm.is_none());
138        assert_eq!(legacy.laser_enable, None);
139    }
140
141    #[test]
142    fn optics_options_are_optional() {
143        let ifaces = interfaces(&juniper::parse_braces(
144            "interfaces {\n    et-0/0/0 {\n        disable;\n    }\n}\n",
145        ));
146        assert!(ifaces[0].optics_options.is_none());
147    }
148}