asterust_junos/protocols/mpls/
static_lsp.rs1use 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#[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#[derive(Clone, Debug, Default, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct MplsStaticIngress {
25 pub to: Option<String>,
26 pub push: Option<String>,
27 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 pub install: Vec<String>,
37 pub link_protection: Option<String>,
39 pub node_protection: Option<MplsStaticNodeProtection>,
40}
41
42#[derive(Clone, Debug, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct MplsStaticTransit {
46 pub incoming_label: String,
47 pub pop: bool,
48 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#[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 pub next_table: Option<String>,
66 pub bandwidth: Option<String>,
67 pub description: Option<String>,
68}
69
70#[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
141fn 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}