Skip to main content

asterust_junos/interfaces/unit/
mod.rs

1//! `unit`
2//! `interfaces { <name> { unit <n> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [unit (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unit-edit-interfaces.html)
7//! - [encapsulation (Logical Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/encapsulation-edit-interfaces-logical.html)
8//! - [reassemble-packets](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/reassemble-packets-edit-interfaces.html)
9//! - [clear-dont-fragment-bit (Interfaces GRE Tunnels)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/clear-dont-fragment-bit-edit-interfaces.html)
10//! - [copy-tos-to-outer-ip-header](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/copy-tos-to-outer-ip-header-edit-interfaces.html)
11//! - [copy-tos-to-outer-ip-header-transit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/copy-tos-to-outer-ip-header-transit-edit-interfaces.html)
12//! - [extended-telemetry](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/extended-telemetry-edit-interfaces-interface-unit.html)
13//! - [alias](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/alias-edit-interfaces.html)
14//! - [bandwidth (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bandwidth-edit-interfaces.html)
15//! - [arp-resp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-arp-resp.html)
16//! - [peer-unit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/peer-unit-edit-interfaces-sv.html)
17//! - [disable-mlppp-inner-ppp-pfc](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/disable-mlppp-inner-ppp-pfc-edit-interfaces.html)
18//! - [host-prefix-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/host-prefix-only-edit-interfaces-unit.html)
19//! - [per-session-scheduler](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/per-session-scheduler-edit-interfaces.html)
20//! - [point-to-point](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/point-to-point-edit-interfaces.html)
21//! - [multipoint](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/multipoint-edit-interfaces.html)
22//! - [demux-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-options-edit-interfaces.html)
23//! - [demux (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-edit-interfaces.html)
24//! - [advisory-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advisory-options-edit-interfaces-unit.html)
25//! - [targeted-distribution](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/targeted-distribution-edit-interfaces.html)
26//! - [pppoe-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pppoe-options-edit-interfaces.html)
27//! - [pppoe-underlying-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pppoe-underlying-options-edit-interfaces-pppoe.html)
28//! - [ppp-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ppp-options-edit-interfaces.html)
29//! - [vlan-id (VLAN ID to Be Bound to a Logical Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-id-edit-interfaces.html)
30
31mod accept_source_mac;
32mod demux;
33mod demux_options;
34mod family;
35mod pppoe_options;
36mod pppoe_underlying_options;
37mod tunnel;
38mod vlan_map;
39mod vlan_tags;
40
41pub use accept_source_mac::{AcceptedMac, PolicerOverhead};
42pub use demux::{
43    Demux, DemuxAddressRanges, DemuxDynamicProfile, DemuxFamily, DemuxNetwork, DemuxRange, DemuxUsernameInclude,
44};
45pub use demux_options::{AdvisoryOptions, DemuxOptions};
46pub use family::{
47    AddressArp, BandwidthThreshold, DhcpClient, DhcpClientIdentifier, Dhcpv6Client, EthernetSwitching,
48    FamilyAccounting, FamilyFilter, FamilyPolicer, FamilySampling, NextHopTunnel, PrefixDelegating,
49    RouterAdvertisementInterface, RpfCheck, TargetedBroadcast, TrackedInterface, TrackedRoute, UnitAddress, UnitFamily,
50    UnnumberedAddress, VlanTranslation, VrrpGroup, VrrpInheritFrom, VrrpTrack,
51};
52pub use pppoe_options::PppoeOptions;
53pub use pppoe_underlying_options::{DuplicateProtection, PppoeUnderlyingOptions, ShortCycleProtection};
54pub use tunnel::UnitTunnel;
55pub use vlan_map::VlanMap;
56pub use vlan_tags::VlanTags;
57
58use super::esi::{parse_esi, Esi};
59use super::ppp_options::{parse_ppp_options, PppOptions};
60use crate::{active_children, arg_of, args_of, child_block, has_leaf, named_children};
61use accept_source_mac::{parse_accept_source_mac, parse_policer_overhead};
62use asterust::config::ConfigLine;
63use demux::parse_demux;
64use demux_options::{parse_advisory_options, parse_demux_options};
65use family::parse_family;
66use pppoe_options::parse_pppoe_options;
67use pppoe_underlying_options::parse_pppoe_underlying_options;
68use tunnel::parse_tunnel;
69use vlan_map::parse_vlan_map;
70use vlan_tags::parse_vlan_tags;
71
72/// A logical `unit <n>` of an interface. Addresses are the raw `address` values (e.g.
73/// `"10.0.0.1/24"`; IS-IS NETs for `family iso`).
74#[derive(Clone, Debug, PartialEq, Eq)]
75#[non_exhaustive]
76pub struct InterfaceUnit {
77    pub number: String,
78    pub description: Option<String>,
79    pub vlan_id: Option<u32>,
80    /// `vlan-id unmatched`.
81    pub vlan_id_unmatched: bool,
82    /// e.g. `"100-200"`.
83    pub vlan_id_range: Option<String>,
84    pub vlan_tags: Option<VlanTags>,
85    pub tunnel: Option<UnitTunnel>,
86    pub esi: Option<Esi>,
87    pub ppp_options: Option<PppOptions>,
88    /// The `pppoe-options` of a `pp0` unit.
89    pub pppoe_options: Option<PppoeOptions>,
90    pub pppoe_underlying_options: Option<PppoeUnderlyingOptions>,
91    pub demux: Option<Demux>,
92    pub demux_options: Option<DemuxOptions>,
93    /// The families of each `demux-source`.
94    pub demux_source: Vec<String>,
95    /// The families of each `demux-destination`.
96    pub demux_destination: Vec<String>,
97    pub advisory_options: Option<AdvisoryOptions>,
98    /// `replicate` or `scale`.
99    pub targeted_distribution: Option<String>,
100    pub accept_source_mac: Vec<AcceptedMac>,
101    pub policer_overhead: Option<PolicerOverhead>,
102    pub alias: Option<String>,
103    /// The bandwidth the unit advertises, e.g. `"10m"`.
104    pub bandwidth: Option<String>,
105    /// `restricted` or `unrestricted`, from `arp-resp`.
106    pub arp_resp: Option<String>,
107    pub peer_unit: Option<String>,
108    pub disable_mlppp_inner_ppp_pfc: bool,
109    pub host_prefix_only: bool,
110    pub per_session_scheduler: bool,
111    pub point_to_point: bool,
112    pub multipoint: bool,
113    pub reassemble_packets: bool,
114    pub clear_dont_fragment_bit: bool,
115    pub copy_tos_to_outer_ip_header: bool,
116    pub copy_tos_to_outer_ip_header_transit: bool,
117    pub extended_telemetry: bool,
118    /// `proxy-macip-advertisement` on an IRB unit.
119    pub proxy_macip_advertisement: bool,
120    /// `enhanced-convergence` on an IRB unit.
121    pub enhanced_convergence: bool,
122    pub virtual_gateway_v4_mac: Option<String>,
123    pub virtual_gateway_v6_mac: Option<String>,
124    /// The `virtual-gateway-esi` of an IRB unit.
125    pub virtual_gateway_esi: Option<VirtualGatewayEsi>,
126    /// `proxy-arp` is configured; `proxy_arp_mode` is its `restricted` or `unrestricted` argument.
127    pub proxy_arp: bool,
128    pub proxy_arp_mode: Option<String>,
129    pub ip_address_owner: Option<String>,
130    pub input_vlan_map: Option<VlanMap>,
131    pub output_vlan_map: Option<VlanMap>,
132    /// Logical encapsulation, e.g. `"vlan-bridge"` or `"vlan-ccc"`.
133    pub encapsulation: Option<String>,
134    /// Every `family <name>` present, e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
135    pub families: Vec<String>,
136    pub inet_addresses: Vec<String>,
137    pub inet6_addresses: Vec<String>,
138    pub iso_addresses: Vec<String>,
139    /// The settings of each `family`, in the order written.
140    pub family_settings: Vec<UnitFamily>,
141}
142
143/// The `virtual-gateway-esi` of an IRB unit: an ESI identifier and `all-active`.
144#[derive(Clone, Debug, Default, PartialEq, Eq)]
145#[non_exhaustive]
146pub struct VirtualGatewayEsi {
147    pub identifier: Option<String>,
148    pub all_active: bool,
149}
150
151fn parse_virtual_gateway_esi(unit: &ConfigLine) -> Option<VirtualGatewayEsi> {
152    let line =
153        active_children(unit).find(|c| c.text == "virtual-gateway-esi" || c.arg("virtual-gateway-esi").is_some())?;
154    let inline = line.arg("virtual-gateway-esi");
155    Some(VirtualGatewayEsi {
156        identifier: inline.filter(|v| *v != "all-active").map(str::to_string).or_else(|| {
157            active_children(line)
158                .find(|c| !c.text.contains(' ') && c.text != "all-active")
159                .map(|c| c.text.clone())
160        }),
161        all_active: inline == Some("all-active") || has_leaf(line, "all-active"),
162    })
163}
164
165pub(super) fn parse_unit(number: &str, unit: &ConfigLine) -> InterfaceUnit {
166    let families: Vec<_> = named_children(unit, "family").collect();
167    let addresses = |family: &str| -> Vec<String> {
168        families
169            .iter()
170            .filter(|(name, _)| *name == family)
171            .flat_map(|(_, f)| args_of(f, "address"))
172            .map(str::to_string)
173            .collect()
174    };
175    InterfaceUnit {
176        number: number.to_string(),
177        description: arg_of(unit, "description").map(str::to_string),
178        vlan_id: arg_of(unit, "vlan-id").and_then(|v| v.parse().ok()),
179        vlan_id_unmatched: arg_of(unit, "vlan-id") == Some("unmatched"),
180        vlan_id_range: arg_of(unit, "vlan-id-range").map(str::to_string),
181        vlan_tags: parse_vlan_tags(unit),
182        tunnel: parse_tunnel(unit),
183        esi: parse_esi(unit),
184        ppp_options: child_block(unit, "ppp-options").map(parse_ppp_options),
185        pppoe_options: child_block(unit, "pppoe-options").map(parse_pppoe_options),
186        pppoe_underlying_options: child_block(unit, "pppoe-underlying-options").map(parse_pppoe_underlying_options),
187        demux: child_block(unit, "demux").map(parse_demux),
188        demux_options: child_block(unit, "demux-options").map(parse_demux_options),
189        demux_source: args_of(unit, "demux-source").into_iter().map(str::to_string).collect(),
190        demux_destination: args_of(unit, "demux-destination")
191            .into_iter()
192            .map(str::to_string)
193            .collect(),
194        advisory_options: child_block(unit, "advisory-options").map(parse_advisory_options),
195        targeted_distribution: arg_of(unit, "targeted-distribution").map(str::to_string),
196        accept_source_mac: parse_accept_source_mac(unit),
197        policer_overhead: child_block(unit, "policer-overhead").map(parse_policer_overhead),
198        alias: arg_of(unit, "alias").map(str::to_string),
199        bandwidth: arg_of(unit, "bandwidth").map(str::to_string),
200        arp_resp: arg_of(unit, "arp-resp").map(str::to_string),
201        peer_unit: arg_of(unit, "peer-unit").map(str::to_string),
202        disable_mlppp_inner_ppp_pfc: has_leaf(unit, "disable-mlppp-inner-ppp-pfc"),
203        host_prefix_only: has_leaf(unit, "host-prefix-only"),
204        per_session_scheduler: has_leaf(unit, "per-session-scheduler"),
205        point_to_point: has_leaf(unit, "point-to-point"),
206        multipoint: has_leaf(unit, "multipoint"),
207        reassemble_packets: has_leaf(unit, "reassemble-packets"),
208        clear_dont_fragment_bit: has_leaf(unit, "clear-dont-fragment-bit"),
209        copy_tos_to_outer_ip_header: has_leaf(unit, "copy-tos-to-outer-ip-header"),
210        copy_tos_to_outer_ip_header_transit: has_leaf(unit, "copy-tos-to-outer-ip-header-transit"),
211        extended_telemetry: has_leaf(unit, "extended-telemetry"),
212        proxy_macip_advertisement: has_leaf(unit, "proxy-macip-advertisement"),
213        enhanced_convergence: has_leaf(unit, "enhanced-convergence"),
214        virtual_gateway_v4_mac: arg_of(unit, "virtual-gateway-v4-mac").map(str::to_string),
215        virtual_gateway_v6_mac: arg_of(unit, "virtual-gateway-v6-mac").map(str::to_string),
216        virtual_gateway_esi: parse_virtual_gateway_esi(unit),
217        proxy_arp: has_leaf(unit, "proxy-arp") || arg_of(unit, "proxy-arp").is_some(),
218        proxy_arp_mode: arg_of(unit, "proxy-arp").map(str::to_string),
219        ip_address_owner: arg_of(unit, "ip-address-owner").map(str::to_string),
220        input_vlan_map: parse_vlan_map(unit, "input-vlan-map"),
221        output_vlan_map: parse_vlan_map(unit, "output-vlan-map"),
222        encapsulation: arg_of(unit, "encapsulation").map(str::to_string),
223        inet_addresses: addresses("inet"),
224        inet6_addresses: addresses("inet6"),
225        iso_addresses: addresses("iso"),
226        families: families.iter().map(|(name, _)| name.to_string()).collect(),
227        family_settings: families
228            .iter()
229            .map(|(name, family)| parse_family(name, family))
230            .collect(),
231    }
232}
233
234#[cfg(test)]
235mod tests {
236    use crate::interfaces::interfaces;
237    use asterust::config::juniper;
238
239    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
240    const CONFIG: &str = r#"
241interfaces {
242    ge-0/0/0 {
243        flexible-vlan-tagging;
244        encapsulation flexible-ethernet-services;
245        unit 100 {
246            encapsulation vlan-bridge;
247            vlan-id 100;
248            input-vlan-map {
249                swap;
250                tag-protocol-id 0x8100;
251                vlan-id 200;
252            }
253            output-vlan-map swap;
254        }
255        unit 200 {
256            vlan-tags outer 0x8100.200 inner 0x8100.300;
257            input-vlan-map {
258                push-push;
259                tag-protocol-id 0x9100;
260                vlan-id 10;
261                inner-tag-protocol-id 0x8100;
262                inner-vlan-id 20;
263            }
264            output-vlan-map {
265                pop-pop;
266            }
267        }
268        unit 300 {
269            vlan-id unmatched;
270        }
271        unit 400 {
272            vlan-id-range 400-499;
273            vlan-tags outer 500 inner-range 1-4094;
274        }
275    }
276}
277"#;
278
279    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
280    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
281    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
282    #[test]
283    fn extracts_tunnel_options_and_vxlan_gpe() {
284        let ifaces = interfaces(&juniper::parse_braces(
285            r#"
286interfaces {
287    gr-0/0/0 {
288        unit 0 {
289            reassemble-packets;
290            clear-dont-fragment-bit;
291            copy-tos-to-outer-ip-header;
292            copy-tos-to-outer-ip-header-transit;
293            extended-telemetry;
294            tunnel {
295                allow-fragmentation;
296                do-not-fragment;
297                no-path-mtu-discovery;
298                ttl 64;
299                traffic-class 8;
300                backup-destination 192.0.2.7;
301                encapsulation gre {
302                    tunnel-routing-instance {
303                        routing-instance RI1;
304                    }
305                }
306            }
307            family inet {
308                next-hop-tunnel 10.0.0.1 ipsec-vpn vpn1;
309                next-hop-tunnel 10.0.0.2 ipsec-vpn vpn2;
310            }
311        }
312    }
313    fti0 {
314        unit 0 {
315            tunnel {
316                path-mtu-discovery;
317                encapsulation vxlan-gpe {
318                    source {
319                        address 192.0.2.1;
320                        interface lo0.0;
321                    }
322                    destination {
323                        address 192.0.2.2;
324                    }
325                    destination-udp-port 4790;
326                    source-udp-port-range {
327                        min 49152;
328                        max 65535;
329                    }
330                    tunnel-endpoint vxlan;
331                    vni 5000;
332                    allow-fragmentation;
333                }
334            }
335        }
336        unit 1 {
337            tunnel {
338                encapsulation udp;
339            }
340        }
341    }
342}
343"#,
344        ));
345        let gr = &ifaces[0].units[0];
346        assert!(gr.reassemble_packets && gr.clear_dont_fragment_bit && gr.extended_telemetry);
347        assert!(gr.copy_tos_to_outer_ip_header && gr.copy_tos_to_outer_ip_header_transit);
348        let tunnel = gr.tunnel.as_ref().unwrap();
349        assert!(tunnel.allow_fragmentation && tunnel.do_not_fragment);
350        assert_eq!(tunnel.path_mtu_discovery, Some(false));
351        assert_eq!(tunnel.ttl.as_deref(), Some("64"));
352        assert_eq!(tunnel.traffic_class.as_deref(), Some("8"));
353        assert_eq!(tunnel.backup_destination.as_deref(), Some("192.0.2.7"));
354        assert_eq!(tunnel.tunnel_routing_instance.as_deref(), Some("RI1"));
355        let hops = &gr.family_settings[0].next_hop_tunnel;
356        assert_eq!(hops.len(), 2);
357        assert_eq!(hops[1].gateway_address, "10.0.0.2");
358        assert_eq!(hops[1].ipsec_vpn, "vpn2");
359
360        let fti = ifaces[1].units[0].tunnel.as_ref().unwrap();
361        assert_eq!(fti.encapsulation.as_deref(), Some("vxlan-gpe"));
362        assert_eq!(fti.source_address.as_deref(), Some("192.0.2.1"));
363        assert_eq!(fti.source_interface.as_deref(), Some("lo0.0"));
364        assert_eq!(fti.destination_address.as_deref(), Some("192.0.2.2"));
365        assert_eq!(fti.destination_udp_port.as_deref(), Some("4790"));
366        assert_eq!(fti.source_udp_port_min.as_deref(), Some("49152"));
367        assert_eq!(fti.source_udp_port_max.as_deref(), Some("65535"));
368        assert_eq!(fti.tunnel_endpoint.as_deref(), Some("vxlan"));
369        assert_eq!(fti.vni.as_deref(), Some("5000"));
370        assert!(fti.allow_fragmentation);
371        assert_eq!(fti.path_mtu_discovery, Some(true));
372
373        let plain = ifaces[1].units[1].tunnel.as_ref().unwrap();
374        assert!(!plain.allow_fragmentation && plain.ttl.is_none() && plain.vni.is_none());
375        assert!(plain.path_mtu_discovery.is_none() && plain.tunnel_routing_instance.is_none());
376        assert!(!ifaces[1].units[1].reassemble_packets && !ifaces[1].units[1].extended_telemetry);
377    }
378
379    #[test]
380    fn extracts_unit_scalars_and_flags() {
381        let ifaces = interfaces(&juniper::parse_braces(
382            r#"
383interfaces {
384    ge-0/0/0 {
385        unit 0 {
386            alias core-link;
387            bandwidth 10m;
388            arp-resp restricted;
389            peer-unit 1;
390            disable-mlppp-inner-ppp-pfc;
391            host-prefix-only;
392            per-session-scheduler;
393            point-to-point;
394            multipoint;
395        }
396        unit 1 {
397            description plain;
398        }
399    }
400}
401"#,
402        ));
403        let units = &ifaces[0].units;
404        assert_eq!(units[0].alias.as_deref(), Some("core-link"));
405        assert_eq!(units[0].bandwidth.as_deref(), Some("10m"));
406        assert_eq!(units[0].arp_resp.as_deref(), Some("restricted"));
407        assert_eq!(units[0].peer_unit.as_deref(), Some("1"));
408        assert!(units[0].disable_mlppp_inner_ppp_pfc && units[0].host_prefix_only && units[0].per_session_scheduler);
409        assert!(units[0].point_to_point && units[0].multipoint);
410        assert!(units[1].alias.is_none() && units[1].bandwidth.is_none() && units[1].arp_resp.is_none());
411        assert!(units[1].peer_unit.is_none() && !units[1].host_prefix_only && !units[1].point_to_point);
412    }
413
414    #[test]
415    fn extracts_unit_ppp_and_pppoe_options() {
416        let ifaces = interfaces(&juniper::parse_braces(
417            r#"
418interfaces {
419    pp0 {
420        unit 0 {
421            pppoe-options {
422                access-concentrator AC1;
423                auto-reconnect 10;
424                client;
425                ignore-eol-tag;
426                service-name isp;
427                underlying-interface ge-0/0/0.0;
428                ppp-max-payload 1500;
429            }
430            ppp-options {
431                peer-ip-address-optional;
432                initiate-ncp ipv6;
433                chap {
434                    challenge-length minimum 8 maximum 16;
435                }
436            }
437        }
438        unit 1 {
439            pppoe-options {
440                server;
441            }
442        }
443    }
444    ge-0/0/1 {
445        unit 100 {
446            pppoe-underlying-options {
447                access-concentrator AC2;
448                dynamic-profile pppoe-prof;
449                direct-connect;
450                duplicate-protection {
451                    include {
452                        relay-session-id;
453                    }
454                }
455                max-sessions 500;
456                max-sessions-vsa-ignore;
457                service-name-table SNT;
458                short-cycle-protection lockout-time-min 1 lockout-time-max 60 filter [ aci ];
459            }
460        }
461        unit 200 {
462            family pppoe {
463                max-sessions 10;
464                duplicate-protection;
465                short-cycle-protection;
466            }
467        }
468        unit 300 {
469            description plain;
470        }
471    }
472}
473"#,
474        ));
475        let pp0 = &ifaces[0].units;
476        let pppoe = pp0[0].pppoe_options.as_ref().unwrap();
477        assert_eq!(pppoe.access_concentrator.as_deref(), Some("AC1"));
478        assert_eq!(pppoe.auto_reconnect.as_deref(), Some("10"));
479        assert_eq!(pppoe.role.as_deref(), Some("client"));
480        assert!(pppoe.ignore_eol_tag);
481        assert_eq!(pppoe.service_name.as_deref(), Some("isp"));
482        assert_eq!(pppoe.underlying_interface.as_deref(), Some("ge-0/0/0.0"));
483        assert_eq!(pppoe.ppp_max_payload.as_deref(), Some("1500"));
484        let ppp = pp0[0].ppp_options.as_ref().unwrap();
485        assert!(ppp.peer_ip_address_optional);
486        assert_eq!(ppp.initiate_ncp.as_deref(), Some("ipv6"));
487        assert_eq!(
488            ppp.chap.as_ref().unwrap().challenge_length_maximum.as_deref(),
489            Some("16")
490        );
491        assert_eq!(pp0[1].pppoe_options.as_ref().unwrap().role.as_deref(), Some("server"));
492        assert!(pp0[1].ppp_options.is_none());
493
494        let units = &ifaces[1].units;
495        let underlying = units[0].pppoe_underlying_options.as_ref().unwrap();
496        assert_eq!(underlying.access_concentrator.as_deref(), Some("AC2"));
497        assert_eq!(underlying.dynamic_profile.as_deref(), Some("pppoe-prof"));
498        assert!(underlying.direct_connect && underlying.max_sessions_vsa_ignore);
499        assert!(
500            underlying
501                .duplicate_protection
502                .as_ref()
503                .unwrap()
504                .include_relay_session_id
505        );
506        assert_eq!(underlying.max_sessions.as_deref(), Some("500"));
507        assert_eq!(underlying.service_name_table.as_deref(), Some("SNT"));
508        let cycle = underlying.short_cycle_protection.as_ref().unwrap();
509        assert_eq!(cycle.lockout_time_min.as_deref(), Some("1"));
510        assert_eq!(cycle.lockout_time_max.as_deref(), Some("60"));
511        assert_eq!(cycle.filter, ["aci"]);
512
513        let family = units[1].family_settings[0].pppoe.as_ref().unwrap();
514        assert_eq!(family.max_sessions.as_deref(), Some("10"));
515        assert!(!family.duplicate_protection.as_ref().unwrap().include_relay_session_id);
516        let bare = family.short_cycle_protection.as_ref().unwrap();
517        assert!(bare.lockout_time_min.is_none() && bare.filter.is_empty());
518
519        assert!(units[2].pppoe_underlying_options.is_none() && units[2].pppoe_options.is_none());
520        assert!(units[2].ppp_options.is_none());
521    }
522
523    #[test]
524    fn extracts_unit_vlan_id_forms_and_encapsulation() {
525        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
526        assert_eq!(units[0].vlan_id, Some(100));
527        assert!(!units[0].vlan_id_unmatched);
528        assert_eq!(units[0].encapsulation.as_deref(), Some("vlan-bridge"));
529
530        assert_eq!(units[2].vlan_id, None);
531        assert!(units[2].vlan_id_unmatched);
532
533        assert_eq!(units[3].vlan_id_range.as_deref(), Some("400-499"));
534        assert_eq!(units[1].encapsulation, None);
535    }
536
537    #[test]
538    fn extracts_vlan_tags() {
539        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
540        let tags = units[1].vlan_tags.as_ref().unwrap();
541        assert_eq!(tags.outer.as_deref(), Some("0x8100.200"));
542        assert_eq!(tags.inner.as_deref(), Some("0x8100.300"));
543        assert_eq!(tags.inner_range, None);
544
545        let ranged = units[3].vlan_tags.as_ref().unwrap();
546        assert_eq!(ranged.outer.as_deref(), Some("500"));
547        assert_eq!(ranged.inner, None);
548        assert_eq!(ranged.inner_range.as_deref(), Some("1-4094"));
549
550        assert!(units[0].vlan_tags.is_none());
551    }
552
553    #[test]
554    fn extracts_vlan_maps_in_block_and_one_line_form() {
555        let units = &interfaces(&juniper::parse_braces(CONFIG))[0].units;
556
557        let input = units[0].input_vlan_map.as_ref().unwrap();
558        assert_eq!(input.operation.as_deref(), Some("swap"));
559        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x8100"));
560        assert_eq!(input.vlan_id.as_deref(), Some("200"));
561        assert_eq!(input.inner_vlan_id, None);
562        assert_eq!(
563            units[0].output_vlan_map.as_ref().unwrap().operation.as_deref(),
564            Some("swap")
565        );
566
567        let input = units[1].input_vlan_map.as_ref().unwrap();
568        assert_eq!(input.operation.as_deref(), Some("push-push"));
569        assert_eq!(input.tag_protocol_id.as_deref(), Some("0x9100"));
570        assert_eq!(input.vlan_id.as_deref(), Some("10"));
571        assert_eq!(input.inner_tag_protocol_id.as_deref(), Some("0x8100"));
572        assert_eq!(input.inner_vlan_id.as_deref(), Some("20"));
573        assert_eq!(
574            units[1].output_vlan_map.as_ref().unwrap().operation.as_deref(),
575            Some("pop-pop")
576        );
577
578        assert!(units[2].input_vlan_map.is_none());
579        assert!(units[2].output_vlan_map.is_none());
580    }
581
582    #[test]
583    fn extracts_bridge_family_vlan_ids() {
584        let ifaces = interfaces(&juniper::parse_braces(
585            "interfaces {\n    ge-0/0/1 {\n        unit 0 {\n            family bridge {\n                vlan-id-list [ 100-200 300 ];\n                vlan-id 50;\n            }\n        }\n    }\n}\n",
586        ));
587        let bridge = &ifaces[0].units[0].family_settings[0];
588        assert_eq!(bridge.name, "bridge");
589        assert_eq!(bridge.vlan_id_list, ["100-200", "300"]);
590        assert_eq!(bridge.vlan_id.as_deref(), Some("50"));
591    }
592
593    // Adapted from the EVPN-VXLAN User Guide, the IGMP snooping example: the `irb` units of the first leaf device; see
594    // the README "Sources".
595    #[test]
596    fn reads_the_guide_virtual_gateway_address() {
597        let ifaces = interfaces(&juniper::parse_braces(
598            "interfaces {\n    irb {\n        unit 1 {\n            family inet {\n                address 10.1.1.1/24 {\n                    virtual-gateway-address 10.1.1.10;\n                }\n            }\n        }\n        unit 2 {\n            family inet {\n                address 10.2.1.1/24 {\n                    virtual-gateway-address 10.2.1.10;\n                }\n            }\n        }\n    }\n}\n",
599        ));
600        let units = &ifaces[0].units;
601        let address = &units[0].family_settings[0].addresses[0];
602        assert_eq!(address.address, "10.1.1.1/24");
603        assert_eq!(address.virtual_gateway_address.as_deref(), Some("10.1.1.10"));
604        assert_eq!(
605            units[1].family_settings[0].addresses[0]
606                .virtual_gateway_address
607                .as_deref(),
608            Some("10.2.1.10")
609        );
610    }
611
612    // Not from documentation: written from the CLI Reference pages; `virtual-gateway-esi` follows the `set` commands of
613    // the EVPN-VXLAN WAN example, in brace form. See the README "Sources".
614    #[test]
615    fn reads_the_irb_unit_statements() {
616        let ifaces = interfaces(&juniper::parse_braces(
617            r#"
618interfaces {
619    irb {
620        unit 1 {
621            proxy-macip-advertisement;
622            enhanced-convergence;
623            virtual-gateway-v4-mac 00:00:5e:00:01:01;
624            virtual-gateway-v6-mac 00:00:5e:00:02:01;
625            virtual-gateway-esi {
626                00:11:aa:aa:aa:aa:aa:aa:aa:aa;
627                all-active;
628            }
629            proxy-arp restricted;
630            ip-address-owner SERVICE;
631            family inet6 {
632                nd6-max-cache 1000;
633                nd6-stale-time 1200;
634                nd6-new-hold-limit 5;
635            }
636        }
637        unit 2 {
638            proxy-arp;
639            family inet;
640        }
641    }
642    ge-0/0/0 {
643        media-type fiber;
644        ether-options {
645            mdi-mode auto;
646            no-auto-mdix;
647        }
648    }
649}
650"#,
651        ));
652        let unit = &ifaces[0].units[0];
653        assert!(unit.proxy_macip_advertisement && unit.enhanced_convergence);
654        assert_eq!(unit.virtual_gateway_v4_mac.as_deref(), Some("00:00:5e:00:01:01"));
655        assert_eq!(unit.virtual_gateway_v6_mac.as_deref(), Some("00:00:5e:00:02:01"));
656        let esi = unit.virtual_gateway_esi.as_ref().unwrap();
657        assert_eq!(esi.identifier.as_deref(), Some("00:11:aa:aa:aa:aa:aa:aa:aa:aa"));
658        assert!(esi.all_active);
659        assert!(unit.proxy_arp);
660        assert_eq!(unit.proxy_arp_mode.as_deref(), Some("restricted"));
661        assert_eq!(unit.ip_address_owner.as_deref(), Some("SERVICE"));
662        let inet6 = &unit.family_settings[0];
663        assert_eq!(inet6.nd6_max_cache.as_deref(), Some("1000"));
664        assert_eq!(inet6.nd6_stale_time.as_deref(), Some("1200"));
665        assert_eq!(inet6.nd6_new_hold_limit.as_deref(), Some("5"));
666
667        let bare = &ifaces[0].units[1];
668        assert!(bare.proxy_arp);
669        assert_eq!(bare.proxy_arp_mode, None);
670        assert!(!bare.proxy_macip_advertisement && bare.virtual_gateway_esi.is_none());
671
672        let port = &ifaces[1];
673        assert_eq!(port.media_type.as_deref(), Some("fiber"));
674        let ether = port.ether_options.as_ref().unwrap();
675        assert_eq!(ether.mdi_mode.as_deref(), Some("auto"));
676        assert!(ether.no_auto_mdix);
677    }
678}