Skip to main content

asterust_junos/protocols/
ospf.rs

1//! `protocols { ospf { ... } }`.
2
3use crate::{arg_of, child_block, has_leaf, named_children, top_level};
4use asterust::config::Config;
5
6/// An `interface <name>` entry under an OSPF area.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct OspfInterface {
10    pub name: String,
11    pub passive: bool,
12    pub metric: Option<u32>,
13}
14
15/// An `area <id>` and its interfaces.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct OspfArea {
19    pub id: String,
20    pub interfaces: Vec<OspfInterface>,
21}
22
23/// The `protocols ospf` configuration. `ospf3` is not covered.
24#[derive(Clone, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct Ospf {
27    pub areas: Vec<OspfArea>,
28}
29
30/// The `protocols ospf` block, or `None` if OSPF is not configured.
31pub fn ospf(config: &Config) -> Option<Ospf> {
32    let ospf = child_block(top_level(config, "protocols")?, "ospf")?;
33    let areas = named_children(ospf, "area")
34        .map(|(id, area)| OspfArea {
35            id: id.to_string(),
36            interfaces: named_children(area, "interface")
37                .map(|(name, iface)| OspfInterface {
38                    name: name.to_string(),
39                    passive: has_leaf(iface, "passive"),
40                    metric: arg_of(iface, "metric").and_then(|m| m.parse().ok()),
41                })
42                .collect(),
43        })
44        .collect();
45    Some(Ospf { areas })
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use asterust::config::juniper;
52
53    // Not from documentation: covers statements the Juniper examples do not use. See the README "Sources".
54    const CONFIG: &str = r#"
55protocols {
56    ospf {
57        area 0.0.0.0 {
58            interface ge-0/0/0.0 {
59                metric 10;
60            }
61            interface lo0.0 {
62                passive;
63            }
64        }
65        area 0.0.0.10 {
66            interface ge-0/0/1.0;
67        }
68    }
69    ospf3 {
70        area 0.0.0.0 {
71            interface ge-0/0/0.0;
72        }
73    }
74}
75"#;
76
77    #[test]
78    fn extracts_areas_and_interfaces() {
79        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
80        assert_eq!(ospf.areas.len(), 2);
81
82        let backbone = &ospf.areas[0];
83        assert_eq!(backbone.id, "0.0.0.0");
84        assert_eq!(backbone.interfaces.len(), 2);
85        assert_eq!(backbone.interfaces[0].name, "ge-0/0/0.0");
86        assert_eq!(backbone.interfaces[0].metric, Some(10));
87        assert!(!backbone.interfaces[0].passive);
88        assert!(backbone.interfaces[1].passive);
89
90        let area10 = &ospf.areas[1];
91        assert_eq!(area10.id, "0.0.0.10");
92        assert_eq!(area10.interfaces[0].name, "ge-0/0/1.0");
93    }
94
95    #[test]
96    fn no_ospf_is_none() {
97        assert!(ospf(&juniper::parse_braces("protocols {\n isis {\n }\n}\n")).is_none());
98    }
99
100    // Adapted from Juniper's BGP User Guide, "BGP Peering Sessions", "Example: Configuring Internal BGP Peer
101    // Sessions", Device A `show protocols` (OSPF part); see the README "Sources".
102    const DOC_OSPF: &str = r#"
103protocols {
104    ospf {
105        area 0.0.0.0 {
106            interface lo0.1 {
107                passive;
108            }
109            interface ge-0/1/0.1;
110        }
111    }
112}
113"#;
114
115    #[test]
116    fn doc_area_with_passive_loopback() {
117        let ospf = ospf(&juniper::parse_braces(DOC_OSPF)).unwrap();
118        assert_eq!(ospf.areas.len(), 1);
119        let area = &ospf.areas[0];
120        assert_eq!(area.id, "0.0.0.0");
121        assert_eq!(area.interfaces[0].name, "lo0.1");
122        assert!(area.interfaces[0].passive);
123        assert_eq!(area.interfaces[1].name, "ge-0/1/0.1");
124        assert!(!area.interfaces[1].passive);
125    }
126}