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