asterust_junos/protocols/isis/interface/
level.rs1use 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#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct IsisInterfaceLevel {
26 pub level: String,
28 pub disable: bool,
29 pub passive: bool,
30 pub metric: Option<String>,
31 pub hello_interval: Option<String>,
33 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 pub hello_authentication_type: Option<String>,
42 pub has_hello_authentication_key: bool,
44 pub hello_authentication_key: Option<crate::Secret>,
46 pub hello_authentication_key_chain: Option<String>,
47 pub srv6_adjacency_segments: Vec<IsisSrv6AdjacencySegment>,
49}
50
51pub(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 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}