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 super::otn_options::{parse_signal_degrade, SignalDegrade};
14use super::tca::{parse_tca, Tca};
15use crate::{active_children, arg_of, child_block, has_leaf, named_children, statement_words, toggle, value_after};
16use asterust::config::ConfigLine;
17
18/// The actions of a `low-light-alarm` or `low-light-warning` statement.
19#[derive(Clone, Debug, Default, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct OpticsLowLight {
22    /// Take the link down.
23    pub link_down: bool,
24    pub syslog: bool,
25}
26
27/// The `application` of an interface: the host, media and domain identifiers of the optic's application code.
28#[derive(Clone, Debug, Default, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct OpticsApplication {
31    pub hostid: Option<String>,
32    pub mediaid: Option<String>,
33    pub domainid: Option<String>,
34}
35
36/// The optics settings of an interface.
37#[derive(Clone, Debug, PartialEq, Eq)]
38#[non_exhaustive]
39pub struct OpticsOptions {
40    pub low_light_alarm: Option<OpticsLowLight>,
41    pub low_light_warning: Option<OpticsLowLight>,
42    /// The legacy `alarms;` flag.
43    pub alarms: bool,
44    /// The ITU grid wavelength in nanometres, e.g. `"1550.12"`.
45    pub wavelength: Option<String>,
46    /// dBm.
47    pub tx_power: Option<String>,
48    /// e.g. `"sdfec"` or `"hgfec"`.
49    pub fec: Option<String>,
50    pub encoding: Option<String>,
51    /// e.g. `"16qam"` or `"qpsk"`.
52    pub modulation_format: Option<String>,
53    pub los_alarm_threshold: Option<String>,
54    pub los_warning_threshold: Option<String>,
55    pub high_polarization: bool,
56    pub high_power_mode: bool,
57    pub low_power_mode: bool,
58    /// `Some(true)` for `laser-enable`, `Some(false)` for `no-laser-enable`.
59    pub laser_enable: Option<bool>,
60    /// `Some(true)` for `is-ma`, `Some(false)` for `no-is-ma`.
61    pub is_ma: Option<bool>,
62    pub application: Option<OpticsApplication>,
63    /// The `id` of `appselid`.
64    pub appselid: Option<String>,
65    /// `"off"`, `"on"` or `"not-supported"`.
66    pub host_side_fec: Option<String>,
67    /// `"off"`, `"on"` or `"unsupported"`.
68    pub media_side_fec: Option<String>,
69    /// The argument of `lane`.
70    pub lane: Option<String>,
71    pub lane_all: bool,
72    /// Each `loopback` as written, e.g. `"media-side-in"` or `"local"`.
73    pub loopbacks: Vec<String>,
74    pub signal_degrade: Option<SignalDegrade>,
75    pub tca: Vec<Tca>,
76}
77
78pub(super) fn parse_optics_options(optics: &ConfigLine) -> OpticsOptions {
79    let field = |keyword: &str| arg_of(optics, keyword).map(str::to_string);
80    OpticsOptions {
81        low_light_alarm: low_light(optics, "alarm", "low-light-alarm"),
82        low_light_warning: low_light(optics, "warning", "low-light-warning"),
83        alarms: has_leaf(optics, "alarms"),
84        wavelength: field("wavelength"),
85        tx_power: field("tx-power"),
86        fec: field("fec"),
87        encoding: field("encoding"),
88        modulation_format: field("modulation-format"),
89        los_alarm_threshold: field("los-alarm-threshold"),
90        los_warning_threshold: field("los-warning-threshold"),
91        high_polarization: has_leaf(optics, "high-polarization"),
92        high_power_mode: has_leaf(optics, "high-power-mode"),
93        low_power_mode: has_leaf(optics, "low-power-mode"),
94        laser_enable: toggle(optics, "laser-enable"),
95        is_ma: toggle(optics, "is-ma"),
96        application: statement_words(optics, "application").map(|words| OpticsApplication {
97            hostid: value_after(&words, "hostid"),
98            mediaid: value_after(&words, "mediaid"),
99            domainid: value_after(&words, "domainid"),
100        }),
101        appselid: child_block(optics, "appselid")
102            .and_then(|block| arg_of(block, "id"))
103            .map(str::to_string),
104        host_side_fec: field("host-side-fec"),
105        media_side_fec: field("media-side-fec"),
106        lane: field("lane"),
107        lane_all: has_leaf(optics, "lane-all"),
108        loopbacks: active_children(optics)
109            .filter_map(|c| c.arg("loopback"))
110            .map(str::to_string)
111            .collect(),
112        signal_degrade: child_block(optics, "signal-degrade").map(parse_signal_degrade),
113        tca: active_children(optics)
114            .filter_map(|c| c.arg("tca"))
115            .map(parse_tca)
116            .collect(),
117    }
118}
119
120/// `<keyword> <name> { link-down; syslog; }`.
121fn low_light(optics: &ConfigLine, keyword: &str, name: &str) -> Option<OpticsLowLight> {
122    let (_, block) = named_children(optics, keyword).find(|(found, _)| *found == name)?;
123    Some(OpticsLowLight {
124        link_down: has_leaf(block, "link-down"),
125        syslog: has_leaf(block, "syslog"),
126    })
127}
128
129#[cfg(test)]
130mod tests {
131    use crate::interfaces::interfaces;
132    use asterust::config::juniper;
133
134    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
135    #[test]
136    fn extracts_optics_options() {
137        let ifaces = interfaces(&juniper::parse_braces(
138            r#"
139interfaces {
140    et-0/0/0 {
141        optics-options {
142            alarm low-light-alarm {
143                link-down;
144            }
145            warning low-light-warning {
146                syslog;
147            }
148            wavelength 1550.12;
149            tx-power -3;
150            fec sdfec;
151            modulation-format 16qam;
152            encoding differential;
153            los-alarm-threshold -20;
154            los-warning-threshold -18;
155            high-polarization;
156            no-laser-enable;
157            is-ma;
158        }
159    }
160    xe-0/0/1 {
161        optics-options {
162            alarms;
163            low-power-mode;
164        }
165    }
166}
167"#,
168        ));
169        let optics = ifaces[0].optics_options.as_ref().unwrap();
170        let alarm = optics.low_light_alarm.as_ref().unwrap();
171        assert!(alarm.link_down);
172        assert!(!alarm.syslog);
173        let warning = optics.low_light_warning.as_ref().unwrap();
174        assert!(!warning.link_down);
175        assert!(warning.syslog);
176        assert!(!optics.alarms);
177        assert_eq!(optics.wavelength.as_deref(), Some("1550.12"));
178        assert_eq!(optics.tx_power.as_deref(), Some("-3"));
179        assert_eq!(optics.fec.as_deref(), Some("sdfec"));
180        assert_eq!(optics.modulation_format.as_deref(), Some("16qam"));
181        assert_eq!(optics.encoding.as_deref(), Some("differential"));
182        assert_eq!(optics.los_alarm_threshold.as_deref(), Some("-20"));
183        assert_eq!(optics.los_warning_threshold.as_deref(), Some("-18"));
184        assert!(optics.high_polarization);
185        assert!(!optics.high_power_mode);
186        assert_eq!(optics.laser_enable, Some(false));
187        assert_eq!(optics.is_ma, Some(true));
188
189        let legacy = ifaces[1].optics_options.as_ref().unwrap();
190        assert!(legacy.alarms);
191        assert!(legacy.low_power_mode);
192        assert!(legacy.low_light_alarm.is_none());
193        assert_eq!(legacy.laser_enable, None);
194    }
195
196    #[test]
197    fn optics_options_are_optional() {
198        let ifaces = interfaces(&juniper::parse_braces(
199            "interfaces {\n    et-0/0/0 {\n        disable;\n    }\n}\n",
200        ));
201        assert!(ifaces[0].optics_options.is_none());
202    }
203
204    // Not from documentation: written from the optics-options CLI Reference page. See the README "Sources".
205    #[test]
206    fn extracts_the_remaining_statements() {
207        let ifaces = interfaces(&juniper::parse_braces(
208            r#"
209interfaces {
210    et-0/0/0 {
211        optics-options {
212            application {
213                hostid 1 mediaid 2 domainid 3;
214            }
215            appselid {
216                id 7;
217            }
218            host-side-fec on;
219            media-side-fec unsupported;
220            lane 2;
221            loopback media-side-in;
222            loopback host-side-out;
223            signal-degrade {
224                ber-threshold-signal-degrade 1.0E-4;
225                ber-threshold-clear 1.0E-5;
226                interval 20;
227                q-threshold-signal-degrade 8;
228                q-threshold-signal-degrade-clear 9;
229            }
230            tca fec-ber enable-tca threshold 5;
231        }
232    }
233    et-0/0/1 {
234        optics-options {
235            lane-all;
236            loopback local;
237            application {
238                hostid 4 mediaid 5;
239            }
240        }
241    }
242}
243"#,
244        ));
245        let optics = ifaces[0].optics_options.as_ref().unwrap();
246        let application = optics.application.as_ref().unwrap();
247        assert_eq!(application.hostid.as_deref(), Some("1"));
248        assert_eq!(application.mediaid.as_deref(), Some("2"));
249        assert_eq!(application.domainid.as_deref(), Some("3"));
250        assert_eq!(optics.appselid.as_deref(), Some("7"));
251        assert_eq!(optics.host_side_fec.as_deref(), Some("on"));
252        assert_eq!(optics.media_side_fec.as_deref(), Some("unsupported"));
253        assert_eq!(optics.lane.as_deref(), Some("2"));
254        assert!(!optics.lane_all);
255        assert_eq!(optics.loopbacks, ["media-side-in", "host-side-out"]);
256        let degrade = optics.signal_degrade.as_ref().unwrap();
257        assert_eq!(degrade.ber_threshold_clear.as_deref(), Some("1.0E-5"));
258        assert_eq!(degrade.interval.as_deref(), Some("20"));
259        assert_eq!(degrade.q_threshold_signal_degrade.as_deref(), Some("8"));
260        assert_eq!(degrade.q_threshold_signal_degrade_clear.as_deref(), Some("9"));
261        assert_eq!(optics.tca.len(), 1);
262        assert_eq!(optics.tca[0].identifier, "fec-ber");
263        assert_eq!(optics.tca[0].threshold.as_deref(), Some("5"));
264
265        let second = ifaces[1].optics_options.as_ref().unwrap();
266        assert!(second.lane_all);
267        assert_eq!(second.lane, None);
268        assert_eq!(second.loopbacks, ["local"]);
269        let application = second.application.as_ref().unwrap();
270        assert_eq!(application.domainid, None);
271        assert!(second.signal_degrade.is_none());
272    }
273}