1use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, statements};
28use asterust::config::ConfigLine;
29
30#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct RecycleBandwidthProfile {
34 pub name: String,
35 pub applications: Vec<(String, String)>,
37}
38
39#[derive(Clone, Debug, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct PfeSystemProfile {
43 pub vxlan_extended: bool,
44 pub vxlan_stitching: bool,
45 pub vxlan_stitching_control_word: bool,
47}
48
49#[derive(Clone, Debug, PartialEq, Eq)]
51#[non_exhaustive]
52pub struct PfeFirewall {
53 pub eracl_scale: bool,
55 pub ethernet_switching_profiles: Vec<String>,
57 pub ethernet_switching_egress_profiles: Vec<String>,
59}
60
61#[derive(Clone, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct HwMonitorProfile {
65 pub name: String,
66 pub lower_threshold: Option<String>,
67 pub notification_type: Option<String>,
69 pub upper_threshold: Option<String>,
70}
71
72#[derive(Clone, Debug, PartialEq, Eq)]
74#[non_exhaustive]
75pub struct HwResourceList {
76 pub name: String,
77 pub resource_names: Vec<String>,
79}
80
81#[derive(Clone, Debug, PartialEq, Eq)]
83#[non_exhaustive]
84pub struct HwResourceMonitor {
85 pub polling_interval: Option<String>,
87 pub monitor_profiles: Vec<HwMonitorProfile>,
88 pub resource_lists: Vec<HwResourceList>,
89}
90
91#[derive(Clone, Debug, PartialEq, Eq)]
93#[non_exhaustive]
94pub struct PacketForwardingOptions {
95 pub eracl_ip6_match: Option<String>,
97 pub flow_export_timer: Option<String>,
99 pub hw_db_profile: Option<String>,
101 pub mcast_stats_enable: bool,
103 pub mcast_repl_type: Option<String>,
105 pub mef_forwarding_profile: bool,
106 pub mpls_lsp_ingress_stats_enable: bool,
107 pub mpls_lsp_transit_stats_enable: bool,
108 pub overlay_interface_num: Option<String>,
110 pub overlay_next_hop: Option<String>,
112 pub packet_format_match: Option<String>,
114 pub recycle_bandwidth_profiles: Vec<RecycleBandwidthProfile>,
115 pub system_profile: Option<PfeSystemProfile>,
116 pub firewall: Option<PfeFirewall>,
117 pub hw_resource_monitor: Option<HwResourceMonitor>,
118}
119
120fn parse_firewall(firewall: &ConfigLine) -> PfeFirewall {
121 let switching = child_block(firewall, "profiles").and_then(|profiles| child_block(profiles, "ethernet-switching"));
122 PfeFirewall {
123 eracl_scale: child_block(firewall, "eracl-profile").is_some_and(|profile| has_leaf(profile, "eracl-scale")),
124 ethernet_switching_profiles: switching
125 .map(|s| {
126 active_children(s)
127 .filter(|c| c.text != "egress")
128 .map(|c| c.text.clone())
129 .collect()
130 })
131 .unwrap_or_default(),
132 ethernet_switching_egress_profiles: switching
133 .and_then(|s| child_block(s, "egress"))
134 .map(|egress| active_children(egress).map(|c| c.text.clone()).collect())
135 .unwrap_or_default(),
136 }
137}
138
139fn parse_hw_resource_monitor(monitor: &ConfigLine) -> HwResourceMonitor {
140 HwResourceMonitor {
141 polling_interval: arg_of(monitor, "polling-interval").map(str::to_string),
142 monitor_profiles: named_children(monitor, "monitor-profile")
143 .map(|(name, profile)| HwMonitorProfile {
144 name: name.to_string(),
145 lower_threshold: arg_of(profile, "lower-threshold").map(str::to_string),
146 notification_type: arg_of(profile, "notification-type").map(str::to_string),
147 upper_threshold: arg_of(profile, "upper-threshold").map(str::to_string),
148 })
149 .collect(),
150 resource_lists: named_children(monitor, "resource-list")
151 .map(|(name, list)| HwResourceList {
152 name: name.to_string(),
153 resource_names: args_of(list, "resource-names")
154 .into_iter()
155 .flat_map(list_values)
156 .collect(),
157 })
158 .collect(),
159 }
160}
161
162pub(super) fn parse_packet_forwarding_options(options: &ConfigLine) -> PacketForwardingOptions {
163 let overlay = child_block(options, "overlay");
164 let mpls = child_block(options, "mpls");
165 let profile = child_block(options, "system-profile");
166 PacketForwardingOptions {
167 eracl_ip6_match: statements(options, "eracl-ip6-match").first().map(|s| s.to_string()),
168 flow_export_timer: arg_of(options, "flow-export-timer").map(str::to_string),
169 hw_db_profile: statements(options, "hw-db-profile").first().map(|s| s.to_string()),
170 mcast_stats_enable: child_block(options, "mcast").is_some_and(|mcast| has_leaf(mcast, "stats-enable")),
171 mcast_repl_type: statements(options, "mcast-repl-type").first().map(|s| s.to_string()),
172 mef_forwarding_profile: has_leaf(options, "mef-forwarding-profile"),
173 mpls_lsp_ingress_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-ingress-stats-enable")),
174 mpls_lsp_transit_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-transit-stats-enable")),
175 overlay_interface_num: overlay.and_then(|o| arg_of(o, "interface-num")).map(str::to_string),
176 overlay_next_hop: overlay.and_then(|o| arg_of(o, "next-hop")).map(str::to_string),
177 packet_format_match: statements(options, "packet-format-match")
178 .first()
179 .map(|s| s.to_string()),
180 recycle_bandwidth_profiles: named_children(options, "recycle-bandwidth-profiles")
181 .map(|(name, profile)| RecycleBandwidthProfile {
182 name: name.to_string(),
183 applications: active_children(profile)
184 .filter_map(|c| {
185 let (application, percentage) = c.text.split_once(char::is_whitespace)?;
186 Some((application.to_string(), percentage.trim().to_string()))
187 })
188 .collect(),
189 })
190 .collect(),
191 system_profile: profile.map(|profile| PfeSystemProfile {
192 vxlan_extended: has_leaf(profile, "vxlan-extended"),
193 vxlan_stitching: child_block(profile, "vxlan-stitching").is_some(),
194 vxlan_stitching_control_word: child_block(profile, "vxlan-stitching")
195 .is_some_and(|stitching| has_leaf(stitching, "control-word")),
196 }),
197 firewall: child_block(options, "firewall").map(parse_firewall),
198 hw_resource_monitor: child_block(options, "hw-resource-monitor").map(parse_hw_resource_monitor),
199 }
200}
201
202#[cfg(test)]
203mod tests {
204 use crate::system::system;
205 use asterust::config::juniper;
206
207 #[test]
209 fn extracts_the_packet_forwarding_options() {
210 let options = system(&juniper::parse_braces(
211 r#"
212system {
213 packet-forwarding-options {
214 eracl-ip6-match {
215 srcip6-only;
216 }
217 flow-export-timer 30;
218 hw-db-profile {
219 cloud-metro;
220 }
221 mcast {
222 stats-enable;
223 }
224 mcast-repl-type {
225 ingress-only;
226 }
227 mef-forwarding-profile;
228 mpls {
229 lsp-ingress-stats-enable;
230 }
231 overlay {
232 interface-num 2048;
233 next-hop 8192;
234 }
235 packet-format-match {
236 mpls-packet-format-3;
237 }
238 recycle-bandwidth-profiles lab {
239 tunnel-services 20;
240 srv6 30;
241 }
242 system-profile {
243 vxlan-extended;
244 vxlan-stitching {
245 control-word;
246 }
247 }
248 firewall {
249 eracl-profile {
250 eracl-scale;
251 }
252 profiles {
253 ethernet-switching {
254 profile1;
255 udf-profile1;
256 egress {
257 profile1;
258 }
259 }
260 }
261 }
262 hw-resource-monitor {
263 polling-interval 5000;
264 monitor-profile alarms {
265 lower-threshold 60;
266 notification-type syslog;
267 upper-threshold 90;
268 }
269 resource-list lpm {
270 resource-names [ lpm-ipv4 lpm-ipv6-64 ];
271 }
272 }
273 }
274}
275"#,
276 ))
277 .unwrap()
278 .packet_forwarding_options
279 .unwrap();
280 assert_eq!(options.eracl_ip6_match.as_deref(), Some("srcip6-only"));
281 assert_eq!(options.flow_export_timer.as_deref(), Some("30"));
282 assert_eq!(options.hw_db_profile.as_deref(), Some("cloud-metro"));
283 assert!(options.mcast_stats_enable);
284 assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-only"));
285 assert!(options.mef_forwarding_profile);
286 assert!(options.mpls_lsp_ingress_stats_enable);
287 assert!(!options.mpls_lsp_transit_stats_enable);
288 assert_eq!(options.overlay_interface_num.as_deref(), Some("2048"));
289 assert_eq!(options.overlay_next_hop.as_deref(), Some("8192"));
290 assert_eq!(options.packet_format_match.as_deref(), Some("mpls-packet-format-3"));
291 assert_eq!(options.recycle_bandwidth_profiles[0].name, "lab");
292 assert_eq!(
293 options.recycle_bandwidth_profiles[0].applications,
294 [
295 ("tunnel-services".to_string(), "20".to_string()),
296 ("srv6".to_string(), "30".to_string())
297 ]
298 );
299 let profile = options.system_profile.unwrap();
300 assert!(profile.vxlan_extended);
301 assert!(profile.vxlan_stitching);
302 assert!(profile.vxlan_stitching_control_word);
303
304 let firewall = options.firewall.unwrap();
305 assert!(firewall.eracl_scale);
306 assert_eq!(firewall.ethernet_switching_profiles, ["profile1", "udf-profile1"]);
307 assert_eq!(firewall.ethernet_switching_egress_profiles, ["profile1"]);
308
309 let monitor = options.hw_resource_monitor.unwrap();
310 assert_eq!(monitor.polling_interval.as_deref(), Some("5000"));
311 assert_eq!(monitor.monitor_profiles[0].name, "alarms");
312 assert_eq!(monitor.monitor_profiles[0].lower_threshold.as_deref(), Some("60"));
313 assert_eq!(monitor.monitor_profiles[0].notification_type.as_deref(), Some("syslog"));
314 assert_eq!(monitor.monitor_profiles[0].upper_threshold.as_deref(), Some("90"));
315 assert_eq!(monitor.resource_lists[0].name, "lpm");
316 assert_eq!(monitor.resource_lists[0].resource_names, ["lpm-ipv4", "lpm-ipv6-64"]);
317 }
318
319 #[test]
320 fn accepts_the_inline_forms() {
321 let options = system(&juniper::parse_braces(
322 "system {\n packet-forwarding-options {\n hw-db-profile bng;\n mcast-repl-type ingress-egress-recycle;\n mcast {\n stats-enable;\n }\n }\n}\n",
323 ))
324 .unwrap()
325 .packet_forwarding_options
326 .unwrap();
327 assert_eq!(options.hw_db_profile.as_deref(), Some("bng"));
328 assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-egress-recycle"));
329 assert!(options.system_profile.is_none());
330 assert!(options.firewall.is_none());
331 }
332
333 #[test]
334 fn no_packet_forwarding_options_is_none() {
335 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
336 assert!(system.packet_forwarding_options.is_none());
337 }
338}