Skip to main content

asterust_junos/interfaces/ether_options/
ethernet_switch_profile.rs

1//! `ethernet-switch-profile`
2//! `interfaces { <name> { ether-options | gigether-options | aggregated-ether-options { ethernet-switch-profile { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [ethernet-switch-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ethernet-switch-profile-edit-interfaces.html)
7
8use crate::{active_children, arg_of, child_block, list_values, named_children, toggle};
9use asterust::config::ConfigLine;
10
11/// A `policer <name>` of an Ethernet policer profile.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct EthernetSwitchPolicer {
15    pub name: String,
16    /// Bits per second, from `aggregate { bandwidth-limit }`.
17    pub aggregate_bandwidth_limit: Option<String>,
18    /// Bytes, from `aggregate { burst-size-limit }`.
19    pub aggregate_burst_size_limit: Option<String>,
20    pub premium_bandwidth_limit: Option<String>,
21    pub premium_burst_size_limit: Option<String>,
22}
23
24/// A `forwarding-class <name>` of the premium output priority map.
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct EthernetSwitchForwardingClass {
28    pub name: String,
29    /// `high` or `low`.
30    pub loss_priority: Option<String>,
31}
32
33/// The `ethernet-switch-profile` of an Ethernet interface.
34#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct EthernetSwitchProfile {
37    /// `Some(true)` for `mac-learn-enable`, `Some(false)` for `no-mac-learn-enable`.
38    pub mac_learn_enable: Option<bool>,
39    pub tag_protocol_id: Option<String>,
40    pub storm_control: Option<String>,
41    /// The 802.1p values of `input-priority-map { ieee802.1p premium [ ... ] }`.
42    pub input_premium_ieee_802_1p: Vec<String>,
43    /// The classes of `output-priority-map { classifier { premium { ... } } }`.
44    pub output_premium_classes: Vec<EthernetSwitchForwardingClass>,
45    pub policers: Vec<EthernetSwitchPolicer>,
46}
47
48pub(in crate::interfaces) fn parse_ethernet_switch_profile(profile: &ConfigLine) -> EthernetSwitchProfile {
49    let policer_profile = child_block(profile, "ethernet-policer-profile");
50    let limits = |block: Option<&ConfigLine>, keyword: &str| block.and_then(|b| arg_of(b, keyword)).map(str::to_string);
51    EthernetSwitchProfile {
52        mac_learn_enable: toggle(profile, "mac-learn-enable"),
53        tag_protocol_id: policer_profile
54            .and_then(|p| arg_of(p, "tag-protocol-id"))
55            .or_else(|| arg_of(profile, "tag-protocol-id"))
56            .map(str::to_string),
57        storm_control: policer_profile
58            .and_then(|p| arg_of(p, "storm-control"))
59            .or_else(|| arg_of(profile, "storm-control"))
60            .map(str::to_string),
61        input_premium_ieee_802_1p: policer_profile
62            .and_then(|p| child_block(p, "input-priority-map"))
63            .map(|map| {
64                active_children(map)
65                    .filter_map(|c| c.arg("ieee802.1p"))
66                    .flat_map(|arg| list_values(arg.trim_start_matches("premium")))
67                    .collect()
68            })
69            .unwrap_or_default(),
70        output_premium_classes: policer_profile
71            .and_then(|p| child_block(p, "output-priority-map"))
72            .and_then(|map| child_block(map, "classifier"))
73            .and_then(|classifier| child_block(classifier, "premium"))
74            .map(|premium| {
75                named_children(premium, "forwarding-class")
76                    .map(|(name, class)| EthernetSwitchForwardingClass {
77                        name: name.to_string(),
78                        loss_priority: arg_of(class, "loss-priority").map(str::to_string),
79                    })
80                    .collect()
81            })
82            .unwrap_or_default(),
83        policers: policer_profile
84            .map(|p| {
85                named_children(p, "policer")
86                    .map(|(name, policer)| {
87                        let aggregate = child_block(policer, "aggregate");
88                        let premium = child_block(policer, "premium");
89                        EthernetSwitchPolicer {
90                            name: name.to_string(),
91                            aggregate_bandwidth_limit: limits(aggregate, "bandwidth-limit"),
92                            aggregate_burst_size_limit: limits(aggregate, "burst-size-limit"),
93                            premium_bandwidth_limit: limits(premium, "bandwidth-limit"),
94                            premium_burst_size_limit: limits(premium, "burst-size-limit"),
95                        }
96                    })
97                    .collect()
98            })
99            .unwrap_or_default(),
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use crate::interfaces::interfaces;
106    use asterust::config::juniper;
107
108    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
109    #[test]
110    fn extracts_the_ethernet_switch_profile() {
111        let ifaces = interfaces(&juniper::parse_braces(
112            r#"
113interfaces {
114    ge-0/0/0 {
115        gigether-options {
116            ethernet-switch-profile {
117                mac-learn-enable;
118                ethernet-policer-profile {
119                    input-priority-map {
120                        ieee802.1p premium [ 5 6 ];
121                    }
122                    output-priority-map {
123                        classifier {
124                            premium {
125                                forwarding-class expedited-forwarding {
126                                    loss-priority low;
127                                }
128                            }
129                        }
130                    }
131                    policer cos-policer {
132                        aggregate {
133                            bandwidth-limit 100m;
134                            burst-size-limit 15k;
135                        }
136                        premium {
137                            bandwidth-limit 50m;
138                            burst-size-limit 10k;
139                        }
140                    }
141                    storm-control sc1;
142                    tag-protocol-id 0x8100;
143                }
144            }
145        }
146    }
147}
148"#,
149        ));
150        let profile = ifaces[0]
151            .ether_options
152            .as_ref()
153            .unwrap()
154            .ethernet_switch_profile
155            .as_ref()
156            .unwrap();
157        assert_eq!(profile.mac_learn_enable, Some(true));
158        assert_eq!(profile.tag_protocol_id.as_deref(), Some("0x8100"));
159        assert_eq!(profile.storm_control.as_deref(), Some("sc1"));
160        assert_eq!(profile.input_premium_ieee_802_1p, ["5", "6"]);
161        assert_eq!(profile.output_premium_classes[0].name, "expedited-forwarding");
162        assert_eq!(profile.output_premium_classes[0].loss_priority.as_deref(), Some("low"));
163        let policer = &profile.policers[0];
164        assert_eq!(policer.name, "cos-policer");
165        assert_eq!(policer.aggregate_bandwidth_limit.as_deref(), Some("100m"));
166        assert_eq!(policer.aggregate_burst_size_limit.as_deref(), Some("15k"));
167        assert_eq!(policer.premium_bandwidth_limit.as_deref(), Some("50m"));
168        assert_eq!(policer.premium_burst_size_limit.as_deref(), Some("10k"));
169    }
170}