Skip to main content

asterust_junos/protocols/
ospf.rs

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