asterust-junos 0.4.5

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
//! `twamp`
//! `services { rpm { twamp { ... } } }`.
//!
//! The Two-Way Active Measurement Protocol client and server. Both the `twamp` syntax of Junos OS and the `managed` and
//! `light` server and the control connection variants of Junos OS Evolved are read; a value that has two spellings
//! (`target-address` and `target`) is read into the same field.
//!
//! # See also
//!
//! - [twamp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/twamp-edit-services.html)
//! - [Understanding Two-Way Active Measurement Protocol](https://networkingdocs.hpe.com/documentation/us/en/software/junos/flow-monitoring/topics/concept/twamp-overview.html)
//! - [Configure TWAMP](https://networkingdocs.hpe.com/documentation/us/en/software/junos/flow-monitoring/topics/task/rpm-twamp.html)

mod client;
mod server;

pub use client::{ControlConnection, TestSession, TwampClient, TwampSourceRouting, TwampThresholds};
pub use server::{
    TwampClientAddress, TwampClientList, TwampKey, TwampKeyChain, TwampLightServer, TwampManagedServer,
    TwampRoutingInstanceList, TwampServer,
};

use crate::{arg_of, child_block, has_leaf};
use asterust::config::ConfigLine;
use client::parse_client;
use server::parse_server;

/// The `services rpm twamp` configuration.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Twamp {
    pub client: Option<TwampClient>,
    pub server: Option<TwampServer>,
    pub post_cli_implicit_firewall: bool,
}

pub(super) fn parse_twamp(line: &ConfigLine) -> Twamp {
    Twamp {
        client: child_block(line, "client").map(parse_client),
        server: child_block(line, "server").map(parse_server),
        post_cli_implicit_firewall: has_leaf(line, "post-cli-implicit-firewall"),
    }
}

/// The argument of the first `<keyword> <value>` child, as an owned string.
fn text(line: &ConfigLine, keyword: &str) -> Option<String> {
    arg_of(line, keyword).map(str::to_string)
}

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

    fn twamp(text: &str) -> super::Twamp {
        services(&juniper::parse_braces(text))
            .and_then(|s| s.rpm)
            .and_then(|r| r.twamp)
            .expect("fixture has services rpm twamp")
    }

    // Adapted from the Flow Monitoring User Guide, Configuring TWAMP on SRX Series devices: the `show` output of the
    // client (R1) and the server (R2); see the README "Sources".
    const DOC_SRX_CLIENT: &str = r#"
services {
    rpm {
        twamp {
            client {
                control-connection c1 {
                    target-address 10.0.12.2;
                    test-session t1 {
                        target-address 10.0.12.2;
                        probe-count 2000;
                    }
                }
            }
        }
    }
}
"#;

    const DOC_SRX_SERVER: &str = r#"
services {
    rpm {
        twamp {
            server {
                authentication-mode none;
                client-list client1 {
                    address {
                        10.0.12.1/24;
                    }
                }
            }
        }
    }
}
"#;

    #[test]
    fn reads_the_guide_srx_client() {
        let twamp = twamp(DOC_SRX_CLIENT);
        assert_eq!(twamp.server, None);
        let connection = &twamp.client.unwrap().control_connections[0];
        assert_eq!(connection.name, "c1");
        assert_eq!(connection.target_address.as_deref(), Some("10.0.12.2"));
        let session = &connection.test_sessions[0];
        assert_eq!(session.name, "t1");
        assert_eq!(session.target_address.as_deref(), Some("10.0.12.2"));
        assert_eq!(session.probe_count.as_deref(), Some("2000"));
    }

    #[test]
    fn reads_the_guide_srx_server() {
        let twamp = twamp(DOC_SRX_SERVER);
        assert_eq!(twamp.client, None);
        let server = twamp.server.unwrap();
        assert_eq!(server.authentication_modes, ["none"]);
        assert_eq!(server.client_lists.len(), 1);
        assert_eq!(server.client_lists[0].name, "client1");
        assert_eq!(server.client_lists[0].addresses[0].address, "10.0.12.1/24");
        assert!(server.client_lists[0].addresses[0].routing_instances.is_empty());
    }

    // The `set` commands of "Configure TWAMP Server and TWAMP Client for Layer 3 Routing Devices" and the SRX and IS-IS
    // link delay examples, written in brace form.
    const DOC_L3: &str = r#"
services {
    rpm {
        twamp {
            client {
                control-connection c1 {
                    destination-interface si-4/1/0.10;
                    history-size 500;
                    persistent-results;
                    target-address 10.70.70.1;
                    tcp-keepcnt 10;
                    tcp-keepidle 20;
                    tcp-keepintvl 4;
                    test-count 1;
                    test-interval 1;
                    traps {
                        test-iteration-done;
                        control-connection-closed;
                    }
                    test-session t1 {
                        target-address 10.70.70.1;
                        data-fill-with-zeros;
                        data-size 1400;
                        probe-count 55;
                        probe-interval 1;
                        thresholds {
                            total-loss 10;
                        }
                    }
                }
            }
            server {
                authentication-mode none;
                client-list Client1 {
                    address {
                        10.60.60.1/32;
                        192.168.20.1/24;
                    }
                }
                max-connection-duration 0;
                maximum-connections 500;
                maximum-connections-per-client 500;
                maximum-sessions 100;
                maximum-sessions-per-connection 50;
                port 862;
                reflector-inactivity-timeout 15;
                server-inactivity-timeout 5;
                tcp-keepcnt 210;
                tcp-keepidle 200;
                tcp-keepintvl 20;
            }
        }
    }
}
"#;

    #[test]
    fn reads_the_guide_client_options() {
        let twamp = twamp(DOC_L3);
        let connection = &twamp.client.unwrap().control_connections[0];
        assert_eq!(connection.destination_interface.as_deref(), Some("si-4/1/0.10"));
        assert_eq!(connection.history_size.as_deref(), Some("500"));
        assert!(connection.persistent_results);
        assert_eq!(connection.tcp_keepcnt.as_deref(), Some("10"));
        assert_eq!(connection.tcp_keepidle.as_deref(), Some("20"));
        assert_eq!(connection.tcp_keepintvl.as_deref(), Some("4"));
        assert_eq!(connection.test_count.as_deref(), Some("1"));
        assert_eq!(connection.test_interval.as_deref(), Some("1"));
        assert_eq!(connection.traps, ["test-iteration-done", "control-connection-closed"]);

        let session = &connection.test_sessions[0];
        assert!(session.data_fill_with_zeros);
        assert_eq!(session.data_size.as_deref(), Some("1400"));
        assert_eq!(session.probe_interval.as_deref(), Some("1"));
        assert_eq!(session.thresholds.total_loss.as_deref(), Some("10"));
        assert_eq!(session.thresholds.rtt, None);
    }

    #[test]
    fn reads_the_guide_server_options() {
        let server = twamp(DOC_L3).server.unwrap();
        let addresses: Vec<_> = server.client_lists[0]
            .addresses
            .iter()
            .map(|a| a.address.as_str())
            .collect();
        assert_eq!(addresses, ["10.60.60.1/32", "192.168.20.1/24"]);
        assert_eq!(server.max_connection_duration.as_deref(), Some("0"));
        assert_eq!(server.maximum_connections.as_deref(), Some("500"));
        assert_eq!(server.maximum_connections_per_client.as_deref(), Some("500"));
        assert_eq!(server.maximum_sessions.as_deref(), Some("100"));
        assert_eq!(server.maximum_sessions_per_connection.as_deref(), Some("50"));
        assert_eq!(server.port.as_deref(), Some("862"));
        assert_eq!(server.reflector_inactivity_timeout.as_deref(), Some("15"));
        assert_eq!(server.server_inactivity_timeout.as_deref(), Some("5"));
        assert_eq!(server.tcp_keepcnt.as_deref(), Some("210"));
        assert_eq!(server.tcp_keepidle.as_deref(), Some("200"));
        assert_eq!(server.tcp_keepintvl.as_deref(), Some("20"));
        assert_eq!(server.light, None);
    }

    // The IS-IS link delay measurement example and the TWAMP light description of the guide.
    #[test]
    fn reads_the_guide_light_server() {
        let twamp = twamp("services {\n    rpm {\n        twamp {\n            server {\n                authentication-mode none;\n                light;\n                port 862;\n            }\n        }\n    }\n}\n");
        let server = twamp.server.unwrap();
        assert_eq!(server.authentication_modes, ["none"]);
        assert_eq!(server.light, Some(super::TwampLightServer::default()));
        assert_eq!(server.port.as_deref(), Some("862"));
    }

    // Not from documentation: written from the `twamp` CLI Reference page. See the README "Sources".
    #[test]
    fn reads_the_statements_of_the_cli_reference() {
        let twamp = twamp(
            r#"
services {
    rpm {
        twamp {
            post-cli-implicit-firewall;
            client {
                control-connection c2 {
                    authentication-mode none;
                    control-type light;
                    destination-port 862;
                    moving-average-size 20;
                    routing-instance IN;
                    test-session t2 {
                        data-fill-with zeros;
                        destination-port 5000;
                        dscp-code-points af11;
                        offload-type pfe-timestamp;
                        source-address 192.0.2.1;
                        target-address fe80::1 local-link ge-0/0/0.0;
                        ttl 64;
                        source-routing {
                            forward srmpls-nodes 16001 16002;
                            return srv6-nodes 2001:db8::1;
                        }
                        thresholds {
                            egress-time 10;
                            ingress-time 11;
                            jitter-egress 12;
                            jitter-ingress 13;
                            jitter-rtt 14;
                            max-rtt 15;
                            rtt 16;
                            std-dev-egress 17;
                            std-dev-ingress 18;
                            std-dev-rtt 19;
                            successive-loss 3;
                            total-loss 4;
                        }
                        traps {
                            rtt-exceeded;
                            probe-failure;
                        }
                    }
                }
            }
            server {
                authentication-key-chain KEYS {
                    key-id 1 {
                        secret "$9$secret";
                    }
                    key-id 2;
                }
                authentication-mode [ authenticated encrypted ];
                client-list C {
                    address 192.0.2.0/24 routing-instance [ A B ];
                }
                offload-type inline-timestamp;
                routing-instance-list VRF {
                    port 51000;
                }
                light {
                    port 878;
                    offload-type none;
                }
            }
        }
    }
}
"#,
        );
        assert!(twamp.post_cli_implicit_firewall);

        let connection = &twamp.client.unwrap().control_connections[0];
        assert_eq!(connection.authentication_mode.as_deref(), Some("none"));
        assert_eq!(connection.control_type.as_deref(), Some("light"));
        assert_eq!(connection.destination_port.as_deref(), Some("862"));
        assert_eq!(connection.moving_average_size.as_deref(), Some("20"));
        assert_eq!(connection.routing_instance.as_deref(), Some("IN"));

        let session = &connection.test_sessions[0];
        assert!(session.data_fill_with_zeros);
        assert_eq!(session.destination_port.as_deref(), Some("5000"));
        assert_eq!(session.dscp_code_points.as_deref(), Some("af11"));
        assert_eq!(session.offload_type.as_deref(), Some("pfe-timestamp"));
        assert_eq!(session.source_address.as_deref(), Some("192.0.2.1"));
        assert_eq!(session.target_address.as_deref(), Some("fe80::1"));
        assert_eq!(session.target_local_link.as_deref(), Some("ge-0/0/0.0"));
        assert_eq!(session.ttl.as_deref(), Some("64"));
        let routing = session.source_routing.as_ref().unwrap();
        assert_eq!(routing.forward.as_deref(), Some("srmpls-nodes 16001 16002"));
        assert_eq!(routing.return_path.as_deref(), Some("srv6-nodes 2001:db8::1"));
        let thresholds = &session.thresholds;
        assert_eq!(thresholds.egress_time.as_deref(), Some("10"));
        assert_eq!(thresholds.ingress_time.as_deref(), Some("11"));
        assert_eq!(thresholds.jitter_egress.as_deref(), Some("12"));
        assert_eq!(thresholds.jitter_ingress.as_deref(), Some("13"));
        assert_eq!(thresholds.jitter_rtt.as_deref(), Some("14"));
        assert_eq!(thresholds.max_rtt.as_deref(), Some("15"));
        assert_eq!(thresholds.rtt.as_deref(), Some("16"));
        assert_eq!(thresholds.std_dev_egress.as_deref(), Some("17"));
        assert_eq!(thresholds.std_dev_ingress.as_deref(), Some("18"));
        assert_eq!(thresholds.std_dev_rtt.as_deref(), Some("19"));
        assert_eq!(thresholds.successive_loss.as_deref(), Some("3"));
        assert_eq!(thresholds.total_loss.as_deref(), Some("4"));
        assert_eq!(session.traps, ["rtt-exceeded", "probe-failure"]);

        let server = twamp.server.unwrap();
        let chain = &server.authentication_key_chains[0];
        assert_eq!(chain.name, "KEYS");
        assert_eq!(chain.keys.len(), 2);
        assert!(chain.keys[0].has_secret);
        assert_eq!(
            chain.keys[0].secret.as_ref().map(Secret::expose),
            crate::secret::expected("$9$secret")
        );
        assert!(!chain.keys[1].has_secret);
        assert_eq!(server.authentication_modes, ["authenticated", "encrypted"]);
        assert_eq!(server.client_lists[0].addresses[0].address, "192.0.2.0/24");
        assert_eq!(server.client_lists[0].addresses[0].routing_instances, ["A", "B"]);
        assert_eq!(server.offload_type.as_deref(), Some("inline-timestamp"));
        assert_eq!(server.routing_instance_lists[0].name, "VRF");
        assert_eq!(server.routing_instance_lists[0].port.as_deref(), Some("51000"));
        let light = server.light.unwrap();
        assert_eq!(light.port.as_deref(), Some("878"));
        assert_eq!(light.offload_type.as_deref(), Some("none"));
    }

    // Not from documentation: the Junos OS Evolved variant on the `twamp` CLI Reference page.
    #[test]
    fn reads_the_evolved_variant() {
        let twamp = twamp(
            r#"
services {
    rpm {
        twamp {
            client {
                control-connection c3 {
                    control-type managed;
                    source-address 192.0.2.1;
                    target 192.0.2.2;
                    test-start auto;
                    test-session t3 {
                        history-size 100;
                        moving-average-size 10;
                        target 192.0.2.2;
                        thresholds {
                            control-failure on;
                            threshold-type average;
                        }
                    }
                }
            }
            server {
                managed {
                    client-limit 20;
                    client-list M {
                        address 198.51.100.0/24;
                    }
                    control-inactivity-timeout 30;
                    control-maximum-duration 3600;
                    control-per-client-limit 4;
                    offload-type pfe-timestamp;
                    port 862;
                    stateful-sequence;
                    test-per-client-limit 8;
                }
                light {
                    stateful-sequence cache-size 1000;
                }
            }
        }
    }
}
"#,
        );
        let connection = &twamp.client.unwrap().control_connections[0];
        assert_eq!(connection.control_type.as_deref(), Some("managed"));
        assert_eq!(connection.source_address.as_deref(), Some("192.0.2.1"));
        assert_eq!(connection.target_address.as_deref(), Some("192.0.2.2"));
        assert_eq!(connection.test_start.as_deref(), Some("auto"));
        let session = &connection.test_sessions[0];
        assert_eq!(session.history_size.as_deref(), Some("100"));
        assert_eq!(session.moving_average_size.as_deref(), Some("10"));
        assert_eq!(session.target_address.as_deref(), Some("192.0.2.2"));
        assert_eq!(session.thresholds.control_failure.as_deref(), Some("on"));
        assert_eq!(session.thresholds.threshold_type.as_deref(), Some("average"));

        let server = twamp.server.unwrap();
        let managed = server.managed.unwrap();
        assert_eq!(managed.client_limit.as_deref(), Some("20"));
        assert_eq!(managed.client_lists[0].addresses[0].address, "198.51.100.0/24");
        assert_eq!(managed.control_inactivity_timeout.as_deref(), Some("30"));
        assert_eq!(managed.control_maximum_duration.as_deref(), Some("3600"));
        assert_eq!(managed.control_per_client_limit.as_deref(), Some("4"));
        assert_eq!(managed.offload_type.as_deref(), Some("pfe-timestamp"));
        assert_eq!(managed.port.as_deref(), Some("862"));
        assert!(managed.stateful_sequence);
        assert_eq!(managed.test_per_client_limit.as_deref(), Some("8"));
        assert_eq!(
            server.light.unwrap().stateful_sequence_cache_size.as_deref(),
            Some("1000")
        );
    }
}