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}
48
49pub(super) fn parse_addresses(family: &ConfigLine) -> Vec<UnitAddress> {
50 active_children(family)
51 .filter_map(|line| Some((line, line.arg("address")?)))
52 .map(|(line, arg)| parse_address(line, arg))
53 .collect()
54}
55
56fn parse_address(line: &ConfigLine, arg: &str) -> UnitAddress {
57 let (address, inline) = split_name(arg);
58 let inline: Vec<&str> = inline.split_whitespace().collect();
59 let flag = |name: &str| has_leaf(line, name) || inline.contains(&name);
60 let value = |keyword: &str| {
61 active_children(line)
62 .find_map(|c| c.arg(keyword))
63 .map(|arg| arg.trim_matches('"').to_string())
64 };
65 UnitAddress {
66 address: address.to_string(),
67 primary: flag("primary"),
68 preferred: flag("preferred"),
69 master_only: flag("master-only"),
70 eui_64: flag("eui-64"),
71 destination: value("destination"),
72 broadcast: value("broadcast"),
73 arps: active_children(line)
74 .filter_map(|c| c.arg("arp"))
75 .map(parse_arp)
76 .collect(),
77 vrrp_groups: parse_vrrp_groups(line),
78 }
79}
80
81fn parse_arp(arg: &str) -> AddressArp {
83 let mut words = arg.split_whitespace();
84 let address = words.next().unwrap_or_default().to_string();
85 let mut arp = AddressArp {
86 address,
87 mac: None,
88 multicast_mac: false,
89 publish: false,
90 };
91 while let Some(word) = words.next() {
92 match word {
93 "mac" => arp.mac = words.next().map(str::to_string),
94 "multicast-mac" => {
95 arp.multicast_mac = true;
96 arp.mac = words.next().map(str::to_string);
97 }
98 "publish" => arp.publish = true,
99 _ => {}
100 }
101 }
102 arp
103}