Skip to main content

asterust_junos/protocols/ospf/interface/
mod.rs

1//! `interface`
2//! `protocols { ospf { area <id> { interface <name> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [interface (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-protocols-ospf.html)
7//! - [interface-type (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-type-edit-protocols-ospf.html)
8//! - [passive (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/passive-edit-protocols-ospf.html)
9//! - [traffic-engineering (Passive TE Mode)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traffic-engineering-edit-protocols-ospf-passive.html)
10//! - [topology (OSPF Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/topology-edit-protocols-ospf-interface.html)
11//! - [link-protection (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/link-protection-edit-protocols-ospf.html)
12//! - [no-advertise-adjacency-segment (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-advertise-adjacency-segment-edit-protocols-ospf-interface-level.html)
13//! - [post-convergence-lfa (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/post-convergence-lfa-edit-protocols-ospf.html)
14
15mod bfd;
16mod post_convergence_lfa;
17
18pub use bfd::OspfBfd;
19pub use post_convergence_lfa::OspfPostConvergenceLfa;
20
21use super::authentication::{parse_authentication, OspfAuthentication};
22use crate::{arg_of, has_leaf, named_children, split_name, statement_words, value_after};
23use asterust::config::ConfigLine;
24use bfd::parse_bfd;
25use post_convergence_lfa::parse_post_convergence_lfa;
26
27/// An `interface <name>` entry under an OSPF area. Values are kept as written. The `passive` traffic-engineering
28/// settings, `bandwidth-based-metrics`, `lan-neighbor` and `ipv4-adjacency-segment` are not extracted.
29#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct OspfInterface {
32    pub name: String,
33    pub passive: bool,
34    pub metric: Option<u32>,
35    /// OSPF is disabled on this interface.
36    pub disable: bool,
37    pub priority: Option<String>,
38    /// Seconds.
39    pub hello_interval: Option<String>,
40    /// Seconds.
41    pub dead_interval: Option<String>,
42    /// Seconds.
43    pub retransmit_interval: Option<String>,
44    /// Seconds.
45    pub transit_delay: Option<String>,
46    /// Seconds, for NBMA interfaces.
47    pub poll_interval: Option<String>,
48    pub te_metric: Option<String>,
49    pub mtu: Option<String>,
50    /// `nbma`, `p2mp`, `p2mp-over-lan` or `p2p`.
51    pub interface_type: Option<String>,
52    pub ipsec_sa: Option<String>,
53    pub authentication: Option<OspfAuthentication>,
54    pub bfd_liveness_detection: Option<OspfBfd>,
55    /// `ldp-synchronization { disable; }`.
56    pub ldp_synchronization_disable: bool,
57    /// Seconds, from `ldp-synchronization { hold-time ...; }`.
58    pub ldp_synchronization_hold_time: Option<String>,
59    pub neighbors: Vec<OspfNeighbor>,
60    pub topologies: Vec<OspfInterfaceTopology>,
61    pub link_protection: bool,
62    pub node_link_protection: bool,
63    pub no_eligible_backup: bool,
64    pub no_eligible_remote_backup: bool,
65    pub secondary: bool,
66    pub flood_reduction: bool,
67    pub demand_circuit: bool,
68    pub no_interface_state_traps: bool,
69    pub no_neighbor_down_notification: bool,
70    pub no_advertise_adjacency_segment: bool,
71    pub dynamic_neighbors: bool,
72    pub strict_bfd: bool,
73    pub post_convergence_lfa: Option<OspfPostConvergenceLfa>,
74}
75
76/// A `neighbor <address> [eligible]` of a non-broadcast interface.
77#[derive(Clone, Debug, PartialEq, Eq)]
78#[non_exhaustive]
79pub struct OspfNeighbor {
80    pub address: String,
81    pub eligible: bool,
82}
83
84/// A `topology <name> { metric ...; }` of an interface, e.g. `ipv4-multicast`.
85#[derive(Clone, Debug, PartialEq, Eq)]
86#[non_exhaustive]
87pub struct OspfInterfaceTopology {
88    pub name: String,
89    pub metric: Option<String>,
90}
91
92pub(super) fn parse_interface(name: &str, iface: &ConfigLine) -> OspfInterface {
93    let field = |keyword: &str| arg_of(iface, keyword).map(str::to_string);
94    let present = |keyword: &str| statement_words(iface, keyword).is_some();
95    let ldp = statement_words(iface, "ldp-synchronization");
96    OspfInterface {
97        name: name.to_string(),
98        passive: has_leaf(iface, "passive"),
99        metric: arg_of(iface, "metric").and_then(|metric| metric.parse().ok()),
100        disable: has_leaf(iface, "disable"),
101        priority: field("priority"),
102        hello_interval: field("hello-interval"),
103        dead_interval: field("dead-interval"),
104        retransmit_interval: field("retransmit-interval"),
105        transit_delay: field("transit-delay"),
106        poll_interval: field("poll-interval"),
107        te_metric: field("te-metric"),
108        mtu: field("mtu"),
109        interface_type: field("interface-type"),
110        ipsec_sa: field("ipsec-sa"),
111        authentication: parse_authentication(iface),
112        bfd_liveness_detection: parse_bfd(iface),
113        ldp_synchronization_disable: ldp.as_ref().is_some_and(|words| words.contains(&"disable")),
114        ldp_synchronization_hold_time: ldp.and_then(|words| value_after(&words, "hold-time")),
115        neighbors: named_children(iface, "neighbor")
116            .map(|(arg, neighbor)| {
117                let (address, rest) = split_name(arg);
118                OspfNeighbor {
119                    address: address.to_string(),
120                    eligible: rest.split_whitespace().any(|word| word == "eligible") || has_leaf(neighbor, "eligible"),
121                }
122            })
123            .collect(),
124        topologies: named_children(iface, "topology")
125            .map(|(name, topology)| OspfInterfaceTopology {
126                name: name.to_string(),
127                metric: arg_of(topology, "metric").map(str::to_string),
128            })
129            .collect(),
130        link_protection: present("link-protection"),
131        node_link_protection: present("node-link-protection"),
132        no_eligible_backup: has_leaf(iface, "no-eligible-backup"),
133        no_eligible_remote_backup: has_leaf(iface, "no-eligible-remote-backup"),
134        secondary: has_leaf(iface, "secondary"),
135        flood_reduction: has_leaf(iface, "flood-reduction"),
136        demand_circuit: has_leaf(iface, "demand-circuit"),
137        no_interface_state_traps: has_leaf(iface, "no-interface-state-traps"),
138        no_neighbor_down_notification: has_leaf(iface, "no-neighbor-down-notification"),
139        no_advertise_adjacency_segment: has_leaf(iface, "no-advertise-adjacency-segment"),
140        dynamic_neighbors: has_leaf(iface, "dynamic-neighbors"),
141        strict_bfd: has_leaf(iface, "strict-bfd"),
142        post_convergence_lfa: parse_post_convergence_lfa(iface),
143    }
144}
145
146#[cfg(test)]
147mod tests {
148    use crate::protocols::ospf::ospf;
149    use asterust::config::juniper;
150
151    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
152    const CONFIG: &str = r#"
153protocols {
154    ospf {
155        area 0.0.0.0 {
156            interface ge-0/0/0.0 {
157                interface-type p2p;
158                metric 10;
159                priority 200;
160                hello-interval 5;
161                dead-interval 20;
162                retransmit-interval 3;
163                transit-delay 2;
164                te-metric 15;
165                mtu 1400;
166                ipsec-sa SA-OSPF;
167                link-protection;
168                no-eligible-backup;
169                strict-bfd;
170                ldp-synchronization {
171                    hold-time 30;
172                }
173            }
174            interface ge-0/0/1.0 {
175                interface-type nbma;
176                poll-interval 60;
177                neighbor 10.0.0.2 eligible;
178                neighbor 10.0.0.3;
179                secondary;
180                flood-reduction;
181                demand-circuit;
182                no-interface-state-traps;
183                topology ipv4-multicast {
184                    metric 25;
185                }
186                ldp-synchronization {
187                    disable;
188                }
189            }
190            interface lo0.0 {
191                passive {
192                    traffic-engineering {
193                        remote-node-id 10.0.0.9;
194                    }
195                }
196            }
197            interface ge-0/0/2.0 {
198                disable;
199            }
200            interface ge-0/0/3.0;
201        }
202    }
203}
204"#;
205
206    #[test]
207    fn reads_the_timers_and_basic_options() {
208        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
209        let iface = &ospf.areas[0].interfaces[0];
210        assert_eq!(iface.interface_type.as_deref(), Some("p2p"));
211        assert_eq!(iface.metric, Some(10));
212        assert_eq!(iface.priority.as_deref(), Some("200"));
213        assert_eq!(iface.hello_interval.as_deref(), Some("5"));
214        assert_eq!(iface.dead_interval.as_deref(), Some("20"));
215        assert_eq!(iface.retransmit_interval.as_deref(), Some("3"));
216        assert_eq!(iface.transit_delay.as_deref(), Some("2"));
217        assert_eq!(iface.te_metric.as_deref(), Some("15"));
218        assert_eq!(iface.mtu.as_deref(), Some("1400"));
219        assert_eq!(iface.ipsec_sa.as_deref(), Some("SA-OSPF"));
220        assert!(iface.link_protection);
221        assert!(iface.no_eligible_backup);
222        assert!(iface.strict_bfd);
223        assert!(!iface.node_link_protection);
224        assert!(!iface.ldp_synchronization_disable);
225        assert_eq!(iface.ldp_synchronization_hold_time.as_deref(), Some("30"));
226    }
227
228    #[test]
229    fn reads_the_non_broadcast_options() {
230        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
231        let iface = &ospf.areas[0].interfaces[1];
232        assert_eq!(iface.interface_type.as_deref(), Some("nbma"));
233        assert_eq!(iface.poll_interval.as_deref(), Some("60"));
234        assert_eq!(iface.neighbors.len(), 2);
235        assert_eq!(iface.neighbors[0].address, "10.0.0.2");
236        assert!(iface.neighbors[0].eligible);
237        assert!(!iface.neighbors[1].eligible);
238        assert!(iface.secondary);
239        assert!(iface.flood_reduction);
240        assert!(iface.demand_circuit);
241        assert!(iface.no_interface_state_traps);
242        assert!(iface.ldp_synchronization_disable);
243        assert_eq!(iface.topologies[0].name, "ipv4-multicast");
244        assert_eq!(iface.topologies[0].metric.as_deref(), Some("25"));
245    }
246
247    #[test]
248    fn reads_passive_blocks_and_disabled_interfaces() {
249        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
250        let interfaces = &ospf.areas[0].interfaces;
251        assert!(interfaces[2].passive);
252        assert!(interfaces[3].disable);
253        assert!(!interfaces[3].passive);
254        let bare = &interfaces[4];
255        assert_eq!(bare.priority, None);
256        assert_eq!(bare.authentication, None);
257        assert_eq!(bare.bfd_liveness_detection, None);
258        assert!(bare.neighbors.is_empty());
259    }
260}