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_chain: Option<String>,
45 pub srv6_adjacency_segments: Vec<IsisSrv6AdjacencySegment>,
47}
48
49pub(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 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}