Skip to main content

asterust_junos/protocols/pim/
rp.rs

1//! `rp`
2//! `protocols { pim { rp { ... } } }`.
3//!
4//! # See also
5//!
6//! - [rp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rp-edit-protocols-pim.html)
7//! - [local](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/local-edit-protocols-pim.html)
8//! - [static (Protocols PIM RP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-edit-protocols-pim.html)
9//! - [auto-rp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/auto-rp-edit-protocols-pim.html)
10//! - [bootstrap](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bootstrap-edit-protocols-pim.html)
11//! - [bootstrap-import](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bootstrap-import-edit-protocols-pim.html)
12//! - [bootstrap-export](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bootstrap-export-edit-protocols-pim.html)
13//! - [bootstrap-priority](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/bootstrap-priority-edit-protocols-pim.html)
14//! - [dr-register-policy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dr-register-policy-edit-protocols-pim.html)
15//! - [rp-register-policy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rp-register-policy-edit-protocols-pim.html)
16//! - [embedded-rp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/embedded-rp-edit-protocols-pim.html)
17//! - [register-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/register-limit-edit-protocols-pim.html)
18//! - [register-probe-time](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/register-probe-time-edit-protocols-pim.html)
19//! - [anycast-pim](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/anycast-pim-edit-protocols-pim.html)
20//! - [group-ranges](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-ranges-edit-protocols-pim.html)
21//! - [hold-time (Protocols PIM)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/hold-time-edit-protocols-pim.html)
22//! - [override](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/override-edit-protocols-pim.html)
23//! - [Configuring Static RP](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-static-rp.html)
24//! - [Configuring PIM Bootstrap Router](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-bootstrap-router.html)
25//! - [Example: Configuring Anycast RP](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-anycast-rp.html)
26//! - [Example: Configuring Bidirectional PIM](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-bidirectional.html)
27
28use super::limits::{parse_limits, PimLimits};
29use crate::{
30    arg_of, args_of, child_block, has_leaf, list_values, named_children, split_name, statement_words, statements,
31    toggle, value_after,
32};
33use asterust::config::ConfigLine;
34
35/// The `auto-rp` statement.
36#[derive(Clone, Debug, Default, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct PimAutoRp {
39    /// `announce`, `discovery` or `mapping`.
40    pub mode: Option<String>,
41    /// `Some(true)` for `mapping-agent-election`, `Some(false)` for `no-mapping-agent-election`.
42    pub mapping_agent_election: Option<bool>,
43}
44
45/// An `address` of `rp bidirectional`, with the groups it is the bidirectional RP for.
46#[derive(Clone, Debug, PartialEq, Eq)]
47#[non_exhaustive]
48pub struct PimBidirectionalRp {
49    pub address: String,
50    pub group_ranges: Vec<String>,
51    /// Seconds.
52    pub hold_time: Option<String>,
53    pub priority: Option<String>,
54}
55
56/// A `family (inet | inet6)` of `rp bootstrap`.
57#[derive(Clone, Debug, PartialEq, Eq)]
58#[non_exhaustive]
59pub struct PimBootstrapFamily {
60    pub family: String,
61    pub priority: Option<String>,
62    pub import_policies: Vec<String>,
63    pub export_policies: Vec<String>,
64}
65
66/// The `embedded-rp` statement.
67#[derive(Clone, Debug, Default, PartialEq, Eq)]
68#[non_exhaustive]
69pub struct PimEmbeddedRp {
70    pub group_ranges: Vec<String>,
71    pub maximum_rps: Option<String>,
72}
73
74/// An `address` of an `anycast-pim` `rp-set`.
75#[derive(Clone, Debug, PartialEq, Eq)]
76#[non_exhaustive]
77pub struct PimAnycastRp {
78    pub address: String,
79    pub forward_msdp_sa: bool,
80}
81
82/// The `anycast-pim` statement of a local RP.
83#[derive(Clone, Debug, Default, PartialEq, Eq)]
84#[non_exhaustive]
85pub struct PimAnycast {
86    pub disable: bool,
87    pub local_address: Option<String>,
88    pub rp_set: Vec<PimAnycastRp>,
89}
90
91/// The options of a local RP, written directly under `local` or under one of its families.
92#[derive(Clone, Debug, Default, PartialEq, Eq)]
93#[non_exhaustive]
94pub struct PimLocalRpOptions {
95    pub address: Option<String>,
96    pub anycast_pim: Option<PimAnycast>,
97    pub group_ranges: Vec<String>,
98    /// Seconds.
99    pub hold_time: Option<String>,
100    /// `override`: this RP wins over dynamically learned ones.
101    pub override_rp: bool,
102    pub priority: Option<String>,
103    pub process_non_null_as_null_register: bool,
104}
105
106/// A `family (inet | inet6)` of `rp local`.
107#[derive(Clone, Debug, PartialEq, Eq)]
108#[non_exhaustive]
109pub struct PimLocalRpFamily {
110    pub family: String,
111    pub disable: bool,
112    pub options: PimLocalRpOptions,
113}
114
115/// The `local` statement.
116#[derive(Clone, Debug, Default, PartialEq, Eq)]
117#[non_exhaustive]
118pub struct PimLocalRp {
119    pub disable: bool,
120    pub options: PimLocalRpOptions,
121    pub families: Vec<PimLocalRpFamily>,
122}
123
124/// An `address` of `rp static`.
125#[derive(Clone, Debug, PartialEq, Eq)]
126#[non_exhaustive]
127pub struct PimStaticRp {
128    pub address: String,
129    pub override_rp: bool,
130    pub version: Option<String>,
131    pub group_ranges: Vec<String>,
132}
133
134/// The `protocols pim rp` configuration. The `group-rp-mapping` statement is not extracted.
135#[derive(Clone, Debug, Default, PartialEq, Eq)]
136#[non_exhaustive]
137pub struct PimRp {
138    pub auto_rp: Option<PimAutoRp>,
139    pub bidirectional: Vec<PimBidirectionalRp>,
140    pub bootstrap: Vec<PimBootstrapFamily>,
141    pub bootstrap_import_policies: Vec<String>,
142    pub bootstrap_export_policies: Vec<String>,
143    pub bootstrap_priority: Option<String>,
144    pub dr_register_policies: Vec<String>,
145    pub embedded_rp: Option<PimEmbeddedRp>,
146    pub local: Option<PimLocalRp>,
147    pub register_limit: Option<PimLimits>,
148    pub register_probe_time: Option<String>,
149    pub rp_register_policies: Vec<String>,
150    pub static_rps: Vec<PimStaticRp>,
151}
152
153fn policies(line: &ConfigLine, keyword: &str) -> Vec<String> {
154    args_of(line, keyword).into_iter().flat_map(list_values).collect()
155}
156
157fn group_ranges(line: &ConfigLine) -> Vec<String> {
158    statements(line, "group-ranges")
159        .into_iter()
160        .map(str::to_string)
161        .collect()
162}
163
164fn parse_auto_rp(rp: &ConfigLine) -> Option<PimAutoRp> {
165    let words = statement_words(rp, "auto-rp")?;
166    Some(PimAutoRp {
167        mode: ["announce", "discovery", "mapping"]
168            .into_iter()
169            .find(|mode| words.contains(mode))
170            .map(str::to_string),
171        mapping_agent_election: child_block(rp, "auto-rp").and_then(|block| toggle(block, "mapping-agent-election")),
172    })
173}
174
175fn parse_anycast(anycast: &ConfigLine) -> PimAnycast {
176    PimAnycast {
177        disable: has_leaf(anycast, "disable"),
178        local_address: arg_of(anycast, "local-address").map(str::to_string),
179        rp_set: child_block(anycast, "rp-set")
180            .map(|set| {
181                named_children(set, "address")
182                    .map(|(arg, _)| {
183                        let (address, rest) = split_name(arg);
184                        PimAnycastRp {
185                            address: address.to_string(),
186                            forward_msdp_sa: rest.split_whitespace().any(|word| word == "forward-msdp-sa"),
187                        }
188                    })
189                    .collect()
190            })
191            .unwrap_or_default(),
192    }
193}
194
195fn parse_local_options(line: &ConfigLine) -> PimLocalRpOptions {
196    PimLocalRpOptions {
197        address: arg_of(line, "address").map(str::to_string),
198        anycast_pim: child_block(line, "anycast-pim").map(parse_anycast),
199        group_ranges: group_ranges(line),
200        hold_time: arg_of(line, "hold-time").map(str::to_string),
201        override_rp: has_leaf(line, "override"),
202        priority: arg_of(line, "priority").map(str::to_string),
203        process_non_null_as_null_register: has_leaf(line, "process-non-null-as-null-register"),
204    }
205}
206
207fn parse_static(arg: &str, entry: &ConfigLine) -> PimStaticRp {
208    let (address, rest) = split_name(arg);
209    let words: Vec<&str> = rest.split_whitespace().collect();
210    PimStaticRp {
211        address: address.to_string(),
212        override_rp: has_leaf(entry, "override") || words.contains(&"override"),
213        version: arg_of(entry, "version")
214            .map(str::to_string)
215            .or_else(|| value_after(&words, "version")),
216        group_ranges: group_ranges(entry),
217    }
218}
219
220pub(super) fn parse_rp(rp: &ConfigLine) -> PimRp {
221    PimRp {
222        auto_rp: parse_auto_rp(rp),
223        bidirectional: child_block(rp, "bidirectional")
224            .map(|bidirectional| {
225                named_children(bidirectional, "address")
226                    .map(|(arg, entry)| PimBidirectionalRp {
227                        address: split_name(arg).0.to_string(),
228                        group_ranges: group_ranges(entry),
229                        hold_time: arg_of(entry, "hold-time").map(str::to_string),
230                        priority: arg_of(entry, "priority").map(str::to_string),
231                    })
232                    .collect()
233            })
234            .unwrap_or_default(),
235        bootstrap: child_block(rp, "bootstrap")
236            .map(|bootstrap| {
237                named_children(bootstrap, "family")
238                    .map(|(family, entry)| PimBootstrapFamily {
239                        family: family.to_string(),
240                        priority: arg_of(entry, "priority").map(str::to_string),
241                        import_policies: policies(entry, "import"),
242                        export_policies: policies(entry, "export"),
243                    })
244                    .collect()
245            })
246            .unwrap_or_default(),
247        bootstrap_import_policies: policies(rp, "bootstrap-import"),
248        bootstrap_export_policies: policies(rp, "bootstrap-export"),
249        bootstrap_priority: arg_of(rp, "bootstrap-priority").map(str::to_string),
250        dr_register_policies: policies(rp, "dr-register-policy"),
251        embedded_rp: child_block(rp, "embedded-rp").map(|embedded| PimEmbeddedRp {
252            group_ranges: group_ranges(embedded),
253            maximum_rps: arg_of(embedded, "maximum-rps").map(str::to_string),
254        }),
255        local: child_block(rp, "local").map(|local| PimLocalRp {
256            disable: has_leaf(local, "disable"),
257            options: parse_local_options(local),
258            families: named_children(local, "family")
259                .map(|(family, entry)| PimLocalRpFamily {
260                    family: family.to_string(),
261                    disable: has_leaf(entry, "disable"),
262                    options: parse_local_options(entry),
263                })
264                .collect(),
265        }),
266        register_limit: child_block(rp, "register-limit").map(parse_limits),
267        register_probe_time: arg_of(rp, "register-probe-time").map(str::to_string),
268        rp_register_policies: policies(rp, "rp-register-policy"),
269        static_rps: child_block(rp, "static")
270            .map(|block| {
271                named_children(block, "address")
272                    .map(|(arg, entry)| parse_static(arg, entry))
273                    .collect()
274            })
275            .unwrap_or_default(),
276    }
277}