Skip to main content

asterust_junos/
isis.rs

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