asterust_junos/routing_instances/
mod.rs1use crate::protocols::bgp::{parse_srv6, BgpSrv6};
14use crate::protocols::l2vpn::{parse_l2vpn, L2vpn};
15use crate::protocols::lldp::{parse_lldp, Lldp};
16use crate::{active_children, arg_of, args_of, child_block, list_values, top_level};
17use asterust::config::{Config, ConfigLine};
18
19#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct RoutingInstance {
24 pub name: String,
25 pub instance_type: Option<String>,
27 pub description: Option<String>,
28 pub route_distinguisher: Option<String>,
29 pub interfaces: Vec<String>,
30 pub vrf_target_import: Option<String>,
32 pub vrf_target_export: Option<String>,
33 pub vrf_import_policies: Vec<String>,
35 pub vrf_export_policies: Vec<String>,
36 pub bgp_srv6: Option<BgpSrv6>,
38 pub l2vpn: Option<L2vpn>,
40 pub lldp: Option<Lldp>,
42}
43
44impl RoutingInstance {
45 pub fn is_vrf(&self) -> bool {
47 self.instance_type.as_deref() == Some("vrf")
48 }
49}
50
51pub fn routing_instances(config: &Config) -> Vec<RoutingInstance> {
65 let Some(root) = top_level(config, "routing-instances") else {
66 return Vec::new();
67 };
68 active_children(root)
70 .filter(|c| !c.text.contains(' '))
71 .map(parse_instance)
72 .collect()
73}
74
75fn parse_instance(line: &ConfigLine) -> RoutingInstance {
76 let target_both = arg_of(line, "vrf-target");
77 let target_block = child_block(line, "vrf-target");
78 let target = |direction: &str| -> Option<String> {
79 target_both
80 .or_else(|| target_block.and_then(|b| arg_of(b, direction)))
81 .map(str::to_string)
82 };
83 let policies =
84 |keyword: &str| -> Vec<String> { args_of(line, keyword).into_iter().flat_map(list_values).collect() };
85
86 RoutingInstance {
87 name: line.text.clone(),
88 instance_type: arg_of(line, "instance-type").map(str::to_string),
89 description: arg_of(line, "description").map(str::to_string),
90 route_distinguisher: arg_of(line, "route-distinguisher").map(str::to_string),
91 interfaces: args_of(line, "interface").into_iter().map(str::to_string).collect(),
92 vrf_target_import: target("import"),
93 vrf_target_export: target("export"),
94 vrf_import_policies: policies("vrf-import"),
95 vrf_export_policies: policies("vrf-export"),
96 bgp_srv6: child_block(line, "protocols")
97 .and_then(|protocols| child_block(protocols, "bgp"))
98 .and_then(parse_srv6),
99 l2vpn: child_block(line, "protocols").and_then(parse_l2vpn),
100 lldp: child_block(line, "protocols")
101 .and_then(|protocols| child_block(protocols, "lldp"))
102 .map(parse_lldp),
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109 use asterust::config::juniper;
110
111 const CONFIG: &str = r#"
113routing-instances {
114 CUSTOMER-A {
115 instance-type vrf;
116 interface ge-0/0/1.100;
117 route-distinguisher 65000:1;
118 vrf-target target:65000:1;
119 }
120 CUSTOMER-B {
121 description "Customer B";
122 instance-type vrf;
123 interface ge-0/0/2.0;
124 interface ge-0/0/3.0;
125 route-distinguisher 65000:2;
126 vrf-import CUSTB-IN;
127 vrf-export [ CUSTB-OUT TAG-ROUTES ];
128 }
129 CUSTOMER-C {
130 instance-type vrf;
131 route-distinguisher 65000:3;
132 vrf-target {
133 import target:65000:3;
134 export target:65000:33;
135 }
136 }
137 MGMT {
138 instance-type virtual-router;
139 }
140}
141"#;
142
143 #[test]
145 fn reads_lldp_inside_an_instance() {
146 let instances = routing_instances(&juniper::parse_braces(
147 r#"
148routing-instances {
149 MGMT {
150 instance-type virtual-router;
151 protocols {
152 lldp {
153 advertisement-interval 20;
154 interface [ ge-0/0/4 ge-0/0/5 ];
155 traceoptions {
156 flag all;
157 }
158 }
159 }
160 }
161 PLAIN {
162 instance-type virtual-router;
163 }
164}
165"#,
166 ));
167 let lldp = instances[0].lldp.as_ref().unwrap();
168 assert_eq!(lldp.advertisement_interval.as_deref(), Some("20"));
169 assert_eq!(lldp.interfaces.len(), 2);
170 assert_eq!(lldp.interfaces[1].name, "ge-0/0/5");
171 assert_eq!(lldp.traceoptions.as_ref().unwrap().flags[0].name, "all");
172 assert!(instances[1].lldp.is_none());
173 }
174
175 #[test]
176 fn vrf_target_in_both_directions() {
177 let instances = routing_instances(&juniper::parse_braces(CONFIG));
178 assert_eq!(instances.len(), 4);
179
180 let a = &instances[0];
181 assert_eq!(a.name, "CUSTOMER-A");
182 assert!(a.is_vrf());
183 assert_eq!(a.route_distinguisher.as_deref(), Some("65000:1"));
184 assert_eq!(a.interfaces, ["ge-0/0/1.100"]);
185 assert_eq!(a.vrf_target_import.as_deref(), Some("target:65000:1"));
186 assert_eq!(a.vrf_target_export.as_deref(), Some("target:65000:1"));
187 }
188
189 #[test]
190 fn policies_target_block_and_non_vrf_instance() {
191 let instances = routing_instances(&juniper::parse_braces(CONFIG));
192
193 let b = &instances[1];
194 assert_eq!(b.description.as_deref(), Some("Customer B"));
195 assert_eq!(b.interfaces, ["ge-0/0/2.0", "ge-0/0/3.0"]);
196 assert_eq!(b.vrf_import_policies, ["CUSTB-IN"]);
197 assert_eq!(b.vrf_export_policies, ["CUSTB-OUT", "TAG-ROUTES"]);
198 assert_eq!(b.vrf_target_import, None);
199
200 let c = &instances[2];
201 assert_eq!(c.vrf_target_import.as_deref(), Some("target:65000:3"));
202 assert_eq!(c.vrf_target_export.as_deref(), Some("target:65000:33"));
203
204 assert!(!instances[3].is_vrf());
205 }
206}