asterust-junos 0.5.12

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
//! `interfaces`
//! `interfaces { ... }`.
//!
//! # See also
//!
//! - [`[edit interfaces]` hierarchy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/topic-map/hierarchy-edit-interfaces.html)
//! - [mtu (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mtu-edit-interfaces-ni.html)
//! - [speed (Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/speed-edit-interfaces-ethernet.html)
//! - [encapsulation (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/encapsulation-edit-interfaces-physical.html)
//! - [vlan-tagging](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-tagging-edit-interfaces.html)
//! - [flexible-vlan-tagging](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/flexible-vlan-tagging-edit-interfaces.html)
//! - [native-vlan-id](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/native-vlan-id-edit-interfaces-qfx-series.html)
//! - [bypass-queuing-chip](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bypass-queuing-chip-edit-interfaces.html)
//! - [cascade-port](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/cascade-port-edit-interfaces.html)
//! - [no-per-unit-scheduler](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-per-unit-scheduler-edit-interfaces-cs-cos.html)
//! - [stacked-vlan-tagging](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/stacked-vlan-tagging-edit-interfaces.html)
//! - [oam-on-svlan](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/oam-on-svlan-edit-interfaces.html)
//! - [promiscuous-mode](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-edit-promiscuous-mode.html)
//! - [no-native-vlan-insert](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-native-vlan-insert.html)
//! - [shared-scheduler](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/shared-scheduler-edit-interfaces.html)
//! - [unidirectional](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unidirectional-edit-interfaces.html)
//! - [gratuitous-arp-reply](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/gratuitous-arp-reply-edit-interfaces-qfx-series.html)
//! - [traps](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traps-edit-interfaces.html)
//! - [input-native-vlan-push](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/input-native-vlan-push-edit-interfaces.html)
//! - [number-of-sub-ports](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/number-of-subports.html)
//! - [accounting-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accounting-profile-edit-interfaces.html)
//! - [per-unit-scheduler](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/per-unit-scheduler-edit-interfaces.html)
//! - [no-gratuitous-arp-request](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-gratuitous-arp-request-edit-interfaces-port-security.html)

mod aggregated_ether_options;
mod aggregated_inline_services_options;
mod auto_configure;
mod es_options;
mod esi;
mod ether_options;
mod forwarding_class_accounting;
mod hierarchical_scheduler;
mod hold_time;
mod interface_range;
mod interface_set;
mod keepalives;
mod layer2_policer;
mod link_degrade_monitor;
mod load_balancing_options;
mod logical_tunnel_options;
mod multi_chassis_protection;
mod multiservice_options;
mod optics_options;
mod otn_options;
mod ppp_options;
mod radius_options;
mod redundancy_options;
mod redundant_ether_options;
mod services_options;
mod targeted_options;
mod tca;
mod unit;

pub use aggregated_ether_options::{
    AggregatedEtherOptions, BfdDamping, BfdLivenessDetection, Dlb, Lacp, LinkProtection, LinkProtectionSubGroup,
    LoadBalance, McAe, McAeEvents, RebalancePeriodic,
};
pub use aggregated_inline_services_options::AggregatedInlineServicesOptions;
pub use auto_configure::{AutoConfigure, AutoConfigureProfile, AutoConfigureRanges};
pub use es_options::EsOptions;
pub use esi::{Esi, EsiAutoDerive, EsiDfElectionType, EsiDfGranularity, EsiPreference, EsiSystemMac};
pub use ether_options::{
    EtherOptions, EthernetSwitchForwardingClass, EthernetSwitchPolicer, EthernetSwitchProfile, Ieee8023ad,
};
pub use forwarding_class_accounting::{EnhancedForwardingClassAccounting, ForwardingClassAccounting};
pub use hierarchical_scheduler::{hierarchical_scheduler, HierarchicalScheduler};
pub use hold_time::HoldTime;
pub use interface_range::{interface_ranges, InterfaceRange, MemberRange};
pub use interface_set::{interface_sets, InterfaceSet, InterfaceSetMember};
pub use keepalives::Keepalives;
pub use layer2_policer::Layer2Policer;
pub use link_degrade_monitor::{LinkDegradeMonitor, LinkDegradeThresholds};
pub use load_balancing_options::LoadBalancingOptions;
pub use logical_tunnel_options::{logical_tunnel_options, LogicalTunnelOptions};
pub use multi_chassis_protection::MultiChassisProtection;
pub use multiservice_options::MultiserviceOptions;
pub use optics_options::{OpticsApplication, OpticsLowLight, OpticsOptions};
pub use otn_options::{OduDelayManagement, OtnOptions, OtnRate, OtnTrigger, PreemptiveFastReroute, SignalDegrade};
pub use ppp_options::{PppChap, PppCompression, PppOptions, PppPap};
pub use radius_options::{NasPortExtendedFormat, NasPortOptions, RadiusOptions};
pub use redundancy_options::RedundancyOptions;
pub use redundant_ether_options::RedundantEtherOptions;
pub use services_options::{ServicesOptions, ServicesSyslog, ServicesSyslogHost, SessionLimit};
pub use targeted_options::TargetedOptions;
pub use tca::Tca;
pub use unit::{
    AddressArp, AdvisoryOptions, BandwidthThreshold, Demux, DemuxAddressRanges, DemuxDynamicProfile, DemuxFamily,
    DemuxNetwork, DemuxOptions, DemuxRange, DemuxUsernameInclude, DhcpClient, DhcpClientIdentifier, Dhcpv6Client,
    DuplicateProtection, EthernetSwitching, FamilyFilter, FamilyPolicer, FamilySampling, InterfaceUnit, PppoeOptions,
    PppoeUnderlyingOptions, PrefixDelegating, RouterAdvertisementInterface, RpfCheck, ShortCycleProtection,
    TrackedInterface, TrackedRoute, UnitAddress, UnitFamily, UnitTunnel, VirtualGatewayEsi, VlanMap, VlanTags,
    VrrpGroup, VrrpInheritFrom, VrrpTrack,
};

use crate::{active_children, arg_of, child_block, has_leaf, named_children, toggle, top_level};
use aggregated_ether_options::parse_aggregated_ether_options;
use aggregated_inline_services_options::parse_aggregated_inline_services_options;
use asterust::config::{Config, ConfigLine};
use auto_configure::parse_auto_configure;
use es_options::parse_es_options;
use esi::parse_esi;
use ether_options::parse_ether_options;
use forwarding_class_accounting::parse_forwarding_class_accounting;
use hierarchical_scheduler::parse_hierarchical_scheduler;
use hold_time::parse_hold_time;
use keepalives::parse_keepalives;
use layer2_policer::parse_layer2_policer;
use link_degrade_monitor::parse_link_degrade_monitor;
use load_balancing_options::parse_load_balancing_options;
use multi_chassis_protection::parse_multi_chassis_protection;
use multiservice_options::parse_multiservice_options;
use optics_options::parse_optics_options;
use otn_options::parse_otn_options;
use ppp_options::parse_ppp_options;
use radius_options::parse_radius_options;
use redundancy_options::parse_redundancy_options;
use redundant_ether_options::parse_redundant_ether_options;
use services_options::parse_services_options;
use targeted_options::parse_targeted_options;
use unit::parse_unit;

/// A physical or loopback interface such as `ge-0/0/0` or `lo0`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Interface {
    pub name: String,
    pub description: Option<String>,
    /// Set by the `disable` statement.
    pub disabled: bool,
    pub mtu: Option<String>,
    /// e.g. `"1g"` or `"auto"`.
    pub speed: Option<String>,
    /// Physical encapsulation, e.g. `"flexible-ethernet-services"`.
    pub encapsulation: Option<String>,
    /// `"copper"` or `"fiber"`, for a dual-purpose uplink port.
    pub media_type: Option<String>,
    pub vlan_tagging: bool,
    pub flexible_vlan_tagging: bool,
    pub native_vlan_id: Option<String>,
    pub per_unit_scheduler: bool,
    pub no_gratuitous_arp_request: bool,
    pub hold_time: Option<HoldTime>,
    pub ether_options: Option<EtherOptions>,
    pub aggregated_ether_options: Option<AggregatedEtherOptions>,
    pub auto_configure: Option<AutoConfigure>,
    pub optics_options: Option<OpticsOptions>,
    pub otn_options: Option<OtnOptions>,
    pub link_degrade_monitor: Option<LinkDegradeMonitor>,
    pub bypass_queuing_chip: bool,
    pub cascade_port: bool,
    pub no_per_unit_scheduler: bool,
    pub no_keepalives: bool,
    pub stacked_vlan_tagging: bool,
    pub oam_on_svlan: bool,
    pub promiscuous_mode: bool,
    pub no_native_vlan_insert: bool,
    pub shared_scheduler: bool,
    pub unidirectional: bool,
    /// `Some(true)` for `gratuitous-arp-reply`, `Some(false)` for `no-gratuitous-arp-reply`.
    pub gratuitous_arp_reply: Option<bool>,
    /// `Some(true)` for `traps`, `Some(false)` for `no-traps`.
    pub traps: Option<bool>,
    /// `Some(true)` for `input-native-vlan-push enable`, `Some(false)` for `disable`.
    pub input_native_vlan_push: Option<bool>,
    pub number_of_sub_ports: Option<String>,
    pub accounting_profile: Option<String>,
    pub keepalives: Option<Keepalives>,
    pub hierarchical_scheduler: Option<HierarchicalScheduler>,
    pub layer2_policer: Option<Layer2Policer>,
    pub redundant_ether_options: Option<RedundantEtherOptions>,
    pub es_options: Option<EsOptions>,
    pub multi_chassis_protection: Option<MultiChassisProtection>,
    pub esi: Option<Esi>,
    pub aggregated_inline_services_options: Option<AggregatedInlineServicesOptions>,
    pub forwarding_class_accounting: Option<ForwardingClassAccounting>,
    pub ppp_options: Option<PppOptions>,
    pub radius_options: Option<RadiusOptions>,
    pub targeted_options: Option<TargetedOptions>,
    pub load_balancing_options: Option<LoadBalancingOptions>,
    pub multiservice_options: Option<MultiserviceOptions>,
    pub redundancy_options: Option<RedundancyOptions>,
    pub services_options: Option<ServicesOptions>,
    pub units: Vec<InterfaceUnit>,
}

/// Blocks written directly under `interfaces` that are not interfaces.
const GLOBAL_BLOCKS: [&str; 2] = ["hierarchical-scheduler", "logical-tunnel-options"];

/// Returns every interface under the top-level `interfaces` block.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::interfaces::interfaces;
///
/// let config = juniper::parse_braces("interfaces {\n    ge-0/0/0 {\n        mtu 9192;\n    }\n}\n");
/// let found = interfaces(&config);
/// assert_eq!(found[0].name, "ge-0/0/0");
/// assert_eq!(found[0].mtu.as_deref(), Some("9192"));
/// ```
pub fn interfaces(config: &Config) -> Vec<Interface> {
    let Some(root) = top_level(config, "interfaces") else {
        return Vec::new();
    };
    // Interface names have no spaces; this skips `apply-groups`, `interface-range <x>` and similar.
    active_children(root)
        .filter(|c| !c.text.contains(' ') && !GLOBAL_BLOCKS.contains(&c.text.as_str()))
        .map(parse_interface)
        .collect()
}

fn parse_interface(line: &ConfigLine) -> Interface {
    Interface {
        name: line.text.clone(),
        description: arg_of(line, "description").map(str::to_string),
        disabled: has_leaf(line, "disable"),
        mtu: arg_of(line, "mtu").map(str::to_string),
        speed: arg_of(line, "speed").map(str::to_string),
        encapsulation: arg_of(line, "encapsulation").map(str::to_string),
        media_type: arg_of(line, "media-type").map(str::to_string),
        vlan_tagging: has_leaf(line, "vlan-tagging"),
        flexible_vlan_tagging: has_leaf(line, "flexible-vlan-tagging"),
        native_vlan_id: arg_of(line, "native-vlan-id").map(str::to_string),
        per_unit_scheduler: has_leaf(line, "per-unit-scheduler"),
        no_gratuitous_arp_request: has_leaf(line, "no-gratuitous-arp-request"),
        hold_time: parse_hold_time(line),
        ether_options: parse_ether_options(line),
        aggregated_ether_options: child_block(line, "aggregated-ether-options").map(parse_aggregated_ether_options),
        auto_configure: child_block(line, "auto-configure").map(parse_auto_configure),
        optics_options: child_block(line, "optics-options").map(parse_optics_options),
        otn_options: child_block(line, "otn-options").map(parse_otn_options),
        link_degrade_monitor: child_block(line, "link-degrade-monitor").map(parse_link_degrade_monitor),
        bypass_queuing_chip: has_leaf(line, "bypass-queuing-chip"),
        cascade_port: has_leaf(line, "cascade-port"),
        no_per_unit_scheduler: has_leaf(line, "no-per-unit-scheduler"),
        no_keepalives: has_leaf(line, "no-keepalives"),
        stacked_vlan_tagging: has_leaf(line, "stacked-vlan-tagging"),
        oam_on_svlan: has_leaf(line, "oam-on-svlan"),
        promiscuous_mode: has_leaf(line, "promiscuous-mode"),
        no_native_vlan_insert: has_leaf(line, "no-native-vlan-insert"),
        shared_scheduler: has_leaf(line, "shared-scheduler"),
        unidirectional: has_leaf(line, "unidirectional"),
        gratuitous_arp_reply: toggle(line, "gratuitous-arp-reply"),
        traps: toggle(line, "traps"),
        input_native_vlan_push: arg_of(line, "input-native-vlan-push").map(|v| v == "enable"),
        number_of_sub_ports: arg_of(line, "number-of-sub-ports").map(str::to_string),
        accounting_profile: arg_of(line, "accounting-profile").map(str::to_string),
        keepalives: parse_keepalives(line),
        hierarchical_scheduler: parse_hierarchical_scheduler(line),
        layer2_policer: child_block(line, "layer2-policer").map(parse_layer2_policer),
        redundant_ether_options: child_block(line, "redundant-ether-options").map(parse_redundant_ether_options),
        es_options: child_block(line, "es-options").map(parse_es_options),
        multi_chassis_protection: parse_multi_chassis_protection(line),
        esi: parse_esi(line),
        forwarding_class_accounting: child_block(line, "forwarding-class-accounting")
            .map(parse_forwarding_class_accounting),
        ppp_options: child_block(line, "ppp-options").map(parse_ppp_options),
        radius_options: child_block(line, "radius-options").map(parse_radius_options),
        targeted_options: child_block(line, "targeted-options").map(parse_targeted_options),
        aggregated_inline_services_options: child_block(line, "aggregated-inline-services-options")
            .map(parse_aggregated_inline_services_options),
        load_balancing_options: child_block(line, "load-balancing-options").map(parse_load_balancing_options),
        multiservice_options: child_block(line, "multiservice-options").map(parse_multiservice_options),
        redundancy_options: child_block(line, "redundancy-options").map(parse_redundancy_options),
        services_options: child_block(line, "services-options").map(parse_services_options),
        units: named_children(line, "unit")
            .map(|(number, unit)| parse_unit(number, unit))
            .collect(),
    }
}

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

    // Not from documentation: covers statements the Juniper examples do not use. See the README "Sources".
    const CONFIG: &str = r#"
interfaces {
    ge-0/0/0 {
        description "to p1";
        unit 0 {
            description "core link";
            family inet {
                address 10.0.1.1/30;
                address 10.0.2.1/30 {
                    primary;
                }
            }
            family inet6 {
                address 2001:db8:1::1/64;
            }
            family iso;
            family mpls;
        }
    }
    ge-0/0/1 {
        disable;
        vlan-tagging;
        unit 100 {
            vlan-id 100;
            family inet {
                address 192.0.2.1/24;
            }
        }
    }
    inactive: ge-0/0/2 {
        unit 0;
    }
    lo0 {
        unit 0 {
            family inet {
                address 10.255.0.1/32;
            }
            family iso {
                address 49.0001.0100.2550.0001.00;
            }
        }
    }
    apply-groups GLOBAL;
}
"#;

    #[test]
    fn extracts_units_families_and_addresses() {
        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
        let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
        assert_eq!(names, ["ge-0/0/0", "ge-0/0/1", "lo0"]);

        let core = &ifaces[0];
        assert_eq!(core.description.as_deref(), Some("to p1"));
        assert!(!core.disabled);
        let unit = &core.units[0];
        assert_eq!(unit.number, "0");
        assert_eq!(unit.description.as_deref(), Some("core link"));
        assert_eq!(unit.families, ["inet", "inet6", "iso", "mpls"]);
        assert_eq!(unit.inet_addresses, ["10.0.1.1/30", "10.0.2.1/30"]);
        assert_eq!(unit.inet6_addresses, ["2001:db8:1::1/64"]);
        assert!(unit.iso_addresses.is_empty());
    }

    #[test]
    fn disabled_vlan_units_and_loopback_net() {
        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
        assert!(ifaces[1].disabled);
        assert_eq!(ifaces[1].units[0].number, "100");
        assert_eq!(ifaces[1].units[0].vlan_id, Some(100));
        assert_eq!(ifaces[2].units[0].iso_addresses, ["49.0001.0100.2550.0001.00"]);
    }

    #[test]
    fn missing_interfaces_block_is_empty() {
        assert!(interfaces(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_empty());
    }

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_physical_interface_settings() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    xe-0/0/0 {
        mtu 9192;
        speed 10g;
        encapsulation flexible-ethernet-services;
        vlan-tagging;
        flexible-vlan-tagging;
        native-vlan-id 10;
        per-unit-scheduler;
        no-gratuitous-arp-request;
    }
    inactive: xe-0/0/1 {
        mtu 1500;
    }
    xe-0/0/2 {
        inactive: mtu 1500;
    }
}
"#,
        ));
        assert_eq!(ifaces.len(), 2);

        let xe0 = &ifaces[0];
        assert_eq!(xe0.mtu.as_deref(), Some("9192"));
        assert_eq!(xe0.speed.as_deref(), Some("10g"));
        assert_eq!(xe0.encapsulation.as_deref(), Some("flexible-ethernet-services"));
        assert!(xe0.vlan_tagging);
        assert!(xe0.flexible_vlan_tagging);
        assert_eq!(xe0.native_vlan_id.as_deref(), Some("10"));
        assert!(xe0.per_unit_scheduler);
        assert!(xe0.no_gratuitous_arp_request);

        assert_eq!(ifaces[1].mtu, None);
    }

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_more_interface_level_statements() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    xe-0/0/1 {
        accounting-profile ap1;
        bypass-queuing-chip;
        cascade-port;
        no-per-unit-scheduler;
        no-keepalives;
        stacked-vlan-tagging;
        oam-on-svlan;
        promiscuous-mode;
        no-native-vlan-insert;
        shared-scheduler;
        unidirectional;
        gratuitous-arp-reply;
        no-traps;
        input-native-vlan-push disable;
        number-of-sub-ports 4;
        keepalives interval 10 down-count 5 up-count 2;
        hierarchical-scheduler {
            implicit-hierarchy;
            maximum-hierarchy-levels 3;
        }
        layer2-policer {
            input-policer p-in;
            input-three-color p3-in;
            output-policer p-out;
            output-three-color p3-out;
            input-hierarchical-policer hp;
        }
        es-options {
            backup-interface ge-0/0/9;
        }
        multi-chassis-protection 10.1.1.1 {
            interface ae0;
        }
    }
    reth0 {
        keepalives;
        input-native-vlan-push enable;
        multi-chassis-protection {
            peer 10.2.2.2 {
                interface ae1;
            }
        }
        redundant-ether-options {
            flow-control;
            lacp {
                active;
                periodic fast;
            }
            link-speed 10g;
            no-loopback;
            minimum-links 1;
            redundancy-group 1;
            source-address-filter 00:11:22:33:44:55;
            no-source-filtering;
        }
    }
}
"#,
        ));
        let xe = &ifaces[0];
        assert_eq!(xe.accounting_profile.as_deref(), Some("ap1"));
        assert!(xe.bypass_queuing_chip && xe.cascade_port && xe.no_per_unit_scheduler && xe.no_keepalives);
        assert!(xe.stacked_vlan_tagging && xe.oam_on_svlan && xe.promiscuous_mode && xe.no_native_vlan_insert);
        assert!(xe.shared_scheduler && xe.unidirectional);
        assert_eq!(xe.gratuitous_arp_reply, Some(true));
        assert_eq!(xe.traps, Some(false));
        assert_eq!(xe.input_native_vlan_push, Some(false));
        assert_eq!(xe.number_of_sub_ports.as_deref(), Some("4"));
        let keepalives = xe.keepalives.as_ref().unwrap();
        assert_eq!(keepalives.interval.as_deref(), Some("10"));
        assert_eq!(keepalives.down_count.as_deref(), Some("5"));
        assert_eq!(keepalives.up_count.as_deref(), Some("2"));
        let scheduler = xe.hierarchical_scheduler.as_ref().unwrap();
        assert!(scheduler.implicit_hierarchy);
        assert_eq!(scheduler.maximum_hierarchy_levels.as_deref(), Some("3"));
        let policer = xe.layer2_policer.as_ref().unwrap();
        assert_eq!(policer.input_policer.as_deref(), Some("p-in"));
        assert_eq!(policer.input_three_color.as_deref(), Some("p3-in"));
        assert_eq!(policer.output_policer.as_deref(), Some("p-out"));
        assert_eq!(policer.output_three_color.as_deref(), Some("p3-out"));
        assert_eq!(policer.input_hierarchical_policer.as_deref(), Some("hp"));
        assert_eq!(
            xe.es_options.as_ref().unwrap().backup_interface.as_deref(),
            Some("ge-0/0/9")
        );
        let protection = xe.multi_chassis_protection.as_ref().unwrap();
        assert_eq!(protection.peer, "10.1.1.1");
        assert_eq!(protection.interface.as_deref(), Some("ae0"));

        let reth = &ifaces[1];
        let bare = reth.keepalives.as_ref().unwrap();
        assert!(bare.interval.is_none() && bare.down_count.is_none() && bare.up_count.is_none());
        assert_eq!(reth.input_native_vlan_push, Some(true));
        let protection = reth.multi_chassis_protection.as_ref().unwrap();
        assert_eq!(protection.peer, "10.2.2.2");
        assert_eq!(protection.interface.as_deref(), Some("ae1"));
        let options = reth.redundant_ether_options.as_ref().unwrap();
        assert_eq!(options.flow_control, Some(true));
        assert_eq!(options.lacp.as_ref().unwrap().mode.as_deref(), Some("active"));
        assert_eq!(options.lacp.as_ref().unwrap().periodic.as_deref(), Some("fast"));
        assert_eq!(options.link_speed.as_deref(), Some("10g"));
        assert_eq!(options.loopback, Some(false));
        assert_eq!(options.minimum_links.as_deref(), Some("1"));
        assert_eq!(options.redundancy_group.as_deref(), Some("1"));
        assert_eq!(options.source_address_filter, ["00:11:22:33:44:55"]);
        assert_eq!(options.source_filtering, Some(false));

        assert!(!reth.bypass_queuing_chip && reth.gratuitous_arp_reply.is_none() && reth.traps.is_none());
        assert!(reth.hierarchical_scheduler.is_none() && reth.layer2_policer.is_none() && reth.es_options.is_none());
        assert!(xe.redundant_ether_options.is_none());
    }

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn global_blocks_are_not_interfaces() {
        let config = juniper::parse_braces(
            r#"
interfaces {
    hierarchical-scheduler {
        implicit-hierarchy;
        maximum-hierarchy-levels 3;
    }
    logical-tunnel-options {
        per-unit-mac-disable;
    }
    interface-range r1 {
        member ge-0/0/1;
    }
    ge-0/0/0 {
        mtu 9000;
    }
}
"#,
        );
        let names: Vec<_> = interfaces(&config).into_iter().map(|i| i.name).collect();
        assert_eq!(names, ["ge-0/0/0"]);
        let scheduler = hierarchical_scheduler(&config).unwrap();
        assert!(scheduler.implicit_hierarchy);
        assert_eq!(scheduler.maximum_hierarchy_levels.as_deref(), Some("3"));
        assert!(hierarchical_scheduler(&juniper::parse_braces(
            "interfaces {\n    ge-0/0/0 {\n        disable;\n    }\n}\n"
        ))
        .is_none());
    }

    #[test]
    fn physical_interface_settings_default_to_off() {
        let ifaces = interfaces(&juniper::parse_braces(
            "interfaces {\n    ge-0/0/0 {\n        disable;\n    }\n}\n",
        ));
        let ge = &ifaces[0];
        assert_eq!(ge.mtu, None);
        assert_eq!(ge.speed, None);
        assert_eq!(ge.encapsulation, None);
        assert!(!ge.vlan_tagging);
        assert!(!ge.flexible_vlan_tagging);
        assert_eq!(ge.native_vlan_id, None);
        assert!(!ge.per_unit_scheduler);
        assert!(!ge.no_gratuitous_arp_request);
    }

    // The DOC_* fixtures are adapted from Juniper's BGP User Guide, "BGP Peering Sessions"; see the README "Sources".

    // "Example: Configuring External BGP Point-to-Point Peer Sessions", Device E `show interfaces` (two of four interfaces).
    const DOC_POINT_TO_POINT: &str = r#"
interfaces {
    ge-1/2/0 {
        unit 0 {
            description to-A;
            family inet {
                address 10.10.10.1/30;
            }
        }
    }
    ge-0/0/1 {
        unit 5 {
            description to-B;
            family inet {
                address 10.10.10.5/30;
            }
        }
    }
}
"#;

    // "Example: Configure Multiple Single-Hop EBGP Sessions on Different Links Using the Same IPv6 Link-Local
    // Address", R1 `show interfaces`.
    const DOC_LINK_LOCAL: &str = r#"
interfaces {
    ge-0/0/1 {
        description R1-to-R2-Link;
        vlan-tagging;
        unit 1 {
            vlan-id 1;
            family inet6 {
                address fe80::10/64;
            }
        }
        unit 2 {
            vlan-id 2;
            family inet6 {
                address fe80::10/64;
            }
        }
    }
    lo0 {
        unit 0 {
            family inet {
                address 198.51.100.1/24 {
                    primary;
                }
            }
        }
    }
}
"#;

    #[test]
    fn doc_point_to_point_interfaces() {
        let ifaces = interfaces(&juniper::parse_braces(DOC_POINT_TO_POINT));
        let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
        assert_eq!(names, ["ge-1/2/0", "ge-0/0/1"]);
        assert_eq!(ifaces[0].units[0].number, "0");
        assert_eq!(ifaces[0].units[0].description.as_deref(), Some("to-A"));
        assert_eq!(ifaces[0].units[0].inet_addresses, ["10.10.10.1/30"]);
        assert_eq!(ifaces[1].units[0].number, "5");
        assert_eq!(ifaces[1].units[0].inet_addresses, ["10.10.10.5/30"]);
    }

    #[test]
    fn doc_link_local_units_vlans_and_primary_address() {
        let ifaces = interfaces(&juniper::parse_braces(DOC_LINK_LOCAL));
        let ge = &ifaces[0];
        assert_eq!(ge.description.as_deref(), Some("R1-to-R2-Link"));
        assert_eq!(ge.units.len(), 2);
        assert_eq!(ge.units[0].vlan_id, Some(1));
        assert_eq!(ge.units[1].vlan_id, Some(2));
        assert_eq!(ge.units[0].inet6_addresses, ["fe80::10/64"]);

        let lo0 = &ifaces[1];
        assert_eq!(lo0.units[0].inet_addresses, ["198.51.100.1/24"]);
    }
}