Skip to main content

asterust_junos/protocols/ospf/area/
virtual_link.rs

1//! `virtual-link`
2//! `protocols { ospf { area <id> { virtual-link neighbor-id <router-id> transit-area <area-id> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [virtual-link](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/virtual-link-edit-protocols-ospf.html)
7//! - [authentication (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-edit-protocols-ospf.html)
8
9use crate::protocols::ospf::authentication::{parse_authentication, OspfAuthentication};
10use crate::{arg_of, has_leaf, value_after};
11use asterust::config::ConfigLine;
12
13/// A `virtual-link` of the backbone area. Values are kept as written.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct OspfVirtualLink {
17    /// The router ID of the other end.
18    pub neighbor_id: String,
19    pub transit_area: String,
20    pub disable: bool,
21    /// Seconds.
22    pub hello_interval: Option<String>,
23    /// Seconds.
24    pub dead_interval: Option<String>,
25    /// Seconds.
26    pub retransmit_interval: Option<String>,
27    /// Seconds.
28    pub transit_delay: Option<String>,
29    pub ipsec_sa: Option<String>,
30    pub authentication: Option<OspfAuthentication>,
31    /// A `keychain` written directly under the link (24.2 and later).
32    pub keychain: Option<String>,
33}
34
35/// `header` is the text after `virtual-link`, e.g. `neighbor-id 192.0.2.3 transit-area 0.0.0.2`.
36pub(super) fn parse_virtual_link(header: &str, link: &ConfigLine) -> OspfVirtualLink {
37    let words: Vec<&str> = header.split_whitespace().collect();
38    let field = |keyword: &str| arg_of(link, keyword).map(str::to_string);
39    OspfVirtualLink {
40        neighbor_id: value_after(&words, "neighbor-id").unwrap_or_default(),
41        transit_area: value_after(&words, "transit-area").unwrap_or_default(),
42        disable: has_leaf(link, "disable"),
43        hello_interval: field("hello-interval"),
44        dead_interval: field("dead-interval"),
45        retransmit_interval: field("retransmit-interval"),
46        transit_delay: field("transit-delay"),
47        ipsec_sa: field("ipsec-sa"),
48        authentication: parse_authentication(link),
49        keychain: field("keychain"),
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use crate::protocols::ospf::ospf;
56    use asterust::config::juniper;
57
58    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
59    const CONFIG: &str = r#"
60protocols {
61    ospf {
62        area 0.0.0.0 {
63            virtual-link neighbor-id 192.0.2.3 transit-area 0.0.0.2;
64            virtual-link neighbor-id 192.0.2.4 transit-area 0.0.0.3 {
65                hello-interval 5;
66                dead-interval 20;
67                retransmit-interval 3;
68                transit-delay 2;
69                ipsec-sa SA-VL;
70                authentication {
71                    md5 1 key "$9$secret"; ## SECRET-DATA
72                }
73            }
74            virtual-link neighbor-id 192.0.2.5 transit-area 0.0.0.4 {
75                disable;
76                keychain VL-KEYS;
77            }
78        }
79    }
80}
81"#;
82
83    #[test]
84    fn reads_the_header_of_a_link_without_options() {
85        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
86        let link = &ospf.areas[0].virtual_links[0];
87        assert_eq!(link.neighbor_id, "192.0.2.3");
88        assert_eq!(link.transit_area, "0.0.0.2");
89        assert!(!link.disable);
90        assert_eq!(link.hello_interval, None);
91        assert_eq!(link.authentication, None);
92    }
93
94    #[test]
95    fn reads_the_timers_and_authentication() {
96        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
97        let link = &ospf.areas[0].virtual_links[1];
98        assert_eq!(link.neighbor_id, "192.0.2.4");
99        assert_eq!(link.hello_interval.as_deref(), Some("5"));
100        assert_eq!(link.dead_interval.as_deref(), Some("20"));
101        assert_eq!(link.retransmit_interval.as_deref(), Some("3"));
102        assert_eq!(link.transit_delay.as_deref(), Some("2"));
103        assert_eq!(link.ipsec_sa.as_deref(), Some("SA-VL"));
104        assert_eq!(link.authentication.as_ref().unwrap().md5[0].id, "1");
105    }
106
107    #[test]
108    fn reads_disable_and_keychain() {
109        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
110        let link = &ospf.areas[0].virtual_links[2];
111        assert!(link.disable);
112        assert_eq!(link.keychain.as_deref(), Some("VL-KEYS"));
113    }
114}