Skip to main content

asterust_junos/protocols/source_packet_routing/
source_routing_path.rs

1//! `source-routing-path`
2//! `protocols { source-packet-routing { source-routing-path <name> { primary ...; } } }`.
3//!
4//! # See also
5//!
6//! - [source-routing-path](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/autogen-protocols-source-packet-routing-source-routing-path.html)
7
8use crate::{active_children, arg_of, has_leaf, value_after};
9use asterust::config::ConfigLine;
10
11/// A `source-routing-path <name>`, a static SR-TE policy. The `compute` blocks of the segment lists are read as their
12/// profile name only.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct SourceRoutingPath {
16    pub name: String,
17    /// The tunnel type is SRv6.
18    pub srv6: bool,
19    /// The tunnel endpoint.
20    pub to: Option<String>,
21    /// The tunnel source address. It is not in the statement's syntax, but the SRv6-TE example sets it.
22    pub from: Option<String>,
23    pub color: Option<String>,
24    pub binding_sid: Option<String>,
25    pub metric: Option<String>,
26    pub no_ingress: bool,
27    pub sr_preference: Option<String>,
28    pub te_group_id: Option<String>,
29    pub lsp_external_controller: Option<String>,
30    pub primary: Vec<PathSegmentList>,
31    pub secondary: Vec<PathSegmentList>,
32}
33
34/// A segment list used by the `primary` or `secondary` paths of a [`SourceRoutingPath`].
35#[derive(Clone, Debug, PartialEq, Eq)]
36#[non_exhaustive]
37pub struct PathSegmentList {
38    /// The name of the `segment-list`, or of the path.
39    pub name: String,
40    pub weight: Option<String>,
41    pub return_path: Option<String>,
42    /// The compute profile.
43    pub compute: Option<String>,
44}
45
46pub(super) fn parse_source_routing_path(name: &str, path: &ConfigLine) -> SourceRoutingPath {
47    let field = |keyword: &str| arg_of(path, keyword).map(str::to_string);
48    SourceRoutingPath {
49        name: name.to_string(),
50        srv6: has_leaf(path, "srv6"),
51        to: field("to"),
52        from: field("from"),
53        color: field("color"),
54        binding_sid: field("binding-sid"),
55        metric: field("metric"),
56        no_ingress: has_leaf(path, "no-ingress"),
57        sr_preference: field("sr-preference"),
58        te_group_id: field("te-group-id"),
59        lsp_external_controller: field("lsp-external-controller"),
60        primary: parse_segment_lists(path, "primary"),
61        secondary: parse_segment_lists(path, "secondary"),
62    }
63}
64
65/// Reads `primary <name> { weight ...; }`, `primary <name> weight ...;` and `primary { <name> weight ...; }`.
66fn parse_segment_lists(path: &ConfigLine, keyword: &str) -> Vec<PathSegmentList> {
67    let mut lists = Vec::new();
68    for statement in active_children(path).filter(|c| c.text == keyword || c.arg(keyword).is_some()) {
69        match statement.arg(keyword) {
70            Some(inline) => lists.push(parse_entry(inline, statement)),
71            None => lists.extend(active_children(statement).map(|entry| parse_entry(&entry.text, entry))),
72        }
73    }
74    lists
75}
76
77/// `text` is the name and any options on the same line; `node` supplies the options written as children.
78fn parse_entry(text: &str, node: &ConfigLine) -> PathSegmentList {
79    let mut words = text.split_whitespace();
80    let name = words.next().unwrap_or_default().to_string();
81    let words: Vec<&str> = words
82        .chain(active_children(node).flat_map(|child| child.text.split_whitespace()))
83        .collect();
84    PathSegmentList {
85        name,
86        weight: value_after(&words, "weight"),
87        return_path: value_after(&words, "return-path"),
88        compute: value_after(&words, "compute"),
89    }
90}