Skip to main content

asterust_junos/protocols/isis/interface/
level.rs

1//! `level`
2//! `protocols { isis { interface <name> { level <1|2> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [level (IS-IS Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/level-edit-protocols-isis-interface.html)
7//! - [metric (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/metric-edit-protocols-isis.html)
8//! - [hold-time (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/hold-time-edit-protocols-isis.html)
9//! - [hello-interval (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/hello-interval-edit-protocols-isis.html), from the Junos 12.3 archive
10//! - [priority (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/priority-edit-protocols-isis.html), from the Junos 12.3 archive
11//! - [te-metric (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/te-metric-edit-protocols-isis.html), from the Junos 12.3 archive
12//! - [hello-authentication-key](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/hello-authentication-key-edit-protocols-isis.html), from the Junos 12.3 archive
13//! - [hello-authentication-type](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/hello-authentication-type-edit-protocols-isis.html), from the Junos 12.3 archive
14//! - [ipv4-multicast-metric](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/ipv4-multicast-metric-edit-protocols-isis.html), from the Junos 12.3 archive
15//! - [ipv6-unicast-metric](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/ipv6-unicast-metric-edit-protocols-isis.html), from the Junos 12.3 archive
16
17use crate::protocols::isis::srv6::{parse_adjacency_segments, parse_lan_neighbor_segments, IsisSrv6AdjacencySegment};
18use crate::{active_children, split_name, value_after};
19use asterust::config::ConfigLine;
20
21/// A `level <1|2>` entry of an IS-IS interface, written `level 1 disable;`, `level 2 metric 10;` or as a block.
22/// The `hello-authentication-key` is never extracted.
23#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct IsisInterfaceLevel {
26    /// `1` or `2`.
27    pub level: String,
28    pub disable: bool,
29    pub passive: bool,
30    pub metric: Option<String>,
31    /// Seconds.
32    pub hello_interval: Option<String>,
33    /// Seconds.
34    pub hold_time: Option<String>,
35    pub priority: Option<String>,
36    pub te_metric: Option<String>,
37    pub ipv4_multicast_metric: Option<String>,
38    pub ipv6_multicast_metric: Option<String>,
39    pub ipv6_unicast_metric: Option<String>,
40    /// `md5` or `simple`.
41    pub hello_authentication_type: Option<String>,
42    /// A `hello-authentication-key` is configured.
43    pub has_hello_authentication_key: bool,
44    pub hello_authentication_key_chain: Option<String>,
45    /// The `srv6-adjacency-segment` locators of the level and of its `lan-neighbor` entries.
46    pub srv6_adjacency_segments: Vec<IsisSrv6AdjacencySegment>,
47}
48
49/// `arg` is the text after `level`, e.g. `1 disable` or `2`.
50pub(super) fn parse_level(arg: &str, level: &ConfigLine) -> IsisInterfaceLevel {
51    let (number, rest) = split_name(arg);
52    let words: Vec<&str> = rest
53        .split_whitespace()
54        .chain(active_children(level).flat_map(|child| child.text.split_whitespace()))
55        .collect();
56    IsisInterfaceLevel {
57        level: number.to_string(),
58        disable: words.contains(&"disable"),
59        passive: words.contains(&"passive"),
60        metric: value_after(&words, "metric"),
61        hello_interval: value_after(&words, "hello-interval"),
62        hold_time: value_after(&words, "hold-time"),
63        priority: value_after(&words, "priority"),
64        te_metric: value_after(&words, "te-metric"),
65        ipv4_multicast_metric: value_after(&words, "ipv4-multicast-metric"),
66        ipv6_multicast_metric: value_after(&words, "ipv6-multicast-metric"),
67        ipv6_unicast_metric: value_after(&words, "ipv6-unicast-metric"),
68        hello_authentication_type: value_after(&words, "hello-authentication-type"),
69        has_hello_authentication_key: words.contains(&"hello-authentication-key"),
70        hello_authentication_key_chain: value_after(&words, "hello-authentication-key-chain"),
71        srv6_adjacency_segments: {
72            let mut segments = parse_adjacency_segments(level, None);
73            segments.extend(parse_lan_neighbor_segments(level));
74            segments
75        },
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use crate::protocols::isis::isis;
82    use asterust::config::juniper;
83
84    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
85    const CONFIG: &str = r#"
86protocols {
87    isis {
88        interface fe-1/2/0.0 {
89            level 1 disable;
90        }
91        interface ge-0/0/0.0 {
92            level 1 {
93                metric 20;
94                hello-interval 5;
95                hold-time 15;
96                priority 100;
97                hello-authentication-key "$9$secret"; ## SECRET-DATA
98                hello-authentication-type md5;
99            }
100            level 2 metric 10;
101        }
102        interface ge-0/0/1.0 {
103            level 2 {
104                te-metric 30;
105                ipv4-multicast-metric 7;
106                ipv6-unicast-metric 40;
107                hello-authentication-key-chain ISIS-KEYS;
108                passive;
109            }
110        }
111    }
112}
113"#;
114
115    #[test]
116    fn reads_the_one_line_forms() {
117        let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
118        let disabled = &isis.interfaces[0].levels[0];
119        assert_eq!(disabled.level, "1");
120        assert!(disabled.disable);
121
122        let metric = &isis.interfaces[1].levels[1];
123        assert_eq!(metric.level, "2");
124        assert_eq!(metric.metric.as_deref(), Some("10"));
125        assert!(!metric.disable);
126    }
127
128    #[test]
129    fn reads_the_block_form_without_the_key() {
130        let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
131        let level = &isis.interfaces[1].levels[0];
132        assert_eq!(level.metric.as_deref(), Some("20"));
133        assert_eq!(level.hello_interval.as_deref(), Some("5"));
134        assert_eq!(level.hold_time.as_deref(), Some("15"));
135        assert_eq!(level.priority.as_deref(), Some("100"));
136        assert_eq!(level.hello_authentication_type.as_deref(), Some("md5"));
137        assert!(level.has_hello_authentication_key);
138        assert_eq!(level.hello_authentication_key_chain, None);
139    }
140
141    #[test]
142    fn reads_the_remaining_metrics() {
143        let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
144        let level = &isis.interfaces[2].levels[0];
145        assert_eq!(level.te_metric.as_deref(), Some("30"));
146        assert_eq!(level.ipv4_multicast_metric.as_deref(), Some("7"));
147        assert_eq!(level.ipv6_unicast_metric.as_deref(), Some("40"));
148        assert_eq!(level.ipv6_multicast_metric, None);
149        assert_eq!(level.hello_authentication_key_chain.as_deref(), Some("ISIS-KEYS"));
150        assert!(level.passive);
151        assert!(!level.has_hello_authentication_key);
152    }
153}