Skip to main content

asterust_junos/system/
management_traffic.rs

1//! `management-traffic`
2//! `system { management-traffic { protocol <name> { priority <priority>; } } }`.
3//!
4//! # See also
5//!
6//! - [priority (Management Traffic)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-management-traffic-protocol-priority.html)
7
8use crate::{arg_of, child_block, named_children};
9use asterust::config::ConfigLine;
10
11/// A `protocol <name>` of `system management-traffic`.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct ManagementTrafficProtocol {
15    pub name: String,
16    /// `low`, `medium` or `high`.
17    pub priority: Option<String>,
18}
19
20pub(super) fn management_traffic(system: &ConfigLine) -> Vec<ManagementTrafficProtocol> {
21    child_block(system, "management-traffic")
22        .map(|traffic| {
23            named_children(traffic, "protocol")
24                .map(|(name, protocol)| ManagementTrafficProtocol {
25                    name: name.to_string(),
26                    priority: arg_of(protocol, "priority").map(str::to_string),
27                })
28                .collect()
29        })
30        .unwrap_or_default()
31}
32
33#[cfg(test)]
34mod tests {
35    use crate::system::system;
36    use asterust::config::juniper;
37
38    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
39    #[test]
40    fn extracts_management_traffic_priorities() {
41        let protocols = system(&juniper::parse_braces(
42            "system {\n    management-traffic {\n        protocol ssh {\n            priority high;\n        }\n        protocol snmp {\n            priority low;\n        }\n    }\n}\n",
43        ))
44        .unwrap()
45        .management_traffic;
46        assert_eq!(protocols.len(), 2);
47        assert_eq!(protocols[0].name, "ssh");
48        assert_eq!(protocols[0].priority.as_deref(), Some("high"));
49        assert_eq!(protocols[1].name, "snmp");
50        assert_eq!(protocols[1].priority.as_deref(), Some("low"));
51    }
52
53    #[test]
54    fn no_management_traffic_is_empty() {
55        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
56        assert!(system.management_traffic.is_empty());
57    }
58}