asterust_junos/protocols/ospf/
source_packet_routing.rs1use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, value_after};
12use asterust::config::ConfigLine;
13
14#[derive(Clone, Debug, Default, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct OspfSourcePacketRouting {
18 pub disable: bool,
19 pub explicit_null: bool,
20 pub ldp_stitching: bool,
21 pub sensor_based_stats: bool,
22 pub strict_asla_based_flex_algorithm: bool,
23 pub node_segment_ipv4_index: Option<String>,
25 pub node_segment_index_range: Option<String>,
27 pub srgb_start_label: Option<String>,
29 pub srgb_index_range: Option<String>,
31 pub adjacency_segment_hold_time: Option<String>,
33 pub prefix_segment: Option<String>,
35 pub flex_algorithms: Vec<String>,
37}
38
39pub(super) fn parse_source_packet_routing(ospf: &ConfigLine) -> Option<OspfSourcePacketRouting> {
40 let spr = child_block(ospf, "source-packet-routing")?;
41 let node = statement_words(spr, "node-segment");
42 let srgb = statement_words(spr, "srgb");
43 let adjacency = statement_words(spr, "adjacency-segment");
44 Some(OspfSourcePacketRouting {
45 disable: has_leaf(spr, "disable"),
46 explicit_null: has_leaf(spr, "explicit-null"),
47 ldp_stitching: has_leaf(spr, "ldp-stitching"),
48 sensor_based_stats: has_leaf(spr, "sensor-based-stats"),
49 strict_asla_based_flex_algorithm: has_leaf(spr, "strict-asla-based-flex-algorithm"),
50 node_segment_ipv4_index: node.as_ref().and_then(|words| value_after(words, "ipv4-index")),
51 node_segment_index_range: node.as_ref().and_then(|words| value_after(words, "index-range")),
52 srgb_start_label: srgb.as_ref().and_then(|words| value_after(words, "start-label")),
53 srgb_index_range: srgb.as_ref().and_then(|words| value_after(words, "index-range")),
54 adjacency_segment_hold_time: adjacency.and_then(|words| value_after(&words, "hold-time")),
55 prefix_segment: arg_of(spr, "prefix-segment").map(str::to_string),
56 flex_algorithms: args_of(spr, "flex-algorithm")
57 .into_iter()
58 .flat_map(list_values)
59 .collect(),
60 })
61}
62
63#[cfg(test)]
64mod tests {
65 use crate::protocols::ospf::ospf;
66 use asterust::config::juniper;
67
68 const CONFIG: &str = r#"
72protocols {
73 ospf {
74 source-packet-routing {
75 prefix-segment prefix-sid;
76 node-segment ipv4-index 0;
77 srgb start-label 800000 index-range 80000;
78 }
79 }
80}
81"#;
82
83 const BLOCKS: &str = r#"
84protocols {
85 ospf {
86 source-packet-routing {
87 explicit-null;
88 ldp-stitching;
89 adjacency-segment {
90 hold-time 200000;
91 }
92 node-segment {
93 ipv4-index 7;
94 index-range 1000;
95 }
96 srgb {
97 start-label 16000;
98 index-range 8000;
99 }
100 }
101 }
102}
103"#;
104
105 #[test]
106 fn reads_the_one_line_forms_of_the_guide_example() {
107 let spr = ospf(&juniper::parse_braces(CONFIG))
108 .unwrap()
109 .source_packet_routing
110 .unwrap();
111 assert_eq!(spr.prefix_segment.as_deref(), Some("prefix-sid"));
112 assert_eq!(spr.node_segment_ipv4_index.as_deref(), Some("0"));
113 assert_eq!(spr.srgb_start_label.as_deref(), Some("800000"));
114 assert_eq!(spr.srgb_index_range.as_deref(), Some("80000"));
115 assert!(!spr.disable);
116 }
117
118 #[test]
119 fn reads_the_block_forms() {
120 let spr = ospf(&juniper::parse_braces(BLOCKS))
121 .unwrap()
122 .source_packet_routing
123 .unwrap();
124 assert!(spr.explicit_null);
125 assert!(spr.ldp_stitching);
126 assert_eq!(spr.adjacency_segment_hold_time.as_deref(), Some("200000"));
127 assert_eq!(spr.node_segment_ipv4_index.as_deref(), Some("7"));
128 assert_eq!(spr.node_segment_index_range.as_deref(), Some("1000"));
129 assert_eq!(spr.srgb_start_label.as_deref(), Some("16000"));
130 assert_eq!(spr.srgb_index_range.as_deref(), Some("8000"));
131 assert_eq!(spr.prefix_segment, None);
132 assert!(spr.flex_algorithms.is_empty());
133 }
134
135 const FLEX: &str = r#"
139protocols {
140 ospf {
141 source-packet-routing {
142 prefix-segment prefix-sid;
143 srgb start-label 80000 index-range 5000;
144 flex-algorithm [ 128 129 ];
145 flex-algorithm 130;
146 flex-algorithm 131 {
147 color 1310;
148 }
149 }
150 }
151}
152"#;
153
154 #[test]
155 fn reads_the_flex_algorithm_participation() {
156 let spr = ospf(&juniper::parse_braces(FLEX))
157 .unwrap()
158 .source_packet_routing
159 .unwrap();
160 assert_eq!(spr.flex_algorithms, ["128", "129", "130", "131"]);
161 assert_eq!(spr.srgb_start_label.as_deref(), Some("80000"));
162 }
163}