asterust-junos 0.5.14

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! `family`
//! `interfaces { <name> { unit <n> { family <name> { ... } } } }`.
//!
//! # See also
//!
//! - [family (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/family-edit-interfaces.html)
//! - [no-redirects](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-redirects-edit-system.html)
//! - [no-redirects-ipv6](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-redirects-ipv6.html)
//! - [core-facing](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/core-facing-edit-interfaces.html)
//! - [receive-options-packets](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/receive-options-packets-edit-interfaces-ni.html)
//! - [receive-ttl-exceeded](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/receive-ttl-exceeded-edit-interfaces-ni.html)
//! - [multicast-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/multicast-only-edit-interfaces.html)
//! - [unconditional-src-learn](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unconditional-src-learn-edit-interfaces.html)
//! - [tunnel-loopback](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tunnel-loopback-edit-interfaces-unit-family-inet.html)
//! - [mac-validate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mac-validate-edit-interfaces.html)
//! - [tcp-mss (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tcp-mss-edit-interfaces-unit-family.html)
//! - [arp-max-cache](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/arp-max-cache-edit-system.html)
//! - [dad-proxy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-unit-family-inet6-dad-proxy.html)
//! - [ndp-proxy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-unit-family-inet6-ndp-proxy.html)
//! - [proxy (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/proxy-edit-interfaces.html)
//! - [mtu (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mtu-edit-interfaces-ni.html)
//! - [vlan-id (Logical Port in Bridge Domain)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-id-edit-interfaces-family-bridge.html)
//! - [vlan-id-list (Interface in Bridge Domain)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-id-list-edit-interfaces.html)

mod accounting;
mod address;
mod dhcp;
mod ethernet_switching;
mod filter;
mod next_hop_tunnel;
mod policer;
mod rpf_check;
mod sampling;

pub use accounting::{FamilyAccounting, TargetedBroadcast, UnnumberedAddress, VlanTranslation};
pub use address::{
    AddressArp, BandwidthThreshold, TrackedInterface, TrackedRoute, UnitAddress, VrrpGroup, VrrpInheritFrom, VrrpTrack,
};
pub use dhcp::{DhcpClient, DhcpClientIdentifier, Dhcpv6Client, PrefixDelegating, RouterAdvertisementInterface};
pub use ethernet_switching::EthernetSwitching;
pub use filter::FamilyFilter;
pub use next_hop_tunnel::NextHopTunnel;
pub use policer::FamilyPolicer;
pub use rpf_check::RpfCheck;
pub use sampling::FamilySampling;

use super::demux_options::demux_prefixes;
use super::pppoe_underlying_options::{parse_pppoe_underlying_options, PppoeUnderlyingOptions};
use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, value_after};
use accounting::{parse_accounting, parse_targeted_broadcast, parse_unnumbered_address, parse_vlan_rewrite};
use address::parse_addresses;
use asterust::config::ConfigLine;
use dhcp::{parse_dhcp_client, parse_dhcpv6_client};
use ethernet_switching::parse_ethernet_switching;
use filter::parse_filter;
use next_hop_tunnel::parse_next_hop_tunnels;
use policer::parse_policer;
use rpf_check::parse_rpf_check;
use sampling::parse_sampling;

/// A `family <name>` of a logical unit, with the settings that apply to it.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnitFamily {
    /// e.g. `"inet"`, `"inet6"`, `"iso"`, `"mpls"`.
    pub name: String,
    pub mtu: Option<String>,
    pub filter: Option<FamilyFilter>,
    pub rpf_check: Option<RpfCheck>,
    pub policer: Option<FamilyPolicer>,
    pub sampling: Option<FamilySampling>,
    /// `vlan-id` of `family bridge`.
    pub vlan_id: Option<String>,
    /// `vlan-id-list` of `family bridge`: IDs and `<from>-<to>` ranges.
    pub vlan_id_list: Vec<String>,
    pub addresses: Vec<UnitAddress>,
    /// `destination-udp-port` of a family on a UDP tunnel such as `fti`.
    pub destination_udp_port: Option<String>,
    /// The port settings of `family ethernet-switching`; `None` for other families.
    pub ethernet_switching: Option<EthernetSwitching>,
    /// The `nd6-max-cache` of `family inet6`.
    pub nd6_max_cache: Option<String>,
    /// Seconds; the `nd6-stale-time` of `family inet6`.
    pub nd6_stale_time: Option<String>,
    /// The `nd6-new-hold-limit` of `family inet6`.
    pub nd6_new_hold_limit: Option<String>,
    /// The settings of `family pppoe`, which are those of `pppoe-underlying-options`.
    pub pppoe: Option<PppoeUnderlyingOptions>,
    /// The prefixes of `demux-source`.
    pub demux_source: Vec<String>,
    /// The prefixes of `demux-destination`.
    pub demux_destination: Vec<String>,
    /// The `dhcp` block of `family inet`.
    pub dhcp: Option<DhcpClient>,
    pub dhcp_client: Option<DhcpClient>,
    pub dhcpv6_client: Option<Dhcpv6Client>,
    pub no_redirects: bool,
    pub no_redirects_ipv6: bool,
    pub core_facing: bool,
    pub receive_options_packets: bool,
    pub receive_ttl_exceeded: bool,
    pub multicast_only: bool,
    pub unconditional_src_learn: bool,
    pub tunnel_loopback: bool,
    /// `loose` or `strict`.
    pub mac_validate: Option<String>,
    pub tcp_mss: Option<String>,
    pub arp_max_cache: Option<String>,
    /// `interface-restricted` or `interface-unrestricted`, from `dad-proxy`.
    pub dad_proxy: Option<String>,
    /// `interface-restricted` or `interface-unrestricted`, from `ndp-proxy`.
    pub ndp_proxy: Option<String>,
    /// The `inet-address` of `proxy` in `family tcc`.
    pub proxy_inet_address: Option<String>,
    pub unnumbered_address: Option<UnnumberedAddress>,
    pub accounting: Option<FamilyAccounting>,
    pub targeted_broadcast: Option<TargetedBroadcast>,
    pub vlan_rewrite: Vec<VlanTranslation>,
    pub next_hop_tunnel: Vec<NextHopTunnel>,
}

/// The mode of `dad-proxy` or `ndp-proxy`, written inside a block or after the keyword.
fn proxy_mode(family: &ConfigLine, keyword: &str) -> Option<String> {
    statement_words(family, keyword)?.first().map(|mode| mode.to_string())
}

pub(super) fn parse_family(name: &str, family: &ConfigLine) -> UnitFamily {
    UnitFamily {
        name: name.to_string(),
        mtu: arg_of(family, "mtu").map(str::to_string),
        filter: parse_filter(family),
        rpf_check: parse_rpf_check(family),
        policer: parse_policer(family),
        sampling: parse_sampling(family),
        vlan_id: arg_of(family, "vlan-id").map(str::to_string),
        vlan_id_list: args_of(family, "vlan-id-list")
            .into_iter()
            .flat_map(list_values)
            .collect(),
        addresses: parse_addresses(family),
        destination_udp_port: arg_of(family, "destination-udp-port").map(str::to_string),
        ethernet_switching: (name == "ethernet-switching").then(|| parse_ethernet_switching(family)),
        nd6_max_cache: arg_of(family, "nd6-max-cache").map(str::to_string),
        nd6_stale_time: arg_of(family, "nd6-stale-time").map(str::to_string),
        nd6_new_hold_limit: arg_of(family, "nd6-new-hold-limit").map(str::to_string),
        pppoe: (name == "pppoe").then(|| parse_pppoe_underlying_options(family)),
        demux_source: demux_prefixes(family, "demux-source"),
        demux_destination: demux_prefixes(family, "demux-destination"),
        dhcp: child_block(family, "dhcp").map(parse_dhcp_client),
        dhcp_client: child_block(family, "dhcp-client").map(parse_dhcp_client),
        dhcpv6_client: child_block(family, "dhcpv6-client").map(parse_dhcpv6_client),
        no_redirects: has_leaf(family, "no-redirects"),
        no_redirects_ipv6: has_leaf(family, "no-redirects-ipv6"),
        core_facing: has_leaf(family, "core-facing"),
        receive_options_packets: has_leaf(family, "receive-options-packets"),
        receive_ttl_exceeded: has_leaf(family, "receive-ttl-exceeded"),
        multicast_only: has_leaf(family, "multicast-only"),
        unconditional_src_learn: has_leaf(family, "unconditional-src-learn"),
        tunnel_loopback: has_leaf(family, "tunnel-loopback"),
        mac_validate: arg_of(family, "mac-validate").map(str::to_string),
        tcp_mss: arg_of(family, "tcp-mss").map(str::to_string),
        arp_max_cache: arg_of(family, "arp-max-cache").map(str::to_string),
        dad_proxy: proxy_mode(family, "dad-proxy"),
        ndp_proxy: proxy_mode(family, "ndp-proxy"),
        proxy_inet_address: statement_words(family, "proxy").and_then(|words| value_after(&words, "inet-address")),
        unnumbered_address: parse_unnumbered_address(family),
        accounting: child_block(family, "accounting").map(parse_accounting),
        targeted_broadcast: child_block(family, "targeted-broadcast").map(parse_targeted_broadcast),
        vlan_rewrite: parse_vlan_rewrite(family),
        next_hop_tunnel: parse_next_hop_tunnels(family),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    const CONFIG: &str = r#"
interfaces {
    ge-0/0/0 {
        unit 0 {
            family inet {
                mtu 1500;
                filter {
                    group 5;
                    input PROTECT-IN;
                    output PROTECT-OUT;
                    input-list [ A B ];
                    output-list C;
                }
                rpf-check {
                    fail-filter RPF-FAIL;
                    mode loose;
                }
                policer {
                    arp ARP-POLICER;
                    input IN-POLICER;
                    output OUT-POLICER;
                }
                sampling {
                    input;
                    output;
                }
                address 192.0.2.1/24 {
                    primary;
                    preferred;
                    destination 192.0.2.2;
                    broadcast 192.0.2.255;
                    arp 192.0.2.50 mac 00:11:22:33:44:55 publish;
                    arp 192.0.2.51 multicast-mac 01:00:5e:00:00:01;
                }
                address 192.0.2.9/24;
            }
            family inet6 {
                rpf-check;
                sampling input;
                address 2001:db8::1/64 {
                    eui-64;
                }
            }
            family iso;
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_more_family_statements() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ge-0/0/0 {
        unit 0 {
            family inet {
                no-redirects;
                core-facing;
                receive-options-packets;
                receive-ttl-exceeded;
                multicast-only;
                unconditional-src-learn;
                tunnel-loopback;
                mac-validate strict;
                tcp-mss 1400;
                arp-max-cache 1000;
                unnumbered-address lo0.0 preferred-source-address 10.0.0.1;
                accounting {
                    destination-class-usage;
                    source-class-usage {
                        input;
                        output;
                    }
                }
                targeted-broadcast {
                    forward-and-send-to-re;
                }
            }
            family inet6 {
                no-redirects-ipv6;
                tcp-mss 1300;
                dad-proxy {
                    interface-restricted;
                }
                ndp-proxy {
                    interface-unrestricted;
                }
                unnumbered-address lo0.1;
            }
        }
        unit 1 {
            family tcc {
                proxy inet-address 192.0.2.9;
            }
            family bridge {
                vlan-rewrite {
                    translate 100 200;
                    translate 300 400;
                }
            }
            family ethernet-switching {
                interface-mode trunk;
                inter-switch-link vlan members primary-vlan;
            }
        }
        unit 2 {
            family inet {
                unnumbered-address ge-0/0/1.0 destination 192.0.2.1 destination-profile prof1;
                accounting {
                    source-class-usage output;
                }
                targeted-broadcast {
                    forward-only;
                }
            }
        }
        unit 3 {
            family inet {
                mtu 1500;
            }
        }
    }
}
"#,
        ));
        let units = &ifaces[0].units;
        let inet = &units[0].family_settings[0];
        assert!(inet.no_redirects && inet.core_facing && inet.receive_options_packets && inet.receive_ttl_exceeded);
        assert!(inet.multicast_only && inet.unconditional_src_learn && inet.tunnel_loopback);
        assert_eq!(inet.mac_validate.as_deref(), Some("strict"));
        assert_eq!(inet.tcp_mss.as_deref(), Some("1400"));
        assert_eq!(inet.arp_max_cache.as_deref(), Some("1000"));
        let unnumbered = inet.unnumbered_address.as_ref().unwrap();
        assert_eq!(unnumbered.interface, "lo0.0");
        assert_eq!(unnumbered.preferred_source_address.as_deref(), Some("10.0.0.1"));
        assert!(unnumbered.destination.is_none() && unnumbered.destination_profile.is_none());
        let accounting = inet.accounting.as_ref().unwrap();
        assert!(accounting.destination_class_usage && accounting.source_class_usage);
        assert_eq!(accounting.source_class_usage_directions, ["input", "output"]);
        let broadcast = inet.targeted_broadcast.as_ref().unwrap();
        assert!(broadcast.forward_and_send_to_re && !broadcast.forward_only);

        let inet6 = &units[0].family_settings[1];
        assert!(inet6.no_redirects_ipv6 && !inet6.no_redirects);
        assert_eq!(inet6.tcp_mss.as_deref(), Some("1300"));
        assert_eq!(inet6.dad_proxy.as_deref(), Some("interface-restricted"));
        assert_eq!(inet6.ndp_proxy.as_deref(), Some("interface-unrestricted"));
        assert_eq!(inet6.unnumbered_address.as_ref().unwrap().interface, "lo0.1");

        assert_eq!(
            units[1].family_settings[0].proxy_inet_address.as_deref(),
            Some("192.0.2.9")
        );
        let rewrite = &units[1].family_settings[1].vlan_rewrite;
        assert_eq!(rewrite.len(), 2);
        assert_eq!((rewrite[1].from.as_str(), rewrite[1].to.as_str()), ("300", "400"));
        let switching = units[1].family_settings[2].ethernet_switching.as_ref().unwrap();
        assert_eq!(switching.inter_switch_link_vlan.as_deref(), Some("primary-vlan"));

        let ppp = units[2].family_settings[0].unnumbered_address.as_ref().unwrap();
        assert_eq!(ppp.interface, "ge-0/0/1.0");
        assert_eq!(ppp.destination.as_deref(), Some("192.0.2.1"));
        assert_eq!(ppp.destination_profile.as_deref(), Some("prof1"));
        let source = units[2].family_settings[0].accounting.as_ref().unwrap();
        assert_eq!(source.source_class_usage_directions, ["output"]);
        assert!(
            units[2].family_settings[0]
                .targeted_broadcast
                .as_ref()
                .unwrap()
                .forward_only
        );

        let plain = &units[3].family_settings[0];
        assert!(!plain.no_redirects && !plain.multicast_only && plain.mac_validate.is_none());
        assert!(plain.unnumbered_address.is_none() && plain.accounting.is_none() && plain.targeted_broadcast.is_none());
        assert!(plain.vlan_rewrite.is_empty() && plain.dad_proxy.is_none() && plain.proxy_inet_address.is_none());
    }

    #[test]
    fn extracts_family_filters_policers_and_sampling() {
        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
        let inet = &ifaces[0].units[0].family_settings[0];
        assert_eq!(inet.name, "inet");
        assert_eq!(inet.mtu.as_deref(), Some("1500"));

        let filter = inet.filter.as_ref().unwrap();
        assert_eq!(filter.group.as_deref(), Some("5"));
        assert_eq!(filter.input.as_deref(), Some("PROTECT-IN"));
        assert_eq!(filter.output.as_deref(), Some("PROTECT-OUT"));
        assert_eq!(filter.input_list, ["A", "B"]);
        assert_eq!(filter.output_list, ["C"]);

        let policer = inet.policer.as_ref().unwrap();
        assert_eq!(policer.arp.as_deref(), Some("ARP-POLICER"));
        assert_eq!(policer.input.as_deref(), Some("IN-POLICER"));
        assert_eq!(policer.output.as_deref(), Some("OUT-POLICER"));

        let sampling = inet.sampling.as_ref().unwrap();
        assert!(sampling.input);
        assert!(sampling.output);
    }

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

        let rpf = families[0].rpf_check.as_ref().unwrap();
        assert_eq!(rpf.fail_filter.as_deref(), Some("RPF-FAIL"));
        assert_eq!(rpf.mode.as_deref(), Some("loose"));

        let bare = families[1].rpf_check.as_ref().unwrap();
        assert_eq!(bare.fail_filter, None);
        assert_eq!(bare.mode, None);
    }

    #[test]
    fn extracts_address_options_and_arp_entries() {
        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
        let addresses = &ifaces[0].units[0].family_settings[0].addresses;
        assert_eq!(addresses.len(), 2);

        let first = &addresses[0];
        assert_eq!(first.address, "192.0.2.1/24");
        assert!(first.primary);
        assert!(first.preferred);
        assert!(!first.master_only);
        assert_eq!(first.destination.as_deref(), Some("192.0.2.2"));
        assert_eq!(first.broadcast.as_deref(), Some("192.0.2.255"));
        assert_eq!(first.arps.len(), 2);
        assert_eq!(first.arps[0].address, "192.0.2.50");
        assert_eq!(first.arps[0].mac.as_deref(), Some("00:11:22:33:44:55"));
        assert!(first.arps[0].publish);
        assert!(!first.arps[0].multicast_mac);
        assert!(first.arps[1].multicast_mac);
        assert!(!first.arps[1].publish);

        let second = &addresses[1];
        assert_eq!(second.address, "192.0.2.9/24");
        assert!(!second.primary);
        assert!(second.arps.is_empty());
    }

    #[test]
    fn extracts_inline_sampling_and_eui_64() {
        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
        let inet6 = &ifaces[0].units[0].family_settings[1];
        let sampling = inet6.sampling.as_ref().unwrap();
        assert!(sampling.input);
        assert!(!sampling.output);
        assert!(inet6.addresses[0].eui_64);
    }

    #[test]
    fn family_without_settings_has_none() {
        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
        let iso = &ifaces[0].units[0].family_settings[2];
        assert_eq!(iso.name, "iso");
        assert!(iso.mtu.is_none());
        assert!(iso.filter.is_none());
        assert!(iso.rpf_check.is_none());
        assert!(iso.policer.is_none());
        assert!(iso.sampling.is_none());
        assert!(iso.addresses.is_empty());
    }
}