asterust_junos/services/monitoring/twamp/
client.rs1use super::text;
5use crate::services::rpm::twamp::{parse_source_routing, TwampSourceRouting};
6use crate::{arg_of, child_block, named_children, split_keyword, statements};
7use asterust::config::ConfigLine;
8
9#[derive(Clone, Debug, PartialEq, Eq)]
11#[non_exhaustive]
12pub struct TwampClient {
13 pub control_connections: Vec<ControlConnection>,
14}
15
16#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct ControlConnection {
20 pub name: String,
21 pub control_type: Option<String>,
23 pub destination_port: Option<String>,
24 pub routing_instance: Option<String>,
25 pub source_address: Option<String>,
26 pub target_address: Option<String>,
28 pub test_interval: Option<String>,
29 pub test_start: Option<String>,
31 pub traps: Vec<String>,
33 pub test_sessions: Vec<TestSession>,
34}
35
36#[derive(Clone, Debug, PartialEq, Eq)]
38#[non_exhaustive]
39pub struct TestSession {
40 pub name: String,
41 pub data_size: Option<String>,
42 pub destination_port: Option<String>,
43 pub dscp_code_points: Option<String>,
44 pub history_size: Option<String>,
45 pub moving_average_size: Option<String>,
46 pub offload_type: Option<String>,
48 pub probe_count: Option<String>,
49 pub probe_interval: Option<String>,
50 pub source_address: Option<String>,
51 pub source_routing: Option<TwampSourceRouting>,
52 pub target_address: Option<String>,
54 pub target_local_link: Option<String>,
56 pub thresholds: TwampThresholds,
57 pub traps: Vec<String>,
59 pub ttl: Option<String>,
60}
61
62#[derive(Clone, Debug, Default, PartialEq, Eq)]
64#[non_exhaustive]
65pub struct TwampThresholds {
66 pub control_failure: Option<String>,
68 pub successive_loss: Option<String>,
69 pub total_loss: Option<String>,
70 pub threshold_type: Option<String>,
72}
73
74pub(super) fn parse_client(line: &ConfigLine) -> TwampClient {
75 TwampClient {
76 control_connections: named_children(line, "control-connection")
77 .map(|(name, connection)| parse_control_connection(name, connection))
78 .collect(),
79 }
80}
81
82fn parse_control_connection(name: &str, line: &ConfigLine) -> ControlConnection {
83 ControlConnection {
84 name: name.to_string(),
85 control_type: text(line, "control-type"),
86 destination_port: text(line, "destination-port"),
87 routing_instance: text(line, "routing-instance"),
88 source_address: text(line, "source-address"),
89 target_address: text(line, "target"),
90 test_interval: text(line, "test-interval"),
91 test_start: text(line, "test-start"),
92 traps: traps(line),
93 test_sessions: named_children(line, "test-session")
94 .map(|(name, session)| parse_test_session(name, session))
95 .collect(),
96 }
97}
98
99fn traps(line: &ConfigLine) -> Vec<String> {
100 statements(line, "traps").into_iter().map(str::to_string).collect()
101}
102
103fn parse_test_session(name: &str, line: &ConfigLine) -> TestSession {
104 let (target_address, target_rest) = arg_of(line, "target").map_or((None, ""), |t| {
105 let (address, rest) = split_keyword(t);
106 (Some(address.to_string()), rest)
107 });
108 TestSession {
109 name: name.to_string(),
110 data_size: text(line, "data-size"),
111 destination_port: text(line, "destination-port"),
112 dscp_code_points: text(line, "dscp-code-points"),
113 history_size: text(line, "history-size"),
114 moving_average_size: text(line, "moving-average-size"),
115 offload_type: text(line, "offload-type"),
116 probe_count: text(line, "probe-count"),
117 probe_interval: text(line, "probe-interval"),
118 source_address: text(line, "source-address"),
119 source_routing: child_block(line, "source-routing").map(parse_source_routing),
120 target_address,
121 target_local_link: target_rest
122 .strip_prefix("local-link")
123 .map(|link| link.trim().to_string())
124 .filter(|link| !link.is_empty()),
125 thresholds: parse_thresholds(line),
126 traps: traps(line),
127 ttl: text(line, "ttl"),
128 }
129}
130
131fn parse_thresholds(line: &ConfigLine) -> TwampThresholds {
132 let mut thresholds = TwampThresholds::default();
133 for statement in statements(line, "thresholds") {
134 let (name, value) = split_keyword(statement);
135 let slot = match name {
136 "control-failure" => &mut thresholds.control_failure,
137 "successive-loss" => &mut thresholds.successive_loss,
138 "total-loss" => &mut thresholds.total_loss,
139 "threshold-type" => &mut thresholds.threshold_type,
140 _ => continue,
141 };
142 *slot = Some(value.to_string()).filter(|v| !v.is_empty());
143 }
144 thresholds
145}