asterust_junos/protocols/source_packet_routing/
source_routing_path.rs1use crate::{active_children, arg_of, has_leaf, value_after};
9use asterust::config::ConfigLine;
10
11#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct SourceRoutingPath {
16 pub name: String,
17 pub srv6: bool,
19 pub to: Option<String>,
21 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#[derive(Clone, Debug, PartialEq, Eq)]
36#[non_exhaustive]
37pub struct PathSegmentList {
38 pub name: String,
40 pub weight: Option<String>,
41 pub return_path: Option<String>,
42 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
65fn 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
77fn 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}