Skip to main content

asterust_junos/protocols/source_packet_routing/
segment_list.rs

1//! `segment-list`
2//! `protocols { source-packet-routing { segment-list <name> { <hop> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [segment-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/segment-list-protocols-source-packet-routing.html)
7//! - [dynamic (Source Packet Routing)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dynamic-edit-protocols-source-packet-routing-segment-list.html)
8
9use crate::{active_children, has_leaf, value_after};
10use asterust::config::ConfigLine;
11
12/// The statements of a segment list that are not hops.
13const OPTIONS: [&str; 5] = ["srv6", "compute", "dynamic", "inherit-label-nexthops", "auto-translate"];
14
15/// A `segment-list <name>`. The contents of `dynamic` are not extracted.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct SegmentList {
19    pub name: String,
20    /// An SRv6-TE segment list, whose hops are `srv6-sid`s.
21    pub srv6: bool,
22    pub compute: bool,
23    pub dynamic: bool,
24    pub inherit_label_nexthops: bool,
25    /// An `auto-translate` block is configured, which allows IP addresses instead of labels.
26    pub auto_translate: bool,
27    pub hops: Vec<SegmentHop>,
28}
29
30/// A hop of a [`SegmentList`], written `<name> srv6-sid <sid>;` or as a block.
31#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct SegmentHop {
34    pub name: String,
35    pub strict: bool,
36    pub loose: bool,
37    pub ip_address: Option<String>,
38    pub label: Option<String>,
39    /// `node`, for a hop that is the address of a node rather than a link.
40    pub label_type: Option<String>,
41    pub srv6_sid: Option<String>,
42}
43
44pub(super) fn parse_segment_list(name: &str, list: &ConfigLine) -> SegmentList {
45    SegmentList {
46        name: name.to_string(),
47        srv6: has_leaf(list, "srv6"),
48        compute: has_leaf(list, "compute"),
49        dynamic: statement_present(list, "dynamic"),
50        inherit_label_nexthops: has_leaf(list, "inherit-label-nexthops"),
51        auto_translate: statement_present(list, "auto-translate"),
52        hops: active_children(list)
53            .filter(|child| {
54                child
55                    .text
56                    .split_whitespace()
57                    .next()
58                    .is_some_and(|first| !OPTIONS.contains(&first))
59            })
60            .map(parse_hop)
61            .collect(),
62    }
63}
64
65fn statement_present(list: &ConfigLine, keyword: &str) -> bool {
66    active_children(list).any(|child| child.text == keyword || child.arg(keyword).is_some())
67}
68
69fn parse_hop(hop: &ConfigLine) -> SegmentHop {
70    let mut words = hop.text.split_whitespace();
71    let name = words.next().unwrap_or_default().to_string();
72    let words: Vec<&str> = words
73        .chain(active_children(hop).flat_map(|child| child.text.split_whitespace()))
74        .collect();
75    SegmentHop {
76        name,
77        strict: words.contains(&"strict"),
78        loose: words.contains(&"loose"),
79        ip_address: value_after(&words, "ip-address"),
80        label: value_after(&words, "label"),
81        label_type: value_after(&words, "label-type"),
82        srv6_sid: value_after(&words, "srv6-sid"),
83    }
84}