asterust-junos 0.4.2

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
//! `ldp`
//! `protocols { ldp { ... } }`.
//!
//! # See also
//!
//! - [ldp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ldp-edit-protocols.html)
//! - [keepalive-interval](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/keepalive-interval-edit-protocols-ldp.html)
//! - [keepalive-timeout](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/keepalive-timeout-edit-protocols-ldp.html)
//! - [preference (Protocols LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/preference-edit-protocols-ldp.html)
//! - [family (protocols ldp)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/family-edit-protocols-ldp.html)
//! - [dual-transport](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dual-transport-edit-protocols-ldp.html)
//! - [transport-address](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/transport-address-edit-protocols-ldp.html)
//! - [deaggregate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/deaggregate-edit-protocols-ldp.html)
//! - [explicit-null (Protocols LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/explicit-null-edit-protocols-ldp.html)
//! - [no-forwarding](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-forwarding-edit-protocols-ldp.html)
//! - [track-igp-metric](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/track-igp-metric-edit-protocols-ldp.html)
//! - [l2-smart-policy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/l2-smart-policy-edit-protocols-ldp.html)
//! - [strict-targeted-hellos](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/strict-targeted-hellos-edit-protocols-ldp.html)
//! - [longest-match](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/longest-match-edit-protocols-ldp.html)
//! - [export (Protocols LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/export-edit-protocols-ldp.html)
//! - [import (Protocols LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/import-edit-protocols-ldp.html)
//! - [egress-policy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/egress-policy-edit-protocols-ldp.html)
//! - [dod-request-policy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dod-request-policy-edit-protocols-ldp.html)
//! - [igp-synchronization](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/igp-synchronization-edit-protocols-ldp.html)
//! - [graceful-restart (Protocols LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/graceful-restart-edit-protocols-ldp.html)
//! - [session-protection](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/session-protection-edit-protocols-ldp.html)
//! - [targeted-hello](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/targeted-hello-edit-protocols-ldp.html)
//! - [auto-targeted-session](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/auto-targeted-session-edit-protocols-ldp.html)
//! - [make-before-break (LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/make-before-break-edit-protocols-ldp.html)
//! - [p2mp (Protocols LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/p2mp-edit-protocols-ldp.html)
//! - [label-withdrawal-delay](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/label-withdrawal-delay-edit-protocols-ldp.html)
//! - [log-updown (Protocols LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/log-updown-edit-protocols-ldp.html)
//! - [traceoptions (Protocols LDP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-protocols-ldp.html)
//! - [LDP Configuration](https://networkingdocs.hpe.com/documentation/us/en/software/junos/mpls/topics/topic-map/ldp-configuration.html), for `transport-preference`, `upstream-label-assignment` and the examples

mod interface;
mod session;

pub use interface::{LdpInterface, LdpLinkProtection};
pub use session::LdpSession;

use super::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{
    active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, statement_words, toggle,
    top_level, value_after,
};
use asterust::config::Config;
use interface::parse_interface;
use session::parse_session;

/// `graceful-restart { ... }`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpGracefulRestart {
    pub disable: bool,
    pub helper_disable: bool,
    /// Seconds.
    pub maximum_neighbor_recovery_time: Option<String>,
    /// Seconds.
    pub reconnect_time: Option<String>,
    /// Seconds.
    pub recovery_time: Option<String>,
}

/// `dual-transport { inet-lsr-id ...; inet6-lsr-id ...; }`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpDualTransport {
    pub inet_lsr_id: Option<String>,
    pub inet6_lsr_id: Option<String>,
}

/// `targeted-hello { ... }`, both in seconds.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpTargetedHello {
    pub hello_interval: Option<String>,
    pub hold_time: Option<String>,
}

/// `auto-targeted-session { ... }`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpAutoTargetedSession {
    pub maximum_sessions: Option<String>,
    /// Seconds.
    pub teardown_delay: Option<String>,
}

/// `make-before-break { ... }`, both in seconds.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpMakeBeforeBreak {
    pub timeout: Option<String>,
    pub switchover_delay: Option<String>,
}

/// The `protocols ldp` configuration. The `oam`, `policing`, `traffic-statistics`, `sr-mapping-client`, `neighbor`,
/// `rib-group`, `entropy-label` and `next-hop` statements are not extracted.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Ldp {
    pub disable: bool,
    pub interfaces: Vec<LdpInterface>,
    pub sessions: Vec<LdpSession>,
    pub session_groups: Vec<LdpSession>,
    /// Seconds.
    pub keepalive_interval: Option<String>,
    /// Seconds.
    pub keepalive_timeout: Option<String>,
    pub preference: Option<String>,
    /// The words of `family { ... }`, e.g. `inet` and `inet6`.
    pub families: Vec<String>,
    pub dual_transport: Option<LdpDualTransport>,
    /// An address, `interface` or `router-id`.
    pub transport_address: Option<String>,
    /// `ipv4` or `ipv6`.
    pub transport_preference: Option<String>,
    /// `Some(true)` for `deaggregate`, `Some(false)` for `no-deaggregate`.
    pub deaggregate: Option<bool>,
    /// `Some(true)` for `mtu-discovery`, `Some(false)` for `no-mtu-discovery`.
    pub mtu_discovery: Option<bool>,
    pub explicit_null: bool,
    pub no_forwarding: bool,
    pub track_igp_metric: bool,
    pub l2_smart_policy: bool,
    pub strict_targeted_hellos: bool,
    pub upstream_label_assignment: bool,
    pub p2mp: bool,
    pub p2mp_no_rsvp_tunneling: bool,
    /// The policies of `longest-match { policy ...; }`; empty for a bare `longest-match;`.
    pub longest_match: Option<Vec<String>>,
    pub export_policies: Vec<String>,
    pub import_policies: Vec<String>,
    pub egress_policies: Vec<String>,
    pub dod_request_policy: Option<String>,
    /// Seconds.
    pub igp_synchronization_holddown_interval: Option<String>,
    pub graceful_restart: Option<LdpGracefulRestart>,
    /// `Some(timeout in seconds)` inside, so a bare `session-protection;` is `Some(None)`.
    pub session_protection: Option<Option<String>>,
    pub targeted_hello: Option<LdpTargetedHello>,
    pub auto_targeted_session: Option<LdpAutoTargetedSession>,
    pub make_before_break: Option<LdpMakeBeforeBreak>,
    pub label_withdrawal_delay: Option<String>,
    /// `log-updown { trap disable; }`.
    pub log_updown_trap_disabled: bool,
    pub traceoptions: Option<Traceoptions>,
}

/// Returns the `protocols ldp` block, or `None` if LDP is not configured.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::protocols::ldp::ldp;
///
/// let config = juniper::parse_braces("protocols {\n    ldp {\n        interface all;\n        interface fxp0.0 {\n            disable;\n        }\n    }\n}\n");
/// let ldp = ldp(&config).unwrap();
/// assert_eq!(ldp.interfaces[0].name, "all");
/// assert!(ldp.interfaces[1].disable);
/// ```
pub fn ldp(config: &Config) -> Option<Ldp> {
    let ldp = child_block(top_level(config, "protocols")?, "ldp")?;
    let field = |keyword: &str| arg_of(ldp, keyword).map(str::to_string);
    let policies = |keyword: &str| -> Vec<String> { args_of(ldp, keyword).into_iter().flat_map(list_values).collect() };
    let p2mp = child_block(ldp, "p2mp");
    Some(Ldp {
        disable: has_leaf(ldp, "disable"),
        interfaces: named_children(ldp, "interface")
            .map(|(name, interface)| parse_interface(name, interface))
            .collect(),
        sessions: named_children(ldp, "session")
            .map(|(name, session)| parse_session(name, session))
            .collect(),
        session_groups: named_children(ldp, "session-group")
            .map(|(name, group)| parse_session(name, group))
            .collect(),
        keepalive_interval: field("keepalive-interval"),
        keepalive_timeout: field("keepalive-timeout"),
        preference: field("preference"),
        families: child_block(ldp, "family")
            .map(|family| active_children(family).map(|c| c.text.clone()).collect())
            .unwrap_or_default(),
        dual_transport: child_block(ldp, "dual-transport").map(|block| LdpDualTransport {
            inet_lsr_id: arg_of(block, "inet-lsr-id").map(str::to_string),
            inet6_lsr_id: arg_of(block, "inet6-lsr-id").map(str::to_string),
        }),
        transport_address: field("transport-address"),
        transport_preference: field("transport-preference"),
        deaggregate: toggle(ldp, "deaggregate"),
        mtu_discovery: toggle(ldp, "mtu-discovery"),
        explicit_null: has_leaf(ldp, "explicit-null") || arg_of(ldp, "explicit-null").is_some(),
        no_forwarding: has_leaf(ldp, "no-forwarding"),
        track_igp_metric: has_leaf(ldp, "track-igp-metric"),
        l2_smart_policy: has_leaf(ldp, "l2-smart-policy"),
        strict_targeted_hellos: has_leaf(ldp, "strict-targeted-hellos"),
        upstream_label_assignment: has_leaf(ldp, "upstream-label-assignment"),
        p2mp: p2mp.is_some(),
        p2mp_no_rsvp_tunneling: p2mp.is_some_and(|block| has_leaf(block, "no-rsvp-tunneling")),
        longest_match: child_block(ldp, "longest-match").map(|block| {
            let policy = child_block(block, "policy");
            policy
                .map(|p| active_children(p).flat_map(|c| list_values(&c.text)).collect())
                .unwrap_or_else(|| args_of(block, "policy").into_iter().flat_map(list_values).collect())
        }),
        export_policies: policies("export"),
        import_policies: policies("import"),
        egress_policies: policies("egress-policy"),
        dod_request_policy: field("dod-request-policy"),
        igp_synchronization_holddown_interval: statement_words(ldp, "igp-synchronization")
            .and_then(|words| value_after(&words, "holddown-interval")),
        graceful_restart: child_block(ldp, "graceful-restart").map(|block| LdpGracefulRestart {
            disable: has_leaf(block, "disable"),
            helper_disable: has_leaf(block, "helper-disable"),
            maximum_neighbor_recovery_time: arg_of(block, "maximum-neighbor-recovery-time").map(str::to_string),
            reconnect_time: arg_of(block, "reconnect-time").map(str::to_string),
            recovery_time: arg_of(block, "recovery-time").map(str::to_string),
        }),
        session_protection: statement_words(ldp, "session-protection").map(|words| value_after(&words, "timeout")),
        targeted_hello: child_block(ldp, "targeted-hello").map(|block| LdpTargetedHello {
            hello_interval: arg_of(block, "hello-interval").map(str::to_string),
            hold_time: arg_of(block, "hold-time").map(str::to_string),
        }),
        auto_targeted_session: child_block(ldp, "auto-targeted-session").map(|block| LdpAutoTargetedSession {
            maximum_sessions: arg_of(block, "maximum-sessions").map(str::to_string),
            teardown_delay: arg_of(block, "teardown-delay").map(str::to_string),
        }),
        make_before_break: child_block(ldp, "make-before-break").map(|block| LdpMakeBeforeBreak {
            timeout: arg_of(block, "timeout").map(str::to_string),
            switchover_delay: arg_of(block, "switchover-delay").map(str::to_string),
        }),
        label_withdrawal_delay: field("label-withdrawal-delay"),
        log_updown_trap_disabled: statement_words(ldp, "log-updown").is_some_and(|words| words.contains(&"disable")),
        traceoptions: parse_traceoptions(ldp),
    })
}

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

    // Blocks of the LDP Configuration page of the MPLS Applications User Guide (the link protection, native IPv6
    // support, downstream on demand, longest match and tracing examples); see the README "Sources".
    const DOC_LINK_PROTECTION: &str = r#"
protocols {
    ldp {
        interface all {
            link-protection {
                dynamic-rsvp-lsp;
            }
        }
        interface fxp0.0 {
            disable;
        }
        p2mp;
    }
}
"#;

    const DOC_IPV6: &str = r#"
protocols {
    ldp {
        deaggregate;
        interface ge-1/0/0.0;
        interface lo0.0;
        family {
            inet6;
            inet;
        }
        transport-preference ipv4;
    }
}
"#;

    const DOC_DUAL_TRANSPORT: &str = r#"
protocols {
    ldp {
        deaggregate;
        interface ge-1/0/0.0;
        interface lo0.0;
        family {
            inet6;
            inet;
        }
        dual-transport {
            inet-lsr-id 10.255.0.1;
            inet6-lsr-id 10.1.1.1;
        }
    }
}
"#;

    const DOC_DOWNSTREAM_ON_DEMAND: &str = r#"
protocols {
    ldp {
        dod-request-policy DOD-Request-Loopbacks;
        session 172.16.1.1 {
            downstream-on-demand;
        }
    }
}
"#;

    const DOC_LONGEST_MATCH: &str = r#"
protocols {
    ldp {
        longest-match;
        interface ge-0/0/2.0;
        interface lo0.0;
    }
}
"#;

    const DOC_TRACEOPTIONS: &str = r#"
protocols {
    ldp {
        traceoptions {
            file ldp size 10m files 5 world-readable;
            flag packets receive;
            flag binding;
        }
        interface all {
        }
    }
}
"#;

    const DOC_POLICIES: &str = r#"
protocols {
    ldp {
        egress-policy connected-only;
        export block-one;
        import only-32;
        graceful-restart {
            helper-disable;
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference pages of the statements. See the README "Sources".
    const EXTRA: &str = r#"
protocols {
    ldp {
        keepalive-interval 10;
        keepalive-timeout 30;
        preference 9;
        transport-address router-id;
        no-deaggregate;
        no-mtu-discovery;
        explicit-null;
        no-forwarding;
        track-igp-metric;
        l2-smart-policy;
        strict-targeted-hellos;
        upstream-label-assignment;
        label-withdrawal-delay 60;
        export [ a b ];
        import [ c d ];
        egress-policy [ e f ];
        longest-match {
            policy [ lm1 lm2 ];
        }
        igp-synchronization {
            holddown-interval 20;
        }
        graceful-restart {
            disable;
            maximum-neighbor-recovery-time 140;
            reconnect-time 60;
            recovery-time 100;
        }
        session-protection {
            timeout 120;
        }
        targeted-hello {
            hello-interval 5;
            hold-time 15;
        }
        auto-targeted-session {
            maximum-sessions 200;
            teardown-delay 30;
        }
        make-before-break {
            timeout 60;
            switchover-delay 5;
        }
        p2mp {
            no-rsvp-tunneling;
        }
        log-updown {
            trap disable;
        }
        interface ge-0/0/0.0 {
            hello-interval 3;
            hold-time 9;
            transport-address loopback;
            allow-subnet-mismatch;
            link-protection {
                disable;
                dynamic-rsvp-lsp;
            }
        }
        interface ge-0/0/1.0 {
            link-protection;
        }
        session 10.0.0.2 {
            authentication-algorithm hmac-sha-1-96;
            authentication-key "$9$secret"; ## SECRET-DATA
            authentication-key-chain chain1;
            no-mtu-discovery;
            transport-address 10.255.0.1;
        }
        session-group 10.1.0.0/16 {
            downstream-on-demand;
            mtu-discovery;
        }
    }
}
"#;

    fn ldp_of(text: &str) -> Ldp {
        ldp(&juniper::parse_braces(text)).unwrap()
    }

    #[test]
    fn reads_the_guide_link_protection() {
        let ldp = ldp_of(DOC_LINK_PROTECTION);
        assert_eq!(ldp.interfaces.len(), 2);
        assert_eq!(ldp.interfaces[0].name, "all");
        let protection = ldp.interfaces[0].link_protection.as_ref().unwrap();
        assert!(protection.dynamic_rsvp_lsp);
        assert!(!protection.disable);
        assert_eq!(ldp.interfaces[1].name, "fxp0.0");
        assert!(ldp.interfaces[1].disable);
        assert!(ldp.p2mp);
        assert!(!ldp.p2mp_no_rsvp_tunneling);
    }

    #[test]
    fn reads_the_guide_ipv6_transport() {
        let ldp = ldp_of(DOC_IPV6);
        assert_eq!(ldp.deaggregate, Some(true));
        assert_eq!(ldp.families, ["inet6", "inet"]);
        assert_eq!(ldp.transport_preference.as_deref(), Some("ipv4"));
        assert_eq!(ldp.dual_transport, None);

        let dual = ldp_of(DOC_DUAL_TRANSPORT).dual_transport.unwrap();
        assert_eq!(dual.inet_lsr_id.as_deref(), Some("10.255.0.1"));
        assert_eq!(dual.inet6_lsr_id.as_deref(), Some("10.1.1.1"));
    }

    #[test]
    fn reads_the_guide_downstream_on_demand_session() {
        let ldp = ldp_of(DOC_DOWNSTREAM_ON_DEMAND);
        assert_eq!(ldp.dod_request_policy.as_deref(), Some("DOD-Request-Loopbacks"));
        assert_eq!(ldp.sessions.len(), 1);
        assert_eq!(ldp.sessions[0].name, "172.16.1.1");
        assert!(ldp.sessions[0].downstream_on_demand);
        assert!(ldp.session_groups.is_empty());
    }

    #[test]
    fn reads_the_guide_longest_match_policies_and_tracing() {
        let ldp = ldp_of(DOC_LONGEST_MATCH);
        assert_eq!(ldp.longest_match, Some(Vec::new()));
        assert_eq!(ldp.interfaces.len(), 2);

        let ldp = ldp_of(DOC_POLICIES);
        assert_eq!(ldp.egress_policies, ["connected-only"]);
        assert_eq!(ldp.export_policies, ["block-one"]);
        assert_eq!(ldp.import_policies, ["only-32"]);
        let restart = ldp.graceful_restart.unwrap();
        assert!(restart.helper_disable);
        assert!(!restart.disable);

        let ldp = ldp_of(DOC_TRACEOPTIONS);
        let trace = ldp.traceoptions.unwrap();
        assert_eq!(trace.file.as_deref(), Some("ldp"));
        assert_eq!(trace.size.as_deref(), Some("10m"));
        assert_eq!(trace.files.as_deref(), Some("5"));
        assert_eq!(trace.world_readable, Some(true));
        assert_eq!(trace.flags[0].name, "packets");
        assert_eq!(trace.flags[0].modifiers, ["receive"]);
        assert_eq!(trace.flags[1].name, "binding");
        assert_eq!(ldp.interfaces[0].name, "all");
    }

    #[test]
    fn reads_the_global_options() {
        let ldp = ldp_of(EXTRA);
        assert!(!ldp.disable);
        assert_eq!(ldp.keepalive_interval.as_deref(), Some("10"));
        assert_eq!(ldp.keepalive_timeout.as_deref(), Some("30"));
        assert_eq!(ldp.preference.as_deref(), Some("9"));
        assert_eq!(ldp.transport_address.as_deref(), Some("router-id"));
        assert_eq!(ldp.deaggregate, Some(false));
        assert_eq!(ldp.mtu_discovery, Some(false));
        assert!(ldp.explicit_null);
        assert!(ldp.no_forwarding);
        assert!(ldp.track_igp_metric);
        assert!(ldp.l2_smart_policy);
        assert!(ldp.strict_targeted_hellos);
        assert!(ldp.upstream_label_assignment);
        assert_eq!(ldp.label_withdrawal_delay.as_deref(), Some("60"));
        assert!(ldp.log_updown_trap_disabled);
        assert!(ldp.p2mp);
        assert!(ldp.p2mp_no_rsvp_tunneling);
        assert_eq!(ldp.export_policies, ["a", "b"]);
        assert_eq!(ldp.import_policies, ["c", "d"]);
        assert_eq!(ldp.egress_policies, ["e", "f"]);
        assert_eq!(ldp.longest_match, Some(vec!["lm1".to_string(), "lm2".to_string()]));
        assert_eq!(ldp.igp_synchronization_holddown_interval.as_deref(), Some("20"));
    }

    #[test]
    fn reads_the_grouped_options() {
        let ldp = ldp_of(EXTRA);
        let restart = ldp.graceful_restart.unwrap();
        assert!(restart.disable);
        assert_eq!(restart.maximum_neighbor_recovery_time.as_deref(), Some("140"));
        assert_eq!(restart.reconnect_time.as_deref(), Some("60"));
        assert_eq!(restart.recovery_time.as_deref(), Some("100"));
        assert_eq!(ldp.session_protection, Some(Some("120".to_string())));
        let hello = ldp.targeted_hello.unwrap();
        assert_eq!(hello.hello_interval.as_deref(), Some("5"));
        assert_eq!(hello.hold_time.as_deref(), Some("15"));
        let auto = ldp.auto_targeted_session.unwrap();
        assert_eq!(auto.maximum_sessions.as_deref(), Some("200"));
        assert_eq!(auto.teardown_delay.as_deref(), Some("30"));
        let mbb = ldp.make_before_break.unwrap();
        assert_eq!(mbb.timeout.as_deref(), Some("60"));
        assert_eq!(mbb.switchover_delay.as_deref(), Some("5"));
    }

    #[test]
    fn reads_the_interface_options() {
        let ldp = ldp_of(EXTRA);
        let interface = &ldp.interfaces[0];
        assert_eq!(interface.hello_interval.as_deref(), Some("3"));
        assert_eq!(interface.hold_time.as_deref(), Some("9"));
        assert_eq!(interface.transport_address.as_deref(), Some("loopback"));
        assert!(interface.allow_subnet_mismatch);
        let protection = interface.link_protection.as_ref().unwrap();
        assert!(protection.disable);
        assert!(protection.dynamic_rsvp_lsp);
        assert_eq!(ldp.interfaces[1].link_protection, Some(LdpLinkProtection::default()));
    }

    #[test]
    fn reads_sessions_without_the_key() {
        let ldp = ldp_of(EXTRA);
        let session = &ldp.sessions[0];
        assert_eq!(session.name, "10.0.0.2");
        assert_eq!(session.authentication_algorithm.as_deref(), Some("hmac-sha-1-96"));
        assert!(session.has_authentication_key);
        assert_eq!(
            session.authentication_key.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
        assert_eq!(session.authentication_key_chain.as_deref(), Some("chain1"));
        assert_eq!(session.mtu_discovery, Some(false));
        assert_eq!(session.transport_address.as_deref(), Some("10.255.0.1"));
        assert!(!session.downstream_on_demand);

        let group = &ldp.session_groups[0];
        assert_eq!(group.name, "10.1.0.0/16");
        assert!(group.downstream_on_demand);
        assert_eq!(group.mtu_discovery, Some(true));
        assert!(!group.has_authentication_key);
    }

    #[test]
    fn bare_statements_and_absent_values() {
        let ldp = ldp_of("protocols {\n    ldp {\n        session-protection;\n        disable;\n    }\n}\n");
        assert!(ldp.disable);
        assert_eq!(ldp.session_protection, Some(None));
        assert_eq!(ldp.graceful_restart, None);
        assert_eq!(ldp.longest_match, None);
        assert!(!ldp.p2mp);
        assert!(!ldp.log_updown_trap_disabled);
    }

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