Skip to main content

asterust_junos/interfaces/
optics_options.rs

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