asterust_junos/interfaces/ether_options/
mod.rs1mod ieee_802_3ad;
21
22pub use ieee_802_3ad::Ieee8023ad;
23
24use crate::{arg_of, child_block, statement_words, toggle, value_after};
25use asterust::config::ConfigLine;
26use ieee_802_3ad::parse_ieee_802_3ad;
27
28#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct EtherOptions {
32 pub auto_negotiation: Option<bool>,
34 pub remote_fault: Option<String>,
36 pub flow_control: Option<bool>,
38 pub loopback: Option<bool>,
40 pub source_filtering: Option<bool>,
42 pub speed: Option<String>,
43 pub link_mode: Option<String>,
45 pub fec: Option<String>,
47 pub ieee_802_3ad: Option<Ieee8023ad>,
49}
50
51pub(super) fn parse_ether_options(interface: &ConfigLine) -> Option<EtherOptions> {
52 let options = ["ether-options", "gigether-options", "fastether-options"]
53 .iter()
54 .find_map(|name| child_block(interface, name))?;
55 let negotiation =
56 statement_words(options, "auto-negotiation").or_else(|| statement_words(options, "no-auto-negotiation"));
57 Some(EtherOptions {
58 auto_negotiation: toggle(options, "auto-negotiation"),
59 remote_fault: negotiation.and_then(|words| value_after(&words, "remote-fault")),
60 flow_control: toggle(options, "flow-control"),
61 loopback: toggle(options, "loopback"),
62 source_filtering: toggle(options, "source-filtering"),
63 speed: arg_of(options, "speed").map(str::to_string),
64 link_mode: arg_of(options, "link-mode").map(str::to_string),
65 fec: arg_of(options, "fec").map(str::to_string),
66 ieee_802_3ad: parse_ieee_802_3ad(options),
67 })
68}
69
70#[cfg(test)]
71mod tests {
72 use crate::interfaces::interfaces;
73 use asterust::config::juniper;
74
75 const CONFIG: &str = r#"
77interfaces {
78 ge-0/0/0 {
79 gigether-options {
80 802.3ad ae0;
81 no-auto-negotiation;
82 no-flow-control;
83 loopback;
84 source-filtering;
85 speed 1g;
86 }
87 }
88 ge-0/0/1 {
89 gigether-options {
90 802.3ad ae1 {
91 primary;
92 link-index 2;
93 distribution-list DL1;
94 lacp {
95 port-priority 64;
96 }
97 }
98 auto-negotiation remote-fault local-interface-offline;
99 }
100 }
101 xe-0/0/0 {
102 ether-options {
103 802.3ad {
104 ae2;
105 backup;
106 }
107 link-mode full-duplex;
108 fec fec91;
109 no-loopback;
110 no-source-filtering;
111 flow-control;
112 }
113 }
114}
115"#;
116
117 #[test]
118 fn extracts_ether_options_and_bundle_membership() {
119 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
120 let options = ifaces[0].ether_options.as_ref().unwrap();
121 assert_eq!(options.auto_negotiation, Some(false));
122 assert_eq!(options.remote_fault, None);
123 assert_eq!(options.flow_control, Some(false));
124 assert_eq!(options.loopback, Some(true));
125 assert_eq!(options.source_filtering, Some(true));
126 assert_eq!(options.speed.as_deref(), Some("1g"));
127 assert_eq!(options.link_mode, None);
128
129 let bundle = options.ieee_802_3ad.as_ref().unwrap();
130 assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae0"));
131 assert_eq!(bundle.role, None);
132 }
133
134 #[test]
135 fn extracts_802_3ad_block_and_remote_fault() {
136 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
137 let options = ifaces[1].ether_options.as_ref().unwrap();
138 assert_eq!(options.auto_negotiation, Some(true));
139 assert_eq!(options.remote_fault.as_deref(), Some("local-interface-offline"));
140
141 let bundle = options.ieee_802_3ad.as_ref().unwrap();
142 assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae1"));
143 assert_eq!(bundle.role.as_deref(), Some("primary"));
144 assert_eq!(bundle.link_index.as_deref(), Some("2"));
145 assert_eq!(bundle.distribution_list.as_deref(), Some("DL1"));
146 assert_eq!(bundle.lacp_port_priority.as_deref(), Some("64"));
147 }
148
149 #[test]
150 fn extracts_ether_options_with_the_bundle_named_inside_the_block() {
151 let ifaces = interfaces(&juniper::parse_braces(CONFIG));
152 let options = ifaces[2].ether_options.as_ref().unwrap();
153 assert_eq!(options.link_mode.as_deref(), Some("full-duplex"));
154 assert_eq!(options.fec.as_deref(), Some("fec91"));
155 assert_eq!(options.loopback, Some(false));
156 assert_eq!(options.source_filtering, Some(false));
157 assert_eq!(options.flow_control, Some(true));
158 assert_eq!(options.auto_negotiation, None);
159
160 let bundle = options.ieee_802_3ad.as_ref().unwrap();
161 assert_eq!(bundle.aggregated_interface.as_deref(), Some("ae2"));
162 assert_eq!(bundle.role.as_deref(), Some("backup"));
163 }
164
165 #[test]
166 fn ether_options_are_optional() {
167 let ifaces = interfaces(&juniper::parse_braces(
168 "interfaces {\n ge-0/0/0 {\n disable;\n }\n}\n",
169 ));
170 assert!(ifaces[0].ether_options.is_none());
171 }
172}