asterust_junos/interfaces/unit/family/address/
mod.rs1mod 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#[derive(Clone, Debug, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct AddressArp {
25 pub address: String,
26 pub mac: Option<String>,
27 pub multicast_mac: bool,
29 pub publish: bool,
31}
32
33#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct UnitAddress {
37 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 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
84fn 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}