asterust-junos 0.4.4

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! `evpn`
//! `protocols { evpn { ... } }`.
//!
//! The `interconnect`, `oism` and `source-packet-routing` blocks are not extracted.
//!
//! # See also
//!
//! - [evpn](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/evpn-edit-routing-instances-protocols.html)
//! - [advertise-zero-ethernet-tag](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/advertise-zero-ethernet-tag-edit-protocols-evpn.html)
//! - [assisted-replication](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/assisted-replication-edit-routing-instances-protocols-evpn.html)
//! - [leaf (assisted replication)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/leaf-edit-protocols-evpn-assisted-replication.html)
//! - [replicator](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/replicator-edit-protocols-evpn-assisted-replication.html)
//! - [colored-tunnel-address](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/colored-tunnel-address-edit-routing-instances-protocols-evpn.html)
//! - [default-gateway](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/default-gateway.html)
//! - [duplicate-mac-detection](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/duplicate-mac-detection.html)
//! - [encapsulation (EVPN)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/enapsulation-vxlan-edit-routing-instances.html)
//! - [extended-vlan-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/extended-vlan-list-edit-routing-instances.html)
//! - [extended-vni-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/extended-vni-list.html)
//! - [interconnect multihoming peer gateways](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interconnect-multihoming-peer-gateways-protocols-evpn.html)
//! - [interface (EVPN)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-routing-instances-evpn.html)
//! - [irb-symmetric-routing](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/irb-symmetric-routing-protocols-evpn.html)
//! - [label-allocation](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/label-allocation-edit-routing-instances-protocols-evpn.html)
//! - [mac-ip-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mac-ip-limit-edit-protocols-evpn.html)
//! - [mac-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mac-list-edit-protocols-evpn.html)
//! - [multicast-mode](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/multicast-mode-evpn-edit-routing-instances.html)
//! - [no-default-gateway-ext-comm](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-default-gateway-ext-comm-edit-routing-instances.html)
//! - [pbb-evpn-core](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pbb-evpn-core-edit-routing-instances.html)
//! - [remote-ip-host-routes](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/remote-ip-host-routes.html)
//! - [static-multihoming-peer](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-multihoming-peer-edit-protocols-evpn.html)
//! - [traceoptions (EVPN)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-protocols-evpns.html)
//! - [Example: Configuring an EVPN-VXLAN Fabric (MX and QFX)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/evpn/topics/example/evpn-vxlan-mx-qfx-configuring.html)
//! - [EVPN-VXLAN QFX5110 L3 gateway](https://networkingdocs.hpe.com/documentation/us/en/software/junos/evpn/topics/example/evpn-vxlan-qfx5110-l3-vxlan-gateway-collapsed.html)
//! - [Configuring Loop Detection Settings](https://networkingdocs.hpe.com/documentation/us/en/software/junos/evpn/topics/task/configuring-loop-detection-settings.html)
//! - [Configuring MAC Mobility Settings](https://networkingdocs.hpe.com/documentation/us/en/software/junos/evpn/topics/task/configuring-mac-mobility-settings.html)

use super::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, top_level};
use asterust::config::{Config, ConfigLine};

/// A MAC limit with its `packet-action`, as in `interface-mac-limit 100 { packet-action drop; }`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EvpnMacLimit {
    pub limit: String,
    pub packet_action: Option<String>,
}

/// An `interface <name>` entry of EVPN.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EvpnInterface {
    pub name: String,
    pub ignore_encapsulation_mismatch: bool,
    pub interface_mac_limit: Option<EvpnMacLimit>,
    pub mac_pinning: bool,
    pub no_mac_learning: bool,
    pub static_macs: Vec<String>,
}

/// A `vrf-target` statement: a bare community, or a block with `import`, `export` and `auto`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EvpnVrfTarget {
    pub community: Option<String>,
    pub auto: bool,
    pub import: Option<String>,
    pub export: Option<String>,
}

/// A `vni <number>` entry of `vni-options`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EvpnVniOptions {
    pub vni: String,
    /// Seconds.
    pub designated_forwarder_election_hold_time: Option<String>,
    pub vrf_target: Option<EvpnVrfTarget>,
}

/// The `duplicate-mac-detection` block.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EvpnDuplicateMacDetection {
    /// Minutes.
    pub auto_recovery_time: Option<String>,
    pub detection_threshold: Option<String>,
    /// Seconds.
    pub detection_window: Option<String>,
    /// `block` or `shutdown`.
    pub action: Option<String>,
    pub include_local_moves: bool,
}

/// The `assisted-replication` block.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EvpnAssistedReplication {
    /// Seconds, from `leaf { replicator-activation-delay ...; }`.
    pub leaf_replicator_activation_delay: Option<String>,
    /// From `replicator { inet ...; }`.
    pub replicator_inet: Option<String>,
    /// From `replicator { vxlan-encapsulation-source-ip ...; }`.
    pub replicator_vxlan_encapsulation_source_ip: Option<String>,
}

/// An entry of `mac-list`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EvpnMacList {
    pub name: String,
    pub mac_addresses: Vec<String>,
}

/// The `remote-ip-host-routes` block.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EvpnRemoteIpHostRoutes {
    pub import: Vec<String>,
    pub no_advertise_community: bool,
}

/// The `protocols evpn` configuration.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Evpn {
    pub advertise_zero_ethernet_tag: bool,
    pub assisted_replication: Option<EvpnAssistedReplication>,
    /// `inet` and `inet6`, from `colored-tunnel-address`.
    pub colored_tunnel_addresses: Vec<String>,
    /// `advertise`, `do-not-advertise` or `no-gateway-community`.
    pub default_gateway: Option<String>,
    /// Seconds.
    pub designated_forwarder_election_hold_time: Option<String>,
    /// `highest` or `least`, from `designated-forwarder-preference-highest` and `-least`.
    pub designated_forwarder_preference: Option<String>,
    pub df_election_ntp: bool,
    pub duplicate_mac_detection: Option<EvpnDuplicateMacDetection>,
    /// e.g. `vxlan` or `mpls`.
    pub encapsulation: Option<String>,
    pub es_import_oldstyle: bool,
    pub es_label_oldstyle: bool,
    pub esi_resolution_per_bridge_domain: bool,
    pub evpn_etree: bool,
    pub extended_vlan_list: Vec<String>,
    /// VNIs, or `all`.
    pub extended_vni_list: Vec<String>,
    pub interconnect_multihoming_peer_gateways: Vec<String>,
    pub interconnected_vlan_list: Vec<String>,
    pub interconnected_vni_list: Vec<String>,
    pub interface_mac_limit: Option<EvpnMacLimit>,
    pub interfaces: Vec<EvpnInterface>,
    /// The VNI of `irb-symmetric-routing`.
    pub irb_symmetric_routing_vni: Option<String>,
    pub label_allocation: Option<String>,
    pub leave_sync_route_oldstyle: bool,
    pub mac_ip_limit: Option<String>,
    pub mac_lists: Vec<EvpnMacList>,
    /// `mac-mobility { no-sequence-numbers; }`.
    pub mac_mobility_no_sequence_numbers: bool,
    pub mac_statistics: bool,
    pub mac_table_size: Option<EvpnMacLimit>,
    /// `client` or `ingress-replication`.
    pub multicast_mode: Option<String>,
    pub no_core_isolation: bool,
    pub no_default_gateway_ext_comm: bool,
    pub no_mac_learning: bool,
    pub p2mp_bud_support: bool,
    pub pbb_evpn_core: bool,
    pub peg_df_election_history: Option<String>,
    pub pmsi_tunnel_endpoint: Option<String>,
    pub remote_ip_host_routes: Option<EvpnRemoteIpHostRoutes>,
    pub smet_etag_carry_vid: bool,
    pub static_multihoming_peers: Vec<String>,
    pub suppress_remote_host_routes_on_vlan_down: bool,
    pub traceoptions: Option<Traceoptions>,
    pub vni_options: Vec<EvpnVniOptions>,
}

fn list(line: &ConfigLine, keyword: &str) -> Vec<String> {
    args_of(line, keyword).into_iter().flat_map(list_values).collect()
}

fn parse_mac_limit(limit: &str, entry: &ConfigLine) -> EvpnMacLimit {
    EvpnMacLimit {
        limit: limit.to_string(),
        packet_action: arg_of(entry, "packet-action").map(str::to_string),
    }
}

fn parse_vrf_target(parent: &ConfigLine) -> Option<EvpnVrfTarget> {
    let leaf = arg_of(parent, "vrf-target");
    let block = child_block(parent, "vrf-target");
    if leaf.is_none() && block.is_none() {
        return None;
    }
    let mut target = EvpnVrfTarget::default();
    match leaf {
        Some("auto") => target.auto = true,
        Some(community) => target.community = Some(community.to_string()),
        None => {}
    }
    if let Some(block) = block {
        target.auto |= has_leaf(block, "auto");
        target.import = arg_of(block, "import").map(str::to_string);
        target.export = arg_of(block, "export").map(str::to_string);
        target.community = active_children(block)
            .map(|c| c.text.as_str())
            .find(|text| *text != "auto" && !text.starts_with("import ") && !text.starts_with("export "))
            .map(str::to_string);
    }
    Some(target)
}

fn parse_interface(name: &str, interface: &ConfigLine) -> EvpnInterface {
    EvpnInterface {
        name: name.to_string(),
        ignore_encapsulation_mismatch: has_leaf(interface, "ignore-encapsulation-mismatch"),
        interface_mac_limit: named_children(interface, "interface-mac-limit")
            .next()
            .map(|(limit, entry)| parse_mac_limit(limit, entry)),
        mac_pinning: has_leaf(interface, "mac-pinning"),
        no_mac_learning: has_leaf(interface, "no-mac-learning"),
        static_macs: args_of(interface, "static-mac")
            .into_iter()
            .map(str::to_string)
            .collect(),
    }
}

/// Returns the `protocols evpn` block, or `None` if EVPN is not configured.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::protocols::evpn::evpn;
///
/// let config = juniper::parse_braces("protocols {\n    evpn {\n        encapsulation vxlan;\n        extended-vni-list all;\n    }\n}\n");
/// let evpn = evpn(&config).unwrap();
/// assert_eq!(evpn.encapsulation.as_deref(), Some("vxlan"));
/// assert_eq!(evpn.extended_vni_list, ["all"]);
/// ```
pub fn evpn(config: &Config) -> Option<Evpn> {
    let evpn = child_block(top_level(config, "protocols")?, "evpn")?;
    let field = |keyword: &str| arg_of(evpn, keyword).map(str::to_string);
    let flag = |keyword: &str| has_leaf(evpn, keyword);
    Some(Evpn {
        advertise_zero_ethernet_tag: flag("advertise-zero-ethernet-tag"),
        assisted_replication: child_block(evpn, "assisted-replication").map(|block| EvpnAssistedReplication {
            leaf_replicator_activation_delay: child_block(block, "leaf")
                .and_then(|leaf| arg_of(leaf, "replicator-activation-delay"))
                .map(str::to_string),
            replicator_inet: child_block(block, "replicator")
                .and_then(|replicator| arg_of(replicator, "inet"))
                .map(str::to_string),
            replicator_vxlan_encapsulation_source_ip: child_block(block, "replicator")
                .and_then(|replicator| arg_of(replicator, "vxlan-encapsulation-source-ip"))
                .map(str::to_string),
        }),
        colored_tunnel_addresses: child_block(evpn, "colored-tunnel-address")
            .map(|block| active_children(block).map(|c| c.text.clone()).collect())
            .unwrap_or_default(),
        default_gateway: field("default-gateway"),
        designated_forwarder_election_hold_time: field("designated-forwarder-election-hold-time"),
        designated_forwarder_preference: if flag("designated-forwarder-preference-highest") {
            Some("highest".to_string())
        } else if flag("designated-forwarder-preference-least") {
            Some("least".to_string())
        } else {
            None
        },
        df_election_ntp: flag("df-election-ntp"),
        duplicate_mac_detection: child_block(evpn, "duplicate-mac-detection").map(|block| EvpnDuplicateMacDetection {
            auto_recovery_time: arg_of(block, "auto-recovery-time").map(str::to_string),
            detection_threshold: arg_of(block, "detection-threshold").map(str::to_string),
            detection_window: arg_of(block, "detection-window").map(str::to_string),
            action: arg_of(block, "action").map(str::to_string),
            include_local_moves: has_leaf(block, "include-local-moves"),
        }),
        encapsulation: field("encapsulation"),
        es_import_oldstyle: flag("es-import-oldstyle"),
        es_label_oldstyle: flag("es-label-oldstyle"),
        esi_resolution_per_bridge_domain: flag("esi-resolution-per-bridge-domain"),
        evpn_etree: flag("evpn-etree"),
        extended_vlan_list: list(evpn, "extended-vlan-list"),
        extended_vni_list: list(evpn, "extended-vni-list"),
        interconnect_multihoming_peer_gateways: list(evpn, "interconnect-multihoming-peer-gateways"),
        interconnected_vlan_list: list(evpn, "interconnected-vlan-list"),
        interconnected_vni_list: list(evpn, "interconnected-vni-list"),
        interface_mac_limit: named_children(evpn, "interface-mac-limit")
            .next()
            .map(|(limit, entry)| parse_mac_limit(limit, entry)),
        interfaces: named_children(evpn, "interface")
            .map(|(name, interface)| parse_interface(name, interface))
            .collect(),
        irb_symmetric_routing_vni: child_block(evpn, "irb-symmetric-routing")
            .and_then(|block| arg_of(block, "vni"))
            .map(str::to_string),
        label_allocation: field("label-allocation"),
        leave_sync_route_oldstyle: flag("leave-sync-route-oldstyle"),
        mac_ip_limit: field("mac-ip-limit"),
        mac_lists: named_children(evpn, "mac-list")
            .map(|(name, entry)| EvpnMacList {
                name: name.to_string(),
                mac_addresses: active_children(entry).map(|c| c.text.clone()).collect(),
            })
            .collect(),
        mac_mobility_no_sequence_numbers: child_block(evpn, "mac-mobility")
            .is_some_and(|block| has_leaf(block, "no-sequence-numbers")),
        mac_statistics: flag("mac-statistics"),
        mac_table_size: named_children(evpn, "mac-table-size")
            .next()
            .map(|(limit, entry)| parse_mac_limit(limit, entry)),
        multicast_mode: field("multicast-mode"),
        no_core_isolation: flag("no-core-isolation"),
        no_default_gateway_ext_comm: flag("no-default-gateway-ext-comm"),
        no_mac_learning: flag("no-mac-learning"),
        p2mp_bud_support: flag("p2mp-bud-support"),
        pbb_evpn_core: flag("pbb-evpn-core"),
        peg_df_election_history: field("peg-df-election-history"),
        pmsi_tunnel_endpoint: field("pmsi-tunnel-endpoint"),
        remote_ip_host_routes: child_block(evpn, "remote-ip-host-routes").map(|block| EvpnRemoteIpHostRoutes {
            import: list(block, "import"),
            no_advertise_community: has_leaf(block, "no-advertise-community"),
        }),
        smet_etag_carry_vid: flag("smet-etag-carry-vid"),
        static_multihoming_peers: list(evpn, "static-multihoming-peer"),
        suppress_remote_host_routes_on_vlan_down: flag("suppress-remote-host-routes-on-vlan-down"),
        traceoptions: parse_traceoptions(evpn),
        vni_options: child_block(evpn, "vni-options")
            .map(|options| {
                named_children(options, "vni")
                    .map(|(vni, entry)| EvpnVniOptions {
                        vni: vni.to_string(),
                        designated_forwarder_election_hold_time: arg_of(
                            entry,
                            "designated-forwarder-election-hold-time",
                        )
                        .map(str::to_string),
                        vrf_target: parse_vrf_target(entry),
                    })
                    .collect()
            })
            .unwrap_or_default(),
    })
}

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

    // The `set protocols evpn` commands of Example: Configuring EVPN-VXLAN ... (MX and QFX, leaf-1), converted to
    // hierarchy form; see the README "Sources".
    const DOC_MX_QFX: &str = r#"
protocols {
    evpn {
        encapsulation vxlan;
        multicast-mode ingress-replication;
        vni-options {
            vni 101 {
                vrf-target target:65000:101;
            }
            vni 102 {
                vrf-target target:65000:102;
            }
        }
        extended-vni-list 101;
        extended-vni-list 102;
    }
}
"#;

    // The `set protocols evpn` commands of the QFX5110 L3 VXLAN gateway example, converted to hierarchy form.
    const DOC_QFX5110: &str = r#"
protocols {
    evpn {
        encapsulation vxlan;
        default-gateway no-gateway-community;
        multicast-mode ingress-replication;
        vni-options {
            vni 101 {
                vrf-target target:101:1;
            }
            vni 102 {
                vrf-target target:102:1;
            }
            vni 103 {
                vrf-target target:103:1;
            }
        }
        extended-vni-list all;
    }
}
"#;

    // The `duplicate-mac-detection` commands of Configuring Loop Detection Settings (written for a routing instance),
    // converted to hierarchy form under `protocols evpn`.
    const DOC_DUPLICATE_MAC: &str = r#"
protocols {
    evpn {
        duplicate-mac-detection {
            detection-threshold 3;
            detection-window 5;
            auto-recovery-time 10;
            action block;
            include-local-moves;
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference pages of the statements. See the README "Sources".
    const EXTRA: &str = r#"
protocols {
    evpn {
        advertise-zero-ethernet-tag;
        colored-tunnel-address {
            inet;
            inet6;
        }
        designated-forwarder-election-hold-time 30;
        designated-forwarder-preference-least;
        df-election-ntp;
        es-import-oldstyle;
        es-label-oldstyle;
        esi-resolution-per-bridge-domain;
        evpn-etree;
        leave-sync-route-oldstyle;
        extended-vlan-list [ 10 20 ];
        extended-vni-list [ 1010 1020 ];
        interconnect-multihoming-peer-gateways [ 10.0.0.1 10.0.0.2 ];
        interconnected-vlan-list 30;
        interconnected-vni-list all;
        interface-mac-limit 1000 {
            packet-action drop;
        }
        mac-table-size 2000 {
            packet-action drop;
        }
        irb-symmetric-routing {
            vni 9000;
        }
        label-allocation per-instance;
        mac-ip-limit 500;
        mac-mobility {
            no-sequence-numbers;
        }
        mac-statistics;
        no-core-isolation;
        no-default-gateway-ext-comm;
        no-mac-learning;
        p2mp-bud-support;
        pbb-evpn-core;
        peg-df-election-history 5;
        pmsi-tunnel-endpoint 10.9.9.9;
        smet-etag-carry-vid;
        suppress-remote-host-routes-on-vlan-down;
        static-multihoming-peer [ 10.1.1.1 10.1.1.2 ];
        default-gateway advertise;
        multicast-mode client;
        encapsulation mpls;
        assisted-replication {
            leaf {
                replicator-activation-delay 10;
            }
            replicator {
                inet 10.255.0.1;
                vxlan-encapsulation-source-ip ingress-replication-ip;
            }
        }
        mac-list BLOCKED {
            00:11:22:33:44:55;
            00:11:22:33:44:66;
        }
        remote-ip-host-routes {
            import [ rih1 rih2 ];
            no-advertise-community;
        }
        traceoptions {
            file evpn.log size 1m;
            flag all;
        }
        interface ge-0/0/0.0 {
            ignore-encapsulation-mismatch;
            interface-mac-limit 100 {
                packet-action drop;
            }
            mac-pinning;
            no-mac-learning;
            static-mac 00:aa:bb:cc:dd:ee;
        }
        vni-options {
            vni 1010 {
                designated-forwarder-election-hold-time 20;
                vrf-target {
                    target:65000:1010;
                    auto;
                    import target:65000:1;
                    export target:65000:2;
                }
            }
            vni 1020 {
                vrf-target auto;
            }
        }
    }
}
"#;

    fn evpn_of(text: &str) -> Evpn {
        evpn(&juniper::parse_braces(text)).unwrap()
    }

    #[test]
    fn reads_the_guide_mx_qfx_example() {
        let evpn = evpn_of(DOC_MX_QFX);
        assert_eq!(evpn.encapsulation.as_deref(), Some("vxlan"));
        assert_eq!(evpn.multicast_mode.as_deref(), Some("ingress-replication"));
        assert_eq!(evpn.extended_vni_list, ["101", "102"]);
        assert_eq!(evpn.vni_options.len(), 2);
        assert_eq!(evpn.vni_options[0].vni, "101");
        let target = evpn.vni_options[0].vrf_target.as_ref().unwrap();
        assert_eq!(target.community.as_deref(), Some("target:65000:101"));
        assert!(!target.auto);
        assert_eq!(
            evpn.vni_options[1].vrf_target.as_ref().unwrap().community.as_deref(),
            Some("target:65000:102")
        );
    }

    #[test]
    fn reads_the_guide_qfx5110_example() {
        let evpn = evpn_of(DOC_QFX5110);
        assert_eq!(evpn.default_gateway.as_deref(), Some("no-gateway-community"));
        assert_eq!(evpn.extended_vni_list, ["all"]);
        assert_eq!(evpn.vni_options.len(), 3);
        assert_eq!(
            evpn.vni_options[2].vrf_target.as_ref().unwrap().community.as_deref(),
            Some("target:103:1")
        );
    }

    #[test]
    fn reads_the_guide_duplicate_mac_detection() {
        let detection = evpn_of(DOC_DUPLICATE_MAC).duplicate_mac_detection.unwrap();
        assert_eq!(detection.detection_threshold.as_deref(), Some("3"));
        assert_eq!(detection.detection_window.as_deref(), Some("5"));
        assert_eq!(detection.auto_recovery_time.as_deref(), Some("10"));
        assert_eq!(detection.action.as_deref(), Some("block"));
        assert!(detection.include_local_moves);
    }

    #[test]
    fn reads_the_flags_and_simple_options() {
        let evpn = evpn_of(EXTRA);
        assert!(evpn.advertise_zero_ethernet_tag);
        assert_eq!(evpn.colored_tunnel_addresses, ["inet", "inet6"]);
        assert_eq!(evpn.designated_forwarder_election_hold_time.as_deref(), Some("30"));
        assert_eq!(evpn.designated_forwarder_preference.as_deref(), Some("least"));
        assert!(evpn.df_election_ntp);
        assert!(evpn.es_import_oldstyle);
        assert!(evpn.es_label_oldstyle);
        assert!(evpn.esi_resolution_per_bridge_domain);
        assert!(evpn.evpn_etree);
        assert!(evpn.leave_sync_route_oldstyle);
        assert_eq!(evpn.default_gateway.as_deref(), Some("advertise"));
        assert_eq!(evpn.multicast_mode.as_deref(), Some("client"));
        assert_eq!(evpn.encapsulation.as_deref(), Some("mpls"));
        assert_eq!(evpn.label_allocation.as_deref(), Some("per-instance"));
        assert_eq!(evpn.mac_ip_limit.as_deref(), Some("500"));
        assert!(evpn.mac_mobility_no_sequence_numbers);
        assert!(evpn.mac_statistics);
        assert!(evpn.no_core_isolation);
        assert!(evpn.no_default_gateway_ext_comm);
        assert!(evpn.no_mac_learning);
        assert!(evpn.p2mp_bud_support);
        assert!(evpn.pbb_evpn_core);
        assert_eq!(evpn.peg_df_election_history.as_deref(), Some("5"));
        assert_eq!(evpn.pmsi_tunnel_endpoint.as_deref(), Some("10.9.9.9"));
        assert!(evpn.smet_etag_carry_vid);
        assert!(evpn.suppress_remote_host_routes_on_vlan_down);
        assert_eq!(evpn.irb_symmetric_routing_vni.as_deref(), Some("9000"));
    }

    #[test]
    fn reads_the_lists_and_limits() {
        let evpn = evpn_of(EXTRA);
        assert_eq!(evpn.extended_vlan_list, ["10", "20"]);
        assert_eq!(evpn.extended_vni_list, ["1010", "1020"]);
        assert_eq!(evpn.interconnect_multihoming_peer_gateways, ["10.0.0.1", "10.0.0.2"]);
        assert_eq!(evpn.interconnected_vlan_list, ["30"]);
        assert_eq!(evpn.interconnected_vni_list, ["all"]);
        assert_eq!(evpn.static_multihoming_peers, ["10.1.1.1", "10.1.1.2"]);
        let limit = evpn.interface_mac_limit.unwrap();
        assert_eq!(limit.limit, "1000");
        assert_eq!(limit.packet_action.as_deref(), Some("drop"));
        assert_eq!(evpn.mac_table_size.unwrap().limit, "2000");
    }

    #[test]
    fn reads_the_blocks() {
        let evpn = evpn_of(EXTRA);
        let replication = evpn.assisted_replication.unwrap();
        assert_eq!(replication.leaf_replicator_activation_delay.as_deref(), Some("10"));
        assert_eq!(replication.replicator_inet.as_deref(), Some("10.255.0.1"));
        assert_eq!(
            replication.replicator_vxlan_encapsulation_source_ip.as_deref(),
            Some("ingress-replication-ip")
        );
        assert_eq!(evpn.mac_lists.len(), 1);
        assert_eq!(evpn.mac_lists[0].name, "BLOCKED");
        assert_eq!(
            evpn.mac_lists[0].mac_addresses,
            ["00:11:22:33:44:55", "00:11:22:33:44:66"]
        );
        let routes = evpn.remote_ip_host_routes.unwrap();
        assert_eq!(routes.import, ["rih1", "rih2"]);
        assert!(routes.no_advertise_community);
        let trace = evpn.traceoptions.unwrap();
        assert_eq!(trace.file.as_deref(), Some("evpn.log"));
        assert_eq!(trace.flags[0].name, "all");
    }

    #[test]
    fn reads_the_interface_options() {
        let evpn = evpn_of(EXTRA);
        let interface = &evpn.interfaces[0];
        assert_eq!(interface.name, "ge-0/0/0.0");
        assert!(interface.ignore_encapsulation_mismatch);
        assert_eq!(interface.interface_mac_limit.as_ref().unwrap().limit, "100");
        assert!(interface.mac_pinning);
        assert!(interface.no_mac_learning);
        assert_eq!(interface.static_macs, ["00:aa:bb:cc:dd:ee"]);
    }

    #[test]
    fn reads_the_vni_options_forms() {
        let evpn = evpn_of(EXTRA);
        let first = &evpn.vni_options[0];
        assert_eq!(first.designated_forwarder_election_hold_time.as_deref(), Some("20"));
        let target = first.vrf_target.as_ref().unwrap();
        assert_eq!(target.community.as_deref(), Some("target:65000:1010"));
        assert!(target.auto);
        assert_eq!(target.import.as_deref(), Some("target:65000:1"));
        assert_eq!(target.export.as_deref(), Some("target:65000:2"));

        let auto_only = evpn.vni_options[1].vrf_target.as_ref().unwrap();
        assert!(auto_only.auto);
        assert_eq!(auto_only.community, None);
    }

    #[test]
    fn a_bare_evpn_block_has_nothing_set() {
        assert_eq!(evpn_of("protocols {\n    evpn;\n}\n"), Evpn::default());
    }

    #[test]
    fn no_evpn_is_none() {
        let config = juniper::parse_braces("protocols {\n    ospf {\n        area 0.0.0.0;\n    }\n}\n");
        assert_eq!(evpn(&config), None);
    }
}