asterust-junos 0.5.14

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! `rsvp`
//! `protocols { rsvp { ... } }`.
//!
//! # See also
//!
//! - [`[edit protocols rsvp]` hierarchy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/topic-map/hierarchy-edit-protocols.html)
//! - [graceful-deletion-timeout](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/graceful-deletion-timeout-edit-protocols-rsvp.html)
//! - [rsvp (with associated-bidirectional-lsp)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rsvp-edit-protocols-mpls-ex-series.html)
//! - [keep-multiplier](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/keep-multiplier-edit-protocols-rsvp.html)
//! - [refresh-time](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/refresh-time-edit-protocols-rsvp.html)
//! - [node-hello](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/node-hello-edit-protocols-rsvp.html)
//! - [no-enhanced-frr-bypass (Protocols RSVP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-enhanced-frr-bypass-edit-protocols-rsvp.html)
//! - [load-balance (Protocols RSVP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/load-balance-edit-protocols-rsvp.html)
//! - [preemption](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/preemption-edit-protocols-rsvp.html)
//! - [soft-preemption (Protocols RSVP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/soft-preemption-edit-protocols-rsvp.html)
//! - [graceful-restart (Protocols RSVP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/graceful-restart-edit-protocols-rsvp.html)
//! - [disable (Protocols RSVP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/disable-edit-protocols-rsvp.html)
//! - [traceoptions (Protocols RSVP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-protocols-rsvp.html)
//! - [Basic MPLS Configuration](https://networkingdocs.hpe.com/documentation/us/en/software/junos/mpls/topics/topic-map/mpls-basic-configuration.html)
//! - [Link-State Distribution Using BGP](https://networkingdocs.hpe.com/documentation/us/en/software/junos/bgp/topics/topic-map/link-state-distribution-using-bgp.html)

mod interface;
mod link_protection;
mod peer_interface;

pub use interface::RsvpInterface;
pub use link_protection::{RsvpBypass, RsvpLinkProtection};
pub use peer_interface::RsvpPeerInterface;

use super::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{arg_of, child_block, has_leaf, named_children, statement_words, toggle, top_level};
use asterust::config::{Config, ConfigLine};
use interface::parse_interface;
use peer_interface::parse_peer_interface;

/// The `preemption` setting.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RsvpPreemption {
    /// `aggressive`, `disabled` or `normal`.
    pub mode: Option<String>,
    /// Seconds, from `soft-preemption { cleanup-timer ...; }`.
    pub soft_preemption_cleanup_timer: Option<String>,
}

/// The `graceful-restart` options.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RsvpGracefulRestart {
    pub disable: bool,
    pub helper_disable: bool,
    /// Seconds.
    pub maximum_helper_recovery_time: Option<String>,
    /// Seconds.
    pub maximum_helper_restart_time: Option<String>,
}

/// The `protocols rsvp` configuration. The `no-cspf`, `bandwidth`-model and `update-threshold`
/// statements outside interfaces are not extracted.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Rsvp {
    pub disable: bool,
    pub interfaces: Vec<RsvpInterface>,
    pub peer_interfaces: Vec<RsvpPeerInterface>,
    /// `associated-bidirectional-lsp single-sided-provisioning`.
    pub associated_bidirectional_lsp_single_sided_provisioning: bool,
    /// Seconds.
    pub graceful_deletion_timeout: Option<String>,
    pub keep_multiplier: Option<String>,
    /// Seconds.
    pub refresh_time: Option<String>,
    /// `Some(true)` for `node-hello`, `Some(false)` for `no-node-hello`.
    pub node_hello: Option<bool>,
    pub no_enhanced_frr_bypass: bool,
    /// `load-balance { bandwidth; }`.
    pub load_balance_bandwidth: bool,
    pub preemption: Option<RsvpPreemption>,
    /// Seconds, from the `soft-preemption` statement directly under `rsvp`.
    pub soft_preemption_cleanup_timer: Option<String>,
    pub graceful_restart: Option<RsvpGracefulRestart>,
    pub traceoptions: Option<Traceoptions>,
}

/// Returns the `protocols rsvp` block, or `None` if RSVP is not configured.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::protocols::rsvp::rsvp;
///
/// let config = juniper::parse_braces("protocols {\n    rsvp {\n        interface all;\n        interface fxp0.0 {\n            disable;\n        }\n    }\n}\n");
/// let rsvp = rsvp(&config).unwrap();
/// assert_eq!(rsvp.interfaces[0].name, "all");
/// assert!(rsvp.interfaces[1].disable);
/// ```
pub fn rsvp(config: &Config) -> Option<Rsvp> {
    let rsvp = child_block(top_level(config, "protocols")?, "rsvp")?;
    let field = |keyword: &str| arg_of(rsvp, keyword).map(str::to_string);
    Some(Rsvp {
        disable: has_leaf(rsvp, "disable"),
        interfaces: named_children(rsvp, "interface")
            .map(|(name, interface)| parse_interface(name, interface))
            .collect(),
        peer_interfaces: named_children(rsvp, "peer-interface")
            .map(|(name, peer)| parse_peer_interface(name, peer))
            .collect(),
        associated_bidirectional_lsp_single_sided_provisioning: statement_words(rsvp, "associated-bidirectional-lsp")
            .is_some_and(|words| words.contains(&"single-sided-provisioning")),
        graceful_deletion_timeout: field("graceful-deletion-timeout"),
        keep_multiplier: field("keep-multiplier"),
        refresh_time: field("refresh-time"),
        node_hello: toggle(rsvp, "node-hello"),
        no_enhanced_frr_bypass: has_leaf(rsvp, "no-enhanced-frr-bypass"),
        load_balance_bandwidth: statement_words(rsvp, "load-balance").is_some_and(|words| words.contains(&"bandwidth")),
        preemption: parse_preemption(rsvp),
        soft_preemption_cleanup_timer: child_block(rsvp, "soft-preemption")
            .and_then(|block| arg_of(block, "cleanup-timer"))
            .map(str::to_string),
        graceful_restart: parse_graceful_restart(rsvp),
        traceoptions: parse_traceoptions(rsvp),
    })
}

fn parse_preemption(rsvp: &ConfigLine) -> Option<RsvpPreemption> {
    let words = statement_words(rsvp, "preemption")?;
    Some(RsvpPreemption {
        mode: ["aggressive", "disabled", "normal"]
            .into_iter()
            .find(|mode| words.contains(mode))
            .map(str::to_string),
        soft_preemption_cleanup_timer: child_block(rsvp, "preemption")
            .and_then(|block| child_block(block, "soft-preemption"))
            .and_then(|soft| arg_of(soft, "cleanup-timer"))
            .map(str::to_string),
    })
}

fn parse_graceful_restart(rsvp: &ConfigLine) -> Option<RsvpGracefulRestart> {
    let block = child_block(rsvp, "graceful-restart")?;
    Some(RsvpGracefulRestart {
        disable: has_leaf(block, "disable"),
        helper_disable: has_leaf(block, "helper-disable"),
        maximum_helper_recovery_time: arg_of(block, "maximum-helper-recovery-time").map(str::to_string),
        maximum_helper_restart_time: arg_of(block, "maximum-helper-restart-time").map(str::to_string),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use asterust::config::juniper;

    // Router R1 `show protocols` (RSVP part) of "Example: Configuring Link State Distribution Using BGP" (BGP User Guide)
    // and the provider switch of "Example: Configuring MPLS on EX8200 and EX4500 Switches" (MPLS Applications User
    // Guide, Basic MPLS Configuration); see the README "Sources".
    const DOC_RSVP: &str = r#"
protocols {
    rsvp {
        interface all;
        interface fxp0.0 {
            disable;
        }
    }
}
"#;

    const DOC_RSVP_PROVIDER: &str = r#"
protocols {
    rsvp {
        interface lo0.0;
        interface ge-0/0/5.0;
        interface ge-0/0/6.0;
        interface ae0.0;
    }
}
"#;

    // Not from documentation: written from the CLI Reference pages of the statements. See the README "Sources".
    const EXTRA: &str = r#"
protocols {
    rsvp {
        keep-multiplier 3;
        refresh-time 600;
        no-node-hello;
        no-enhanced-frr-bypass;
        load-balance {
            bandwidth;
        }
        preemption {
            aggressive;
            soft-preemption {
                cleanup-timer 60;
            }
        }
        soft-preemption {
            cleanup-timer 30;
        }
        graceful-restart {
            helper-disable;
            maximum-helper-recovery-time 120;
            maximum-helper-restart-time 180;
        }
        traceoptions {
            file rsvp.log size 1m;
            flag error;
        }
        interface ge-0/0/0.0 {
            no-aggregate;
            authentication-key "$9$secret"; ## SECRET-DATA
            bandwidth 800m;
            hello-interval 5;
            reliable;
            subscription 80;
            update-threshold 5;
            link-protection {
                admin-group {
                    exclude red;
                    include-any [ gold silver ];
                }
                bandwidth 100m;
                class-of-service 3;
                hop-limit 6;
                max-bypasses 2;
                no-cspf;
                no-node-protection;
                optimize-timer 300;
                path 10.0.0.9 strict;
                priority 5 5;
                subscription 90;
                bypass byp1 {
                    bandwidth 50m;
                    description "around R2";
                    class-of-service 2;
                    hop-limit 4;
                    no-cspf;
                    path 10.0.0.5 loose;
                    path 10.0.0.6 strict;
                    priority 6 6;
                    to 10.255.0.2;
                }
            }
        }
        interface ge-0/0/1.0 {
            link-protection {
                disable;
            }
        }
        interface ge-0/0/2.0 {
            link-protection;
        }
    }
}
"#;

    // Adapted from the MPLS Applications User Guide, GMPLS Configuration, Example: Configuring GMPLS RSVP-TE VLAN LSP
    // Signaling (the `rsvp` block of the Results of Router PE1 and of Router CE1); see the README "Sources".
    #[test]
    fn reads_the_guide_peer_interfaces() {
        let server = rsvp(&juniper::parse_braces(
            r#"
protocols {
    rsvp {
        associated-bidirectional-lsp single-sided-provisioning;
        interface all;
        interface fxp0.0 {
            disable;
        }
        peer-interface CE1 {
            dynamic-bidirectional-transport;
        }
    }
}
"#,
        ))
        .unwrap();
        assert!(server.associated_bidirectional_lsp_single_sided_provisioning);
        assert_eq!(server.peer_interfaces.len(), 1);
        assert_eq!(server.peer_interfaces[0].name, "CE1");
        assert!(server.peer_interfaces[0].dynamic_bidirectional_transport);
        assert!(server.peer_interfaces[0]
            .dynamic_bidirectional_transport_template
            .is_none());

        let client = rsvp(&juniper::parse_braces(
            "protocols {\n    rsvp {\n        interface all;\n        peer-interface PE1;\n    }\n}\n",
        ))
        .unwrap();
        assert!(!client.associated_bidirectional_lsp_single_sided_provisioning);
        assert_eq!(client.peer_interfaces[0].name, "PE1");
        assert!(!client.peer_interfaces[0].dynamic_bidirectional_transport);
    }

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn reads_the_peer_interface_options() {
        let rsvp = rsvp(&juniper::parse_braces(
            r#"
protocols {
    rsvp {
        graceful-deletion-timeout 60;
        peer-interface P1 {
            disable;
            no-aggregate;
            authentication-key "$9$peer";
            dynamic-bidirectional-transport {
                template T1;
            }
            hello-interval 9;
            reliable;
        }
        peer-interface P2 {
            aggregate;
            no-reliable;
            dynamic-bidirectional-transport template T2;
        }
    }
}
"#,
        ))
        .unwrap();
        assert_eq!(rsvp.graceful_deletion_timeout.as_deref(), Some("60"));
        let first = &rsvp.peer_interfaces[0];
        assert!(first.disable && first.has_authentication_key);
        assert_eq!(first.aggregate, Some(false));
        assert_eq!(
            first.authentication_key.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$peer")
        );
        assert_eq!(first.dynamic_bidirectional_transport_template.as_deref(), Some("T1"));
        assert_eq!(first.hello_interval.as_deref(), Some("9"));
        assert_eq!(first.reliable, Some(true));
        let second = &rsvp.peer_interfaces[1];
        assert_eq!(second.aggregate, Some(true));
        assert_eq!(second.reliable, Some(false));
        assert_eq!(second.dynamic_bidirectional_transport_template.as_deref(), Some("T2"));
        assert!(!second.disable && !second.has_authentication_key && second.hello_interval.is_none());
    }

    #[test]
    fn reads_the_guide_interfaces() {
        let rsvp = rsvp(&juniper::parse_braces(DOC_RSVP)).unwrap();
        assert_eq!(rsvp.interfaces.len(), 2);
        assert_eq!(rsvp.interfaces[0].name, "all");
        assert!(!rsvp.interfaces[0].disable);
        assert_eq!(rsvp.interfaces[1].name, "fxp0.0");
        assert!(rsvp.interfaces[1].disable);
        assert!(!rsvp.disable);

        let provider = rsvp_of(DOC_RSVP_PROVIDER);
        let names: Vec<&str> = provider.interfaces.iter().map(|i| i.name.as_str()).collect();
        assert_eq!(names, ["lo0.0", "ge-0/0/5.0", "ge-0/0/6.0", "ae0.0"]);
    }

    fn rsvp_of(text: &str) -> Rsvp {
        rsvp(&juniper::parse_braces(text)).unwrap()
    }

    #[test]
    fn reads_the_global_options() {
        let rsvp = rsvp_of(EXTRA);
        assert_eq!(rsvp.keep_multiplier.as_deref(), Some("3"));
        assert_eq!(rsvp.refresh_time.as_deref(), Some("600"));
        assert_eq!(rsvp.node_hello, Some(false));
        assert!(rsvp.no_enhanced_frr_bypass);
        assert!(rsvp.load_balance_bandwidth);
        assert_eq!(rsvp.soft_preemption_cleanup_timer.as_deref(), Some("30"));

        let preemption = rsvp.preemption.unwrap();
        assert_eq!(preemption.mode.as_deref(), Some("aggressive"));
        assert_eq!(preemption.soft_preemption_cleanup_timer.as_deref(), Some("60"));

        let restart = rsvp.graceful_restart.unwrap();
        assert!(restart.helper_disable);
        assert!(!restart.disable);
        assert_eq!(restart.maximum_helper_recovery_time.as_deref(), Some("120"));
        assert_eq!(restart.maximum_helper_restart_time.as_deref(), Some("180"));

        let trace = rsvp.traceoptions.unwrap();
        assert_eq!(trace.file.as_deref(), Some("rsvp.log"));
        assert_eq!(trace.flags[0].name, "error");
    }

    #[test]
    fn reads_the_interface_options_without_the_key() {
        let rsvp = rsvp_of(EXTRA);
        let interface = &rsvp.interfaces[0];
        assert_eq!(interface.aggregate, Some(false));
        assert!(interface.has_authentication_key);
        assert_eq!(
            interface.authentication_key.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
        assert_eq!(interface.bandwidth.as_deref(), Some("800m"));
        assert_eq!(interface.hello_interval.as_deref(), Some("5"));
        assert_eq!(interface.reliable, Some(true));
        assert_eq!(interface.subscription.as_deref(), Some("80"));
        assert_eq!(interface.update_threshold.as_deref(), Some("5"));
        assert!(!interface.disable);
    }

    #[test]
    fn reads_link_protection_and_bypasses() {
        let rsvp = rsvp_of(EXTRA);
        let protection = rsvp.interfaces[0].link_protection.as_ref().unwrap();
        assert!(!protection.disable);
        assert_eq!(protection.admin_group.exclude, ["red"]);
        assert_eq!(protection.admin_group.include_any, ["gold", "silver"]);
        assert_eq!(protection.bandwidth.as_deref(), Some("100m"));
        assert_eq!(protection.class_of_service.as_deref(), Some("3"));
        assert_eq!(protection.hop_limit.as_deref(), Some("6"));
        assert_eq!(protection.max_bypasses.as_deref(), Some("2"));
        assert!(protection.no_cspf);
        assert!(protection.no_node_protection);
        assert_eq!(protection.optimize_timer.as_deref(), Some("300"));
        assert_eq!(protection.path, ["10.0.0.9 strict"]);
        assert_eq!(protection.priority.as_deref(), Some("5 5"));
        assert_eq!(protection.subscription.as_deref(), Some("90"));

        assert_eq!(protection.bypasses.len(), 1);
        let bypass = &protection.bypasses[0];
        assert_eq!(bypass.name, "byp1");
        assert_eq!(bypass.bandwidth.as_deref(), Some("50m"));
        assert_eq!(bypass.description.as_deref(), Some("around R2"));
        assert_eq!(bypass.class_of_service.as_deref(), Some("2"));
        assert_eq!(bypass.hop_limit.as_deref(), Some("4"));
        assert!(bypass.no_cspf);
        assert_eq!(bypass.path, ["10.0.0.5 loose", "10.0.0.6 strict"]);
        assert_eq!(bypass.priority.as_deref(), Some("6 6"));
        assert_eq!(bypass.to.as_deref(), Some("10.255.0.2"));
    }

    #[test]
    fn reads_the_link_protection_forms() {
        let rsvp = rsvp_of(EXTRA);
        assert!(rsvp.interfaces[1].link_protection.as_ref().unwrap().disable);
        assert_eq!(rsvp.interfaces[2].link_protection, Some(RsvpLinkProtection::default()));
        assert_eq!(rsvp_of(DOC_RSVP).interfaces[0].link_protection, None);
    }

    #[test]
    fn no_rsvp_is_none() {
        let config = juniper::parse_braces("protocols {\n    ospf {\n        area 0.0.0.0;\n    }\n}\n");
        assert_eq!(rsvp(&config), None);
    }
}