asterust-junos 0.3.7

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `isis`
//! `protocols { isis { ... } }`.
//!
//! # See also
//!
//! - [isis](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/isis-edit-protocols.html)
//! - [interface (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-protocols-isis.html)

use crate::{child_block, has_leaf, named_children, top_level};
use asterust::config::Config;

/// An `interface <name>` entry under IS-IS.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct IsisInterface {
    pub name: String,
    pub passive: bool,
    pub point_to_point: bool,
}

/// The `protocols isis` configuration. The NET lives on the loopback's `family iso` address, see
/// [`crate::interfaces::InterfaceUnit::iso_addresses`].
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Isis {
    pub interfaces: Vec<IsisInterface>,
}

/// Returns the `protocols isis` block, or `None` if IS-IS is not configured.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::protocols::isis::isis;
///
/// let config = juniper::parse_braces("protocols {\n    isis {\n        interface ge-0/0/0.0 {\n            point-to-point;\n        }\n    }\n}\n");
/// let isis = isis(&config).unwrap();
/// assert_eq!(isis.interfaces[0].name, "ge-0/0/0.0");
/// assert!(isis.interfaces[0].point_to_point);
/// ```
pub fn isis(config: &Config) -> Option<Isis> {
    let isis = child_block(top_level(config, "protocols")?, "isis")?;
    Some(Isis {
        interfaces: named_children(isis, "interface")
            .map(|(name, iface)| IsisInterface {
                name: name.to_string(),
                passive: has_leaf(iface, "passive"),
                point_to_point: has_leaf(iface, "point-to-point"),
            })
            .collect(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use asterust::config::juniper;

    // Written from JunOS hierarchy knowledge, not adapted from documentation; see the README "Sources".
    const CONFIG: &str = r#"
protocols {
    isis {
        level 2 wide-metrics-only;
        interface ge-0/0/0.0 {
            point-to-point;
            level 1 disable;
        }
        interface lo0.0 {
            passive;
        }
        interface ge-0/0/1.0;
    }
}
"#;

    #[test]
    fn extracts_interfaces() {
        let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
        let names: Vec<_> = isis.interfaces.iter().map(|i| i.name.as_str()).collect();
        assert_eq!(names, ["ge-0/0/0.0", "lo0.0", "ge-0/0/1.0"]);
        assert!(isis.interfaces[0].point_to_point);
        assert!(!isis.interfaces[0].passive);
        assert!(isis.interfaces[1].passive);
        assert!(!isis.interfaces[2].point_to_point);
    }

    #[test]
    fn no_isis_is_none() {
        assert!(isis(&juniper::parse_braces("protocols {\n bgp {\n }\n}\n")).is_none());
    }
}