Skip to main content

asterust_junos/policy_options/
lists.rs

1//! `mac-list` and `rib-list`
2//! `policy-options { mac-list <name> { ... } }` and `policy-options { rib-list <name> { ... } }`.
3//!
4//! # See also
5//!
6//! - [mac-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mac-list-edit-policy-options.html)
7//! - [rib-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rib-list-edit-policy-options.html)
8
9use crate::{active_children, args_of};
10use asterust::config::ConfigLine;
11
12/// A `mac-list <name>`, which a `from mac-filter-list` condition refers to.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct MacList {
16    pub name: String,
17    pub mac_addresses: Vec<String>,
18}
19
20/// A `rib-list <name>`.
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct RibList {
24    pub name: String,
25    /// The `rib-list-item` names.
26    pub items: Vec<String>,
27}
28
29pub(super) fn parse_mac_list(name: &str, line: &ConfigLine) -> MacList {
30    MacList {
31        name: name.to_string(),
32        // Addresses are the only statements without arguments; `apply-groups` and the like have a space.
33        mac_addresses: active_children(line)
34            .filter(|c| !c.text.contains(' '))
35            .map(|c| c.text.clone())
36            .collect(),
37    }
38}
39
40pub(super) fn parse_rib_list(name: &str, line: &ConfigLine) -> RibList {
41    RibList {
42        name: name.to_string(),
43        items: args_of(line, "rib-list-item").into_iter().map(str::to_string).collect(),
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use crate::policy_options::policy_options;
50    use asterust::config::juniper;
51
52    // Not from documentation: written from the mac-list and rib-list CLI Reference pages. See the README "Sources".
53    #[test]
54    fn reads_mac_and_rib_lists() {
55        let config = juniper::parse_braces(
56            "policy-options {\n    mac-list MACS {\n        00:11:22:33:44:55;\n        00:11:22:33:44:66;\n    }\n    rib-list RIBS {\n        rib-list-item inet.0;\n        rib-list-item vrf-a.inet.0;\n    }\n}\n",
57        );
58        let options = policy_options(&config).unwrap();
59        assert_eq!(options.mac_lists[0].name, "MACS");
60        assert_eq!(
61            options.mac_lists[0].mac_addresses,
62            ["00:11:22:33:44:55", "00:11:22:33:44:66"]
63        );
64        assert_eq!(options.rib_lists[0].name, "RIBS");
65        assert_eq!(options.rib_lists[0].items, ["inet.0", "vrf-a.inet.0"]);
66    }
67}