Skip to main content

asterust_junos/interfaces/
mod.rs

1//! `interfaces`
2//! `interfaces { ... }`.
3//!
4//! # See also
5//!
6//! - [`[edit interfaces]` hierarchy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/topic-map/hierarchy-edit-interfaces.html)
7//! - [mtu (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mtu-edit-interfaces-ni.html)
8//! - [speed (Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/speed-edit-interfaces-ethernet.html)
9//! - [encapsulation (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/encapsulation-edit-interfaces-physical.html)
10//! - [vlan-tagging](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-tagging-edit-interfaces.html)
11//! - [flexible-vlan-tagging](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/flexible-vlan-tagging-edit-interfaces.html)
12//! - [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)
13//! - [bypass-queuing-chip](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bypass-queuing-chip-edit-interfaces.html)
14//! - [cascade-port](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/cascade-port-edit-interfaces.html)
15//! - [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)
16//! - [stacked-vlan-tagging](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/stacked-vlan-tagging-edit-interfaces.html)
17//! - [oam-on-svlan](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/oam-on-svlan-edit-interfaces.html)
18//! - [promiscuous-mode](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-edit-promiscuous-mode.html)
19//! - [no-native-vlan-insert](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-native-vlan-insert.html)
20//! - [shared-scheduler](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/shared-scheduler-edit-interfaces.html)
21//! - [unidirectional](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unidirectional-edit-interfaces.html)
22//! - [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)
23//! - [traps](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traps-edit-interfaces.html)
24//! - [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)
25//! - [number-of-sub-ports](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/number-of-subports.html)
26//! - [accounting-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accounting-profile-edit-interfaces.html)
27//! - [per-unit-scheduler](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/per-unit-scheduler-edit-interfaces.html)
28//! - [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)
29
30mod aggregated_ether_options;
31mod aggregated_inline_services_options;
32mod auto_configure;
33mod es_options;
34mod esi;
35mod ether_options;
36mod forwarding_class_accounting;
37mod hierarchical_scheduler;
38mod hold_time;
39mod interface_range;
40mod interface_set;
41mod keepalives;
42mod layer2_policer;
43mod link_degrade_monitor;
44mod load_balancing_options;
45mod logical_tunnel_options;
46mod multi_chassis_protection;
47mod multiservice_options;
48mod optics_options;
49mod otn_options;
50mod ppp_options;
51mod radius_options;
52mod redundancy_options;
53mod redundant_ether_options;
54mod services_options;
55mod targeted_options;
56mod tca;
57mod unit;
58
59pub use aggregated_ether_options::{
60    AggregatedEtherOptions, BfdDamping, BfdLivenessDetection, Dlb, Lacp, LinkProtection, LinkProtectionSubGroup,
61    LoadBalance, McAe, McAeEvents, RebalancePeriodic,
62};
63pub use aggregated_inline_services_options::AggregatedInlineServicesOptions;
64pub use auto_configure::{AutoConfigure, AutoConfigureProfile, AutoConfigureRanges};
65pub use es_options::EsOptions;
66pub use esi::{Esi, EsiAutoDerive, EsiDfElectionType, EsiDfGranularity, EsiPreference, EsiSystemMac};
67pub use ether_options::{
68    EtherOptions, EthernetSwitchForwardingClass, EthernetSwitchPolicer, EthernetSwitchProfile, Ieee8023ad,
69};
70pub use forwarding_class_accounting::{EnhancedForwardingClassAccounting, ForwardingClassAccounting};
71pub use hierarchical_scheduler::{hierarchical_scheduler, HierarchicalScheduler};
72pub use hold_time::HoldTime;
73pub use interface_range::{interface_ranges, InterfaceRange, MemberRange};
74pub use interface_set::{interface_sets, InterfaceSet, InterfaceSetMember};
75pub use keepalives::Keepalives;
76pub use layer2_policer::Layer2Policer;
77pub use link_degrade_monitor::{LinkDegradeMonitor, LinkDegradeThresholds};
78pub use load_balancing_options::LoadBalancingOptions;
79pub use logical_tunnel_options::{logical_tunnel_options, LogicalTunnelOptions};
80pub use multi_chassis_protection::MultiChassisProtection;
81pub use multiservice_options::MultiserviceOptions;
82pub use optics_options::{OpticsApplication, OpticsLowLight, OpticsOptions};
83pub use otn_options::{OduDelayManagement, OtnOptions, OtnRate, OtnTrigger, PreemptiveFastReroute, SignalDegrade};
84pub use ppp_options::{PppChap, PppCompression, PppOptions, PppPap};
85pub use radius_options::{NasPortExtendedFormat, NasPortOptions, RadiusOptions};
86pub use redundancy_options::RedundancyOptions;
87pub use redundant_ether_options::RedundantEtherOptions;
88pub use services_options::{ServicesOptions, ServicesSyslog, ServicesSyslogHost, SessionLimit};
89pub use targeted_options::TargetedOptions;
90pub use tca::Tca;
91pub use unit::{
92    AcceptedMac, AddressArp, AdvisoryOptions, BandwidthThreshold, Demux, DemuxAddressRanges, DemuxDynamicProfile,
93    DemuxFamily, DemuxNetwork, DemuxOptions, DemuxRange, DemuxUsernameInclude, DhcpClient, DhcpClientIdentifier,
94    Dhcpv6Client, DuplicateProtection, EthernetSwitching, FamilyAccounting, FamilyFilter, FamilyPolicer,
95    FamilySampling, InterfaceUnit, NextHopTunnel, PolicerOverhead, PppoeOptions, PppoeUnderlyingOptions,
96    PrefixDelegating, RouterAdvertisementInterface, RpfCheck, ShortCycleProtection, TargetedBroadcast,
97    TrackedInterface, TrackedRoute, UnitAddress, UnitFamily, UnitTunnel, UnnumberedAddress, VirtualGatewayEsi, VlanMap,
98    VlanTags, VlanTranslation, VrrpGroup, VrrpInheritFrom, VrrpTrack,
99};
100
101use crate::{active_children, arg_of, child_block, has_leaf, named_children, toggle, top_level};
102use aggregated_ether_options::parse_aggregated_ether_options;
103use aggregated_inline_services_options::parse_aggregated_inline_services_options;
104use asterust::config::{Config, ConfigLine};
105use auto_configure::parse_auto_configure;
106use es_options::parse_es_options;
107use esi::parse_esi;
108use ether_options::parse_ether_options;
109use forwarding_class_accounting::parse_forwarding_class_accounting;
110use hierarchical_scheduler::parse_hierarchical_scheduler;
111use hold_time::parse_hold_time;
112use keepalives::parse_keepalives;
113use layer2_policer::parse_layer2_policer;
114use link_degrade_monitor::parse_link_degrade_monitor;
115use load_balancing_options::parse_load_balancing_options;
116use multi_chassis_protection::parse_multi_chassis_protection;
117use multiservice_options::parse_multiservice_options;
118use optics_options::parse_optics_options;
119use otn_options::parse_otn_options;
120use ppp_options::parse_ppp_options;
121use radius_options::parse_radius_options;
122use redundancy_options::parse_redundancy_options;
123use redundant_ether_options::parse_redundant_ether_options;
124use services_options::parse_services_options;
125use targeted_options::parse_targeted_options;
126use unit::parse_unit;
127
128/// A physical or loopback interface such as `ge-0/0/0` or `lo0`.
129#[derive(Clone, Debug, PartialEq, Eq)]
130#[non_exhaustive]
131pub struct Interface {
132    pub name: String,
133    pub description: Option<String>,
134    /// Set by the `disable` statement.
135    pub disabled: bool,
136    pub mtu: Option<String>,
137    /// e.g. `"1g"` or `"auto"`.
138    pub speed: Option<String>,
139    /// Physical encapsulation, e.g. `"flexible-ethernet-services"`.
140    pub encapsulation: Option<String>,
141    /// `"copper"` or `"fiber"`, for a dual-purpose uplink port.
142    pub media_type: Option<String>,
143    pub vlan_tagging: bool,
144    pub flexible_vlan_tagging: bool,
145    pub native_vlan_id: Option<String>,
146    pub per_unit_scheduler: bool,
147    pub no_gratuitous_arp_request: bool,
148    pub hold_time: Option<HoldTime>,
149    pub ether_options: Option<EtherOptions>,
150    pub aggregated_ether_options: Option<AggregatedEtherOptions>,
151    pub auto_configure: Option<AutoConfigure>,
152    pub optics_options: Option<OpticsOptions>,
153    pub otn_options: Option<OtnOptions>,
154    pub link_degrade_monitor: Option<LinkDegradeMonitor>,
155    pub bypass_queuing_chip: bool,
156    pub cascade_port: bool,
157    pub no_per_unit_scheduler: bool,
158    pub no_keepalives: bool,
159    pub stacked_vlan_tagging: bool,
160    pub oam_on_svlan: bool,
161    pub promiscuous_mode: bool,
162    pub no_native_vlan_insert: bool,
163    pub shared_scheduler: bool,
164    pub unidirectional: bool,
165    /// `Some(true)` for `gratuitous-arp-reply`, `Some(false)` for `no-gratuitous-arp-reply`.
166    pub gratuitous_arp_reply: Option<bool>,
167    /// `Some(true)` for `traps`, `Some(false)` for `no-traps`.
168    pub traps: Option<bool>,
169    /// `Some(true)` for `input-native-vlan-push enable`, `Some(false)` for `disable`.
170    pub input_native_vlan_push: Option<bool>,
171    pub number_of_sub_ports: Option<String>,
172    pub accounting_profile: Option<String>,
173    pub keepalives: Option<Keepalives>,
174    pub hierarchical_scheduler: Option<HierarchicalScheduler>,
175    pub layer2_policer: Option<Layer2Policer>,
176    pub redundant_ether_options: Option<RedundantEtherOptions>,
177    pub es_options: Option<EsOptions>,
178    pub multi_chassis_protection: Option<MultiChassisProtection>,
179    pub esi: Option<Esi>,
180    pub aggregated_inline_services_options: Option<AggregatedInlineServicesOptions>,
181    pub forwarding_class_accounting: Option<ForwardingClassAccounting>,
182    pub ppp_options: Option<PppOptions>,
183    pub radius_options: Option<RadiusOptions>,
184    pub targeted_options: Option<TargetedOptions>,
185    pub load_balancing_options: Option<LoadBalancingOptions>,
186    pub multiservice_options: Option<MultiserviceOptions>,
187    pub redundancy_options: Option<RedundancyOptions>,
188    pub services_options: Option<ServicesOptions>,
189    pub units: Vec<InterfaceUnit>,
190}
191
192/// Blocks written directly under `interfaces` that are not interfaces.
193const GLOBAL_BLOCKS: [&str; 2] = ["hierarchical-scheduler", "logical-tunnel-options"];
194
195/// Returns every interface under the top-level `interfaces` block.
196///
197/// # Examples
198///
199/// ```
200/// use asterust::config::juniper;
201/// use asterust_junos::interfaces::interfaces;
202///
203/// let config = juniper::parse_braces("interfaces {\n    ge-0/0/0 {\n        mtu 9192;\n    }\n}\n");
204/// let found = interfaces(&config);
205/// assert_eq!(found[0].name, "ge-0/0/0");
206/// assert_eq!(found[0].mtu.as_deref(), Some("9192"));
207/// ```
208pub fn interfaces(config: &Config) -> Vec<Interface> {
209    let Some(root) = top_level(config, "interfaces") else {
210        return Vec::new();
211    };
212    // Interface names have no spaces; this skips `apply-groups`, `interface-range <x>` and similar.
213    active_children(root)
214        .filter(|c| !c.text.contains(' ') && !GLOBAL_BLOCKS.contains(&c.text.as_str()))
215        .map(parse_interface)
216        .collect()
217}
218
219fn parse_interface(line: &ConfigLine) -> Interface {
220    Interface {
221        name: line.text.clone(),
222        description: arg_of(line, "description").map(str::to_string),
223        disabled: has_leaf(line, "disable"),
224        mtu: arg_of(line, "mtu").map(str::to_string),
225        speed: arg_of(line, "speed").map(str::to_string),
226        encapsulation: arg_of(line, "encapsulation").map(str::to_string),
227        media_type: arg_of(line, "media-type").map(str::to_string),
228        vlan_tagging: has_leaf(line, "vlan-tagging"),
229        flexible_vlan_tagging: has_leaf(line, "flexible-vlan-tagging"),
230        native_vlan_id: arg_of(line, "native-vlan-id").map(str::to_string),
231        per_unit_scheduler: has_leaf(line, "per-unit-scheduler"),
232        no_gratuitous_arp_request: has_leaf(line, "no-gratuitous-arp-request"),
233        hold_time: parse_hold_time(line),
234        ether_options: parse_ether_options(line),
235        aggregated_ether_options: child_block(line, "aggregated-ether-options").map(parse_aggregated_ether_options),
236        auto_configure: child_block(line, "auto-configure").map(parse_auto_configure),
237        optics_options: child_block(line, "optics-options").map(parse_optics_options),
238        otn_options: child_block(line, "otn-options").map(parse_otn_options),
239        link_degrade_monitor: child_block(line, "link-degrade-monitor").map(parse_link_degrade_monitor),
240        bypass_queuing_chip: has_leaf(line, "bypass-queuing-chip"),
241        cascade_port: has_leaf(line, "cascade-port"),
242        no_per_unit_scheduler: has_leaf(line, "no-per-unit-scheduler"),
243        no_keepalives: has_leaf(line, "no-keepalives"),
244        stacked_vlan_tagging: has_leaf(line, "stacked-vlan-tagging"),
245        oam_on_svlan: has_leaf(line, "oam-on-svlan"),
246        promiscuous_mode: has_leaf(line, "promiscuous-mode"),
247        no_native_vlan_insert: has_leaf(line, "no-native-vlan-insert"),
248        shared_scheduler: has_leaf(line, "shared-scheduler"),
249        unidirectional: has_leaf(line, "unidirectional"),
250        gratuitous_arp_reply: toggle(line, "gratuitous-arp-reply"),
251        traps: toggle(line, "traps"),
252        input_native_vlan_push: arg_of(line, "input-native-vlan-push").map(|v| v == "enable"),
253        number_of_sub_ports: arg_of(line, "number-of-sub-ports").map(str::to_string),
254        accounting_profile: arg_of(line, "accounting-profile").map(str::to_string),
255        keepalives: parse_keepalives(line),
256        hierarchical_scheduler: parse_hierarchical_scheduler(line),
257        layer2_policer: child_block(line, "layer2-policer").map(parse_layer2_policer),
258        redundant_ether_options: child_block(line, "redundant-ether-options").map(parse_redundant_ether_options),
259        es_options: child_block(line, "es-options").map(parse_es_options),
260        multi_chassis_protection: parse_multi_chassis_protection(line),
261        esi: parse_esi(line),
262        forwarding_class_accounting: child_block(line, "forwarding-class-accounting")
263            .map(parse_forwarding_class_accounting),
264        ppp_options: child_block(line, "ppp-options").map(parse_ppp_options),
265        radius_options: child_block(line, "radius-options").map(parse_radius_options),
266        targeted_options: child_block(line, "targeted-options").map(parse_targeted_options),
267        aggregated_inline_services_options: child_block(line, "aggregated-inline-services-options")
268            .map(parse_aggregated_inline_services_options),
269        load_balancing_options: child_block(line, "load-balancing-options").map(parse_load_balancing_options),
270        multiservice_options: child_block(line, "multiservice-options").map(parse_multiservice_options),
271        redundancy_options: child_block(line, "redundancy-options").map(parse_redundancy_options),
272        services_options: child_block(line, "services-options").map(parse_services_options),
273        units: named_children(line, "unit")
274            .map(|(number, unit)| parse_unit(number, unit))
275            .collect(),
276    }
277}
278
279#[cfg(test)]
280mod tests {
281    use super::*;
282    use asterust::config::juniper;
283
284    // Not from documentation: covers statements the Juniper examples do not use. See the README "Sources".
285    const CONFIG: &str = r#"
286interfaces {
287    ge-0/0/0 {
288        description "to p1";
289        unit 0 {
290            description "core link";
291            family inet {
292                address 10.0.1.1/30;
293                address 10.0.2.1/30 {
294                    primary;
295                }
296            }
297            family inet6 {
298                address 2001:db8:1::1/64;
299            }
300            family iso;
301            family mpls;
302        }
303    }
304    ge-0/0/1 {
305        disable;
306        vlan-tagging;
307        unit 100 {
308            vlan-id 100;
309            family inet {
310                address 192.0.2.1/24;
311            }
312        }
313    }
314    inactive: ge-0/0/2 {
315        unit 0;
316    }
317    lo0 {
318        unit 0 {
319            family inet {
320                address 10.255.0.1/32;
321            }
322            family iso {
323                address 49.0001.0100.2550.0001.00;
324            }
325        }
326    }
327    apply-groups GLOBAL;
328}
329"#;
330
331    #[test]
332    fn extracts_units_families_and_addresses() {
333        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
334        let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
335        assert_eq!(names, ["ge-0/0/0", "ge-0/0/1", "lo0"]);
336
337        let core = &ifaces[0];
338        assert_eq!(core.description.as_deref(), Some("to p1"));
339        assert!(!core.disabled);
340        let unit = &core.units[0];
341        assert_eq!(unit.number, "0");
342        assert_eq!(unit.description.as_deref(), Some("core link"));
343        assert_eq!(unit.families, ["inet", "inet6", "iso", "mpls"]);
344        assert_eq!(unit.inet_addresses, ["10.0.1.1/30", "10.0.2.1/30"]);
345        assert_eq!(unit.inet6_addresses, ["2001:db8:1::1/64"]);
346        assert!(unit.iso_addresses.is_empty());
347    }
348
349    #[test]
350    fn disabled_vlan_units_and_loopback_net() {
351        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
352        assert!(ifaces[1].disabled);
353        assert_eq!(ifaces[1].units[0].number, "100");
354        assert_eq!(ifaces[1].units[0].vlan_id, Some(100));
355        assert_eq!(ifaces[2].units[0].iso_addresses, ["49.0001.0100.2550.0001.00"]);
356    }
357
358    #[test]
359    fn missing_interfaces_block_is_empty() {
360        assert!(interfaces(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_empty());
361    }
362
363    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
364    #[test]
365    fn extracts_physical_interface_settings() {
366        let ifaces = interfaces(&juniper::parse_braces(
367            r#"
368interfaces {
369    xe-0/0/0 {
370        mtu 9192;
371        speed 10g;
372        encapsulation flexible-ethernet-services;
373        vlan-tagging;
374        flexible-vlan-tagging;
375        native-vlan-id 10;
376        per-unit-scheduler;
377        no-gratuitous-arp-request;
378    }
379    inactive: xe-0/0/1 {
380        mtu 1500;
381    }
382    xe-0/0/2 {
383        inactive: mtu 1500;
384    }
385}
386"#,
387        ));
388        assert_eq!(ifaces.len(), 2);
389
390        let xe0 = &ifaces[0];
391        assert_eq!(xe0.mtu.as_deref(), Some("9192"));
392        assert_eq!(xe0.speed.as_deref(), Some("10g"));
393        assert_eq!(xe0.encapsulation.as_deref(), Some("flexible-ethernet-services"));
394        assert!(xe0.vlan_tagging);
395        assert!(xe0.flexible_vlan_tagging);
396        assert_eq!(xe0.native_vlan_id.as_deref(), Some("10"));
397        assert!(xe0.per_unit_scheduler);
398        assert!(xe0.no_gratuitous_arp_request);
399
400        assert_eq!(ifaces[1].mtu, None);
401    }
402
403    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
404    #[test]
405    fn extracts_more_interface_level_statements() {
406        let ifaces = interfaces(&juniper::parse_braces(
407            r#"
408interfaces {
409    xe-0/0/1 {
410        accounting-profile ap1;
411        bypass-queuing-chip;
412        cascade-port;
413        no-per-unit-scheduler;
414        no-keepalives;
415        stacked-vlan-tagging;
416        oam-on-svlan;
417        promiscuous-mode;
418        no-native-vlan-insert;
419        shared-scheduler;
420        unidirectional;
421        gratuitous-arp-reply;
422        no-traps;
423        input-native-vlan-push disable;
424        number-of-sub-ports 4;
425        keepalives interval 10 down-count 5 up-count 2;
426        hierarchical-scheduler {
427            implicit-hierarchy;
428            maximum-hierarchy-levels 3;
429        }
430        layer2-policer {
431            input-policer p-in;
432            input-three-color p3-in;
433            output-policer p-out;
434            output-three-color p3-out;
435            input-hierarchical-policer hp;
436        }
437        es-options {
438            backup-interface ge-0/0/9;
439        }
440        multi-chassis-protection 10.1.1.1 {
441            interface ae0;
442        }
443    }
444    reth0 {
445        keepalives;
446        input-native-vlan-push enable;
447        multi-chassis-protection {
448            peer 10.2.2.2 {
449                interface ae1;
450            }
451        }
452        redundant-ether-options {
453            flow-control;
454            lacp {
455                active;
456                periodic fast;
457            }
458            link-speed 10g;
459            no-loopback;
460            minimum-links 1;
461            redundancy-group 1;
462            source-address-filter 00:11:22:33:44:55;
463            no-source-filtering;
464        }
465    }
466}
467"#,
468        ));
469        let xe = &ifaces[0];
470        assert_eq!(xe.accounting_profile.as_deref(), Some("ap1"));
471        assert!(xe.bypass_queuing_chip && xe.cascade_port && xe.no_per_unit_scheduler && xe.no_keepalives);
472        assert!(xe.stacked_vlan_tagging && xe.oam_on_svlan && xe.promiscuous_mode && xe.no_native_vlan_insert);
473        assert!(xe.shared_scheduler && xe.unidirectional);
474        assert_eq!(xe.gratuitous_arp_reply, Some(true));
475        assert_eq!(xe.traps, Some(false));
476        assert_eq!(xe.input_native_vlan_push, Some(false));
477        assert_eq!(xe.number_of_sub_ports.as_deref(), Some("4"));
478        let keepalives = xe.keepalives.as_ref().unwrap();
479        assert_eq!(keepalives.interval.as_deref(), Some("10"));
480        assert_eq!(keepalives.down_count.as_deref(), Some("5"));
481        assert_eq!(keepalives.up_count.as_deref(), Some("2"));
482        let scheduler = xe.hierarchical_scheduler.as_ref().unwrap();
483        assert!(scheduler.implicit_hierarchy);
484        assert_eq!(scheduler.maximum_hierarchy_levels.as_deref(), Some("3"));
485        let policer = xe.layer2_policer.as_ref().unwrap();
486        assert_eq!(policer.input_policer.as_deref(), Some("p-in"));
487        assert_eq!(policer.input_three_color.as_deref(), Some("p3-in"));
488        assert_eq!(policer.output_policer.as_deref(), Some("p-out"));
489        assert_eq!(policer.output_three_color.as_deref(), Some("p3-out"));
490        assert_eq!(policer.input_hierarchical_policer.as_deref(), Some("hp"));
491        assert_eq!(
492            xe.es_options.as_ref().unwrap().backup_interface.as_deref(),
493            Some("ge-0/0/9")
494        );
495        let protection = xe.multi_chassis_protection.as_ref().unwrap();
496        assert_eq!(protection.peer, "10.1.1.1");
497        assert_eq!(protection.interface.as_deref(), Some("ae0"));
498
499        let reth = &ifaces[1];
500        let bare = reth.keepalives.as_ref().unwrap();
501        assert!(bare.interval.is_none() && bare.down_count.is_none() && bare.up_count.is_none());
502        assert_eq!(reth.input_native_vlan_push, Some(true));
503        let protection = reth.multi_chassis_protection.as_ref().unwrap();
504        assert_eq!(protection.peer, "10.2.2.2");
505        assert_eq!(protection.interface.as_deref(), Some("ae1"));
506        let options = reth.redundant_ether_options.as_ref().unwrap();
507        assert_eq!(options.flow_control, Some(true));
508        assert_eq!(options.lacp.as_ref().unwrap().mode.as_deref(), Some("active"));
509        assert_eq!(options.lacp.as_ref().unwrap().periodic.as_deref(), Some("fast"));
510        assert_eq!(options.link_speed.as_deref(), Some("10g"));
511        assert_eq!(options.loopback, Some(false));
512        assert_eq!(options.minimum_links.as_deref(), Some("1"));
513        assert_eq!(options.redundancy_group.as_deref(), Some("1"));
514        assert_eq!(options.source_address_filter, ["00:11:22:33:44:55"]);
515        assert_eq!(options.source_filtering, Some(false));
516
517        assert!(!reth.bypass_queuing_chip && reth.gratuitous_arp_reply.is_none() && reth.traps.is_none());
518        assert!(reth.hierarchical_scheduler.is_none() && reth.layer2_policer.is_none() && reth.es_options.is_none());
519        assert!(xe.redundant_ether_options.is_none());
520    }
521
522    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
523    #[test]
524    fn global_blocks_are_not_interfaces() {
525        let config = juniper::parse_braces(
526            r#"
527interfaces {
528    hierarchical-scheduler {
529        implicit-hierarchy;
530        maximum-hierarchy-levels 3;
531    }
532    logical-tunnel-options {
533        per-unit-mac-disable;
534    }
535    interface-range r1 {
536        member ge-0/0/1;
537    }
538    ge-0/0/0 {
539        mtu 9000;
540    }
541}
542"#,
543        );
544        let names: Vec<_> = interfaces(&config).into_iter().map(|i| i.name).collect();
545        assert_eq!(names, ["ge-0/0/0"]);
546        let scheduler = hierarchical_scheduler(&config).unwrap();
547        assert!(scheduler.implicit_hierarchy);
548        assert_eq!(scheduler.maximum_hierarchy_levels.as_deref(), Some("3"));
549        assert!(hierarchical_scheduler(&juniper::parse_braces(
550            "interfaces {\n    ge-0/0/0 {\n        disable;\n    }\n}\n"
551        ))
552        .is_none());
553    }
554
555    #[test]
556    fn physical_interface_settings_default_to_off() {
557        let ifaces = interfaces(&juniper::parse_braces(
558            "interfaces {\n    ge-0/0/0 {\n        disable;\n    }\n}\n",
559        ));
560        let ge = &ifaces[0];
561        assert_eq!(ge.mtu, None);
562        assert_eq!(ge.speed, None);
563        assert_eq!(ge.encapsulation, None);
564        assert!(!ge.vlan_tagging);
565        assert!(!ge.flexible_vlan_tagging);
566        assert_eq!(ge.native_vlan_id, None);
567        assert!(!ge.per_unit_scheduler);
568        assert!(!ge.no_gratuitous_arp_request);
569    }
570
571    // The DOC_* fixtures are adapted from Juniper's BGP User Guide, "BGP Peering Sessions"; see the README "Sources".
572
573    // "Example: Configuring External BGP Point-to-Point Peer Sessions", Device E `show interfaces` (two of four interfaces).
574    const DOC_POINT_TO_POINT: &str = r#"
575interfaces {
576    ge-1/2/0 {
577        unit 0 {
578            description to-A;
579            family inet {
580                address 10.10.10.1/30;
581            }
582        }
583    }
584    ge-0/0/1 {
585        unit 5 {
586            description to-B;
587            family inet {
588                address 10.10.10.5/30;
589            }
590        }
591    }
592}
593"#;
594
595    // "Example: Configure Multiple Single-Hop EBGP Sessions on Different Links Using the Same IPv6 Link-Local
596    // Address", R1 `show interfaces`.
597    const DOC_LINK_LOCAL: &str = r#"
598interfaces {
599    ge-0/0/1 {
600        description R1-to-R2-Link;
601        vlan-tagging;
602        unit 1 {
603            vlan-id 1;
604            family inet6 {
605                address fe80::10/64;
606            }
607        }
608        unit 2 {
609            vlan-id 2;
610            family inet6 {
611                address fe80::10/64;
612            }
613        }
614    }
615    lo0 {
616        unit 0 {
617            family inet {
618                address 198.51.100.1/24 {
619                    primary;
620                }
621            }
622        }
623    }
624}
625"#;
626
627    #[test]
628    fn doc_point_to_point_interfaces() {
629        let ifaces = interfaces(&juniper::parse_braces(DOC_POINT_TO_POINT));
630        let names: Vec<_> = ifaces.iter().map(|i| i.name.as_str()).collect();
631        assert_eq!(names, ["ge-1/2/0", "ge-0/0/1"]);
632        assert_eq!(ifaces[0].units[0].number, "0");
633        assert_eq!(ifaces[0].units[0].description.as_deref(), Some("to-A"));
634        assert_eq!(ifaces[0].units[0].inet_addresses, ["10.10.10.1/30"]);
635        assert_eq!(ifaces[1].units[0].number, "5");
636        assert_eq!(ifaces[1].units[0].inet_addresses, ["10.10.10.5/30"]);
637    }
638
639    #[test]
640    fn doc_link_local_units_vlans_and_primary_address() {
641        let ifaces = interfaces(&juniper::parse_braces(DOC_LINK_LOCAL));
642        let ge = &ifaces[0];
643        assert_eq!(ge.description.as_deref(), Some("R1-to-R2-Link"));
644        assert_eq!(ge.units.len(), 2);
645        assert_eq!(ge.units[0].vlan_id, Some(1));
646        assert_eq!(ge.units[1].vlan_id, Some(2));
647        assert_eq!(ge.units[0].inet6_addresses, ["fe80::10/64"]);
648
649        let lo0 = &ifaces[1];
650        assert_eq!(lo0.units[0].inet_addresses, ["198.51.100.1/24"]);
651    }
652}