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
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
//! `unit`
//! `interfaces { <name> { unit <n> { ... } } }`.
//!
//! # See also
//!
//! - [unit (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unit-edit-interfaces.html)
//! - [encapsulation (Logical Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/encapsulation-edit-interfaces-logical.html)
//! - [reassemble-packets](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/reassemble-packets-edit-interfaces.html)
//! - [clear-dont-fragment-bit (Interfaces GRE Tunnels)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/clear-dont-fragment-bit-edit-interfaces.html)
//! - [copy-tos-to-outer-ip-header](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/copy-tos-to-outer-ip-header-edit-interfaces.html)
//! - [copy-tos-to-outer-ip-header-transit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/copy-tos-to-outer-ip-header-transit-edit-interfaces.html)
//! - [extended-telemetry](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/extended-telemetry-edit-interfaces-interface-unit.html)
//! - [alias](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/alias-edit-interfaces.html)
//! - [bandwidth (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bandwidth-edit-interfaces.html)
//! - [arp-resp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-arp-resp.html)
//! - [peer-unit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/peer-unit-edit-interfaces-sv.html)
//! - [disable-mlppp-inner-ppp-pfc](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/disable-mlppp-inner-ppp-pfc-edit-interfaces.html)
//! - [host-prefix-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/host-prefix-only-edit-interfaces-unit.html)
//! - [per-session-scheduler](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/per-session-scheduler-edit-interfaces.html)
//! - [point-to-point](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/point-to-point-edit-interfaces.html)
//! - [multipoint](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/multipoint-edit-interfaces.html)
//! - [demux-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-options-edit-interfaces.html)
//! - [demux (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-edit-interfaces.html)
//! - [advisory-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advisory-options-edit-interfaces-unit.html)
//! - [targeted-distribution](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/targeted-distribution-edit-interfaces.html)
//! - [pppoe-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pppoe-options-edit-interfaces.html)
//! - [pppoe-underlying-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pppoe-underlying-options-edit-interfaces-pppoe.html)
//! - [ppp-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ppp-options-edit-interfaces.html)
//! - [vlan-id (VLAN ID to Be Bound to a Logical Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-id-edit-interfaces.html)

mod accept_source_mac;
mod demux;
mod demux_options;
mod family;
mod pppoe_options;
mod pppoe_underlying_options;
mod tunnel;
mod vlan_map;
mod vlan_tags;

pub use accept_source_mac::{AcceptedMac, PolicerOverhead};
pub use demux::{
    Demux, DemuxAddressRanges, DemuxDynamicProfile, DemuxFamily, DemuxNetwork, DemuxRange, DemuxUsernameInclude,
};
pub use demux_options::{AdvisoryOptions, DemuxOptions};
pub use family::{
    AddressArp, BandwidthThreshold, DhcpClient, DhcpClientIdentifier, Dhcpv6Client, EthernetSwitching,
    FamilyAccounting, FamilyFilter, FamilyPolicer, FamilySampling, NextHopTunnel, PrefixDelegating,
    RouterAdvertisementInterface, RpfCheck, TargetedBroadcast, TrackedInterface, TrackedRoute, UnitAddress, UnitFamily,
    UnnumberedAddress, VlanTranslation, VrrpGroup, VrrpInheritFrom, VrrpTrack,
};
pub use pppoe_options::PppoeOptions;
pub use pppoe_underlying_options::{DuplicateProtection, PppoeUnderlyingOptions, ShortCycleProtection};
pub use tunnel::UnitTunnel;
pub use vlan_map::VlanMap;
pub use vlan_tags::VlanTags;

use super::esi::{parse_esi, Esi};
use super::ppp_options::{parse_ppp_options, PppOptions};
use crate::{active_children, arg_of, args_of, child_block, has_leaf, named_children};
use accept_source_mac::{parse_accept_source_mac, parse_policer_overhead};
use asterust::config::ConfigLine;
use demux::parse_demux;
use demux_options::{parse_advisory_options, parse_demux_options};
use family::parse_family;
use pppoe_options::parse_pppoe_options;
use pppoe_underlying_options::parse_pppoe_underlying_options;
use tunnel::parse_tunnel;
use vlan_map::parse_vlan_map;
use vlan_tags::parse_vlan_tags;

/// A logical `unit <n>` of an interface. Addresses are the raw `address` values (e.g.
/// `"10.0.0.1/24"`; IS-IS NETs for `family iso`).
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct InterfaceUnit {
    pub number: String,
    pub description: Option<String>,
    pub vlan_id: Option<u32>,
    /// `vlan-id unmatched`.
    pub vlan_id_unmatched: bool,
    /// e.g. `"100-200"`.
    pub vlan_id_range: Option<String>,
    pub vlan_tags: Option<VlanTags>,
    pub tunnel: Option<UnitTunnel>,
    pub esi: Option<Esi>,
    pub ppp_options: Option<PppOptions>,
    /// The `pppoe-options` of a `pp0` unit.
    pub pppoe_options: Option<PppoeOptions>,
    pub pppoe_underlying_options: Option<PppoeUnderlyingOptions>,
    pub demux: Option<Demux>,
    pub demux_options: Option<DemuxOptions>,
    /// The families of each `demux-source`.
    pub demux_source: Vec<String>,
    /// The families of each `demux-destination`.
    pub demux_destination: Vec<String>,
    pub advisory_options: Option<AdvisoryOptions>,
    /// `replicate` or `scale`.
    pub targeted_distribution: Option<String>,
    pub accept_source_mac: Vec<AcceptedMac>,
    pub policer_overhead: Option<PolicerOverhead>,
    pub alias: Option<String>,
    /// The bandwidth the unit advertises, e.g. `"10m"`.
    pub bandwidth: Option<String>,
    /// `restricted` or `unrestricted`, from `arp-resp`.
    pub arp_resp: Option<String>,
    pub peer_unit: Option<String>,
    pub disable_mlppp_inner_ppp_pfc: bool,
    pub host_prefix_only: bool,
    pub per_session_scheduler: bool,
    pub point_to_point: bool,
    pub multipoint: bool,
    pub reassemble_packets: bool,
    pub clear_dont_fragment_bit: bool,
    pub copy_tos_to_outer_ip_header: bool,
    pub copy_tos_to_outer_ip_header_transit: bool,
    pub extended_telemetry: bool,
    /// `proxy-macip-advertisement` on an IRB unit.
    pub proxy_macip_advertisement: bool,
    /// `enhanced-convergence` on an IRB unit.
    pub enhanced_convergence: bool,
    pub virtual_gateway_v4_mac: Option<String>,
    pub virtual_gateway_v6_mac: Option<String>,
    /// The `virtual-gateway-esi` of an IRB unit.
    pub virtual_gateway_esi: Option<VirtualGatewayEsi>,
    /// `proxy-arp` is configured; `proxy_arp_mode` is its `restricted` or `unrestricted` argument.
    pub proxy_arp: bool,
    pub proxy_arp_mode: Option<String>,
    pub ip_address_owner: Option<String>,
    pub input_vlan_map: Option<VlanMap>,
    pub output_vlan_map: Option<VlanMap>,
    /// Logical encapsulation, e.g. `"vlan-bridge"` or `"vlan-ccc"`.
    pub encapsulation: Option<String>,
    /// Every `family <name>` present, e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
    pub families: Vec<String>,
    pub inet_addresses: Vec<String>,
    pub inet6_addresses: Vec<String>,
    pub iso_addresses: Vec<String>,
    /// The settings of each `family`, in the order written.
    pub family_settings: Vec<UnitFamily>,
}

/// The `virtual-gateway-esi` of an IRB unit: an ESI identifier and `all-active`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct VirtualGatewayEsi {
    pub identifier: Option<String>,
    pub all_active: bool,
}

fn parse_virtual_gateway_esi(unit: &ConfigLine) -> Option<VirtualGatewayEsi> {
    let line =
        active_children(unit).find(|c| c.text == "virtual-gateway-esi" || c.arg("virtual-gateway-esi").is_some())?;
    let inline = line.arg("virtual-gateway-esi");
    Some(VirtualGatewayEsi {
        identifier: inline.filter(|v| *v != "all-active").map(str::to_string).or_else(|| {
            active_children(line)
                .find(|c| !c.text.contains(' ') && c.text != "all-active")
                .map(|c| c.text.clone())
        }),
        all_active: inline == Some("all-active") || has_leaf(line, "all-active"),
    })
}

pub(super) fn parse_unit(number: &str, unit: &ConfigLine) -> InterfaceUnit {
    let families: Vec<_> = named_children(unit, "family").collect();
    let addresses = |family: &str| -> Vec<String> {
        families
            .iter()
            .filter(|(name, _)| *name == family)
            .flat_map(|(_, f)| args_of(f, "address"))
            .map(str::to_string)
            .collect()
    };
    InterfaceUnit {
        number: number.to_string(),
        description: arg_of(unit, "description").map(str::to_string),
        vlan_id: arg_of(unit, "vlan-id").and_then(|v| v.parse().ok()),
        vlan_id_unmatched: arg_of(unit, "vlan-id") == Some("unmatched"),
        vlan_id_range: arg_of(unit, "vlan-id-range").map(str::to_string),
        vlan_tags: parse_vlan_tags(unit),
        tunnel: parse_tunnel(unit),
        esi: parse_esi(unit),
        ppp_options: child_block(unit, "ppp-options").map(parse_ppp_options),
        pppoe_options: child_block(unit, "pppoe-options").map(parse_pppoe_options),
        pppoe_underlying_options: child_block(unit, "pppoe-underlying-options").map(parse_pppoe_underlying_options),
        demux: child_block(unit, "demux").map(parse_demux),
        demux_options: child_block(unit, "demux-options").map(parse_demux_options),
        demux_source: args_of(unit, "demux-source").into_iter().map(str::to_string).collect(),
        demux_destination: args_of(unit, "demux-destination")
            .into_iter()
            .map(str::to_string)
            .collect(),
        advisory_options: child_block(unit, "advisory-options").map(parse_advisory_options),
        targeted_distribution: arg_of(unit, "targeted-distribution").map(str::to_string),
        accept_source_mac: parse_accept_source_mac(unit),
        policer_overhead: child_block(unit, "policer-overhead").map(parse_policer_overhead),
        alias: arg_of(unit, "alias").map(str::to_string),
        bandwidth: arg_of(unit, "bandwidth").map(str::to_string),
        arp_resp: arg_of(unit, "arp-resp").map(str::to_string),
        peer_unit: arg_of(unit, "peer-unit").map(str::to_string),
        disable_mlppp_inner_ppp_pfc: has_leaf(unit, "disable-mlppp-inner-ppp-pfc"),
        host_prefix_only: has_leaf(unit, "host-prefix-only"),
        per_session_scheduler: has_leaf(unit, "per-session-scheduler"),
        point_to_point: has_leaf(unit, "point-to-point"),
        multipoint: has_leaf(unit, "multipoint"),
        reassemble_packets: has_leaf(unit, "reassemble-packets"),
        clear_dont_fragment_bit: has_leaf(unit, "clear-dont-fragment-bit"),
        copy_tos_to_outer_ip_header: has_leaf(unit, "copy-tos-to-outer-ip-header"),
        copy_tos_to_outer_ip_header_transit: has_leaf(unit, "copy-tos-to-outer-ip-header-transit"),
        extended_telemetry: has_leaf(unit, "extended-telemetry"),
        proxy_macip_advertisement: has_leaf(unit, "proxy-macip-advertisement"),
        enhanced_convergence: has_leaf(unit, "enhanced-convergence"),
        virtual_gateway_v4_mac: arg_of(unit, "virtual-gateway-v4-mac").map(str::to_string),
        virtual_gateway_v6_mac: arg_of(unit, "virtual-gateway-v6-mac").map(str::to_string),
        virtual_gateway_esi: parse_virtual_gateway_esi(unit),
        proxy_arp: has_leaf(unit, "proxy-arp") || arg_of(unit, "proxy-arp").is_some(),
        proxy_arp_mode: arg_of(unit, "proxy-arp").map(str::to_string),
        ip_address_owner: arg_of(unit, "ip-address-owner").map(str::to_string),
        input_vlan_map: parse_vlan_map(unit, "input-vlan-map"),
        output_vlan_map: parse_vlan_map(unit, "output-vlan-map"),
        encapsulation: arg_of(unit, "encapsulation").map(str::to_string),
        inet_addresses: addresses("inet"),
        inet6_addresses: addresses("inet6"),
        iso_addresses: addresses("iso"),
        families: families.iter().map(|(name, _)| name.to_string()).collect(),
        family_settings: families
            .iter()
            .map(|(name, family)| parse_family(name, family))
            .collect(),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    const CONFIG: &str = r#"
interfaces {
    ge-0/0/0 {
        flexible-vlan-tagging;
        encapsulation flexible-ethernet-services;
        unit 100 {
            encapsulation vlan-bridge;
            vlan-id 100;
            input-vlan-map {
                swap;
                tag-protocol-id 0x8100;
                vlan-id 200;
            }
            output-vlan-map swap;
        }
        unit 200 {
            vlan-tags outer 0x8100.200 inner 0x8100.300;
            input-vlan-map {
                push-push;
                tag-protocol-id 0x9100;
                vlan-id 10;
                inner-tag-protocol-id 0x8100;
                inner-vlan-id 20;
            }
            output-vlan-map {
                pop-pop;
            }
        }
        unit 300 {
            vlan-id unmatched;
        }
        unit 400 {
            vlan-id-range 400-499;
            vlan-tags outer 500 inner-range 1-4094;
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_tunnel_options_and_vxlan_gpe() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    gr-0/0/0 {
        unit 0 {
            reassemble-packets;
            clear-dont-fragment-bit;
            copy-tos-to-outer-ip-header;
            copy-tos-to-outer-ip-header-transit;
            extended-telemetry;
            tunnel {
                allow-fragmentation;
                do-not-fragment;
                no-path-mtu-discovery;
                ttl 64;
                traffic-class 8;
                backup-destination 192.0.2.7;
                encapsulation gre {
                    tunnel-routing-instance {
                        routing-instance RI1;
                    }
                }
            }
            family inet {
                next-hop-tunnel 10.0.0.1 ipsec-vpn vpn1;
                next-hop-tunnel 10.0.0.2 ipsec-vpn vpn2;
            }
        }
    }
    fti0 {
        unit 0 {
            tunnel {
                path-mtu-discovery;
                encapsulation vxlan-gpe {
                    source {
                        address 192.0.2.1;
                        interface lo0.0;
                    }
                    destination {
                        address 192.0.2.2;
                    }
                    destination-udp-port 4790;
                    source-udp-port-range {
                        min 49152;
                        max 65535;
                    }
                    tunnel-endpoint vxlan;
                    vni 5000;
                    allow-fragmentation;
                }
            }
        }
        unit 1 {
            tunnel {
                encapsulation udp;
            }
        }
    }
}
"#,
        ));
        let gr = &ifaces[0].units[0];
        assert!(gr.reassemble_packets && gr.clear_dont_fragment_bit && gr.extended_telemetry);
        assert!(gr.copy_tos_to_outer_ip_header && gr.copy_tos_to_outer_ip_header_transit);
        let tunnel = gr.tunnel.as_ref().unwrap();
        assert!(tunnel.allow_fragmentation && tunnel.do_not_fragment);
        assert_eq!(tunnel.path_mtu_discovery, Some(false));
        assert_eq!(tunnel.ttl.as_deref(), Some("64"));
        assert_eq!(tunnel.traffic_class.as_deref(), Some("8"));
        assert_eq!(tunnel.backup_destination.as_deref(), Some("192.0.2.7"));
        assert_eq!(tunnel.tunnel_routing_instance.as_deref(), Some("RI1"));
        let hops = &gr.family_settings[0].next_hop_tunnel;
        assert_eq!(hops.len(), 2);
        assert_eq!(hops[1].gateway_address, "10.0.0.2");
        assert_eq!(hops[1].ipsec_vpn, "vpn2");

        let fti = ifaces[1].units[0].tunnel.as_ref().unwrap();
        assert_eq!(fti.encapsulation.as_deref(), Some("vxlan-gpe"));
        assert_eq!(fti.source_address.as_deref(), Some("192.0.2.1"));
        assert_eq!(fti.source_interface.as_deref(), Some("lo0.0"));
        assert_eq!(fti.destination_address.as_deref(), Some("192.0.2.2"));
        assert_eq!(fti.destination_udp_port.as_deref(), Some("4790"));
        assert_eq!(fti.source_udp_port_min.as_deref(), Some("49152"));
        assert_eq!(fti.source_udp_port_max.as_deref(), Some("65535"));
        assert_eq!(fti.tunnel_endpoint.as_deref(), Some("vxlan"));
        assert_eq!(fti.vni.as_deref(), Some("5000"));
        assert!(fti.allow_fragmentation);
        assert_eq!(fti.path_mtu_discovery, Some(true));

        let plain = ifaces[1].units[1].tunnel.as_ref().unwrap();
        assert!(!plain.allow_fragmentation && plain.ttl.is_none() && plain.vni.is_none());
        assert!(plain.path_mtu_discovery.is_none() && plain.tunnel_routing_instance.is_none());
        assert!(!ifaces[1].units[1].reassemble_packets && !ifaces[1].units[1].extended_telemetry);
    }

    #[test]
    fn extracts_unit_scalars_and_flags() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ge-0/0/0 {
        unit 0 {
            alias core-link;
            bandwidth 10m;
            arp-resp restricted;
            peer-unit 1;
            disable-mlppp-inner-ppp-pfc;
            host-prefix-only;
            per-session-scheduler;
            point-to-point;
            multipoint;
        }
        unit 1 {
            description plain;
        }
    }
}
"#,
        ));
        let units = &ifaces[0].units;
        assert_eq!(units[0].alias.as_deref(), Some("core-link"));
        assert_eq!(units[0].bandwidth.as_deref(), Some("10m"));
        assert_eq!(units[0].arp_resp.as_deref(), Some("restricted"));
        assert_eq!(units[0].peer_unit.as_deref(), Some("1"));
        assert!(units[0].disable_mlppp_inner_ppp_pfc && units[0].host_prefix_only && units[0].per_session_scheduler);
        assert!(units[0].point_to_point && units[0].multipoint);
        assert!(units[1].alias.is_none() && units[1].bandwidth.is_none() && units[1].arp_resp.is_none());
        assert!(units[1].peer_unit.is_none() && !units[1].host_prefix_only && !units[1].point_to_point);
    }

    #[test]
    fn extracts_unit_ppp_and_pppoe_options() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    pp0 {
        unit 0 {
            pppoe-options {
                access-concentrator AC1;
                auto-reconnect 10;
                client;
                ignore-eol-tag;
                service-name isp;
                underlying-interface ge-0/0/0.0;
                ppp-max-payload 1500;
            }
            ppp-options {
                peer-ip-address-optional;
                initiate-ncp ipv6;
                chap {
                    challenge-length minimum 8 maximum 16;
                }
            }
        }
        unit 1 {
            pppoe-options {
                server;
            }
        }
    }
    ge-0/0/1 {
        unit 100 {
            pppoe-underlying-options {
                access-concentrator AC2;
                dynamic-profile pppoe-prof;
                direct-connect;
                duplicate-protection {
                    include {
                        relay-session-id;
                    }
                }
                max-sessions 500;
                max-sessions-vsa-ignore;
                service-name-table SNT;
                short-cycle-protection lockout-time-min 1 lockout-time-max 60 filter [ aci ];
            }
        }
        unit 200 {
            family pppoe {
                max-sessions 10;
                duplicate-protection;
                short-cycle-protection;
            }
        }
        unit 300 {
            description plain;
        }
    }
}
"#,
        ));
        let pp0 = &ifaces[0].units;
        let pppoe = pp0[0].pppoe_options.as_ref().unwrap();
        assert_eq!(pppoe.access_concentrator.as_deref(), Some("AC1"));
        assert_eq!(pppoe.auto_reconnect.as_deref(), Some("10"));
        assert_eq!(pppoe.role.as_deref(), Some("client"));
        assert!(pppoe.ignore_eol_tag);
        assert_eq!(pppoe.service_name.as_deref(), Some("isp"));
        assert_eq!(pppoe.underlying_interface.as_deref(), Some("ge-0/0/0.0"));
        assert_eq!(pppoe.ppp_max_payload.as_deref(), Some("1500"));
        let ppp = pp0[0].ppp_options.as_ref().unwrap();
        assert!(ppp.peer_ip_address_optional);
        assert_eq!(ppp.initiate_ncp.as_deref(), Some("ipv6"));
        assert_eq!(
            ppp.chap.as_ref().unwrap().challenge_length_maximum.as_deref(),
            Some("16")
        );
        assert_eq!(pp0[1].pppoe_options.as_ref().unwrap().role.as_deref(), Some("server"));
        assert!(pp0[1].ppp_options.is_none());

        let units = &ifaces[1].units;
        let underlying = units[0].pppoe_underlying_options.as_ref().unwrap();
        assert_eq!(underlying.access_concentrator.as_deref(), Some("AC2"));
        assert_eq!(underlying.dynamic_profile.as_deref(), Some("pppoe-prof"));
        assert!(underlying.direct_connect && underlying.max_sessions_vsa_ignore);
        assert!(
            underlying
                .duplicate_protection
                .as_ref()
                .unwrap()
                .include_relay_session_id
        );
        assert_eq!(underlying.max_sessions.as_deref(), Some("500"));
        assert_eq!(underlying.service_name_table.as_deref(), Some("SNT"));
        let cycle = underlying.short_cycle_protection.as_ref().unwrap();
        assert_eq!(cycle.lockout_time_min.as_deref(), Some("1"));
        assert_eq!(cycle.lockout_time_max.as_deref(), Some("60"));
        assert_eq!(cycle.filter, ["aci"]);

        let family = units[1].family_settings[0].pppoe.as_ref().unwrap();
        assert_eq!(family.max_sessions.as_deref(), Some("10"));
        assert!(!family.duplicate_protection.as_ref().unwrap().include_relay_session_id);
        let bare = family.short_cycle_protection.as_ref().unwrap();
        assert!(bare.lockout_time_min.is_none() && bare.filter.is_empty());

        assert!(units[2].pppoe_underlying_options.is_none() && units[2].pppoe_options.is_none());
        assert!(units[2].ppp_options.is_none());
    }

    #[test]
    fn extracts_unit_vlan_id_forms_and_encapsulation() {
        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
        assert_eq!(units[0].vlan_id, Some(100));
        assert!(!units[0].vlan_id_unmatched);
        assert_eq!(units[0].encapsulation.as_deref(), Some("vlan-bridge"));

        assert_eq!(units[2].vlan_id, None);
        assert!(units[2].vlan_id_unmatched);

        assert_eq!(units[3].vlan_id_range.as_deref(), Some("400-499"));
        assert_eq!(units[1].encapsulation, None);
    }

    #[test]
    fn extracts_vlan_tags() {
        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
        let tags = units[1].vlan_tags.as_ref().unwrap();
        assert_eq!(tags.outer.as_deref(), Some("0x8100.200"));
        assert_eq!(tags.inner.as_deref(), Some("0x8100.300"));
        assert_eq!(tags.inner_range, None);

        let ranged = units[3].vlan_tags.as_ref().unwrap();
        assert_eq!(ranged.outer.as_deref(), Some("500"));
        assert_eq!(ranged.inner, None);
        assert_eq!(ranged.inner_range.as_deref(), Some("1-4094"));

        assert!(units[0].vlan_tags.is_none());
    }

    #[test]
    fn extracts_vlan_maps_in_block_and_one_line_form() {
        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;

        let input = units[0].input_vlan_map.as_ref().unwrap();
        assert_eq!(input.operation.as_deref(), Some("swap"));
        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x8100"));
        assert_eq!(input.vlan_id.as_deref(), Some("200"));
        assert_eq!(input.inner_vlan_id, None);
        assert_eq!(
            units[0].output_vlan_map.as_ref().unwrap().operation.as_deref(),
            Some("swap")
        );

        let input = units[1].input_vlan_map.as_ref().unwrap();
        assert_eq!(input.operation.as_deref(), Some("push-push"));
        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x9100"));
        assert_eq!(input.vlan_id.as_deref(), Some("10"));
        assert_eq!(input.inner_tag_protocol_id.as_deref(), Some("0x8100"));
        assert_eq!(input.inner_vlan_id.as_deref(), Some("20"));
        assert_eq!(
            units[1].output_vlan_map.as_ref().unwrap().operation.as_deref(),
            Some("pop-pop")
        );

        assert!(units[2].input_vlan_map.is_none());
        assert!(units[2].output_vlan_map.is_none());
    }

    #[test]
    fn extracts_bridge_family_vlan_ids() {
        let ifaces = interfaces(&juniper::parse_braces(
            "interfaces {\n    ge-0/0/1 {\n        unit 0 {\n            family bridge {\n                vlan-id-list [ 100-200 300 ];\n                vlan-id 50;\n            }\n        }\n    }\n}\n",
        ));
        let bridge = &ifaces[0].units[0].family_settings[0];
        assert_eq!(bridge.name, "bridge");
        assert_eq!(bridge.vlan_id_list, ["100-200", "300"]);
        assert_eq!(bridge.vlan_id.as_deref(), Some("50"));
    }

    // Adapted from the EVPN-VXLAN User Guide, the IGMP snooping example: the `irb` units of the first leaf device; see
    // the README "Sources".
    #[test]
    fn reads_the_guide_virtual_gateway_address() {
        let ifaces = interfaces(&juniper::parse_braces(
            "interfaces {\n    irb {\n        unit 1 {\n            family inet {\n                address 10.1.1.1/24 {\n                    virtual-gateway-address 10.1.1.10;\n                }\n            }\n        }\n        unit 2 {\n            family inet {\n                address 10.2.1.1/24 {\n                    virtual-gateway-address 10.2.1.10;\n                }\n            }\n        }\n    }\n}\n",
        ));
        let units = &ifaces[0].units;
        let address = &units[0].family_settings[0].addresses[0];
        assert_eq!(address.address, "10.1.1.1/24");
        assert_eq!(address.virtual_gateway_address.as_deref(), Some("10.1.1.10"));
        assert_eq!(
            units[1].family_settings[0].addresses[0]
                .virtual_gateway_address
                .as_deref(),
            Some("10.2.1.10")
        );
    }

    // Not from documentation: written from the CLI Reference pages; `virtual-gateway-esi` follows the `set` commands of
    // the EVPN-VXLAN WAN example, in brace form. See the README "Sources".
    #[test]
    fn reads_the_irb_unit_statements() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    irb {
        unit 1 {
            proxy-macip-advertisement;
            enhanced-convergence;
            virtual-gateway-v4-mac 00:00:5e:00:01:01;
            virtual-gateway-v6-mac 00:00:5e:00:02:01;
            virtual-gateway-esi {
                00:11:aa:aa:aa:aa:aa:aa:aa:aa;
                all-active;
            }
            proxy-arp restricted;
            ip-address-owner SERVICE;
            family inet6 {
                nd6-max-cache 1000;
                nd6-stale-time 1200;
                nd6-new-hold-limit 5;
            }
        }
        unit 2 {
            proxy-arp;
            family inet;
        }
    }
    ge-0/0/0 {
        media-type fiber;
        ether-options {
            mdi-mode auto;
            no-auto-mdix;
        }
    }
}
"#,
        ));
        let unit = &ifaces[0].units[0];
        assert!(unit.proxy_macip_advertisement && unit.enhanced_convergence);
        assert_eq!(unit.virtual_gateway_v4_mac.as_deref(), Some("00:00:5e:00:01:01"));
        assert_eq!(unit.virtual_gateway_v6_mac.as_deref(), Some("00:00:5e:00:02:01"));
        let esi = unit.virtual_gateway_esi.as_ref().unwrap();
        assert_eq!(esi.identifier.as_deref(), Some("00:11:aa:aa:aa:aa:aa:aa:aa:aa"));
        assert!(esi.all_active);
        assert!(unit.proxy_arp);
        assert_eq!(unit.proxy_arp_mode.as_deref(), Some("restricted"));
        assert_eq!(unit.ip_address_owner.as_deref(), Some("SERVICE"));
        let inet6 = &unit.family_settings[0];
        assert_eq!(inet6.nd6_max_cache.as_deref(), Some("1000"));
        assert_eq!(inet6.nd6_stale_time.as_deref(), Some("1200"));
        assert_eq!(inet6.nd6_new_hold_limit.as_deref(), Some("5"));

        let bare = &ifaces[0].units[1];
        assert!(bare.proxy_arp);
        assert_eq!(bare.proxy_arp_mode, None);
        assert!(!bare.proxy_macip_advertisement && bare.virtual_gateway_esi.is_none());

        let port = &ifaces[1];
        assert_eq!(port.media_type.as_deref(), Some("fiber"));
        let ether = port.ether_options.as_ref().unwrap();
        assert_eq!(ether.mdi_mode.as_deref(), Some("auto"));
        assert!(ether.no_auto_mdix);
    }
}