Skip to main content

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

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