use crate::{
active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, statements, value_after,
};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RecycleBandwidthProfile {
pub name: String,
pub applications: Vec<(String, String)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CounterProfileApp {
pub name: String,
pub counter_4k: Option<String>,
pub counter_8k: Option<String>,
pub counter_16k: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CounterProfile {
pub name: String,
pub apps: Vec<CounterProfileApp>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HwProfile {
pub name: String,
pub counter_profile: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PfeSystemProfile {
pub vxlan_extended: bool,
pub vxlan_stitching: bool,
pub vxlan_stitching_control_word: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PfeFirewall {
pub eracl_scale: bool,
pub force_fbf_terms: bool,
pub ethernet_switching_profiles: Vec<String>,
pub ethernet_switching_egress_profiles: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HwMonitorProfile {
pub name: String,
pub lower_threshold: Option<String>,
pub notification_type: Option<String>,
pub upper_threshold: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HwResourceList {
pub name: String,
pub resource_names: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HwResourceMonitor {
pub polling_interval: Option<String>,
pub monitor_profiles: Vec<HwMonitorProfile>,
pub resource_lists: Vec<HwResourceList>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PacketForwardingOptions {
pub eracl_ip6_match: Option<String>,
pub flow_export_timer: Option<String>,
pub hw_db_profile: Option<String>,
pub mcast_stats_enable: bool,
pub mcast_repl_type: Option<String>,
pub mef_forwarding_profile: bool,
pub mpls_lsp_ingress_stats_enable: bool,
pub mpls_lsp_transit_stats_enable: bool,
pub overlay_interface_num: Option<String>,
pub overlay_next_hop: Option<String>,
pub packet_format_match: Option<String>,
pub recycle_bandwidth_profiles: Vec<RecycleBandwidthProfile>,
pub system_profile: Option<PfeSystemProfile>,
pub firewall: Option<PfeFirewall>,
pub hw_resource_monitor: Option<HwResourceMonitor>,
pub counter_profiles: Vec<CounterProfile>,
pub hw_profiles: Vec<HwProfile>,
pub hw_profile: Option<String>,
pub internal_forwarding_bandwidth: Option<String>,
pub internal_forwarding_reserve_ingress_egress_mcast: Option<String>,
pub ipfix_sw_mode: bool,
pub no_ip_tos_rewrite: bool,
pub multicast_statistics: bool,
pub egress_l2_extended_match: bool,
}
fn parse_firewall(firewall: &ConfigLine) -> PfeFirewall {
let switching = child_block(firewall, "profiles").and_then(|profiles| child_block(profiles, "ethernet-switching"));
PfeFirewall {
eracl_scale: child_block(firewall, "eracl-profile").is_some_and(|profile| has_leaf(profile, "eracl-scale")),
force_fbf_terms: has_leaf(firewall, "force-fbf-terms"),
ethernet_switching_profiles: switching
.map(|s| {
active_children(s)
.filter(|c| c.text != "egress")
.map(|c| c.text.clone())
.collect()
})
.unwrap_or_default(),
ethernet_switching_egress_profiles: switching
.and_then(|s| child_block(s, "egress"))
.map(|egress| active_children(egress).map(|c| c.text.clone()).collect())
.unwrap_or_default(),
}
}
fn parse_hw_resource_monitor(monitor: &ConfigLine) -> HwResourceMonitor {
HwResourceMonitor {
polling_interval: arg_of(monitor, "polling-interval").map(str::to_string),
monitor_profiles: named_children(monitor, "monitor-profile")
.map(|(name, profile)| HwMonitorProfile {
name: name.to_string(),
lower_threshold: arg_of(profile, "lower-threshold").map(str::to_string),
notification_type: arg_of(profile, "notification-type").map(str::to_string),
upper_threshold: arg_of(profile, "upper-threshold").map(str::to_string),
})
.collect(),
resource_lists: named_children(monitor, "resource-list")
.map(|(name, list)| HwResourceList {
name: name.to_string(),
resource_names: args_of(list, "resource-names")
.into_iter()
.flat_map(list_values)
.collect(),
})
.collect(),
}
}
fn entry_words(entry: &ConfigLine) -> Vec<&str> {
entry
.text
.split_whitespace()
.chain(active_children(entry).flat_map(|c| c.text.split_whitespace()))
.collect()
}
pub(super) fn parse_packet_forwarding_options(options: &ConfigLine) -> PacketForwardingOptions {
let overlay = child_block(options, "overlay");
let mpls = child_block(options, "mpls");
let profile = child_block(options, "system-profile");
let custom = child_block(options, "custom-profiles");
let bandwidth = child_block(options, "internal-forwarding-bandwidth");
PacketForwardingOptions {
eracl_ip6_match: statements(options, "eracl-ip6-match").first().map(|s| s.to_string()),
flow_export_timer: arg_of(options, "flow-export-timer").map(str::to_string),
hw_db_profile: statements(options, "hw-db-profile").first().map(|s| s.to_string()),
mcast_stats_enable: child_block(options, "mcast").is_some_and(|mcast| has_leaf(mcast, "stats-enable")),
mcast_repl_type: statements(options, "mcast-repl-type").first().map(|s| s.to_string()),
mef_forwarding_profile: has_leaf(options, "mef-forwarding-profile"),
mpls_lsp_ingress_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-ingress-stats-enable")),
mpls_lsp_transit_stats_enable: mpls.is_some_and(|m| has_leaf(m, "lsp-transit-stats-enable")),
overlay_interface_num: overlay.and_then(|o| arg_of(o, "interface-num")).map(str::to_string),
overlay_next_hop: overlay.and_then(|o| arg_of(o, "next-hop")).map(str::to_string),
packet_format_match: statements(options, "packet-format-match")
.first()
.map(|s| s.to_string()),
recycle_bandwidth_profiles: named_children(options, "recycle-bandwidth-profiles")
.map(|(name, profile)| RecycleBandwidthProfile {
name: name.to_string(),
applications: active_children(profile)
.filter_map(|c| {
let (application, percentage) = c.text.split_once(char::is_whitespace)?;
Some((application.to_string(), percentage.trim().to_string()))
})
.collect(),
})
.collect(),
system_profile: profile.map(|profile| PfeSystemProfile {
vxlan_extended: has_leaf(profile, "vxlan-extended"),
vxlan_stitching: child_block(profile, "vxlan-stitching").is_some(),
vxlan_stitching_control_word: child_block(profile, "vxlan-stitching")
.is_some_and(|stitching| has_leaf(stitching, "control-word")),
}),
firewall: child_block(options, "firewall").map(parse_firewall),
hw_resource_monitor: child_block(options, "hw-resource-monitor").map(parse_hw_resource_monitor),
counter_profiles: custom
.map(|custom| {
named_children(custom, "counter-profiles")
.map(|(name, profile)| CounterProfile {
name: name.to_string(),
apps: child_block(profile, "app")
.map(|app| {
active_children(app)
.map(|entry| {
let words = entry_words(entry);
CounterProfileApp {
name: words.first().unwrap_or(&"").to_string(),
counter_4k: value_after(&words, "counter-4k"),
counter_8k: value_after(&words, "counter-8k"),
counter_16k: value_after(&words, "counter-16k"),
}
})
.collect()
})
.unwrap_or_default(),
})
.collect()
})
.unwrap_or_default(),
hw_profiles: custom
.map(|custom| {
named_children(custom, "hw-profiles")
.map(|(name, profile)| HwProfile {
name: name.to_string(),
counter_profile: arg_of(profile, "counter-profile").map(str::to_string),
})
.collect()
})
.unwrap_or_default(),
hw_profile: arg_of(options, "hw-profile").map(str::to_string),
internal_forwarding_bandwidth: arg_of(options, "internal-forwarding-bandwidth").map(str::to_string),
internal_forwarding_reserve_ingress_egress_mcast: bandwidth
.and_then(|b| arg_of(b, "reserve"))
.and_then(|reserve| value_after(&reserve.split_whitespace().collect::<Vec<_>>(), "ingress-egress-mcast")),
ipfix_sw_mode: has_leaf(options, "ipfix-sw-mode"),
no_ip_tos_rewrite: has_leaf(options, "no-ip-tos-rewrite"),
multicast_statistics: has_leaf(options, "multicast-statistics"),
egress_l2_extended_match: child_block(options, "firewall-profile")
.is_some_and(|profile| has_leaf(profile, "egress-l2-extended-match")),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_packet_forwarding_options() {
let options = system(&juniper::parse_braces(
r#"
system {
packet-forwarding-options {
eracl-ip6-match {
srcip6-only;
}
flow-export-timer 30;
hw-db-profile {
cloud-metro;
}
mcast {
stats-enable;
}
mcast-repl-type {
ingress-only;
}
mef-forwarding-profile;
mpls {
lsp-ingress-stats-enable;
}
overlay {
interface-num 2048;
next-hop 8192;
}
packet-format-match {
mpls-packet-format-3;
}
recycle-bandwidth-profiles lab {
tunnel-services 20;
srv6 30;
}
system-profile {
vxlan-extended;
vxlan-stitching {
control-word;
}
}
firewall {
eracl-profile {
eracl-scale;
}
profiles {
ethernet-switching {
profile1;
udf-profile1;
egress {
profile1;
}
}
}
}
hw-resource-monitor {
polling-interval 5000;
monitor-profile alarms {
lower-threshold 60;
notification-type syslog;
upper-threshold 90;
}
resource-list lpm {
resource-names [ lpm-ipv4 lpm-ipv6-64 ];
}
}
}
}
"#,
))
.unwrap()
.packet_forwarding_options
.unwrap();
assert_eq!(options.eracl_ip6_match.as_deref(), Some("srcip6-only"));
assert_eq!(options.flow_export_timer.as_deref(), Some("30"));
assert_eq!(options.hw_db_profile.as_deref(), Some("cloud-metro"));
assert!(options.mcast_stats_enable);
assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-only"));
assert!(options.mef_forwarding_profile);
assert!(options.mpls_lsp_ingress_stats_enable);
assert!(!options.mpls_lsp_transit_stats_enable);
assert_eq!(options.overlay_interface_num.as_deref(), Some("2048"));
assert_eq!(options.overlay_next_hop.as_deref(), Some("8192"));
assert_eq!(options.packet_format_match.as_deref(), Some("mpls-packet-format-3"));
assert_eq!(options.recycle_bandwidth_profiles[0].name, "lab");
assert_eq!(
options.recycle_bandwidth_profiles[0].applications,
[
("tunnel-services".to_string(), "20".to_string()),
("srv6".to_string(), "30".to_string())
]
);
let profile = options.system_profile.unwrap();
assert!(profile.vxlan_extended);
assert!(profile.vxlan_stitching);
assert!(profile.vxlan_stitching_control_word);
let firewall = options.firewall.unwrap();
assert!(firewall.eracl_scale);
assert_eq!(firewall.ethernet_switching_profiles, ["profile1", "udf-profile1"]);
assert_eq!(firewall.ethernet_switching_egress_profiles, ["profile1"]);
let monitor = options.hw_resource_monitor.unwrap();
assert_eq!(monitor.polling_interval.as_deref(), Some("5000"));
assert_eq!(monitor.monitor_profiles[0].name, "alarms");
assert_eq!(monitor.monitor_profiles[0].lower_threshold.as_deref(), Some("60"));
assert_eq!(monitor.monitor_profiles[0].notification_type.as_deref(), Some("syslog"));
assert_eq!(monitor.monitor_profiles[0].upper_threshold.as_deref(), Some("90"));
assert_eq!(monitor.resource_lists[0].name, "lpm");
assert_eq!(monitor.resource_lists[0].resource_names, ["lpm-ipv4", "lpm-ipv6-64"]);
}
#[test]
fn extracts_the_custom_profiles_and_other_statements() {
let options = system(&juniper::parse_braces(
r#"
system {
packet-forwarding-options {
custom-profiles {
counter-profiles flows {
app {
acl {
counter-4k 2;
counter-8k 1;
}
telemetry {
counter-16k 4;
}
}
}
hw-profiles edge {
counter-profile flows;
}
}
hw-profile edge;
internal-forwarding-bandwidth {
reserve ingress-egress-mcast 40;
}
ipfix-sw-mode;
no-ip-tos-rewrite;
multicast-statistics;
firewall {
force-fbf-terms;
}
firewall-profile {
egress-l2-extended-match;
}
}
}
"#,
))
.unwrap()
.packet_forwarding_options
.unwrap();
assert_eq!(options.counter_profiles[0].name, "flows");
let acl = &options.counter_profiles[0].apps[0];
assert_eq!(acl.name, "acl");
assert_eq!(acl.counter_4k.as_deref(), Some("2"));
assert_eq!(acl.counter_8k.as_deref(), Some("1"));
assert_eq!(acl.counter_16k, None);
assert_eq!(options.counter_profiles[0].apps[1].counter_16k.as_deref(), Some("4"));
assert_eq!(options.hw_profiles[0].name, "edge");
assert_eq!(options.hw_profiles[0].counter_profile.as_deref(), Some("flows"));
assert_eq!(options.hw_profile.as_deref(), Some("edge"));
assert_eq!(options.internal_forwarding_bandwidth, None);
assert_eq!(
options.internal_forwarding_reserve_ingress_egress_mcast.as_deref(),
Some("40")
);
assert!(options.ipfix_sw_mode);
assert!(options.no_ip_tos_rewrite);
assert!(options.multicast_statistics);
assert!(options.firewall.unwrap().force_fbf_terms);
assert!(options.egress_l2_extended_match);
}
#[test]
fn reads_the_inline_internal_forwarding_bandwidth() {
let options = system(&juniper::parse_braces(
"system {\n packet-forwarding-options {\n internal-forwarding-bandwidth 50g;\n }\n}\n",
))
.unwrap()
.packet_forwarding_options
.unwrap();
assert_eq!(options.internal_forwarding_bandwidth.as_deref(), Some("50g"));
assert_eq!(options.internal_forwarding_reserve_ingress_egress_mcast, None);
assert!(options.counter_profiles.is_empty());
}
#[test]
fn accepts_the_inline_forms() {
let options = system(&juniper::parse_braces(
"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",
))
.unwrap()
.packet_forwarding_options
.unwrap();
assert_eq!(options.hw_db_profile.as_deref(), Some("bng"));
assert_eq!(options.mcast_repl_type.as_deref(), Some("ingress-egress-recycle"));
assert!(options.system_profile.is_none());
assert!(options.firewall.is_none());
}
#[test]
fn no_packet_forwarding_options_is_none() {
let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
assert!(system.packet_forwarding_options.is_none());
}
}