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
//! `server`
//! `services { rpm { twamp { server { ... } } } }`.

use super::text;
use crate::{
    active_children, arg_of, args_of, child_block, list_values, named_children, secret_value, split_keyword, Secret,
};
use asterust::config::ConfigLine;

/// The `twamp server`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampServer {
    pub authentication_key_chains: Vec<TwampKeyChain>,
    /// The `authentication-mode` values: `"authenticated"`, `"control-only-encrypted"`, `"encrypted"` and `"none"`.
    pub authentication_modes: Vec<String>,
    pub client_lists: Vec<TwampClientList>,
    /// The `light` server; `light;` on its own is a server with no options.
    pub light: Option<TwampLightServer>,
    pub max_connection_duration: Option<String>,
    pub maximum_connections: Option<String>,
    pub maximum_connections_per_client: Option<String>,
    pub maximum_sessions: Option<String>,
    pub maximum_sessions_per_connection: Option<String>,
    /// `"inline-timestamp"`, `"none"` or `"pfe-timestamp"`.
    pub offload_type: Option<String>,
    pub port: Option<String>,
    pub reflector_inactivity_timeout: Option<String>,
    pub routing_instance_lists: Vec<TwampRoutingInstanceList>,
    pub server_inactivity_timeout: Option<String>,
    pub tcp_keepcnt: Option<String>,
    pub tcp_keepidle: Option<String>,
    pub tcp_keepintvl: Option<String>,
}

/// An `authentication-key-chain <name>`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampKeyChain {
    pub name: String,
    pub keys: Vec<TwampKey>,
}

/// A `key-id <id>` of a key chain.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampKey {
    pub id: String,
    pub has_secret: bool,
    /// The `secret`; `None` unless the `secrets` feature is enabled.
    pub secret: Option<Secret>,
}

/// A `client-list <name>`, the clients the server accepts.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampClientList {
    pub name: String,
    pub addresses: Vec<TwampClientAddress>,
}

/// An `address <prefix>` of a client list, optionally with `routing-instance`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampClientAddress {
    pub address: String,
    pub routing_instances: Vec<String>,
}

/// The `light` server.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampLightServer {
    pub offload_type: Option<String>,
    pub port: Option<String>,
}

/// A `routing-instance-list <name>` with its own port.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampRoutingInstanceList {
    pub name: String,
    pub port: Option<String>,
}

pub(super) fn parse_server(line: &ConfigLine) -> TwampServer {
    TwampServer {
        authentication_key_chains: named_children(line, "authentication-key-chain")
            .map(|(name, chain)| parse_key_chain(name, chain))
            .collect(),
        authentication_modes: args_of(line, "authentication-mode")
            .into_iter()
            .flat_map(list_values)
            .collect(),
        client_lists: parse_client_lists(line),
        light: child_block(line, "light").map(parse_light),
        max_connection_duration: text(line, "max-connection-duration"),
        maximum_connections: text(line, "maximum-connections"),
        maximum_connections_per_client: text(line, "maximum-connections-per-client"),
        maximum_sessions: text(line, "maximum-sessions"),
        maximum_sessions_per_connection: text(line, "maximum-sessions-per-connection"),
        offload_type: text(line, "offload-type"),
        port: text(line, "port"),
        reflector_inactivity_timeout: text(line, "reflector-inactivity-timeout"),
        routing_instance_lists: named_children(line, "routing-instance-list")
            .map(|(name, list)| TwampRoutingInstanceList {
                name: name.to_string(),
                port: text(list, "port"),
            })
            .collect(),
        server_inactivity_timeout: text(line, "server-inactivity-timeout"),
        tcp_keepcnt: text(line, "tcp-keepcnt"),
        tcp_keepidle: text(line, "tcp-keepidle"),
        tcp_keepintvl: text(line, "tcp-keepintvl"),
    }
}

fn parse_key_chain(name: &str, line: &ConfigLine) -> TwampKeyChain {
    TwampKeyChain {
        name: name.to_string(),
        keys: named_children(line, "key-id")
            .map(|(id, key)| {
                let secret = arg_of(key, "secret");
                TwampKey {
                    id: id.to_string(),
                    has_secret: secret.is_some(),
                    secret: secret_value(secret),
                }
            })
            .collect(),
    }
}

pub(crate) fn parse_client_lists(line: &ConfigLine) -> Vec<TwampClientList> {
    named_children(line, "client-list")
        .map(|(name, list)| TwampClientList {
            name: name.to_string(),
            addresses: parse_addresses(list),
        })
        .collect()
}

/// Addresses written `address <prefix>;` or as an `address { <prefix>; }` block.
fn parse_addresses(list: &ConfigLine) -> Vec<TwampClientAddress> {
    let mut addresses = Vec::new();
    for child in active_children(list) {
        if child.text == "address" {
            addresses.extend(active_children(child).map(|a| parse_address(&a.text)));
        } else if let Some(arg) = child.arg("address") {
            addresses.push(parse_address(arg));
        }
    }
    addresses
}

fn parse_address(entry: &str) -> TwampClientAddress {
    let (address, rest) = split_keyword(entry);
    TwampClientAddress {
        address: address.to_string(),
        routing_instances: rest
            .strip_prefix("routing-instance")
            .map(list_values)
            .unwrap_or_default(),
    }
}

fn parse_light(line: &ConfigLine) -> TwampLightServer {
    TwampLightServer {
        offload_type: text(line, "offload-type"),
        port: text(line, "port"),
    }
}