Skip to main content

asterust_junos/protocols/
isis.rs

1//! `isis`
2//! `protocols { isis { ... } }`.
3//!
4//! # See also
5//!
6//! - [isis](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/isis-edit-protocols.html)
7//! - [interface (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-protocols-isis.html)
8
9use crate::{child_block, has_leaf, named_children, top_level};
10use asterust::config::Config;
11
12/// An `interface <name>` entry under IS-IS.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct IsisInterface {
16    pub name: String,
17    pub passive: bool,
18    pub point_to_point: bool,
19}
20
21/// The `protocols isis` configuration. The NET lives on the loopback's `family iso` address, see
22/// [`crate::interfaces::InterfaceUnit::iso_addresses`].
23#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct Isis {
26    pub interfaces: Vec<IsisInterface>,
27}
28
29/// Returns the `protocols isis` block, or `None` if IS-IS is not configured.
30///
31/// # Examples
32///
33/// ```
34/// use asterust::config::juniper;
35/// use asterust_junos::protocols::isis::isis;
36///
37/// let config = juniper::parse_braces("protocols {\n    isis {\n        interface ge-0/0/0.0 {\n            point-to-point;\n        }\n    }\n}\n");
38/// let isis = isis(&config).unwrap();
39/// assert_eq!(isis.interfaces[0].name, "ge-0/0/0.0");
40/// assert!(isis.interfaces[0].point_to_point);
41/// ```
42pub fn isis(config: &Config) -> Option<Isis> {
43    let isis = child_block(top_level(config, "protocols")?, "isis")?;
44    Some(Isis {
45        interfaces: named_children(isis, "interface")
46            .map(|(name, iface)| IsisInterface {
47                name: name.to_string(),
48                passive: has_leaf(iface, "passive"),
49                point_to_point: has_leaf(iface, "point-to-point"),
50            })
51            .collect(),
52    })
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use asterust::config::juniper;
59
60    // Written from JunOS hierarchy knowledge, not adapted from documentation; see the README "Sources".
61    const CONFIG: &str = r#"
62protocols {
63    isis {
64        level 2 wide-metrics-only;
65        interface ge-0/0/0.0 {
66            point-to-point;
67            level 1 disable;
68        }
69        interface lo0.0 {
70            passive;
71        }
72        interface ge-0/0/1.0;
73    }
74}
75"#;
76
77    #[test]
78    fn extracts_interfaces() {
79        let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
80        let names: Vec<_> = isis.interfaces.iter().map(|i| i.name.as_str()).collect();
81        assert_eq!(names, ["ge-0/0/0.0", "lo0.0", "ge-0/0/1.0"]);
82        assert!(isis.interfaces[0].point_to_point);
83        assert!(!isis.interfaces[0].passive);
84        assert!(isis.interfaces[1].passive);
85        assert!(!isis.interfaces[2].point_to_point);
86    }
87
88    #[test]
89    fn no_isis_is_none() {
90        assert!(isis(&juniper::parse_braces("protocols {\n bgp {\n }\n}\n")).is_none());
91    }
92}