asterust-junos 0.5.1

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `client`
//! `services { rpm { twamp { client { control-connection <name> { ... } } } } }`.

use super::text;
use crate::{arg_of, child_block, has_leaf, named_children, split_keyword, statements};
use asterust::config::ConfigLine;

/// The `twamp client`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampClient {
    pub control_connections: Vec<ControlConnection>,
}

/// A `control-connection <name>`, which holds the test sessions run against one server.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ControlConnection {
    pub name: String,
    /// The `authentication-mode`, e.g. `"none"`.
    pub authentication_mode: Option<String>,
    /// `"managed"` or `"light"`.
    pub control_type: Option<String>,
    pub destination_interface: Option<String>,
    pub destination_port: Option<String>,
    pub history_size: Option<String>,
    pub moving_average_size: Option<String>,
    pub persistent_results: bool,
    pub routing_instance: Option<String>,
    pub source_address: Option<String>,
    pub target_address: Option<String>,
    pub tcp_keepcnt: Option<String>,
    pub tcp_keepidle: Option<String>,
    pub tcp_keepintvl: Option<String>,
    pub test_count: Option<String>,
    pub test_interval: Option<String>,
    /// The `traps`, e.g. `"test-iteration-done"`.
    pub traps: Vec<String>,
    pub test_sessions: Vec<TestSession>,
}

/// A `test-session <name>`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TestSession {
    pub name: String,
    /// `data-fill-with zeros`, or the `data-fill-with-zeros` that the guide writes.
    pub data_fill_with_zeros: bool,
    pub data_size: Option<String>,
    pub destination_port: Option<String>,
    pub dscp_code_points: Option<String>,
    /// `"inline-timestamp"`, `"none"` or `"pfe-timestamp"`.
    pub offload_type: Option<String>,
    pub probe_count: Option<String>,
    pub probe_interval: Option<String>,
    pub source_address: Option<String>,
    pub source_routing: Option<TwampSourceRouting>,
    pub target_address: Option<String>,
    /// The interface after `local-link` of an IPv6 link-local target.
    pub target_local_link: Option<String>,
    pub thresholds: TwampThresholds,
    /// The `traps`, e.g. `"rtt-exceeded"`.
    pub traps: Vec<String>,
    pub ttl: Option<String>,
}

/// The `source-routing` of a test session; each is everything after `forward` or `return`, e.g.
/// `"srmpls-nodes 16001 16002"`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampSourceRouting {
    pub forward: Option<String>,
    pub return_path: Option<String>,
}

/// The `thresholds` of a test session. Values are microseconds unless the name says otherwise.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampThresholds {
    pub egress_time: Option<String>,
    pub ingress_time: Option<String>,
    pub jitter_egress: Option<String>,
    pub jitter_ingress: Option<String>,
    pub jitter_rtt: Option<String>,
    pub max_rtt: Option<String>,
    pub rtt: Option<String>,
    pub std_dev_egress: Option<String>,
    pub std_dev_ingress: Option<String>,
    pub std_dev_rtt: Option<String>,
    pub successive_loss: Option<String>,
    pub total_loss: Option<String>,
}

pub(super) fn parse_client(line: &ConfigLine) -> TwampClient {
    TwampClient {
        control_connections: named_children(line, "control-connection")
            .map(|(name, connection)| parse_control_connection(name, connection))
            .collect(),
    }
}

fn parse_control_connection(name: &str, line: &ConfigLine) -> ControlConnection {
    ControlConnection {
        name: name.to_string(),
        authentication_mode: text(line, "authentication-mode"),
        control_type: text(line, "control-type"),
        destination_interface: text(line, "destination-interface"),
        destination_port: text(line, "destination-port"),
        history_size: text(line, "history-size"),
        moving_average_size: text(line, "moving-average-size"),
        persistent_results: has_leaf(line, "persistent-results"),
        routing_instance: text(line, "routing-instance"),
        source_address: text(line, "source-address"),
        target_address: text(line, "target-address"),
        tcp_keepcnt: text(line, "tcp-keepcnt"),
        tcp_keepidle: text(line, "tcp-keepidle"),
        tcp_keepintvl: text(line, "tcp-keepintvl"),
        test_count: text(line, "test-count"),
        test_interval: text(line, "test-interval"),
        traps: traps(line),
        test_sessions: named_children(line, "test-session")
            .map(|(name, session)| parse_test_session(name, session))
            .collect(),
    }
}

fn traps(line: &ConfigLine) -> Vec<String> {
    statements(line, "traps").into_iter().map(str::to_string).collect()
}

fn parse_test_session(name: &str, line: &ConfigLine) -> TestSession {
    let target = arg_of(line, "target-address");
    let (target_address, target_rest) = target.map_or((None, ""), |t| {
        let (address, rest) = split_keyword(t);
        (Some(address.to_string()), rest)
    });
    TestSession {
        name: name.to_string(),
        data_fill_with_zeros: has_leaf(line, "data-fill-with-zeros") || arg_of(line, "data-fill-with") == Some("zeros"),
        data_size: text(line, "data-size"),
        destination_port: text(line, "destination-port"),
        dscp_code_points: text(line, "dscp-code-points"),
        offload_type: text(line, "offload-type"),
        probe_count: text(line, "probe-count"),
        probe_interval: text(line, "probe-interval"),
        source_address: text(line, "source-address"),
        source_routing: child_block(line, "source-routing").map(parse_source_routing),
        target_address,
        target_local_link: target_rest
            .strip_prefix("local-link")
            .map(|link| link.trim().to_string())
            .filter(|link| !link.is_empty()),
        thresholds: parse_thresholds(line),
        traps: traps(line),
        ttl: text(line, "ttl"),
    }
}

pub(crate) fn parse_source_routing(line: &ConfigLine) -> TwampSourceRouting {
    TwampSourceRouting {
        forward: text(line, "forward"),
        return_path: text(line, "return"),
    }
}

fn parse_thresholds(line: &ConfigLine) -> TwampThresholds {
    let mut thresholds = TwampThresholds::default();
    for statement in statements(line, "thresholds") {
        let (name, value) = split_keyword(statement);
        let slot = match name {
            "egress-time" => &mut thresholds.egress_time,
            "ingress-time" => &mut thresholds.ingress_time,
            "jitter-egress" => &mut thresholds.jitter_egress,
            "jitter-ingress" => &mut thresholds.jitter_ingress,
            "jitter-rtt" => &mut thresholds.jitter_rtt,
            "max-rtt" => &mut thresholds.max_rtt,
            "rtt" => &mut thresholds.rtt,
            "std-dev-egress" => &mut thresholds.std_dev_egress,
            "std-dev-ingress" => &mut thresholds.std_dev_ingress,
            "std-dev-rtt" => &mut thresholds.std_dev_rtt,
            "successive-loss" => &mut thresholds.successive_loss,
            "total-loss" => &mut thresholds.total_loss,
            _ => continue,
        };
        *slot = Some(value.to_string()).filter(|v| !v.is_empty());
    }
    thresholds
}