Skip to main content

asterust_junos/interfaces/aggregated_ether_options/
mod.rs

1//! `interfaces { ae<n> { aggregated-ether-options { ... } } }`.
2
3mod lacp;
4
5pub use lacp::Lacp;
6
7use crate::{arg_of, child_block, toggle};
8use asterust::config::ConfigLine;
9use lacp::parse_lacp;
10
11/// The `aggregated-ether-options` of an aggregated Ethernet interface.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct AggregatedEtherOptions {
15    pub lacp: Option<Lacp>,
16    /// e.g. `"10g"` or `"mixed"`.
17    pub link_speed: Option<String>,
18    pub minimum_links: Option<String>,
19    /// `Some(true)` for `flow-control`, `Some(false)` for `no-flow-control`.
20    pub flow_control: Option<bool>,
21    /// `Some(true)` for `loopback`, `Some(false)` for `no-loopback`.
22    pub loopback: Option<bool>,
23    /// `Some(true)` for `source-filtering`, `Some(false)` for `no-source-filtering`.
24    pub source_filtering: Option<bool>,
25}
26
27pub(super) fn parse_aggregated_ether_options(options: &ConfigLine) -> AggregatedEtherOptions {
28    AggregatedEtherOptions {
29        lacp: child_block(options, "lacp").map(parse_lacp),
30        link_speed: arg_of(options, "link-speed").map(str::to_string),
31        minimum_links: arg_of(options, "minimum-links").map(str::to_string),
32        flow_control: toggle(options, "flow-control"),
33        loopback: toggle(options, "loopback"),
34        source_filtering: toggle(options, "source-filtering"),
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use crate::interfaces::interfaces;
41    use asterust::config::juniper;
42
43    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
44    #[test]
45    fn extracts_aggregated_ether_options_and_lacp() {
46        let ifaces = interfaces(&juniper::parse_braces(
47            r#"
48interfaces {
49    ae0 {
50        aggregated-ether-options {
51            link-speed 10g;
52            minimum-links 2;
53            no-flow-control;
54            lacp {
55                active;
56                periodic fast;
57                system-priority 100;
58                system-id 00:11:22:33:44:55;
59                admin-key 7;
60                force-up;
61                fast-failover;
62                accept-data;
63            }
64        }
65    }
66}
67"#,
68        ));
69        let options = ifaces[0].aggregated_ether_options.as_ref().unwrap();
70        assert_eq!(options.link_speed.as_deref(), Some("10g"));
71        assert_eq!(options.minimum_links.as_deref(), Some("2"));
72        assert_eq!(options.flow_control, Some(false));
73        assert_eq!(options.loopback, None);
74        assert_eq!(options.source_filtering, None);
75
76        let lacp = options.lacp.as_ref().unwrap();
77        assert_eq!(lacp.mode.as_deref(), Some("active"));
78        assert_eq!(lacp.periodic.as_deref(), Some("fast"));
79        assert_eq!(lacp.system_priority.as_deref(), Some("100"));
80        assert_eq!(lacp.system_id.as_deref(), Some("00:11:22:33:44:55"));
81        assert_eq!(lacp.admin_key.as_deref(), Some("7"));
82        assert!(lacp.force_up);
83        assert!(lacp.fast_failover);
84        assert!(lacp.accept_data);
85    }
86
87    #[test]
88    fn bare_lacp_has_no_settings() {
89        let ifaces = interfaces(&juniper::parse_braces(
90            "interfaces {\n    ae1 {\n        aggregated-ether-options {\n            lacp {\n                passive;\n            }\n        }\n    }\n}\n",
91        ));
92        let lacp = ifaces[0]
93            .aggregated_ether_options
94            .as_ref()
95            .unwrap()
96            .lacp
97            .as_ref()
98            .unwrap();
99        assert_eq!(lacp.mode.as_deref(), Some("passive"));
100        assert_eq!(lacp.periodic, None);
101        assert!(!lacp.force_up);
102    }
103
104    #[test]
105    fn aggregated_ether_options_are_optional() {
106        let ifaces = interfaces(&juniper::parse_braces(
107            "interfaces {\n    ae0 {\n        disable;\n    }\n}\n",
108        ));
109        assert!(ifaces[0].aggregated_ether_options.is_none());
110    }
111}