Skip to main content

asterust_junos/firewall/
three_color_policer.rs

1//! `three-color-policer`
2//! `firewall { three-color-policer <name> { ... } }`.
3//!
4//! # See also
5//!
6//! - [three-color-policer (Configuring)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/three-color-policer-edit-firewall.html)
7
8use crate::{active_children, arg_of, child_block, has_leaf};
9use asterust::config::ConfigLine;
10
11/// The `single-rate` or `two-rate` settings of a three-color policer.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct ThreeColorRate {
15    /// `Some(true)` for `color-aware`, `Some(false)` for `color-blind`.
16    pub color_aware: Option<bool>,
17    pub committed_information_rate: Option<String>,
18    pub committed_burst_size: Option<String>,
19    /// Only `single-rate`.
20    pub excess_burst_size: Option<String>,
21    /// Only `two-rate`.
22    pub peak_information_rate: Option<String>,
23    /// Only `two-rate`.
24    pub peak_burst_size: Option<String>,
25}
26
27/// A `three-color-policer <name>`.
28#[derive(Clone, Debug, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct ThreeColorPolicer {
31    pub name: String,
32    /// The statements of `action`, e.g. `"loss-priority high then discard"`.
33    pub actions: Vec<String>,
34    pub filter_specific: bool,
35    pub logical_interface_policer: bool,
36    pub physical_interface_policer: bool,
37    pub shared_bandwidth_policer: bool,
38    pub single_rate: Option<ThreeColorRate>,
39    pub two_rate: Option<ThreeColorRate>,
40}
41
42pub(super) fn parse_three_color_policer(name: &str, line: &ConfigLine) -> ThreeColorPolicer {
43    ThreeColorPolicer {
44        name: name.to_string(),
45        actions: child_block(line, "action")
46            .map(|action| active_children(action).map(|c| c.text.clone()).collect())
47            .unwrap_or_default(),
48        filter_specific: has_leaf(line, "filter-specific"),
49        logical_interface_policer: has_leaf(line, "logical-interface-policer"),
50        physical_interface_policer: has_leaf(line, "physical-interface-policer"),
51        shared_bandwidth_policer: has_leaf(line, "shared-bandwidth-policer"),
52        single_rate: child_block(line, "single-rate").map(parse_rate),
53        two_rate: child_block(line, "two-rate").map(parse_rate),
54    }
55}
56
57fn parse_rate(rate: &ConfigLine) -> ThreeColorRate {
58    let field = |keyword: &str| arg_of(rate, keyword).map(str::to_string);
59    ThreeColorRate {
60        color_aware: if has_leaf(rate, "color-aware") {
61            Some(true)
62        } else if has_leaf(rate, "color-blind") {
63            Some(false)
64        } else {
65            None
66        },
67        committed_information_rate: field("committed-information-rate"),
68        committed_burst_size: field("committed-burst-size"),
69        excess_burst_size: field("excess-burst-size"),
70        peak_information_rate: field("peak-information-rate"),
71        peak_burst_size: field("peak-burst-size"),
72    }
73}