use super::text;
use crate::{arg_of, child_block, has_leaf, named_children, split_keyword, statements};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampClient {
pub control_connections: Vec<ControlConnection>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ControlConnection {
pub name: String,
pub authentication_mode: Option<String>,
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>,
pub test_start: Option<String>,
pub traps: Vec<String>,
pub test_sessions: Vec<TestSession>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TestSession {
pub name: String,
pub data_fill_with_zeros: bool,
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>,
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>,
pub target_local_link: Option<String>,
pub thresholds: TwampThresholds,
pub traps: Vec<String>,
pub ttl: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TwampSourceRouting {
pub forward: Option<String>,
pub return_path: Option<String>,
}
#[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 control_failure: Option<String>,
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(),
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").or_else(|| text(line, "target")),
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"),
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 = arg_of(line, "target-address").or_else(|| arg_of(line, "target"));
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"),
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_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,
"control-failure" => &mut thresholds.control_failure,
"threshold-type" => &mut thresholds.threshold_type,
_ => continue,
};
*slot = Some(value.to_string()).filter(|v| !v.is_empty());
}
thresholds
}