asterust-junos 0.5.14

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `lsp-attributes`
//! `protocols { mpls { label-switched-path <name> { lsp-attributes { ... } } } }`.
//!
//! The GMPLS attributes of an LSP.
//!
//! # See also
//!
//! - [lsp-attributes](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/lsp-attributes-edit-protocols-mpls.html)
//! - [encoding-type](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/encoding-type-edit-protocols-mpls.html)
//! - [gpid](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/gpid-edit-protocols-mpls.html)
//! - [signal-bandwidth](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/signal-bandwidth-edit-protocols-mpls.html)
//! - [switching-type](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/switching-type-edit-protocols-mpls.html)
//! - [upstream-label](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/upstream-label-edit-protocols-mpls.html)
//! - [GMPLS Configuration](https://networkingdocs.hpe.com/documentation/us/en/software/junos/mpls/topics/topic-map/gmpls-configuration.html), for the example behind the tests

use crate::{arg_of, child_block};
use asterust::config::ConfigLine;

/// The `lsp-attributes` of an LSP.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsLspAttributes {
    /// `ethernet`, `packet`, `pdh` or `sonet-sdh`.
    pub encoding_type: Option<String>,
    /// The generalized payload identifier, e.g. `ipv4` or `ppp`.
    pub gpid: Option<String>,
    pub signal_bandwidth: Option<String>,
    /// `fiber`, `lambda`, `psc-1`, `tdm`, or `ethernet-vlan` as the guide writes it.
    pub switching_type: Option<String>,
    /// The `vlan-id` of `upstream-label`.
    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;

    // Adapted from the MPLS Applications User Guide, GMPLS Configuration, Example: Configuring GMPLS RSVP-TE VLAN LSP
    // Signaling (the `mpls` block of the Results of Router CE1, shortened); see the README "Sources".
    #[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()
        );
    }

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[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());
    }
}