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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! `pim`
//! `protocols { pim { ... } }`.
//!
//! This covers the router-wide options, the interfaces and the rendezvous point settings. The `mdt`, `mvpn` and
//! `rpf-selection` statements are not extracted.
//!
//! # See also
//!
//! - [pim](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pim-edit-protocols.html)
//! - [disable (PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/disable-edit-protocols-pim.html)
//! - [assert-timeout](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/assert-timeout-edit-protocols-pim.html)
//! - [dense-groups](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dense-groups-edit-protocols-pim.html)
//! - [dr-election-on-p2p](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dr-election-on-p2p-edit-protocols-pim.html)
//! - [export (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/export-edit-protocols-pim.html)
//! - [import (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/import-edit-protocols-pim.html)
//! - [family (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/family-edit-protocols-pim.html)
//! - [graceful-restart (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/graceful-restart-edit-protocols-pim.html)
//! - [join-load-balance](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/join-load-balance-edit-protocols-pim.html)
//! - [join-prune-timeout](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/join-prune-timeout-edit-protocols-pim.html)
//! - [mldp-inband-signalling (Protocols Multipoint LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mldp-inband-signalling-edit-protocols-pim.html)
//! - [override-interval](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/override-interval-edit-protocols-pim.html)
//! - [propagation-delay](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/propagation-delay-edit-protocols-pim.html)
//! - [reset-tracking-bit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/reset-tracking-bit-edit-protocols-pim.html)
//! - [rib-group (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rib-group-edit-protocols-pim.html)
//! - [spt-threshold](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/spt-threshold-edit-protocols-pim.html)
//! - [traceoptions (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-protocols-pim.html)
//! - [Examples: Configuring PIM Sparse Mode](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-sparse-mode.html)
//! - [Example: Configuring Nonstop Active Routing for PIM](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-graceful-restart.html)

mod interface;
mod limits;
mod rp;

pub use interface::{PimBfd, PimDfElection, PimFamily, PimInterface, PimMultipleTriggeredJoins};
pub use limits::PimLimits;
pub use rp::{
    PimAnycast, PimAnycastRp, PimAutoRp, PimBidirectionalRp, PimBootstrapFamily, PimEmbeddedRp, PimLocalRp,
    PimLocalRpFamily, PimLocalRpOptions, PimRp, PimStaticRp,
};

use super::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, top_level};
use asterust::config::Config;
use interface::{parse_families, parse_interface};
use limits::parse_limits;
use rp::parse_rp;

/// `graceful-restart { ... }`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PimGracefulRestart {
    pub disable: bool,
    pub no_bidirectional_mode: bool,
    /// Seconds.
    pub restart_duration: Option<String>,
}

/// The `protocols pim` configuration.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Pim {
    pub disable: bool,
    /// Seconds.
    pub assert_timeout: Option<String>,
    /// The groups listed under `dense-groups`.
    pub dense_groups: Vec<String>,
    pub dr_election_on_p2p: bool,
    pub export_policies: Vec<String>,
    pub import_policies: Vec<String>,
    pub families: Vec<PimFamily>,
    pub graceful_restart: Option<PimGracefulRestart>,
    pub interfaces: Vec<PimInterface>,
    pub join_load_balance: bool,
    /// Seconds.
    pub join_prune_timeout: Option<String>,
    /// The policy of `mldp-inband-signalling`.
    pub mldp_inband_signalling_policy: Option<String>,
    pub nonstop_routing: bool,
    /// Milliseconds.
    pub override_interval: Option<String>,
    /// Milliseconds.
    pub propagation_delay: Option<String>,
    pub reset_tracking_bit: bool,
    pub rib_group: Option<String>,
    pub rp: Option<PimRp>,
    pub sglimit: Option<PimLimits>,
    /// The policies of `spt-threshold { infinity ...; }`.
    pub spt_threshold_infinity: Vec<String>,
    pub traceoptions: Option<Traceoptions>,
    pub tunnel_devices: Vec<String>,
}

/// Returns the `protocols pim` block, or `None` if PIM is not configured.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::protocols::pim::pim;
///
/// let config = juniper::parse_braces("protocols {\n    pim {\n        interface all {\n            mode sparse;\n        }\n    }\n}\n");
/// let pim = pim(&config).unwrap();
/// assert_eq!(pim.interfaces[0].name, "all");
/// assert_eq!(pim.interfaces[0].mode.as_deref(), Some("sparse"));
/// ```
pub fn pim(config: &Config) -> Option<Pim> {
    let pim = child_block(top_level(config, "protocols")?, "pim")?;
    let field = |keyword: &str| arg_of(pim, keyword).map(str::to_string);
    let policies = |keyword: &str| -> Vec<String> { args_of(pim, keyword).into_iter().flat_map(list_values).collect() };
    Some(Pim {
        disable: has_leaf(pim, "disable"),
        assert_timeout: field("assert-timeout"),
        dense_groups: child_block(pim, "dense-groups")
            .map(|groups| active_children(groups).map(|group| group.text.clone()).collect())
            .unwrap_or_default(),
        dr_election_on_p2p: has_leaf(pim, "dr-election-on-p2p"),
        export_policies: policies("export"),
        import_policies: policies("import"),
        families: parse_families(pim),
        graceful_restart: child_block(pim, "graceful-restart").map(|block| PimGracefulRestart {
            disable: has_leaf(block, "disable"),
            no_bidirectional_mode: has_leaf(block, "no-bidirectional-mode"),
            restart_duration: arg_of(block, "restart-duration").map(str::to_string),
        }),
        interfaces: named_children(pim, "interface")
            .map(|(name, interface)| parse_interface(name, interface))
            .collect(),
        join_load_balance: has_leaf(pim, "join-load-balance"),
        join_prune_timeout: field("join-prune-timeout"),
        mldp_inband_signalling_policy: child_block(pim, "mldp-inband-signalling")
            .and_then(|block| arg_of(block, "policy"))
            .map(str::to_string),
        nonstop_routing: has_leaf(pim, "nonstop-routing"),
        override_interval: field("override-interval"),
        propagation_delay: field("propagation-delay"),
        reset_tracking_bit: has_leaf(pim, "reset-tracking-bit"),
        rib_group: field("rib-group"),
        rp: child_block(pim, "rp").map(parse_rp),
        sglimit: child_block(pim, "sglimit").map(parse_limits),
        spt_threshold_infinity: child_block(pim, "spt-threshold")
            .map(|block| args_of(block, "infinity").into_iter().flat_map(list_values).collect())
            .unwrap_or_default(),
        traceoptions: parse_traceoptions(pim),
        tunnel_devices: args_of(pim, "tunnel-devices")
            .into_iter()
            .flat_map(list_values)
            .collect(),
    })
}

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

    // Blocks of the PIM pages of the Multicast Protocols User Guide (Examples: Configuring PIM Sparse Mode,
    // Configuring Static RP, Configuring PIM Bootstrap Router, Example: Configuring Anycast RP, Example: Configuring
    // Bidirectional PIM, Configuring PIM and BFD, Example: Configuring Nonstop Active Routing for PIM); see the README
    // "Sources".
    const DOC_SPARSE: &str = r#"
protocols {
    pim {
        traceoptions {
            file pim.log size 5m world-readable;
            flag join detail;
            flag prune detail;
            flag normal detail;
            flag register detail;
        }
        rp {
            static {
                address 10.255.112.160;
            }
        }
        interface all {
            mode sparse;
            version 2;
        }
        interface fxp0.0 {
            disable;
        }
        reset-tracking-bit;
        propagation-delay 500;
        override-interval 4000;
    }
}
"#;

    const DOC_STATIC_RP: &str = r#"
protocols {
    pim {
        rp {
            static {
                address 192.168.14.27;
            }
        }
        interface all;
        interface ge-0/0/0.0 {
            disable;
        }
    }
}
"#;

    const DOC_ANYCAST: &str = r#"
protocols {
    pim {
        rp {
            local {
                family inet {
                    address 198.51.100.253;
                    anycast-pim {
                        rp-set {
                            address 198.51.100.240;
                            address 198.51.100.241 forward-msdp-sa;
                        }
                        local-address 198.51.100.254;
                    }
                }
            }
        }
        interface all {
            mode sparse;
            version 2;
        }
        interface fxp0.0 {
            disable;
        }
    }
}
"#;

    const DOC_BOOTSTRAP: &str = r#"
protocols {
    pim {
        rp {
            bootstrap {
                family inet {
                    priority 1;
                    import pim-import;
                    export pim-export;
                }
                family inet6 {
                    priority 1;
                    import pim-import;
                    export pim-export;
                }
            }
        }
    }
}
"#;

    const DOC_BSR_FILTERS: &str = r#"
protocols {
    pim {
        rp {
            bootstrap-import no-bsr;
            bootstrap-export no-bsr;
        }
    }
}
"#;

    const DOC_BIDIRECTIONAL: &str = r#"
protocols {
    pim {
        rp {
            bidirectional {
                address 10.10.1.3 {
                    group-ranges {
                        224.1.3.0/24;
                        225.1.3.0/24;
                    }
                }
                address 10.10.13.2 {
                    group-ranges {
                        224.1.1.0/24;
                        225.1.1.0/24;
                    }
                }
            }
        }
        interface ge-0/0/1.0 {
            mode bidirectional-sparse-dense;
        }
        interface xe-2/1/0.0 {
            mode bidirectional-sparse-dense;
        }
        traceoptions {
            file df;
            flag bidirectional-df-election detail;
        }
    }
}
"#;

    const DOC_BFD: &str = r#"
protocols {
    pim {
        interface ge-0/1/5.0 {
            family inet6;
            bfd-liveness-detection {
                authentication {
                    algorithm keyed-sha-1;
                    key-chain bfd-pim;
                }
            }
        }
    }
}
"#;

    // The routing-instances example of the same page, here at the top level.
    const DOC_BFD_FAMILY: &str = r#"
protocols {
    pim {
        interface all {
            family inet6 {
                bfd-liveness-detection {
                    authentication {
                        algorithm keyed-sha-1;
                        key-chain bfd-pim;
                    }
                }
            }
        }
    }
}
"#;

    const DOC_NSR: &str = r#"
protocols {
    pim {
        traceoptions {
            file r1-nsr-pim size 10m files 10 world-readable;
            flag mdt detail;
            flag nsr-synchronization;
        }
        rp {
            static {
                address 10.210.255.202;
                address abcd::10:210:255:202;
            }
        }
        interface lo0.0;
        interface fe-0/1/3.0 {
            mode sparse;
            version 2;
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference pages of the statements. See the README "Sources".
    const EXTRA: &str = r#"
protocols {
    pim {
        assert-timeout 180;
        dr-election-on-p2p;
        join-load-balance;
        join-prune-timeout 210;
        nonstop-routing;
        rib-group mcast-rib;
        export [ pim-out other-out ];
        import pim-in;
        tunnel-devices [ mt-1/1/0 mt-1/1/1 ];
        dense-groups {
            224.0.1.39/32;
            224.0.1.40/32;
        }
        family inet {
            disable;
        }
        family inet6;
        graceful-restart {
            no-bidirectional-mode;
            restart-duration 120;
        }
        mldp-inband-signalling {
            policy mldp-policy;
        }
        spt-threshold {
            infinity [ spt-a spt-b ];
        }
        sglimit {
            maximum 1000;
            threshold 80;
            log-interval 60;
        }
        interface ge-0/0/0.0 {
            hello-interval 30;
            priority 10;
            override-interval 2500;
            propagation-delay 500;
            reset-tracking-bit;
            accept-remote-source;
            neighbor-policy [ np1 np2 ];
            accept-join-always-from allow-joins;
            dual-dr {
                enhanced;
            }
            bidirectional {
                df-election {
                    backoff-period 1000;
                    offer-period 100;
                    robustness-count 3;
                }
            }
            multiple-triggered-joins {
                count 2;
                interval 50;
            }
            bfd-liveness-detection {
                minimum-interval 300;
                minimum-receive-interval 400;
                multiplier 3;
                version automatic;
                no-adaptation;
                detection-time {
                    threshold 1000;
                }
                transmit-interval {
                    minimum-interval 200;
                    threshold 500;
                }
                authentication {
                    algorithm meticulous-keyed-sha-1;
                    key-chain BFD-KEYS;
                    loose-check;
                }
            }
        }
        rp {
            auto-rp {
                mapping;
                no-mapping-agent-election;
            }
            bootstrap-priority 5;
            dr-register-policy [ drp1 drp2 ];
            rp-register-policy rrp;
            register-probe-time 5;
            register-limit {
                maximum 500;
                threshold 70;
                log-interval 30;
            }
            embedded-rp {
                group-ranges {
                    ff7e::/16;
                }
                maximum-rps 50;
            }
            bidirectional {
                address 10.10.1.3 {
                    group-ranges {
                        224.1.3.0/24;
                    }
                    hold-time 150;
                    priority 100;
                }
            }
            local {
                disable;
                address 10.0.0.1;
                priority 50;
                hold-time 90;
                override;
                process-non-null-as-null-register;
                group-ranges {
                    239.0.0.0/8;
                }
                family inet6 {
                    disable;
                    address 2001:db8::1;
                }
            }
            static {
                address 10.1.1.1 {
                    override;
                    version 1;
                    group-ranges {
                        224.0.0.0/4;
                    }
                }
                address 10.1.1.2 version 2;
            }
        }
    }
}
"#;

    fn pim_of(text: &str) -> Pim {
        pim(&juniper::parse_braces(text)).unwrap()
    }

    #[test]
    fn reads_the_guide_sparse_mode_example() {
        let pim = pim_of(DOC_SPARSE);
        let trace = pim.traceoptions.unwrap();
        assert_eq!(trace.file.as_deref(), Some("pim.log"));
        assert_eq!(trace.size.as_deref(), Some("5m"));
        assert_eq!(trace.world_readable, Some(true));
        let flags: Vec<_> = trace
            .flags
            .iter()
            .map(|f| (f.name.as_str(), f.modifiers.clone()))
            .collect();
        assert_eq!(flags[0], ("join", vec!["detail".to_string()]));
        assert_eq!(flags.len(), 4);
        assert_eq!(pim.rp.unwrap().static_rps[0].address, "10.255.112.160");
        assert_eq!(pim.interfaces.len(), 2);
        assert_eq!(pim.interfaces[0].name, "all");
        assert_eq!(pim.interfaces[0].mode.as_deref(), Some("sparse"));
        assert_eq!(pim.interfaces[0].version.as_deref(), Some("2"));
        assert!(pim.interfaces[1].disable);
        assert!(pim.reset_tracking_bit);
        assert_eq!(pim.propagation_delay.as_deref(), Some("500"));
        assert_eq!(pim.override_interval.as_deref(), Some("4000"));
    }

    #[test]
    fn reads_the_guide_static_rp_example() {
        let pim = pim_of(DOC_STATIC_RP);
        let rp = pim.rp.unwrap();
        assert_eq!(rp.static_rps.len(), 1);
        assert_eq!(rp.static_rps[0].address, "192.168.14.27");
        assert_eq!(rp.static_rps[0].version, None);
        assert_eq!(pim.interfaces[0].name, "all");
        assert_eq!(pim.interfaces[0].mode, None);
        assert!(pim.interfaces[1].disable);
    }

    #[test]
    fn reads_the_guide_anycast_rp_example() {
        let pim = pim_of(DOC_ANYCAST);
        let local = pim.rp.unwrap().local.unwrap();
        assert!(!local.disable);
        assert_eq!(local.families.len(), 1);
        let family = &local.families[0];
        assert_eq!(family.family, "inet");
        assert_eq!(family.options.address.as_deref(), Some("198.51.100.253"));
        let anycast = family.options.anycast_pim.as_ref().unwrap();
        assert_eq!(anycast.local_address.as_deref(), Some("198.51.100.254"));
        assert_eq!(anycast.rp_set.len(), 2);
        assert_eq!(anycast.rp_set[0].address, "198.51.100.240");
        assert!(!anycast.rp_set[0].forward_msdp_sa);
        assert_eq!(anycast.rp_set[1].address, "198.51.100.241");
        assert!(anycast.rp_set[1].forward_msdp_sa);
    }

    #[test]
    fn reads_the_guide_bootstrap_examples() {
        let bootstrap = pim_of(DOC_BOOTSTRAP).rp.unwrap().bootstrap;
        assert_eq!(bootstrap.len(), 2);
        assert_eq!(bootstrap[0].family, "inet");
        assert_eq!(bootstrap[0].priority.as_deref(), Some("1"));
        assert_eq!(bootstrap[0].import_policies, ["pim-import"]);
        assert_eq!(bootstrap[1].family, "inet6");
        assert_eq!(bootstrap[1].export_policies, ["pim-export"]);

        let rp = pim_of(DOC_BSR_FILTERS).rp.unwrap();
        assert_eq!(rp.bootstrap_import_policies, ["no-bsr"]);
        assert_eq!(rp.bootstrap_export_policies, ["no-bsr"]);
    }

    #[test]
    fn reads_the_guide_bidirectional_example() {
        let pim = pim_of(DOC_BIDIRECTIONAL);
        let rp = pim.rp.unwrap();
        assert_eq!(rp.bidirectional.len(), 2);
        assert_eq!(rp.bidirectional[0].address, "10.10.1.3");
        assert_eq!(rp.bidirectional[0].group_ranges, ["224.1.3.0/24", "225.1.3.0/24"]);
        assert_eq!(rp.bidirectional[1].address, "10.10.13.2");
        assert_eq!(pim.interfaces[1].mode.as_deref(), Some("bidirectional-sparse-dense"));
        let trace = pim.traceoptions.unwrap();
        assert_eq!(trace.file.as_deref(), Some("df"));
        assert_eq!(trace.flags[0].name, "bidirectional-df-election");
    }

    #[test]
    fn reads_the_guide_bfd_examples() {
        let interface = &pim_of(DOC_BFD).interfaces[0];
        assert_eq!(interface.families[0].name, "inet6");
        let bfd = interface.bfd_liveness_detection.as_ref().unwrap();
        assert_eq!(bfd.authentication_algorithm.as_deref(), Some("keyed-sha-1"));
        assert_eq!(bfd.authentication_key_chain.as_deref(), Some("bfd-pim"));

        let interface = &pim_of(DOC_BFD_FAMILY).interfaces[0];
        assert_eq!(interface.bfd_liveness_detection, None);
        let bfd = interface.families[0].bfd_liveness_detection.as_ref().unwrap();
        assert_eq!(bfd.authentication_key_chain.as_deref(), Some("bfd-pim"));
    }

    #[test]
    fn reads_the_guide_nonstop_routing_example() {
        let pim = pim_of(DOC_NSR);
        let trace = pim.traceoptions.unwrap();
        assert_eq!(trace.files.as_deref(), Some("10"));
        assert_eq!(trace.flags[1].name, "nsr-synchronization");
        assert!(trace.flags[1].modifiers.is_empty());
        let rp = pim.rp.unwrap();
        assert_eq!(rp.static_rps[1].address, "abcd::10:210:255:202");
        assert_eq!(pim.interfaces[0].name, "lo0.0");
        assert_eq!(pim.interfaces[1].mode.as_deref(), Some("sparse"));
    }

    #[test]
    fn reads_the_global_options() {
        let pim = pim_of(EXTRA);
        assert!(!pim.disable);
        assert_eq!(pim.assert_timeout.as_deref(), Some("180"));
        assert!(pim.dr_election_on_p2p);
        assert!(pim.join_load_balance);
        assert_eq!(pim.join_prune_timeout.as_deref(), Some("210"));
        assert!(pim.nonstop_routing);
        assert_eq!(pim.rib_group.as_deref(), Some("mcast-rib"));
        assert_eq!(pim.export_policies, ["pim-out", "other-out"]);
        assert_eq!(pim.import_policies, ["pim-in"]);
        assert_eq!(pim.tunnel_devices, ["mt-1/1/0", "mt-1/1/1"]);
        assert_eq!(pim.dense_groups, ["224.0.1.39/32", "224.0.1.40/32"]);
        assert_eq!(pim.mldp_inband_signalling_policy.as_deref(), Some("mldp-policy"));
        assert_eq!(pim.spt_threshold_infinity, ["spt-a", "spt-b"]);
        let restart = pim.graceful_restart.unwrap();
        assert!(restart.no_bidirectional_mode);
        assert!(!restart.disable);
        assert_eq!(restart.restart_duration.as_deref(), Some("120"));
        let sglimit = pim.sglimit.unwrap();
        assert_eq!(sglimit.maximum.as_deref(), Some("1000"));
        assert_eq!(sglimit.threshold.as_deref(), Some("80"));
        assert_eq!(sglimit.log_interval.as_deref(), Some("60"));
        assert_eq!(pim.families.len(), 2);
        assert!(pim.families[0].disable);
        assert_eq!(pim.families[1].name, "inet6");
        assert!(!pim.families[1].disable);
    }

    #[test]
    fn reads_the_interface_options() {
        let pim = pim_of(EXTRA);
        let interface = &pim.interfaces[0];
        assert_eq!(interface.hello_interval.as_deref(), Some("30"));
        assert_eq!(interface.priority.as_deref(), Some("10"));
        assert_eq!(interface.override_interval.as_deref(), Some("2500"));
        assert_eq!(interface.propagation_delay.as_deref(), Some("500"));
        assert!(interface.reset_tracking_bit);
        assert!(interface.accept_remote_source);
        assert!(interface.dual_dr_enhanced);
        assert_eq!(interface.neighbor_policies, ["np1", "np2"]);
        assert_eq!(interface.accept_join_always_from, ["allow-joins"]);
        let df = interface.df_election.as_ref().unwrap();
        assert_eq!(df.backoff_period.as_deref(), Some("1000"));
        assert_eq!(df.offer_period.as_deref(), Some("100"));
        assert_eq!(df.robustness_count.as_deref(), Some("3"));
        let joins = interface.multiple_triggered_joins.as_ref().unwrap();
        assert_eq!(joins.count.as_deref(), Some("2"));
        assert_eq!(joins.interval.as_deref(), Some("50"));
    }

    #[test]
    fn reads_the_interface_bfd_options() {
        let pim = pim_of(EXTRA);
        let bfd = pim.interfaces[0].bfd_liveness_detection.as_ref().unwrap();
        assert_eq!(bfd.minimum_interval.as_deref(), Some("300"));
        assert_eq!(bfd.minimum_receive_interval.as_deref(), Some("400"));
        assert_eq!(bfd.multiplier.as_deref(), Some("3"));
        assert_eq!(bfd.version.as_deref(), Some("automatic"));
        assert!(bfd.no_adaptation);
        assert_eq!(bfd.detection_time_threshold.as_deref(), Some("1000"));
        assert_eq!(bfd.transmit_interval_minimum.as_deref(), Some("200"));
        assert_eq!(bfd.transmit_interval_threshold.as_deref(), Some("500"));
        assert_eq!(bfd.authentication_algorithm.as_deref(), Some("meticulous-keyed-sha-1"));
        assert_eq!(bfd.authentication_key_chain.as_deref(), Some("BFD-KEYS"));
        assert!(bfd.authentication_loose_check);
    }

    #[test]
    fn reads_the_rp_options() {
        let rp = pim_of(EXTRA).rp.unwrap();
        let auto = rp.auto_rp.unwrap();
        assert_eq!(auto.mode.as_deref(), Some("mapping"));
        assert_eq!(auto.mapping_agent_election, Some(false));
        assert_eq!(rp.bootstrap_priority.as_deref(), Some("5"));
        assert_eq!(rp.dr_register_policies, ["drp1", "drp2"]);
        assert_eq!(rp.rp_register_policies, ["rrp"]);
        assert_eq!(rp.register_probe_time.as_deref(), Some("5"));
        let limit = rp.register_limit.unwrap();
        assert_eq!(limit.maximum.as_deref(), Some("500"));
        assert_eq!(limit.threshold.as_deref(), Some("70"));
        assert_eq!(limit.log_interval.as_deref(), Some("30"));
        let embedded = rp.embedded_rp.unwrap();
        assert_eq!(embedded.group_ranges, ["ff7e::/16"]);
        assert_eq!(embedded.maximum_rps.as_deref(), Some("50"));
        let bidirectional = &rp.bidirectional[0];
        assert_eq!(bidirectional.hold_time.as_deref(), Some("150"));
        assert_eq!(bidirectional.priority.as_deref(), Some("100"));
    }

    #[test]
    fn reads_the_local_and_static_rps() {
        let rp = pim_of(EXTRA).rp.unwrap();
        let local = rp.local.unwrap();
        assert!(local.disable);
        assert_eq!(local.options.address.as_deref(), Some("10.0.0.1"));
        assert_eq!(local.options.priority.as_deref(), Some("50"));
        assert_eq!(local.options.hold_time.as_deref(), Some("90"));
        assert!(local.options.override_rp);
        assert!(local.options.process_non_null_as_null_register);
        assert_eq!(local.options.group_ranges, ["239.0.0.0/8"]);
        assert_eq!(local.options.anycast_pim, None);
        assert_eq!(local.families[0].family, "inet6");
        assert!(local.families[0].disable);
        assert_eq!(local.families[0].options.address.as_deref(), Some("2001:db8::1"));

        assert_eq!(rp.static_rps.len(), 2);
        assert_eq!(rp.static_rps[0].address, "10.1.1.1");
        assert!(rp.static_rps[0].override_rp);
        assert_eq!(rp.static_rps[0].version.as_deref(), Some("1"));
        assert_eq!(rp.static_rps[0].group_ranges, ["224.0.0.0/4"]);
        assert_eq!(rp.static_rps[1].address, "10.1.1.2");
        assert_eq!(rp.static_rps[1].version.as_deref(), Some("2"));
        assert!(!rp.static_rps[1].override_rp);
    }

    #[test]
    fn bare_statements_and_absent_values() {
        let pim = pim_of("protocols {\n    pim {\n        disable;\n        rp {\n            auto-rp discovery;\n        }\n    }\n}\n");
        assert!(pim.disable);
        let auto = pim.rp.unwrap().auto_rp.unwrap();
        assert_eq!(auto.mode.as_deref(), Some("discovery"));
        assert_eq!(auto.mapping_agent_election, None);
        assert_eq!(pim.graceful_restart, None);
        assert!(pim.interfaces.is_empty());
        assert!(pim.dense_groups.is_empty());
        assert_eq!(pim.sglimit, None);
    }

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