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 read only with the `secrets` feature.
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 /// The `authentication-key`; `None` unless the `secrets` feature is enabled.
42 pub authentication_key: Option<crate::Secret>,
43 pub authentication_key_chain: Option<String>,
44 pub no_authentication_check: bool,
45 pub no_csnp_authentication: bool,
46 pub no_hello_authentication: bool,
47 pub no_psnp_authentication: bool,
48 /// `empty` or `self`.
49 pub purge_originator: Option<String>,
50}
51
52/// `arg` is the text after `level`, e.g. `2 wide-metrics-only` or `1`.
53pub(super) fn parse_level(arg: &str, level: &ConfigLine) -> IsisLevel {
54 let (number, rest) = split_name(arg);
55 let words: Vec<&str> = rest
56 .split_whitespace()
57 .chain(active_children(level).flat_map(|child| child.text.split_whitespace()))
58 .collect();
59 IsisLevel {
60 level: number.to_string(),
61 disable: words.contains(&"disable"),
62 wide_metrics_only: words.contains(&"wide-metrics-only"),
63 preference: value_after(&words, "preference"),
64 external_preference: value_after(&words, "external-preference"),
65 prefix_export_limit: value_after(&words, "prefix-export-limit"),
66 max_lsp_size: value_after(&words, "max-lsp-size"),
67 authentication_type: value_after(&words, "authentication-type"),
68 has_authentication_key: words.contains(&"authentication-key"),
69 authentication_key: crate::secret_value(value_after(&words, "authentication-key").as_deref()),
70 authentication_key_chain: value_after(&words, "authentication-key-chain"),
71 no_authentication_check: words.contains(&"no-authentication-check"),
72 no_csnp_authentication: words.contains(&"no-csnp-authentication"),
73 no_hello_authentication: words.contains(&"no-hello-authentication"),
74 no_psnp_authentication: words.contains(&"no-psnp-authentication"),
75 purge_originator: value_after(&words, "purge-originator"),
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 level 1 disable;
89 level 2 wide-metrics-only;
90 level 2 {
91 preference 20;
92 external-preference 170;
93 prefix-export-limit 500;
94 max-lsp-size 1492;
95 authentication-key "$9$secret"; ## SECRET-DATA
96 authentication-type md5;
97 no-hello-authentication;
98 purge-originator self;
99 }
100 }
101}
102"#;
103
104 #[test]
105 fn reads_the_one_line_forms() {
106 let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
107 assert!(isis.levels[0].disable);
108 assert_eq!(isis.levels[0].level, "1");
109 assert!(isis.levels[1].wide_metrics_only);
110 assert!(!isis.levels[1].disable);
111 }
112
113 #[test]
114 fn reads_the_block_form_without_the_key() {
115 let isis = isis(&juniper::parse_braces(CONFIG)).unwrap();
116 let level = &isis.levels[2];
117 assert_eq!(level.preference.as_deref(), Some("20"));
118 assert_eq!(level.external_preference.as_deref(), Some("170"));
119 assert_eq!(level.prefix_export_limit.as_deref(), Some("500"));
120 assert_eq!(level.max_lsp_size.as_deref(), Some("1492"));
121 assert_eq!(level.authentication_type.as_deref(), Some("md5"));
122 assert!(level.has_authentication_key);
123 assert_eq!(
124 level.authentication_key.as_ref().map(crate::Secret::expose),
125 crate::secret::expected("$9$secret")
126 );
127 assert_eq!(level.authentication_key_chain, None);
128 assert!(level.no_hello_authentication);
129 assert!(!level.no_csnp_authentication);
130 assert_eq!(level.purge_originator.as_deref(), Some("self"));
131 }
132}