asterust-junos 0.5.3

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `twamp`
//! `services { rpm { twamp { ... } } }`.
//!
//! The Two-Way Active Measurement Protocol client and server of Junos OS. Junos OS Evolved configures TWAMP at
//! `services monitoring twamp` instead, with different server and control connection statements; see
//! [`monitoring::twamp`](crate::services::monitoring::twamp).
//!
//! # 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(crate) use client::parse_source_routing;
pub use client::{ControlConnection, TestSession, TwampClient, TwampSourceRouting, TwampThresholds};
pub(crate) use server::parse_client_lists;
pub use server::{
    TwampClientAddress, TwampClientList, TwampKey, TwampKeyChain, TwampLightServer, 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"));
    }
}