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
//! `congestion-notification-profile`, `drop-congestion-notification` and `mpls-ecn-map`
//! `class-of-service { congestion-notification-profile <name> { ... } ... }`.
//!
//! # See also
//!
//! - [congestion-notification-profile](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/congestion-notification-profile-edit-class-of-service.html)
//! - [mru (congestion notification profiles)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mru-edit-class-of-service-congestion-notification-qfx-series.html)
//! - [Configure CoS PFC with Congestion Notification Profiles](https://networkingdocs.hpe.com/documentation/us/en/software/junos/traffic-mgmt-qfx/topics/task/cos-congestion-notification-qfx-series-cli.html)
//! - [drop-congestion-notification](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/drop-congestion-notification-edit-class-of-service.html)
//! - [mpls-ecn-map](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mpls-ecn-map-edit-class-of-service.html)

use super::words;
use crate::{active_children, arg_of, child_block, list_values, named_children, value_after};
use asterust::config::ConfigLine;

/// A `code-point` entry of the `input` block of a congestion notification profile.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NotificationInput {
    /// `dscp` or `ieee-802.1`.
    pub kind: String,
    pub code_points: Vec<String>,
    /// `pfc` is configured for the code points.
    pub pfc: bool,
    /// The `mru` of `pfc`.
    pub pfc_mru: Option<String>,
    pub xon: Option<String>,
}

/// A `code-point` entry of the `output` block of a congestion notification profile.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NotificationOutput {
    pub kind: String,
    pub code_points: Vec<String>,
    pub flow_control_queues: Vec<String>,
}

/// The `pfc-watchdog` block.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PfcWatchdog {
    pub detection: Option<String>,
    pub poll_interval: Option<String>,
    pub recovery: Option<String>,
    /// `watchdog-action drop`.
    pub drop: bool,
}

/// A `congestion-notification-profile` entry.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CongestionNotificationProfile {
    pub name: String,
    pub input: Vec<NotificationInput>,
    pub cable_length: Option<String>,
    pub output: Vec<NotificationOutput>,
    pub pfc_watchdog: Option<PfcWatchdog>,
}

/// The `drop-congestion-notification` block.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct DropCongestionNotification {
    pub trimmable_dscp: Option<String>,
    pub trimmed_dscp: Option<String>,
    pub forwarding_class: Option<String>,
    pub udp_port: Option<String>,
}

/// An `ecn-capable-exp` entry of `mpls-ecn-map`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsEcnEntry {
    pub ecn_capable_exp: String,
    pub congestion_experienced_exp: Option<String>,
}

fn code_points(entry: &ConfigLine) -> Vec<String> {
    entry.arg("code-point").map(list_values).unwrap_or_default()
}

fn parse_input(block: &ConfigLine) -> Vec<NotificationInput> {
    active_children(block)
        .filter(|kind| kind.text == "dscp" || kind.text == "ieee-802.1")
        .flat_map(|kind| {
            active_children(kind)
                .filter(|entry| entry.arg("code-point").is_some())
                .map(|entry| NotificationInput {
                    kind: kind.text.clone(),
                    code_points: code_points(entry),
                    pfc: child_block(entry, "pfc").is_some(),
                    pfc_mru: child_block(entry, "pfc")
                        .and_then(|pfc| arg_of(pfc, "mru"))
                        .map(str::to_string),
                    xon: arg_of(entry, "xon").map(str::to_string),
                })
        })
        .collect()
}

fn parse_output(block: &ConfigLine) -> Vec<NotificationOutput> {
    active_children(block)
        .filter(|kind| kind.text == "dscp" || kind.text == "ieee-802.1")
        .flat_map(|kind| {
            active_children(kind)
                .filter(|entry| entry.arg("code-point").is_some())
                .map(|entry| NotificationOutput {
                    kind: kind.text.clone(),
                    code_points: code_points(entry),
                    flow_control_queues: arg_of(entry, "flow-control-queue").map(list_values).unwrap_or_default(),
                })
        })
        .collect()
}

pub(super) fn parse_congestion_notification_profiles(cos: &ConfigLine) -> Vec<CongestionNotificationProfile> {
    named_children(cos, "congestion-notification-profile")
        .map(|(name, profile)| CongestionNotificationProfile {
            name: name.to_string(),
            input: child_block(profile, "input").map(parse_input).unwrap_or_default(),
            cable_length: child_block(profile, "input")
                .and_then(|input| arg_of(input, "cable-length"))
                .map(str::to_string),
            output: child_block(profile, "output").map(parse_output).unwrap_or_default(),
            pfc_watchdog: child_block(profile, "pfc-watchdog").map(|watchdog| PfcWatchdog {
                detection: arg_of(watchdog, "detection").map(str::to_string),
                poll_interval: arg_of(watchdog, "poll-interval").map(str::to_string),
                recovery: arg_of(watchdog, "recovery").map(str::to_string),
                drop: child_block(watchdog, "watchdog-action").is_some_and(|action| crate::has_leaf(action, "drop")),
            }),
        })
        .collect()
}

pub(super) fn parse_drop_congestion_notification(cos: &ConfigLine) -> Option<DropCongestionNotification> {
    let drop = child_block(cos, "drop-congestion-notification")?;
    let map = crate::statement_words(drop, "dscp-map");
    Some(DropCongestionNotification {
        trimmable_dscp: map.as_ref().and_then(|words| value_after(words, "trimmable")),
        trimmed_dscp: map.as_ref().and_then(|words| value_after(words, "trimmed")),
        forwarding_class: arg_of(drop, "forwarding-class").map(str::to_string),
        udp_port: arg_of(drop, "udp-port").map(str::to_string),
    })
}

pub(super) fn parse_mpls_ecn_map(cos: &ConfigLine) -> Vec<MplsEcnEntry> {
    let Some(map) = child_block(cos, "mpls-ecn-map") else {
        return Vec::new();
    };
    named_children(map, "ecn-capable-exp")
        .map(|(exp, entry)| MplsEcnEntry {
            ecn_capable_exp: exp.to_string(),
            congestion_experienced_exp: words(entry, 0)
                .windows(2)
                // The CLI Reference writes `congestion-experienced-exp-value`; the guide writes `congestion-experienced-exp`.
                .find(|pair| pair[0] == "congestion-experienced-exp" || pair[0] == "congestion-experienced-exp-value")
                .map(|pair| pair[1].to_string()),
        })
        .collect()
}

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

    // Not from documentation: written from the CLI Reference. See the README "Sources".
    const EXTRA: &str = r#"
class-of-service {
    congestion-notification-profile CNP1 {
        input {
            ieee-802.1 {
                code-point [ 011 100 ] {
                    pfc {
                        mru 2500;
                    }
                    xon 150;
                }
            }
            dscp {
                code-point 110000 {
                    xon 100;
                }
            }
            cable-length 100;
        }
        output {
            ieee-802.1 {
                code-point 011 {
                    flow-control-queue [ 3 4 ];
                }
            }
        }
        pfc-watchdog {
            detection 100;
            poll-interval 10;
            recovery 200;
            watchdog-action {
                drop;
            }
        }
    }
    congestion-notification-profile CNP2;
    drop-congestion-notification {
        dscp-map {
            trimmable 8 trimmed 9;
        }
        forwarding-class trimmed-fc;
        udp-port 4791;
    }
    mpls-ecn-map {
        ecn-capable-exp 2 {
            congestion-experienced-exp-value 3;
        }
    }
}
"#;

    #[test]
    fn reads_the_congestion_notification_input() {
        let profiles = class_of_service(&juniper::parse_braces(EXTRA))
            .unwrap()
            .congestion_notification_profiles;
        assert_eq!(profiles.len(), 2);
        let profile = &profiles[0];
        assert_eq!(profile.name, "CNP1");
        assert_eq!(profile.cable_length.as_deref(), Some("100"));
        assert_eq!(profile.input.len(), 2);
        assert_eq!(profile.input[0].kind, "ieee-802.1");
        assert_eq!(profile.input[0].code_points, ["011", "100"]);
        assert_eq!(profile.input[0].pfc_mru.as_deref(), Some("2500"));
        assert_eq!(profile.input[0].xon.as_deref(), Some("150"));
        assert_eq!(profile.input[1].kind, "dscp");
        assert_eq!(profile.input[1].pfc_mru, None);
        assert!(profiles[1].input.is_empty() && profiles[1].pfc_watchdog.is_none());
    }

    #[test]
    fn reads_the_output_and_the_watchdog() {
        let profile = class_of_service(&juniper::parse_braces(EXTRA))
            .unwrap()
            .congestion_notification_profiles
            .remove(0);
        assert_eq!(profile.output[0].code_points, ["011"]);
        assert_eq!(profile.output[0].flow_control_queues, ["3", "4"]);
        let watchdog = profile.pfc_watchdog.unwrap();
        assert_eq!(watchdog.detection.as_deref(), Some("100"));
        assert_eq!(watchdog.poll_interval.as_deref(), Some("10"));
        assert_eq!(watchdog.recovery.as_deref(), Some("200"));
        assert!(watchdog.drop);
    }

    #[test]
    fn reads_the_drop_notification_and_the_ecn_map() {
        let cos = class_of_service(&juniper::parse_braces(EXTRA)).unwrap();
        let drop = cos.drop_congestion_notification.unwrap();
        assert_eq!(drop.trimmable_dscp.as_deref(), Some("8"));
        assert_eq!(drop.trimmed_dscp.as_deref(), Some("9"));
        assert_eq!(drop.forwarding_class.as_deref(), Some("trimmed-fc"));
        assert_eq!(drop.udp_port.as_deref(), Some("4791"));
        assert_eq!(cos.mpls_ecn_map.len(), 1);
        assert_eq!(cos.mpls_ecn_map[0].ecn_capable_exp, "2");
        assert_eq!(cos.mpls_ecn_map[0].congestion_experienced_exp.as_deref(), Some("3"));
    }

    // The `class-of-service` statements of Example: Configuring CoS PFC for FCoE Traffic, Data Center Quantized
    // Congestion Notification (DCQCN), ECN Support on MPLS Networks and Drop Congestion Notification (DCN) (Traffic
    // Management User Guide for QFX Series Switches and EX4600 Switches), written in brace form; see the README
    // "Sources".
    const DOC_FCOE_PFC: &str = r#"
class-of-service {
    classifiers {
        ieee-802.1 fcoe-classifier {
            forwarding-class fcoe {
                loss-priority low code-points 011;
            }
        }
    }
    congestion-notification-profile fcoe-cnp {
        input {
            ieee-802.1 {
                code-point 011 {
                    pfc {
                        mru 2240;
                    }
                }
            }
            cable-length 100;
        }
    }
    interfaces {
        xe-0/0/31 {
            congestion-notification-profile fcoe-cnp;
            unit 0 {
                classifiers {
                    ieee-802.1 fcoe-classifier;
                }
            }
        }
    }
}
"#;

    const DOC_DCQCN: &str = r#"
class-of-service {
    congestion-notification-profile cnp {
        input {
            dscp {
                code-point 011010 {
                    pfc;
                }
            }
        }
        output {
            ieee-802.1 {
                code-point 011 {
                    flow-control-queue 4;
                }
            }
        }
    }
    interfaces {
        et-* {
            congestion-notification-profile cnp;
        }
    }
}
"#;

    const DOC_MPLS_ECN: &str = r#"
class-of-service {
    mpls-ecn-map {
        ecn-capable-exp 6 {
            congestion-experienced-exp 7;
        }
        ecn-capable-exp 5 {
            congestion-experienced-exp 4;
        }
    }
}
"#;

    const DOC_DCN: &str = r#"
class-of-service {
    drop-congestion-notification {
        forwarding-class dcn;
        udp-port 13742;
    }
    interfaces {
        et-0/0/0 {
            drop-congestion-notification;
        }
    }
}
"#;

    #[test]
    fn reads_the_guide_fcoe_profile() {
        let cos = class_of_service(&juniper::parse_braces(DOC_FCOE_PFC)).unwrap();
        let profile = &cos.congestion_notification_profiles[0];
        assert_eq!(profile.name, "fcoe-cnp");
        assert_eq!(profile.cable_length.as_deref(), Some("100"));
        let input = &profile.input[0];
        assert_eq!(input.kind, "ieee-802.1");
        assert_eq!(input.code_points, ["011"]);
        assert!(input.pfc);
        assert_eq!(input.pfc_mru.as_deref(), Some("2240"));
        let interface = &cos.interfaces[0];
        assert_eq!(
            interface.settings.congestion_notification_profile.as_deref(),
            Some("fcoe-cnp")
        );
        assert_eq!(interface.units[0].settings.classifiers[0].name, "fcoe-classifier");
    }

    #[test]
    fn reads_the_guide_dcqcn_profile() {
        let cos = class_of_service(&juniper::parse_braces(DOC_DCQCN)).unwrap();
        let profile = &cos.congestion_notification_profiles[0];
        assert_eq!(profile.input[0].kind, "dscp");
        assert_eq!(profile.input[0].code_points, ["011010"]);
        assert!(profile.input[0].pfc && profile.input[0].pfc_mru.is_none());
        assert_eq!(profile.output[0].code_points, ["011"]);
        assert_eq!(profile.output[0].flow_control_queues, ["4"]);
        assert_eq!(cos.interfaces[0].name, "et-*");
    }

    #[test]
    fn reads_the_guide_mpls_ecn_map() {
        let map = class_of_service(&juniper::parse_braces(DOC_MPLS_ECN))
            .unwrap()
            .mpls_ecn_map;
        assert_eq!(map.len(), 2);
        assert_eq!(map[0].ecn_capable_exp, "6");
        assert_eq!(map[0].congestion_experienced_exp.as_deref(), Some("7"));
        assert_eq!(map[1].congestion_experienced_exp.as_deref(), Some("4"));
    }

    #[test]
    fn reads_the_guide_drop_congestion_notification() {
        let cos = class_of_service(&juniper::parse_braces(DOC_DCN)).unwrap();
        let drop = cos.drop_congestion_notification.unwrap();
        assert_eq!(drop.forwarding_class.as_deref(), Some("dcn"));
        assert_eq!(drop.udp_port.as_deref(), Some("13742"));
        assert_eq!(drop.trimmable_dscp, None);
        assert!(cos.interfaces[0].settings.drop_congestion_notification);
    }

    // The output `flow-control-queue` statements of Configure CoS PFC with Congestion Notification Profiles (Traffic
    // Management User Guide for QFX Series Switches and EX4600 Switches), written in brace form; see the README
    // "Sources".
    const DOC_OUTPUT_QUEUES: &str = r#"
class-of-service {
    congestion-notification-profile fcoe-cnp {
        input {
            ieee-802.1 {
                code-point 011 {
                    pfc {
                        mru 2240;
                    }
                }
            }
            cable-length 100;
        }
        output {
            ieee-802.1 {
                code-point 011 {
                    flow-control-queue [3 5];
                }
                code-point 100 {
                    flow-control-queue 4;
                }
            }
        }
    }
    interfaces {
        xe-0/0/7 {
            congestion-notification-profile fcoe-cnp;
        }
    }
}
"#;

    #[test]
    fn reads_the_guide_output_flow_control_queues() {
        let cos = class_of_service(&juniper::parse_braces(DOC_OUTPUT_QUEUES)).unwrap();
        let profile = &cos.congestion_notification_profiles[0];
        assert_eq!(profile.output.len(), 2);
        assert_eq!(profile.output[0].kind, "ieee-802.1");
        assert_eq!(profile.output[0].code_points, ["011"]);
        assert_eq!(profile.output[0].flow_control_queues, ["3", "5"]);
        assert_eq!(profile.output[1].code_points, ["100"]);
        assert_eq!(profile.output[1].flow_control_queues, ["4"]);
        assert_eq!(profile.input[0].pfc_mru.as_deref(), Some("2240"));
        assert_eq!(profile.cable_length.as_deref(), Some("100"));
        assert_eq!(
            cos.interfaces[0].settings.congestion_notification_profile.as_deref(),
            Some("fcoe-cnp")
        );
    }
}