asterust-junos 0.3.17

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! `pcep`
//! `protocols { pcep { ... } }`.
//!
//! The `maximum-srv6-segment-list-depth` and `propagate-lsp-max-segment-list-depth` statements are written without a
//! semicolon on the `pcep` page of the CLI Reference, so they are read as a value and a flag.
//!
//! # See also
//!
//! - [pcep](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pcep-edit-protocols.html)
//! - [pce](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pce-edit-protocols.html)
//! - [pce-group (Protocols PCEP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pce-group-edit-protoocols-pcep.html)
//! - [pce-type](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pce-type-edit-protocols-pcep-pce.html)
//! - [delegation-cleanup-timeout](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delegation-cleanup-timeout-edit-protocols-pcep-pce.html)
//! - [delegation-priority](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delegation-priority-edit-protocols-pcep-pce.html)
//! - [destination-ipv4-address](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/destination-ipv4-address-edit-protocols-pcep-pce.html)
//! - [destination-port (protocols pcep)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/destination-port-edit-protocols-pcep-pce.html)
//! - [lsp-cleanup-timer](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/lsp-cleanup-timer-edit-protocols-pcep-pce.html)
//! - [max-unknown-messages](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/max-unknown-messages-edit-protocols-pcep-pce.html)
//! - [max-unknown-requests](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/max-unknown-requests-edit-protocols-pcep-pce.html)
//! - [message-rate-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/message-rate-limit-edit-protocols-pcep.html)
//! - [update-rate-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/update-rate-limit-edit-protocols-pcep.html)
//! - [traceoptions (PCEP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-protocols-pcep.html)
//! - [PCEP Configuration](https://networkingdocs.hpe.com/documentation/us/en/software/junos/mpls/topics/topic-map/pcep-configuration.html), for `max-provisioned-lsps`, `disable-multipath-capability` and
//!   `propagate-max-segmentlist`, which have no CLI Reference page

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

/// The `pce-type` of a PCE or PCE group, written `pce-type active stateful;` or as a block.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PcepPceType {
    pub active: bool,
    pub stateful: bool,
    pub srv6_capability: bool,
}

/// A `pce <address>` entry of PCEP. The `authentication-key` is read only with the `secrets` feature.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PcepPce {
    /// The address of the PCE.
    pub name: String,
    /// An `authentication-key` is configured.
    pub has_authentication_key: bool,
    /// The `authentication-key`; `None` unless the `secrets` feature is enabled.
    pub authentication_key: Option<crate::Secret>,
    pub authentication_key_chain: Option<String>,
    /// Seconds.
    pub delegation_cleanup_timeout: Option<String>,
    pub delegation_priority: Option<String>,
    pub destination_ipv4_address: Option<String>,
    pub destination_port: Option<String>,
    /// The address of the local end (the PCC).
    pub local_address: Option<String>,
    /// Seconds.
    pub lsp_cleanup_timer: Option<String>,
    pub lsp_provisioning: bool,
    pub lsp_retry_delegation: bool,
    /// Seconds.
    pub lsp_retry_delegation_timer: Option<String>,
    pub max_sid_depth: Option<String>,
    pub max_unknown_messages: Option<String>,
    pub max_unknown_requests: Option<String>,
    pub p2mp_lsp_init_capability: bool,
    pub p2mp_lsp_report_capability: bool,
    pub p2mp_lsp_update_capability: bool,
    pub pce_group: Option<String>,
    /// Written `pce_traffic_steering` with underscores.
    pub pce_traffic_steering: bool,
    pub pce_type: Option<PcepPceType>,
    pub request_priority: Option<String>,
    /// Seconds.
    pub request_timer: Option<String>,
    pub spring_capability: bool,
    pub srv6_capability: bool,
    pub traceoptions: Option<Traceoptions>,
}

/// A `pce-group <name>` entry of PCEP.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PcepPceGroup {
    pub name: String,
    /// Seconds.
    pub delegation_cleanup_timeout: Option<String>,
    /// Seconds.
    pub lsp_cleanup_timer: Option<String>,
    pub lsp_provisioning: bool,
    pub max_unknown_messages: Option<String>,
    pub max_unknown_requests: Option<String>,
    pub pce_type: Option<PcepPceType>,
    pub traceoptions: Option<Traceoptions>,
}

/// The `protocols pcep` configuration of a PCC.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Pcep {
    pub message_rate_limit: Option<String>,
    pub update_rate_limit: Option<String>,
    pub max_provisioned_lsps: Option<String>,
    pub maximum_srmpls_segment_list_depth: Option<String>,
    pub maximum_srv6_segment_list_depth: Option<String>,
    pub propagate_lsp_max_segment_list_depth: bool,
    pub propagate_max_segmentlist: bool,
    pub disable_multipath_capability: bool,
    pub traceoptions: Option<Traceoptions>,
    pub pces: Vec<PcepPce>,
    pub pce_groups: Vec<PcepPceGroup>,
}

fn parse_pce_type(line: &ConfigLine) -> Option<PcepPceType> {
    let words = statement_words(line, "pce-type")?;
    Some(PcepPceType {
        active: words.contains(&"active"),
        stateful: words.contains(&"stateful"),
        srv6_capability: words.contains(&"srv6-capability"),
    })
}

fn parse_pce(name: &str, pce: &ConfigLine) -> PcepPce {
    let field = |keyword: &str| arg_of(pce, keyword).map(str::to_string);
    PcepPce {
        name: name.to_string(),
        has_authentication_key: arg_of(pce, "authentication-key").is_some(),
        authentication_key: crate::secret_value(arg_of(pce, "authentication-key")),
        authentication_key_chain: field("authentication-key-chain"),
        delegation_cleanup_timeout: field("delegation-cleanup-timeout"),
        delegation_priority: field("delegation-priority"),
        destination_ipv4_address: field("destination-ipv4-address"),
        destination_port: field("destination-port"),
        local_address: field("local-address"),
        lsp_cleanup_timer: field("lsp-cleanup-timer"),
        lsp_provisioning: has_leaf(pce, "lsp-provisioning"),
        lsp_retry_delegation: has_leaf(pce, "lsp-retry-delegation"),
        lsp_retry_delegation_timer: field("lsp-retry-delegation-timer"),
        max_sid_depth: field("max-sid-depth"),
        max_unknown_messages: field("max-unknown-messages"),
        max_unknown_requests: field("max-unknown-requests"),
        p2mp_lsp_init_capability: has_leaf(pce, "p2mp-lsp-init-capability"),
        p2mp_lsp_report_capability: has_leaf(pce, "p2mp-lsp-report-capability"),
        p2mp_lsp_update_capability: has_leaf(pce, "p2mp-lsp-update-capability"),
        pce_group: field("pce-group"),
        pce_traffic_steering: has_leaf(pce, "pce_traffic_steering"),
        pce_type: parse_pce_type(pce),
        request_priority: field("request-priority"),
        request_timer: field("request-timer"),
        spring_capability: has_leaf(pce, "spring-capability"),
        srv6_capability: has_leaf(pce, "srv6-capability"),
        traceoptions: parse_traceoptions(pce),
    }
}

fn parse_pce_group(name: &str, group: &ConfigLine) -> PcepPceGroup {
    let field = |keyword: &str| arg_of(group, keyword).map(str::to_string);
    PcepPceGroup {
        name: name.to_string(),
        delegation_cleanup_timeout: field("delegation-cleanup-timeout"),
        lsp_cleanup_timer: field("lsp-cleanup-timer"),
        lsp_provisioning: has_leaf(group, "lsp-provisioning"),
        max_unknown_messages: field("max-unknown-messages"),
        max_unknown_requests: field("max-unknown-requests"),
        pce_type: parse_pce_type(group),
        traceoptions: parse_traceoptions(group),
    }
}

/// Returns the `protocols pcep` block, or `None` if PCEP is not configured.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::protocols::pcep::pcep;
///
/// let config = juniper::parse_braces("protocols {\n    pcep {\n        pce pce1 {\n            destination-ipv4-address 10.209.57.166;\n            destination-port 4189;\n        }\n    }\n}\n");
/// let pcep = pcep(&config).unwrap();
/// assert_eq!(pcep.pces[0].name, "pce1");
/// assert_eq!(pcep.pces[0].destination_port.as_deref(), Some("4189"));
/// ```
pub fn pcep(config: &Config) -> Option<Pcep> {
    let pcep = child_block(top_level(config, "protocols")?, "pcep")?;
    let field = |keyword: &str| arg_of(pcep, keyword).map(str::to_string);
    Some(Pcep {
        message_rate_limit: field("message-rate-limit"),
        update_rate_limit: field("update-rate-limit"),
        max_provisioned_lsps: field("max-provisioned-lsps"),
        maximum_srmpls_segment_list_depth: field("maximum-srmpls-segment-list-depth"),
        maximum_srv6_segment_list_depth: field("maximum-srv6-segment-list-depth"),
        propagate_lsp_max_segment_list_depth: has_leaf(pcep, "propagate-lsp-max-segment-list-depth"),
        propagate_max_segmentlist: has_leaf(pcep, "propagate-max-segmentlist"),
        disable_multipath_capability: has_leaf(pcep, "disable-multipath-capability"),
        traceoptions: parse_traceoptions(pcep),
        pces: named_children(pcep, "pce")
            .map(|(name, pce)| parse_pce(name, pce))
            .collect(),
        pce_groups: named_children(pcep, "pce-group")
            .map(|(name, group)| parse_pce_group(name, group))
            .collect(),
    })
}

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

    // The `pcep` blocks of PCEP Configuration (MPLS Applications User Guide), shortened and wrapped in `protocols`;
    // see the README "Sources".
    const DOC_ACTIVE_STATEFUL: &str = r#"
protocols {
    pcep {
        pce pce1 {
            destination-ipv4-address 10.209.57.166;
            destination-port 4189;
            pce-type active stateful;
        }
    }
}
"#;

    const DOC_GROUP: &str = r#"
protocols {
    pcep {
        pce-group PCEGROUP {
            pce-type active stateful;
            lsp-provisioning;
            lsp-cleanup-timer 30;
        }
        pce PCE1 {
            destination-ipv4-address 192.168.69.58;
            destination-port 4189;
            pce-group PCEGROUP;
        }
        pce PCE2 {
            destination-ipv4-address 192.168.70.65;
            destination-port 4189;
            pce-group PCEGROUP;
        }
    }
}
"#;

    const DOC_LIMITS: &str = r#"
protocols {
    pcep {
        message-rate-limit 50;
        max-provisioned-lsps 16000;
        pce PCE {
            destination-ipv4-address 192.168.69.58;
            destination-port 4189;
            lsp-provisioning;
            lsp-cleanup-timer 50;
            request-timer 50;
            max-unknown-requests 5;
            max-unknown-messages 5;
            delegation-cleanup-timeout 20;
        }
    }
}
"#;

    const DOC_P2MP: &str = r#"
protocols {
    pcep {
        pce pce1 {
            local-address 10.102.180.228;
            destination-ipv4-address 10.102.180.246;
            destination-port 4189;
            pce-type active stateful;
            lsp-provisioning;
            lsp-cleanup-timer 0;
            delegation-cleanup-timeout 60;
            p2mp-lsp-report-capability;
        }
    }
}
"#;

    const DOC_SPRING: &str = r#"
protocols {
    pcep {
        pce pce1 {
            local-address 192.168.100.4;
            destination-ipv4-address 10.102.180.232;
            destination-port 4189;
            pce-type active stateful;
            lsp-provisioning;
            spring-capability;
        }
    }
}
"#;

    const DOC_FLAGS: &str = r#"
protocols {
    pcep {
        disable-multipath-capability;
        propagate-max-segmentlist;
    }
}
"#;

    // Not from documentation: written from the CLI Reference. See the README "Sources".
    const EXTRA: &str = r#"
protocols {
    pcep {
        update-rate-limit 100;
        maximum-srmpls-segment-list-depth 8;
        maximum-srv6-segment-list-depth 6;
        propagate-lsp-max-segment-list-depth;
        traceoptions {
            file pcep.log size 1m;
            flag all;
        }
        pce 192.0.2.1 {
            authentication-key "$9$abc";
            delegation-priority 10;
            lsp-retry-delegation;
            lsp-retry-delegation-timer 3600;
            max-sid-depth 5;
            p2mp-lsp-init-capability;
            p2mp-lsp-update-capability;
            pce_traffic_steering;
            request-priority 3;
            srv6-capability;
            traceoptions {
                flag pcep;
            }
        }
        pce 192.0.2.2 {
            authentication-key-chain pce-keys;
        }
        pce-group SRV6 {
            delegation-cleanup-timeout 120;
            max-unknown-messages 5;
            max-unknown-requests 5;
            pce-type {
                active;
                stateful;
                srv6-capability;
            }
            traceoptions {
                flag all;
            }
        }
    }
}
"#;

    fn pcep_of(text: &str) -> Pcep {
        pcep(&juniper::parse_braces(text)).unwrap()
    }

    #[test]
    fn reads_the_guide_active_stateful_pce() {
        let pcep = pcep_of(DOC_ACTIVE_STATEFUL);
        assert_eq!(pcep.pces.len(), 1);
        let pce = &pcep.pces[0];
        assert_eq!(pce.name, "pce1");
        assert_eq!(pce.destination_ipv4_address.as_deref(), Some("10.209.57.166"));
        assert_eq!(pce.destination_port.as_deref(), Some("4189"));
        let pce_type = pce.pce_type.as_ref().unwrap();
        assert!(pce_type.active && pce_type.stateful && !pce_type.srv6_capability);
        assert!(pcep.pce_groups.is_empty());
    }

    #[test]
    fn reads_the_guide_pce_group() {
        let pcep = pcep_of(DOC_GROUP);
        assert_eq!(pcep.pce_groups.len(), 1);
        let group = &pcep.pce_groups[0];
        assert_eq!(group.name, "PCEGROUP");
        assert!(group.pce_type.as_ref().unwrap().active);
        assert!(group.lsp_provisioning);
        assert_eq!(group.lsp_cleanup_timer.as_deref(), Some("30"));
        assert_eq!(pcep.pces.len(), 2);
        assert_eq!(pcep.pces[1].name, "PCE2");
        assert_eq!(pcep.pces[1].pce_group.as_deref(), Some("PCEGROUP"));
        assert_eq!(pcep.pces[1].destination_ipv4_address.as_deref(), Some("192.168.70.65"));
    }

    #[test]
    fn reads_the_guide_limits_and_timers() {
        let pcep = pcep_of(DOC_LIMITS);
        assert_eq!(pcep.message_rate_limit.as_deref(), Some("50"));
        assert_eq!(pcep.max_provisioned_lsps.as_deref(), Some("16000"));
        let pce = &pcep.pces[0];
        assert!(pce.lsp_provisioning);
        assert_eq!(pce.lsp_cleanup_timer.as_deref(), Some("50"));
        assert_eq!(pce.request_timer.as_deref(), Some("50"));
        assert_eq!(pce.max_unknown_requests.as_deref(), Some("5"));
        assert_eq!(pce.max_unknown_messages.as_deref(), Some("5"));
        assert_eq!(pce.delegation_cleanup_timeout.as_deref(), Some("20"));
        assert_eq!(pce.pce_type, None);
    }

    #[test]
    fn reads_the_guide_p2mp_and_spring_capabilities() {
        let p2mp = &pcep_of(DOC_P2MP).pces[0];
        assert_eq!(p2mp.local_address.as_deref(), Some("10.102.180.228"));
        assert!(p2mp.p2mp_lsp_report_capability);
        assert!(!p2mp.p2mp_lsp_init_capability && !p2mp.p2mp_lsp_update_capability);
        assert_eq!(p2mp.lsp_cleanup_timer.as_deref(), Some("0"));

        let spring = &pcep_of(DOC_SPRING).pces[0];
        assert_eq!(spring.local_address.as_deref(), Some("192.168.100.4"));
        assert!(spring.spring_capability && spring.lsp_provisioning);
        assert!(!p2mp.spring_capability);
    }

    #[test]
    fn reads_the_guide_session_flags() {
        let pcep = pcep_of(DOC_FLAGS);
        assert!(pcep.disable_multipath_capability);
        assert!(pcep.propagate_max_segmentlist);
        assert!(!pcep.propagate_lsp_max_segment_list_depth);
        assert!(pcep.pces.is_empty());
    }

    #[test]
    fn reads_the_pcc_options() {
        let pcep = pcep_of(EXTRA);
        assert_eq!(pcep.update_rate_limit.as_deref(), Some("100"));
        assert_eq!(pcep.maximum_srmpls_segment_list_depth.as_deref(), Some("8"));
        assert_eq!(pcep.maximum_srv6_segment_list_depth.as_deref(), Some("6"));
        assert!(pcep.propagate_lsp_max_segment_list_depth);
        let trace = pcep.traceoptions.unwrap();
        assert_eq!(trace.file.as_deref(), Some("pcep.log"));
        assert_eq!(trace.flags[0].name, "all");
    }

    #[test]
    fn reads_the_pce_options() {
        let pcep = pcep_of(EXTRA);
        let pce = &pcep.pces[0];
        assert_eq!(pce.name, "192.0.2.1");
        assert_eq!(pce.delegation_priority.as_deref(), Some("10"));
        assert!(pce.lsp_retry_delegation);
        assert_eq!(pce.lsp_retry_delegation_timer.as_deref(), Some("3600"));
        assert_eq!(pce.max_sid_depth.as_deref(), Some("5"));
        assert!(pce.p2mp_lsp_init_capability && pce.p2mp_lsp_update_capability);
        assert!(pce.pce_traffic_steering);
        assert_eq!(pce.request_priority.as_deref(), Some("3"));
        assert!(pce.srv6_capability);
        assert_eq!(pce.traceoptions.as_ref().unwrap().flags[0].name, "pcep");
    }

    #[test]
    fn reads_the_authentication() {
        let pcep = pcep_of(EXTRA);
        assert!(pcep.pces[0].has_authentication_key);
        assert_eq!(
            pcep.pces[0].authentication_key.as_ref().map(|key| key.expose()),
            expected("$9$abc")
        );
        assert!(!pcep.pces[1].has_authentication_key);
        assert_eq!(pcep.pces[1].authentication_key, None);
        assert_eq!(pcep.pces[1].authentication_key_chain.as_deref(), Some("pce-keys"));
    }

    #[test]
    fn reads_a_pce_group_with_a_pce_type_block() {
        let group = &pcep_of(EXTRA).pce_groups[0];
        assert_eq!(group.name, "SRV6");
        assert_eq!(group.delegation_cleanup_timeout.as_deref(), Some("120"));
        assert_eq!(group.max_unknown_messages.as_deref(), Some("5"));
        let pce_type = group.pce_type.as_ref().unwrap();
        assert!(pce_type.active && pce_type.stateful && pce_type.srv6_capability);
        assert_eq!(group.traceoptions.as_ref().unwrap().flags[0].name, "all");
    }

    #[test]
    fn a_bare_pcep_block_has_nothing_set() {
        assert_eq!(pcep_of("protocols {\n    pcep;\n}\n"), Pcep::default());
    }

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