Skip to main content

asterust_junos/protocols/mpls/
static_lsp.rs

1//! `static-label-switched-path`
2//! `protocols { mpls { static-label-switched-path <name> { ingress { ... } transit <label> { ... } bypass <name> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [static-label-switched-path](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-label-switched-path-edit-protocols-mpls.html)
7
8use crate::{
9    arg_of, args_of, child_block, first_word, has_leaf, named_children, split_name, statement_words, value_after,
10};
11use asterust::config::ConfigLine;
12
13/// A `node-protection bypass-name <name> next-next-label <label>` statement.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct MplsStaticNodeProtection {
17    pub bypass_name: Option<String>,
18    pub next_next_label: Option<String>,
19}
20
21/// The `ingress` role of a static LSP, which pushes a label onto the traffic it receives.
22#[derive(Clone, Debug, Default, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct MplsStaticIngress {
25    pub to: Option<String>,
26    pub push: Option<String>,
27    /// An address, an interface name or `<address>/<interface>`, as written.
28    pub next_hop: Option<String>,
29    pub bandwidth: Option<String>,
30    pub class_of_service: Option<String>,
31    pub description: Option<String>,
32    pub metric: Option<String>,
33    pub preference: Option<String>,
34    pub no_install_to_address: bool,
35    /// The prefixes of `install`.
36    pub install: Vec<String>,
37    /// The `bypass-name` of `link-protection`.
38    pub link_protection: Option<String>,
39    pub node_protection: Option<MplsStaticNodeProtection>,
40}
41
42/// A `transit <incoming-label>` role of a static LSP.
43#[derive(Clone, Debug, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct MplsStaticTransit {
46    pub incoming_label: String,
47    pub pop: bool,
48    /// The label of `swap`.
49    pub swap: Option<String>,
50    pub next_hop: Option<String>,
51    pub bandwidth: Option<String>,
52    pub description: Option<String>,
53    pub link_protection: Option<String>,
54    pub node_protection: Option<MplsStaticNodeProtection>,
55}
56
57/// A `bypass <name>` of a static LSP.
58#[derive(Clone, Debug, PartialEq, Eq)]
59#[non_exhaustive]
60pub struct MplsStaticBypass {
61    pub name: String,
62    pub to: Option<String>,
63    pub next_hop: Option<String>,
64    /// `<table> push <label>`, as written.
65    pub next_table: Option<String>,
66    pub bandwidth: Option<String>,
67    pub description: Option<String>,
68}
69
70/// A `static-label-switched-path <name>`. The `policing` blocks are not extracted.
71#[derive(Clone, Debug, PartialEq, Eq)]
72#[non_exhaustive]
73pub struct MplsStaticLsp {
74    pub name: String,
75    pub ingress: Option<MplsStaticIngress>,
76    pub transit: Vec<MplsStaticTransit>,
77    pub bypasses: Vec<MplsStaticBypass>,
78}
79
80pub(super) fn parse_static_lsp(name: &str, lsp: &ConfigLine) -> MplsStaticLsp {
81    MplsStaticLsp {
82        name: name.to_string(),
83        ingress: child_block(lsp, "ingress").map(parse_ingress),
84        transit: named_children(lsp, "transit")
85            .map(|(label, transit)| parse_transit(label, transit))
86            .collect(),
87        bypasses: named_children(lsp, "bypass")
88            .map(|(name, bypass)| parse_bypass(name, bypass))
89            .collect(),
90    }
91}
92
93fn parse_ingress(ingress: &ConfigLine) -> MplsStaticIngress {
94    let field = |keyword: &str| arg_of(ingress, keyword).map(str::to_string);
95    MplsStaticIngress {
96        to: field("to"),
97        push: field("push"),
98        next_hop: field("next-hop"),
99        bandwidth: field("bandwidth"),
100        class_of_service: field("class-of-service"),
101        description: field("description"),
102        metric: field("metric"),
103        preference: field("preference"),
104        no_install_to_address: has_leaf(ingress, "no-install-to-address"),
105        install: args_of(ingress, "install")
106            .into_iter()
107            .filter_map(first_word)
108            .map(str::to_string)
109            .collect(),
110        link_protection: parse_link_protection(ingress),
111        node_protection: parse_node_protection(ingress),
112    }
113}
114
115fn parse_transit(label: &str, transit: &ConfigLine) -> MplsStaticTransit {
116    let field = |keyword: &str| arg_of(transit, keyword).map(str::to_string);
117    MplsStaticTransit {
118        incoming_label: split_name(label).0.to_string(),
119        pop: has_leaf(transit, "pop"),
120        swap: field("swap"),
121        next_hop: field("next-hop"),
122        bandwidth: field("bandwidth"),
123        description: field("description"),
124        link_protection: parse_link_protection(transit),
125        node_protection: parse_node_protection(transit),
126    }
127}
128
129fn parse_bypass(name: &str, bypass: &ConfigLine) -> MplsStaticBypass {
130    let field = |keyword: &str| arg_of(bypass, keyword).map(str::to_string);
131    MplsStaticBypass {
132        name: name.to_string(),
133        to: field("to"),
134        next_hop: field("next-hop"),
135        next_table: field("next-table"),
136        bandwidth: field("bandwidth"),
137        description: field("description"),
138    }
139}
140
141/// `link-protection bypass-name <name>`, on one line or with the option as a child.
142fn parse_link_protection(role: &ConfigLine) -> Option<String> {
143    statement_words(role, "link-protection").and_then(|words| value_after(&words, "bypass-name"))
144}
145
146fn parse_node_protection(role: &ConfigLine) -> Option<MplsStaticNodeProtection> {
147    let words = statement_words(role, "node-protection")?;
148    Some(MplsStaticNodeProtection {
149        bypass_name: value_after(&words, "bypass-name"),
150        next_next_label: value_after(&words, "next-next-label"),
151    })
152}