Skip to main content

asterust_junos/interfaces/unit/family/address/
mod.rs

1//! `address`
2//! `interfaces { <name> { unit <n> { family <name> { address <address> { ... } } } } }`.
3//!
4//! # See also
5//!
6//! - [address (IP Address)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/address-edit-interfaces-unit-family.html)
7//! - [primary (Address on Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/primary-edit-interfaces.html)
8//! - [preferred](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/preferred-edit-interfaces.html)
9//! - [arp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/arp-edit-interfaces.html)
10//! - [destination (Tunnels)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/destination-edit-interfaces.html)
11//! - [master-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/master-only-edit-interfaces.html)
12
13mod vrrp_group;
14
15pub use vrrp_group::{BandwidthThreshold, TrackedInterface, TrackedRoute, VrrpGroup, VrrpInheritFrom, VrrpTrack};
16
17use crate::{active_children, has_leaf, split_name};
18use asterust::config::ConfigLine;
19use vrrp_group::parse_vrrp_groups;
20
21/// A static `arp` entry of an address.
22#[derive(Clone, Debug, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct AddressArp {
25    pub address: String,
26    pub mac: Option<String>,
27    /// The MAC is written with `multicast-mac`.
28    pub multicast_mac: bool,
29    /// The device also answers ARP requests for the address.
30    pub publish: bool,
31}
32
33/// An `address <address>` of a family, with its options.
34#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct UnitAddress {
37    /// e.g. `"10.0.0.1/24"`.
38    pub address: String,
39    pub primary: bool,
40    pub preferred: bool,
41    pub master_only: bool,
42    pub eui_64: bool,
43    pub destination: Option<String>,
44    pub broadcast: Option<String>,
45    pub arps: Vec<AddressArp>,
46    pub vrrp_groups: Vec<VrrpGroup>,
47    /// The `virtual-gateway-address` of an IRB address.
48    pub virtual_gateway_address: Option<String>,
49}
50
51pub(super) fn parse_addresses(family: &ConfigLine) -> Vec<UnitAddress> {
52    active_children(family)
53        .filter_map(|line| Some((line, line.arg("address")?)))
54        .map(|(line, arg)| parse_address(line, arg))
55        .collect()
56}
57
58fn parse_address(line: &ConfigLine, arg: &str) -> UnitAddress {
59    let (address, inline) = split_name(arg);
60    let inline: Vec<&str> = inline.split_whitespace().collect();
61    let flag = |name: &str| has_leaf(line, name) || inline.contains(&name);
62    let value = |keyword: &str| {
63        active_children(line)
64            .find_map(|c| c.arg(keyword))
65            .map(|arg| arg.trim_matches('"').to_string())
66    };
67    UnitAddress {
68        address: address.to_string(),
69        primary: flag("primary"),
70        preferred: flag("preferred"),
71        master_only: flag("master-only"),
72        eui_64: flag("eui-64"),
73        destination: value("destination"),
74        broadcast: value("broadcast"),
75        arps: active_children(line)
76            .filter_map(|c| c.arg("arp"))
77            .map(parse_arp)
78            .collect(),
79        vrrp_groups: parse_vrrp_groups(line),
80        virtual_gateway_address: value("virtual-gateway-address"),
81    }
82}
83
84/// `<ip-address> (mac | multicast-mac) <mac-address> [publish]`.
85fn parse_arp(arg: &str) -> AddressArp {
86    let mut words = arg.split_whitespace();
87    let address = words.next().unwrap_or_default().to_string();
88    let mut arp = AddressArp {
89        address,
90        mac: None,
91        multicast_mac: false,
92        publish: false,
93    };
94    while let Some(word) = words.next() {
95        match word {
96            "mac" => arp.mac = words.next().map(str::to_string),
97            "multicast-mac" => {
98                arp.multicast_mac = true;
99                arp.mac = words.next().map(str::to_string);
100            }
101            "publish" => arp.publish = true,
102            _ => {}
103        }
104    }
105    arp
106}