use crate::{arg_of, child_block};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsLspAttributes {
pub encoding_type: Option<String>,
pub gpid: Option<String>,
pub signal_bandwidth: Option<String>,
pub switching_type: Option<String>,
pub upstream_label_vlan_id: Option<String>,
}
pub(super) fn parse_lsp_attributes(attributes: &ConfigLine) -> MplsLspAttributes {
let field = |keyword: &str| arg_of(attributes, keyword).map(str::to_string);
MplsLspAttributes {
encoding_type: field("encoding-type"),
gpid: field("gpid"),
signal_bandwidth: field("signal-bandwidth"),
switching_type: field("switching-type"),
upstream_label_vlan_id: child_block(attributes, "upstream-label")
.and_then(|label| arg_of(label, "vlan-id"))
.map(str::to_string),
}
}
#[cfg(test)]
mod tests {
use crate::protocols::mpls::mpls;
use asterust::config::juniper;
#[test]
fn reads_the_guide_lsp_attributes() {
let mpls = mpls(&juniper::parse_braces(
r#"
protocols {
mpls {
no-cspf;
label-switched-path CE1-to-CE2 {
from 10.255.10.1;
to 10.255.10.6;
lsp-attributes {
switching-type ethernet-vlan;
upstream-label {
vlan-id 10;
}
}
bandwidth 100m;
primary path1;
}
}
}
"#,
))
.unwrap();
let attributes = mpls.label_switched_paths[0].lsp_attributes.as_ref().unwrap();
assert_eq!(attributes.switching_type.as_deref(), Some("ethernet-vlan"));
assert_eq!(attributes.upstream_label_vlan_id.as_deref(), Some("10"));
assert!(
attributes.encoding_type.is_none() && attributes.gpid.is_none() && attributes.signal_bandwidth.is_none()
);
}
#[test]
fn reads_every_attribute() {
let mpls = mpls(&juniper::parse_braces(
"protocols {\n mpls {\n label-switched-path gmpls {\n to 10.0.0.9;\n lsp-attributes {\n encoding-type sonet-sdh;\n gpid pos-scrambling-crc-16;\n signal-bandwidth oc-48;\n switching-type tdm;\n }\n }\n label-switched-path plain {\n to 10.0.0.10;\n }\n }\n}\n",
))
.unwrap();
let attributes = mpls.label_switched_paths[0].lsp_attributes.as_ref().unwrap();
assert_eq!(attributes.encoding_type.as_deref(), Some("sonet-sdh"));
assert_eq!(attributes.gpid.as_deref(), Some("pos-scrambling-crc-16"));
assert_eq!(attributes.signal_bandwidth.as_deref(), Some("oc-48"));
assert_eq!(attributes.switching_type.as_deref(), Some("tdm"));
assert!(attributes.upstream_label_vlan_id.is_none());
assert!(mpls.label_switched_paths[1].lsp_attributes.is_none());
}
}