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
//! `client`
//! `services { monitoring { twamp { client { control-connection <name> { ... } } } } }`.

use super::text;
use crate::services::rpm::twamp::{parse_source_routing, TwampSourceRouting};
use crate::{arg_of, child_block, named_children, split_keyword, statements};
use asterust::config::ConfigLine;

/// The `twamp client` of Junos OS Evolved.
#[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,
    /// `"managed"` or `"light"`.
    pub control_type: Option<String>,
    pub destination_port: Option<String>,
    pub routing_instance: Option<String>,
    pub source_address: Option<String>,
    /// The `target` address of the server.
    pub target_address: Option<String>,
    pub test_interval: Option<String>,
    /// `"auto"` or `"manual"`.
    pub test_start: 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,
    pub data_size: Option<String>,
    pub destination_port: Option<String>,
    pub dscp_code_points: Option<String>,
    pub history_size: Option<String>,
    pub moving_average_size: Option<String>,
    /// `"inline-timestamping"`, `"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>,
    /// The `target` address of the server.
    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 `thresholds` of a test session.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampThresholds {
    /// `"on"` or `"off"`.
    pub control_failure: Option<String>,
    pub successive_loss: Option<String>,
    pub total_loss: Option<String>,
    /// `"microseconds"` or `"average"`.
    pub threshold_type: 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(),
        control_type: text(line, "control-type"),
        destination_port: text(line, "destination-port"),
        routing_instance: text(line, "routing-instance"),
        source_address: text(line, "source-address"),
        target_address: text(line, "target"),
        test_interval: text(line, "test-interval"),
        test_start: text(line, "test-start"),
        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_address, target_rest) = arg_of(line, "target").map_or((None, ""), |t| {
        let (address, rest) = split_keyword(t);
        (Some(address.to_string()), rest)
    });
    TestSession {
        name: name.to_string(),
        data_size: text(line, "data-size"),
        destination_port: text(line, "destination-port"),
        dscp_code_points: text(line, "dscp-code-points"),
        history_size: text(line, "history-size"),
        moving_average_size: text(line, "moving-average-size"),
        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"),
    }
}

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 {
            "control-failure" => &mut thresholds.control_failure,
            "successive-loss" => &mut thresholds.successive_loss,
            "total-loss" => &mut thresholds.total_loss,
            "threshold-type" => &mut thresholds.threshold_type,
            _ => continue,
        };
        *slot = Some(value.to_string()).filter(|v| !v.is_empty());
    }
    thresholds
}