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 read only with the `secrets` feature.
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    /// The `hello-authentication-key`; `None` unless the `secrets` feature is enabled.
45    pub hello_authentication_key: Option<crate::Secret>,
46    pub hello_authentication_key_chain: Option<String>,
47    /// The `srv6-adjacency-segment` locators of the level and of its `lan-neighbor` entries.
48    pub srv6_adjacency_segments: Vec<IsisSrv6AdjacencySegment>,
49}
50
51/// `arg` is the text after `level`, e.g. `1 disable` or `2`.
52pub(super) fn parse_level(arg: &str, level: &ConfigLine) -> IsisInterfaceLevel {
53    let (number, rest) = split_name(arg);
54    let words: Vec<&str> = rest
55        .split_whitespace()
56        .chain(active_children(level).flat_map(|child| child.text.split_whitespace()))
57        .collect();
58    IsisInterfaceLevel {
59        level: number.to_string(),
60        disable: words.contains(&"disable"),
61        passive: words.contains(&"passive"),
62        metric: value_after(&words, "metric"),
63        hello_interval: value_after(&words, "hello-interval"),
64        hold_time: value_after(&words, "hold-time"),
65        priority: value_after(&words, "priority"),
66        te_metric: value_after(&words, "te-metric"),
67        ipv4_multicast_metric: value_after(&words, "ipv4-multicast-metric"),
68        ipv6_multicast_metric: value_after(&words, "ipv6-multicast-metric"),
69        ipv6_unicast_metric: value_after(&words, "ipv6-unicast-metric"),
70        hello_authentication_type: value_after(&words, "hello-authentication-type"),
71        has_hello_authentication_key: words.contains(&"hello-authentication-key"),
72        hello_authentication_key: crate::secret_value(value_after(&words, "hello-authentication-key").as_deref()),
73        hello_authentication_key_chain: value_after(&words, "hello-authentication-key-chain"),
74        srv6_adjacency_segments: {
75            let mut segments = parse_adjacency_segments(level, None);
76            segments.extend(parse_lan_neighbor_segments(level));
77            segments
78        },
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use crate::protocols::isis::isis;
85    use asterust::config::juniper;
86
87    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
88    const CONFIG: &str = r#"
89protocols {
90    isis {
91        interface fe-1/2/0.0 {
92            level 1 disable;
93        }
94        interface ge-0/0/0.0 {
95            level 1 {
96                metric 20;
97                hello-interval 5;
98                hold-time 15;
99                priority 100;
100                hello-authentication-key "$9$secret"; ## SECRET-DATA
101                hello-authentication-type md5;
102            }
103            level 2 metric 10;
104        }
105        interface ge-0/0/1.0 {
106            level 2 {
107                te-metric 30;
108                ipv4-multicast-metric 7;
109                ipv6-unicast-metric 40;
110                hello-authentication-key-chain ISIS-KEYS;
111                passive;
112            }
113        }
114    }
115}
116"#;
117
118    #[test]
119    fn reads_the_one_line_forms() {
120        let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
121        let disabled = &isis.interfaces[0].levels[0];
122        assert_eq!(disabled.level, "1");
123        assert!(disabled.disable);
124
125        let metric = &isis.interfaces[1].levels[1];
126        assert_eq!(metric.level, "2");
127        assert_eq!(metric.metric.as_deref(), Some("10"));
128        assert!(!metric.disable);
129    }
130
131    #[test]
132    fn reads_the_block_form_without_the_key() {
133        let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
134        let level = &isis.interfaces[1].levels[0];
135        assert_eq!(level.metric.as_deref(), Some("20"));
136        assert_eq!(level.hello_interval.as_deref(), Some("5"));
137        assert_eq!(level.hold_time.as_deref(), Some("15"));
138        assert_eq!(level.priority.as_deref(), Some("100"));
139        assert_eq!(level.hello_authentication_type.as_deref(), Some("md5"));
140        assert!(level.has_hello_authentication_key);
141        assert_eq!(
142            level.hello_authentication_key.as_ref().map(crate::Secret::expose),
143            crate::secret::expected("$9$secret")
144        );
145        assert_eq!(level.hello_authentication_key_chain, None);
146    }
147
148    #[test]
149    fn reads_the_remaining_metrics() {
150        let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
151        let level = &isis.interfaces[2].levels[0];
152        assert_eq!(level.te_metric.as_deref(), Some("30"));
153        assert_eq!(level.ipv4_multicast_metric.as_deref(), Some("7"));
154        assert_eq!(level.ipv6_unicast_metric.as_deref(), Some("40"));
155        assert_eq!(level.ipv6_multicast_metric, None);
156        assert_eq!(level.hello_authentication_key_chain.as_deref(), Some("ISIS-KEYS"));
157        assert!(level.passive);
158        assert!(!level.has_hello_authentication_key);
159    }
160}