Skip to main content

asterust_junos/interfaces/unit/family/
accounting.rs

1//! `accounting`, `targeted-broadcast`, `unnumbered-address` and `vlan-rewrite`
2//! `interfaces { <name> { unit <n> { family <name> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [accounting (interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accounting-edit-interfaces.html)
7//! - [destination-class-usage](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/destination-class-usage-edit-interfaces.html)
8//! - [source-class-usage](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/source-class-usage-edit-interfaces.html)
9//! - [forward-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/forward-only-edit-interfaces.html)
10//! - [forward-and-send-to-re](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/forward-and-send-to-re-edit-interfaces.html)
11//! - [unnumbered-address (Ethernet)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unnumbered-address-edit-interfaces-ethernet.html)
12//! - [unnumbered-address (PPP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unnumbered-address-edit-interfaces-ppp.html)
13//! - [unnumbered-address (Demux)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/unnumbered-address-edit-interfaces-ip-demux.html)
14//! - [vlan-rewrite](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vlan-rewrite-edit-interfaces.html)
15
16use crate::{active_children, args_of, child_block, has_leaf, statement_words, value_after};
17use asterust::config::ConfigLine;
18
19/// The `accounting` block of `family inet`.
20#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct FamilyAccounting {
23    pub destination_class_usage: bool,
24    pub source_class_usage: bool,
25    /// The directions of `source-class-usage`, e.g. `input` and `output`.
26    pub source_class_usage_directions: Vec<String>,
27}
28
29/// The `targeted-broadcast` block of `family inet`.
30#[derive(Clone, Debug, PartialEq, Eq)]
31#[non_exhaustive]
32pub struct TargetedBroadcast {
33    pub forward_only: bool,
34    pub forward_and_send_to_re: bool,
35}
36
37/// The `unnumbered-address` of a family.
38#[derive(Clone, Debug, PartialEq, Eq)]
39#[non_exhaustive]
40pub struct UnnumberedAddress {
41    /// The interface whose address is borrowed.
42    pub interface: String,
43    pub preferred_source_address: Option<String>,
44    pub destination: Option<String>,
45    pub destination_profile: Option<String>,
46}
47
48/// A `translate <from> <to>` of `vlan-rewrite`.
49#[derive(Clone, Debug, PartialEq, Eq)]
50#[non_exhaustive]
51pub struct VlanTranslation {
52    pub from: String,
53    pub to: String,
54}
55
56pub(super) fn parse_accounting(accounting: &ConfigLine) -> FamilyAccounting {
57    let source =
58        active_children(accounting).find(|c| c.text == "source-class-usage" || c.arg("source-class-usage").is_some());
59    FamilyAccounting {
60        destination_class_usage: has_leaf(accounting, "destination-class-usage"),
61        source_class_usage: source.is_some(),
62        source_class_usage_directions: source
63            .map(|line| {
64                line.text
65                    .split_whitespace()
66                    .skip(1)
67                    .map(str::to_string)
68                    .chain(active_children(line).map(|direction| direction.text.clone()))
69                    .collect()
70            })
71            .unwrap_or_default(),
72    }
73}
74
75pub(super) fn parse_targeted_broadcast(broadcast: &ConfigLine) -> TargetedBroadcast {
76    TargetedBroadcast {
77        forward_only: has_leaf(broadcast, "forward-only"),
78        forward_and_send_to_re: has_leaf(broadcast, "forward-and-send-to-re"),
79    }
80}
81
82pub(super) fn parse_unnumbered_address(family: &ConfigLine) -> Option<UnnumberedAddress> {
83    let words = statement_words(family, "unnumbered-address")?;
84    Some(UnnumberedAddress {
85        interface: words.first()?.to_string(),
86        preferred_source_address: value_after(&words, "preferred-source-address"),
87        destination: value_after(&words, "destination"),
88        destination_profile: value_after(&words, "destination-profile"),
89    })
90}
91
92pub(super) fn parse_vlan_rewrite(family: &ConfigLine) -> Vec<VlanTranslation> {
93    child_block(family, "vlan-rewrite").map_or_else(Vec::new, |rewrite| {
94        args_of(rewrite, "translate")
95            .into_iter()
96            .filter_map(|arg| {
97                let mut ids = arg.split_whitespace();
98                Some(VlanTranslation {
99                    from: ids.next()?.to_string(),
100                    to: ids.next()?.to_string(),
101                })
102            })
103            .collect()
104    })
105}