use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct OspfSourcePacketRouting {
pub disable: bool,
pub explicit_null: bool,
pub ldp_stitching: bool,
pub sensor_based_stats: bool,
pub strict_asla_based_flex_algorithm: bool,
pub node_segment_ipv4_index: Option<String>,
pub node_segment_index_range: Option<String>,
pub srgb_start_label: Option<String>,
pub srgb_index_range: Option<String>,
pub adjacency_segment_hold_time: Option<String>,
pub prefix_segment: Option<String>,
pub flex_algorithms: Vec<String>,
}
pub(super) fn parse_source_packet_routing(ospf: &ConfigLine) -> Option<OspfSourcePacketRouting> {
let spr = child_block(ospf, "source-packet-routing")?;
let node = statement_words(spr, "node-segment");
let srgb = statement_words(spr, "srgb");
let adjacency = statement_words(spr, "adjacency-segment");
Some(OspfSourcePacketRouting {
disable: has_leaf(spr, "disable"),
explicit_null: has_leaf(spr, "explicit-null"),
ldp_stitching: has_leaf(spr, "ldp-stitching"),
sensor_based_stats: has_leaf(spr, "sensor-based-stats"),
strict_asla_based_flex_algorithm: has_leaf(spr, "strict-asla-based-flex-algorithm"),
node_segment_ipv4_index: node.as_ref().and_then(|words| value_after(words, "ipv4-index")),
node_segment_index_range: node.as_ref().and_then(|words| value_after(words, "index-range")),
srgb_start_label: srgb.as_ref().and_then(|words| value_after(words, "start-label")),
srgb_index_range: srgb.as_ref().and_then(|words| value_after(words, "index-range")),
adjacency_segment_hold_time: adjacency.and_then(|words| value_after(&words, "hold-time")),
prefix_segment: arg_of(spr, "prefix-segment").map(str::to_string),
flex_algorithms: args_of(spr, "flex-algorithm")
.into_iter()
.flat_map(list_values)
.collect(),
})
}
#[cfg(test)]
mod tests {
use crate::protocols::ospf::ospf;
use asterust::config::juniper;
const CONFIG: &str = r#"
protocols {
ospf {
source-packet-routing {
prefix-segment prefix-sid;
node-segment ipv4-index 0;
srgb start-label 800000 index-range 80000;
}
}
}
"#;
const BLOCKS: &str = r#"
protocols {
ospf {
source-packet-routing {
explicit-null;
ldp-stitching;
adjacency-segment {
hold-time 200000;
}
node-segment {
ipv4-index 7;
index-range 1000;
}
srgb {
start-label 16000;
index-range 8000;
}
}
}
}
"#;
#[test]
fn reads_the_one_line_forms_of_the_guide_example() {
let spr = ospf(&juniper::parse_braces(CONFIG))
.unwrap()
.source_packet_routing
.unwrap();
assert_eq!(spr.prefix_segment.as_deref(), Some("prefix-sid"));
assert_eq!(spr.node_segment_ipv4_index.as_deref(), Some("0"));
assert_eq!(spr.srgb_start_label.as_deref(), Some("800000"));
assert_eq!(spr.srgb_index_range.as_deref(), Some("80000"));
assert!(!spr.disable);
}
#[test]
fn reads_the_block_forms() {
let spr = ospf(&juniper::parse_braces(BLOCKS))
.unwrap()
.source_packet_routing
.unwrap();
assert!(spr.explicit_null);
assert!(spr.ldp_stitching);
assert_eq!(spr.adjacency_segment_hold_time.as_deref(), Some("200000"));
assert_eq!(spr.node_segment_ipv4_index.as_deref(), Some("7"));
assert_eq!(spr.node_segment_index_range.as_deref(), Some("1000"));
assert_eq!(spr.srgb_start_label.as_deref(), Some("16000"));
assert_eq!(spr.srgb_index_range.as_deref(), Some("8000"));
assert_eq!(spr.prefix_segment, None);
assert!(spr.flex_algorithms.is_empty());
}
const FLEX: &str = r#"
protocols {
ospf {
source-packet-routing {
prefix-segment prefix-sid;
srgb start-label 80000 index-range 5000;
flex-algorithm [ 128 129 ];
flex-algorithm 130;
flex-algorithm 131 {
color 1310;
}
}
}
}
"#;
#[test]
fn reads_the_flex_algorithm_participation() {
let spr = ospf(&juniper::parse_braces(FLEX))
.unwrap()
.source_packet_routing
.unwrap();
assert_eq!(spr.flex_algorithms, ["128", "129", "130", "131"]);
assert_eq!(spr.srgb_start_label.as_deref(), Some("80000"));
}
}