asterust_junos/protocols/isis/level.rs
1//! `level`
2//! `protocols { isis { level <1|2> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [level (Global IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/level-edit-protocols-isis.html)
7//! - [prefix-export-limit (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/prefix-export-limit-edit-protocols-isis.html)
8//! - [max-lsp-size](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/max-lsp-size-edit-protocols-isis.html)
9//! - [wide-metrics-only](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/wide-metrics-only-edit-protocols-isis.html), from the Junos 12.3 archive
10//! - [authentication-key (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/authentication-key-edit-protocols-isis.html), from the Junos 12.3 archive
11//! - [authentication-type (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/authentication-type-edit-protocols-isis.html), from the Junos 12.3 archive
12//! - [authentication-key-chain (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/authentication-key-chain-edit-protocols-isis.html), from the Junos 12.3 archive
13//! - [no-authentication-check](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/no-authentication-check-edit-protocols-isis.html), from the Junos 12.3 archive
14//! - [no-csnp-authentication](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/no-csnp-authentication-edit-protocols-isis.html), from the Junos 12.3 archive
15//! - [no-psnp-authentication](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/no-psnp-authentication-edit-protocols-isis.html), from the Junos 12.3 archive
16//! - [no-hello-authentication](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/no-hello-authentication-edit-protocols-isis.html), from the Junos 12.3 archive
17//! - [preference (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/preference-edit-protocols-isis.html), from the Junos 12.3 archive
18//! - [external-preference (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/external-preference-edit-protocols-isis.html), from the Junos 12.3 archive
19//! - [disable (Protocols IS-IS)](https://networkingdocs.hpe.com/documentation/en_US/junos12.3/topics/reference/configuration-statement/disable-edit-protocols-isis.html), from the Junos 12.3 archive
20
21use crate::{active_children, split_name, value_after};
22use asterust::config::ConfigLine;
23
24/// A `level <1|2>` entry directly under `isis`, written `level 2 wide-metrics-only;` or as a block. The
25/// `authentication-key` is never extracted.
26#[derive(Clone, Debug, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct IsisLevel {
29 /// `1` or `2`.
30 pub level: String,
31 pub disable: bool,
32 pub wide_metrics_only: bool,
33 pub preference: Option<String>,
34 pub external_preference: Option<String>,
35 pub prefix_export_limit: Option<String>,
36 pub max_lsp_size: Option<String>,
37 /// `md5` or `simple`.
38 pub authentication_type: Option<String>,
39 /// An `authentication-key` is configured.
40 pub has_authentication_key: bool,
41 pub authentication_key_chain: Option<String>,
42 pub no_authentication_check: bool,
43 pub no_csnp_authentication: bool,
44 pub no_hello_authentication: bool,
45 pub no_psnp_authentication: bool,
46 /// `empty` or `self`.
47 pub purge_originator: Option<String>,
48}
49
50/// `arg` is the text after `level`, e.g. `2 wide-metrics-only` or `1`.
51pub(super) fn parse_level(arg: &str, level: &ConfigLine) -> IsisLevel {
52 let (number, rest) = split_name(arg);
53 let words: Vec<&str> = rest
54 .split_whitespace()
55 .chain(active_children(level).flat_map(|child| child.text.split_whitespace()))
56 .collect();
57 IsisLevel {
58 level: number.to_string(),
59 disable: words.contains(&"disable"),
60 wide_metrics_only: words.contains(&"wide-metrics-only"),
61 preference: value_after(&words, "preference"),
62 external_preference: value_after(&words, "external-preference"),
63 prefix_export_limit: value_after(&words, "prefix-export-limit"),
64 max_lsp_size: value_after(&words, "max-lsp-size"),
65 authentication_type: value_after(&words, "authentication-type"),
66 has_authentication_key: words.contains(&"authentication-key"),
67 authentication_key_chain: value_after(&words, "authentication-key-chain"),
68 no_authentication_check: words.contains(&"no-authentication-check"),
69 no_csnp_authentication: words.contains(&"no-csnp-authentication"),
70 no_hello_authentication: words.contains(&"no-hello-authentication"),
71 no_psnp_authentication: words.contains(&"no-psnp-authentication"),
72 purge_originator: value_after(&words, "purge-originator"),
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use crate::protocols::isis::isis;
79 use asterust::config::juniper;
80
81 // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
82 const CONFIG: &str = r#"
83protocols {
84 isis {
85 level 1 disable;
86 level 2 wide-metrics-only;
87 level 2 {
88 preference 20;
89 external-preference 170;
90 prefix-export-limit 500;
91 max-lsp-size 1492;
92 authentication-key "$9$secret"; ## SECRET-DATA
93 authentication-type md5;
94 no-hello-authentication;
95 purge-originator self;
96 }
97 }
98}
99"#;
100
101 #[test]
102 fn reads_the_one_line_forms() {
103 let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
104 assert!(isis.levels[0].disable);
105 assert_eq!(isis.levels[0].level, "1");
106 assert!(isis.levels[1].wide_metrics_only);
107 assert!(!isis.levels[1].disable);
108 }
109
110 #[test]
111 fn reads_the_block_form_without_the_key() {
112 let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
113 let level = &isis.levels[2];
114 assert_eq!(level.preference.as_deref(), Some("20"));
115 assert_eq!(level.external_preference.as_deref(), Some("170"));
116 assert_eq!(level.prefix_export_limit.as_deref(), Some("500"));
117 assert_eq!(level.max_lsp_size.as_deref(), Some("1492"));
118 assert_eq!(level.authentication_type.as_deref(), Some("md5"));
119 assert!(level.has_authentication_key);
120 assert_eq!(level.authentication_key_chain, None);
121 assert!(level.no_hello_authentication);
122 assert!(!level.no_csnp_authentication);
123 assert_eq!(level.purge_originator.as_deref(), Some("self"));
124 }
125}