Skip to main content

asterust_junos/interfaces/unit/
demux_options.rs

1//! `demux-options`, `demux-source`, `demux-destination`, `advisory-options` and `targeted-distribution`
2//! `interfaces { <name> { unit <n> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [demux-options (Static Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-options-edit-interfaces.html)
7//! - [demux-source (Underlying Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-source-edit-interfaces-unit.html)
8//! - [demux-destination (Underlying Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-destination-edit-interfaces-unit.html)
9//! - [demux-source (Demux Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-source-edit-interfaces-family.html)
10//! - [demux-destination (Demux Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-destination-edit-interfaces-family.html)
11//! - [advisory-options (Traffic Shaping)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advisory-options-edit-interfaces-unit.html)
12//! - [downstream-rate (Traffic Shaping)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/downstream-rate-edit-interfaces-unit.html)
13//! - [upstream-rate (Traffic Shaping)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/upstream-rate-edit-interfaces-unit.html)
14//! - [targeted-distribution](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/targeted-distribution-edit-interfaces.html)
15
16use crate::{active_children, arg_of};
17use asterust::config::ConfigLine;
18
19/// The `demux-options` of a unit.
20#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct DemuxOptions {
23    pub underlying_interface: Option<String>,
24}
25
26/// The `advisory-options` of a unit: the rates used for traffic shaping.
27#[derive(Clone, Debug, PartialEq, Eq)]
28#[non_exhaustive]
29pub struct AdvisoryOptions {
30    pub downstream_rate: Option<String>,
31    pub upstream_rate: Option<String>,
32}
33
34pub(super) fn parse_demux_options(options: &ConfigLine) -> DemuxOptions {
35    DemuxOptions {
36        underlying_interface: arg_of(options, "underlying-interface").map(str::to_string),
37    }
38}
39
40pub(super) fn parse_advisory_options(options: &ConfigLine) -> AdvisoryOptions {
41    AdvisoryOptions {
42        downstream_rate: arg_of(options, "downstream-rate").map(str::to_string),
43        upstream_rate: arg_of(options, "upstream-rate").map(str::to_string),
44    }
45}
46
47/// The prefixes of a family's `demux-source { ... }` or `demux-destination { ... }`; also accepts `<keyword> <prefix>;`.
48pub(super) fn demux_prefixes(family: &ConfigLine, keyword: &str) -> Vec<String> {
49    active_children(family)
50        .flat_map(|child| {
51            if child.text == keyword {
52                active_children(child).map(|prefix| prefix.text.clone()).collect()
53            } else {
54                child.arg(keyword).map(str::to_string).into_iter().collect::<Vec<_>>()
55            }
56        })
57        .collect()
58}
59
60#[cfg(test)]
61mod tests {
62    use crate::interfaces::interfaces;
63    use asterust::config::juniper;
64
65    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
66    #[test]
67    fn extracts_demux_options_and_advisory_options() {
68        let ifaces = interfaces(&juniper::parse_braces(
69            r#"
70interfaces {
71    ge-0/0/0 {
72        unit 0 {
73            demux-options {
74                underlying-interface ge-0/0/1;
75            }
76            demux-source inet;
77            demux-source inet6;
78            demux-destination inet;
79            advisory-options {
80                downstream-rate 10m;
81                upstream-rate 1m;
82            }
83            targeted-distribution scale;
84            family inet {
85                demux-source {
86                    192.0.2.0/24;
87                    198.51.100.0/24;
88                }
89                demux-destination {
90                    203.0.113.0/24;
91                }
92            }
93            family inet6 {
94                demux-source 2001:db8::/32;
95            }
96        }
97        unit 1 {
98            description plain;
99        }
100    }
101}
102"#,
103        ));
104        let units = &ifaces[0].units;
105        assert_eq!(
106            units[0].demux_options.as_ref().unwrap().underlying_interface.as_deref(),
107            Some("ge-0/0/1")
108        );
109        assert_eq!(units[0].demux_source, ["inet", "inet6"]);
110        assert_eq!(units[0].demux_destination, ["inet"]);
111        let advisory = units[0].advisory_options.as_ref().unwrap();
112        assert_eq!(advisory.downstream_rate.as_deref(), Some("10m"));
113        assert_eq!(advisory.upstream_rate.as_deref(), Some("1m"));
114        assert_eq!(units[0].targeted_distribution.as_deref(), Some("scale"));
115        let inet = &units[0].family_settings[0];
116        assert_eq!(inet.demux_source, ["192.0.2.0/24", "198.51.100.0/24"]);
117        assert_eq!(inet.demux_destination, ["203.0.113.0/24"]);
118        assert_eq!(units[0].family_settings[1].demux_source, ["2001:db8::/32"]);
119        assert!(units[0].family_settings[1].demux_destination.is_empty());
120
121        assert!(units[1].demux_options.is_none() && units[1].advisory_options.is_none());
122        assert!(units[1].demux_source.is_empty() && units[1].targeted_distribution.is_none());
123    }
124}