asterust-junos 0.5.5

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 { monitoring { twamp { ... } } }`.
//!
//! The Two-Way Active Measurement Protocol client and server of Junos OS Evolved. Junos OS configures TWAMP at
//! `services rpm twamp` with different statements, see [`rpm::twamp`](crate::services::rpm::twamp); the types here
//! are separate because the same statement name can have different options or defaults. The client list and the
//! source routing have the same form in both and use the Junos OS types.
//!
//! # See also
//!
//! - [twamp (Junos OS Evolved)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/twamp-edit-services.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, TwampThresholds};
pub use server::{TwampLightServer, TwampManagedServer, TwampServer};

use crate::child_block;
use asterust::config::ConfigLine;
use client::parse_client;
use server::parse_server;

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

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),
    }
}

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

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

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

    // Not from documentation: the Configure TWAMP page only gives the minimum server syntax,
    // `server { (managed | light); }`. See the README "Sources".
    #[test]
    fn reads_the_minimum_server() {
        let managed = twamp("services {\n    monitoring {\n        twamp {\n            server {\n                managed;\n            }\n        }\n    }\n}\n");
        let server = managed.server.unwrap();
        assert_eq!(server.managed, Some(super::TwampManagedServer::default()));
        assert_eq!(server.light, None);

        let light = twamp("services {\n    monitoring {\n        twamp {\n            server {\n                light;\n            }\n        }\n    }\n}\n");
        assert_eq!(light.server.unwrap().light, Some(super::TwampLightServer::default()));
    }

    // Not from documentation: written from the Junos OS Evolved syntax of the `twamp` CLI Reference page.
    #[test]
    fn reads_the_statements_of_the_cli_reference() {
        let twamp = twamp(
            r#"
services {
    monitoring {
        twamp {
            client {
                control-connection c3 {
                    control-type managed;
                    destination-port 862;
                    routing-instance IN;
                    source-address 192.0.2.1;
                    target 192.0.2.2;
                    test-interval 10;
                    test-start auto;
                    traps {
                        test-iteration-done;
                    }
                    test-session t3 {
                        data-size 100;
                        destination-port 5000;
                        dscp-code-points af11;
                        history-size 100;
                        moving-average-size 10;
                        offload-type inline-timestamping;
                        probe-count 20;
                        probe-interval 2;
                        source-address 192.0.2.1;
                        target fe80::1 local-link ge-0/0/0;
                        ttl 64;
                        source-routing {
                            forward srmpls-nodes 16001 16002;
                        }
                        thresholds {
                            control-failure on;
                            successive-loss 3;
                            total-loss 4;
                            threshold-type average;
                        }
                        traps {
                            rtt-exceeded;
                            test-failure;
                        }
                    }
                }
            }
            server {
                managed {
                    client-limit 20;
                    client-list M {
                        address 198.51.100.0/24 routing-instance [ A B ];
                    }
                    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 {
                    offload-type inline-timestamp;
                    port 862;
                    stateful-sequence cache-size 1000;
                }
            }
        }
    }
}
"#,
        );
        let connection = &twamp.client.unwrap().control_connections[0];
        assert_eq!(connection.name, "c3");
        assert_eq!(connection.control_type.as_deref(), Some("managed"));
        assert_eq!(connection.destination_port.as_deref(), Some("862"));
        assert_eq!(connection.routing_instance.as_deref(), Some("IN"));
        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_interval.as_deref(), Some("10"));
        assert_eq!(connection.test_start.as_deref(), Some("auto"));
        assert_eq!(connection.traps, ["test-iteration-done"]);

        let session = &connection.test_sessions[0];
        assert_eq!(session.data_size.as_deref(), Some("100"));
        assert_eq!(session.destination_port.as_deref(), Some("5000"));
        assert_eq!(session.dscp_code_points.as_deref(), Some("af11"));
        assert_eq!(session.history_size.as_deref(), Some("100"));
        assert_eq!(session.moving_average_size.as_deref(), Some("10"));
        assert_eq!(session.offload_type.as_deref(), Some("inline-timestamping"));
        assert_eq!(session.probe_count.as_deref(), Some("20"));
        assert_eq!(session.probe_interval.as_deref(), Some("2"));
        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"));
        assert_eq!(session.ttl.as_deref(), Some("64"));
        assert_eq!(
            session.source_routing.as_ref().unwrap().forward.as_deref(),
            Some("srmpls-nodes 16001 16002")
        );
        assert_eq!(session.thresholds.control_failure.as_deref(), Some("on"));
        assert_eq!(session.thresholds.successive_loss.as_deref(), Some("3"));
        assert_eq!(session.thresholds.total_loss.as_deref(), Some("4"));
        assert_eq!(session.thresholds.threshold_type.as_deref(), Some("average"));
        assert_eq!(session.traps, ["rtt-exceeded", "test-failure"]);

        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].name, "M");
        assert_eq!(managed.client_lists[0].addresses[0].address, "198.51.100.0/24");
        assert_eq!(managed.client_lists[0].addresses[0].routing_instances, ["A", "B"]);
        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"));

        let light = server.light.unwrap();
        assert_eq!(light.offload_type.as_deref(), Some("inline-timestamp"));
        assert_eq!(light.port.as_deref(), Some("862"));
        assert_eq!(light.stateful_sequence_cache_size.as_deref(), Some("1000"));
    }

    #[test]
    fn junos_os_twamp_is_not_read_here() {
        let config = juniper::parse_braces(
            "services {\n    rpm {\n        twamp {\n            server {\n                port 862;\n            }\n        }\n    }\n}\n",
        );
        assert_eq!(services(&config).unwrap().monitoring, None);
    }
}