asterust_junos/protocols/ospf/
mod.rs1mod area;
23mod authentication;
24mod backup_spf_options;
25mod database_protection;
26mod graceful_restart;
27mod interface;
28mod overload;
29mod source_packet_routing;
30mod spf_options;
31mod topology;
32mod traffic_engineering;
33
34pub use area::{OspfArea, OspfAreaRange, OspfNssa, OspfStub, OspfVirtualLink};
35pub use authentication::{OspfAuthentication, OspfMd5Key};
36pub use interface::{OspfBfd, OspfInterface, OspfInterfaceTopology, OspfNeighbor, OspfPostConvergenceLfa};
37
38pub use backup_spf_options::OspfBackupSpfOptions;
39pub use database_protection::OspfDatabaseProtection;
40pub use graceful_restart::OspfGracefulRestart;
41pub use overload::OspfOverload;
42pub use source_packet_routing::OspfSourcePacketRouting;
43pub use spf_options::OspfSpfOptions;
44pub use topology::OspfTopology;
45pub use traffic_engineering::OspfTrafficEngineering;
46
47use crate::{arg_of, args_of, child_block, has_leaf, list_values, named_children, top_level};
48use area::parse_area;
49use asterust::config::Config;
50use backup_spf_options::parse_backup_spf_options;
51use database_protection::parse_database_protection;
52use graceful_restart::parse_graceful_restart;
53use overload::parse_overload;
54use source_packet_routing::parse_source_packet_routing;
55use spf_options::parse_spf_options;
56use topology::parse_topology;
57use traffic_engineering::parse_traffic_engineering;
58
59#[derive(Clone, Debug, PartialEq, Eq)]
61#[non_exhaustive]
62pub struct Ospf {
63 pub areas: Vec<OspfArea>,
64 pub disable: bool,
66 pub reference_bandwidth: Option<String>,
68 pub preference: Option<String>,
70 pub external_preference: Option<String>,
72 pub export_policies: Vec<String>,
73 pub import_policies: Vec<String>,
74 pub rib_group: Option<String>,
75 pub no_rfc_1583: bool,
76 pub no_nssa_abr: bool,
77 pub overload: Option<OspfOverload>,
78 pub spf_options: Option<OspfSpfOptions>,
79 pub graceful_restart: Option<OspfGracefulRestart>,
80 pub traffic_engineering: Option<OspfTrafficEngineering>,
81 pub database_protection: Option<OspfDatabaseProtection>,
82 pub backup_spf_options: Option<OspfBackupSpfOptions>,
83 pub source_packet_routing: Option<OspfSourcePacketRouting>,
84 pub topologies: Vec<OspfTopology>,
85 pub prefix_export_limit: Option<String>,
86 pub lsa_refresh_interval: Option<String>,
88 pub domain_id: Option<String>,
89 pub domain_vpn_tag: Option<String>,
90 pub route_type_community: Option<String>,
92 pub flex_algorithm_preference: Option<String>,
95}
96
97pub fn ospf(config: &Config) -> Option<Ospf> {
111 let ospf = child_block(top_level(config, "protocols")?, "ospf")?;
112 let areas = named_children(ospf, "area")
113 .map(|(id, area)| parse_area(id, area))
114 .collect();
115 let policies =
116 |keyword: &str| -> Vec<String> { args_of(ospf, keyword).into_iter().flat_map(list_values).collect() };
117 Some(Ospf {
118 areas,
119 disable: has_leaf(ospf, "disable"),
120 reference_bandwidth: arg_of(ospf, "reference-bandwidth").map(str::to_string),
121 preference: arg_of(ospf, "preference").map(str::to_string),
122 external_preference: arg_of(ospf, "external-preference").map(str::to_string),
123 export_policies: policies("export"),
124 import_policies: policies("import"),
125 rib_group: arg_of(ospf, "rib-group").map(str::to_string),
126 no_rfc_1583: has_leaf(ospf, "no-rfc-1583"),
127 no_nssa_abr: has_leaf(ospf, "no-nssa-abr"),
128 overload: parse_overload(ospf),
129 spf_options: parse_spf_options(ospf),
130 graceful_restart: parse_graceful_restart(ospf),
131 traffic_engineering: parse_traffic_engineering(ospf),
132 database_protection: parse_database_protection(ospf),
133 backup_spf_options: parse_backup_spf_options(ospf),
134 source_packet_routing: parse_source_packet_routing(ospf),
135 topologies: named_children(ospf, "topology")
136 .map(|(name, topology)| parse_topology(name, topology))
137 .collect(),
138 prefix_export_limit: arg_of(ospf, "prefix-export-limit").map(str::to_string),
139 lsa_refresh_interval: arg_of(ospf, "lsa-refresh-interval").map(str::to_string),
140 domain_id: arg_of(ospf, "domain-id").map(str::to_string),
141 domain_vpn_tag: arg_of(ospf, "domain-vpn-tag").map(str::to_string),
142 route_type_community: arg_of(ospf, "route-type-community").map(str::to_string),
143 flex_algorithm_preference: arg_of(ospf, "flex-algorithm-preference").map(str::to_string),
144 })
145}
146
147#[cfg(test)]
148mod tests {
149 use super::*;
150 use asterust::config::juniper;
151
152 const CONFIG: &str = r#"
154protocols {
155 ospf {
156 area 0.0.0.0 {
157 interface ge-0/0/0.0 {
158 metric 10;
159 }
160 interface lo0.0 {
161 passive;
162 }
163 }
164 area 0.0.0.10 {
165 interface ge-0/0/1.0;
166 }
167 }
168 ospf3 {
169 area 0.0.0.0 {
170 interface ge-0/0/0.0;
171 }
172 }
173}
174"#;
175
176 #[test]
177 fn extracts_areas_and_interfaces() {
178 let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
179 assert_eq!(ospf.areas.len(), 2);
180
181 let backbone = &ospf.areas[0];
182 assert_eq!(backbone.id, "0.0.0.0");
183 assert_eq!(backbone.interfaces.len(), 2);
184 assert_eq!(backbone.interfaces[0].name, "ge-0/0/0.0");
185 assert_eq!(backbone.interfaces[0].metric, Some(10));
186 assert!(!backbone.interfaces[0].passive);
187 assert!(backbone.interfaces[1].passive);
188
189 let area10 = &ospf.areas[1];
190 assert_eq!(area10.id, "0.0.0.10");
191 assert_eq!(area10.interfaces[0].name, "ge-0/0/1.0");
192 }
193
194 const GLOBAL: &str = r#"
196protocols {
197 ospf {
198 disable;
199 reference-bandwidth 100g;
200 preference 20;
201 external-preference 160;
202 export [ EXPORT-A EXPORT-B ];
203 import IMPORT-A;
204 rib-group RG-OSPF;
205 no-rfc-1583;
206 no-nssa-abr;
207 area 0.0.0.0;
208 }
209}
210"#;
211
212 #[test]
213 fn reads_the_global_options() {
214 let ospf = ospf(&juniper::parse_braces(GLOBAL)).unwrap();
215 assert!(ospf.disable);
216 assert_eq!(ospf.reference_bandwidth.as_deref(), Some("100g"));
217 assert_eq!(ospf.preference.as_deref(), Some("20"));
218 assert_eq!(ospf.external_preference.as_deref(), Some("160"));
219 assert_eq!(ospf.export_policies, ["EXPORT-A", "EXPORT-B"]);
220 assert_eq!(ospf.import_policies, ["IMPORT-A"]);
221 assert_eq!(ospf.rib_group.as_deref(), Some("RG-OSPF"));
222 assert!(ospf.no_rfc_1583);
223 assert!(ospf.no_nssa_abr);
224 }
225
226 #[test]
227 fn leaves_the_global_options_unset_when_absent() {
228 let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
229 assert!(!ospf.disable);
230 assert_eq!(ospf.reference_bandwidth, None);
231 assert!(ospf.export_policies.is_empty());
232 assert_eq!(ospf.overload, None);
233 assert_eq!(ospf.spf_options, None);
234 assert_eq!(ospf.graceful_restart, None);
235 }
236
237 const SIMPLE: &str = r#"
239protocols {
240 ospf {
241 prefix-export-limit 5000;
242 lsa-refresh-interval 30;
243 domain-id 10.0.0.1:5;
244 domain-vpn-tag 12345;
245 route-type-community vendor;
246 flex-algorithm-preference 20;
247 }
248}
249"#;
250
251 #[test]
252 fn reads_the_single_argument_options() {
253 let ospf = ospf(&juniper::parse_braces(SIMPLE)).unwrap();
254 assert_eq!(ospf.prefix_export_limit.as_deref(), Some("5000"));
255 assert_eq!(ospf.lsa_refresh_interval.as_deref(), Some("30"));
256 assert_eq!(ospf.domain_id.as_deref(), Some("10.0.0.1:5"));
257 assert_eq!(ospf.domain_vpn_tag.as_deref(), Some("12345"));
258 assert_eq!(ospf.route_type_community.as_deref(), Some("vendor"));
259 assert_eq!(ospf.flex_algorithm_preference.as_deref(), Some("20"));
260 assert!(ospf.topologies.is_empty());
261 assert_eq!(ospf.traffic_engineering, None);
262 assert_eq!(ospf.database_protection, None);
263 assert_eq!(ospf.backup_spf_options, None);
264 assert_eq!(ospf.source_packet_routing, None);
265 }
266
267 #[test]
268 fn no_ospf_is_none() {
269 assert!(ospf(&juniper::parse_braces("protocols {\n isis {\n }\n}\n")).is_none());
270 }
271
272 const DOC_OSPF: &str = r#"
275protocols {
276 ospf {
277 area 0.0.0.0 {
278 interface lo0.1 {
279 passive;
280 }
281 interface ge-0/1/0.1;
282 }
283 }
284}
285"#;
286
287 #[test]
288 fn doc_area_with_passive_loopback() {
289 let ospf = ospf(&juniper::parse_braces(DOC_OSPF)).unwrap();
290 assert_eq!(ospf.areas.len(), 1);
291 let area = &ospf.areas[0];
292 assert_eq!(area.id, "0.0.0.0");
293 assert_eq!(area.interfaces[0].name, "lo0.1");
294 assert!(area.interfaces[0].passive);
295 assert_eq!(area.interfaces[1].name, "ge-0/1/0.1");
296 assert!(!area.interfaces[1].passive);
297 }
298}