asterust_junos/protocols/bgp/peer_options/
multipath.rs1use crate::{args_of, child_block, has_leaf, list_values, statement_words, value_after};
10use asterust::config::ConfigLine;
11
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct BgpMultipath {
16 pub disable: bool,
18 pub multiple_as: bool,
20 pub allow_external_internal: bool,
21 pub allow_protection: bool,
22 pub list_nexthop: bool,
23 pub starving_age: Option<String>,
25 pub prioritization: bool,
27 pub prioritization_high_priority_inline: bool,
28 pub prioritization_policies: Vec<String>,
30}
31
32pub(super) fn parse_multipath(line: &ConfigLine) -> Option<BgpMultipath> {
33 let words = statement_words(line, "multipath")?;
34 let prioritization = child_block(line, "multipath").and_then(|multipath| child_block(multipath, "prioritization"));
35 Some(BgpMultipath {
36 disable: words.contains(&"disable"),
37 multiple_as: words.contains(&"multiple-as"),
38 allow_external_internal: words.contains(&"allow-external-internal"),
39 allow_protection: words.contains(&"allow-protection"),
40 list_nexthop: words.contains(&"list-nexthop"),
41 starving_age: value_after(&words, "starving-age"),
42 prioritization: prioritization.is_some(),
43 prioritization_high_priority_inline: prioritization
44 .is_some_and(|block| has_leaf(block, "high-priority-inline")),
45 prioritization_policies: prioritization
46 .map(|block| args_of(block, "policy").into_iter().flat_map(list_values).collect())
47 .unwrap_or_default(),
48 })
49}
50
51#[cfg(test)]
52mod tests {
53 use crate::protocols::bgp::bgp;
54 use asterust::config::juniper;
55
56 const CONFIG: &str = r#"
58protocols {
59 bgp {
60 multipath;
61 group EBGP {
62 multipath {
63 multiple-as;
64 starving-age 30;
65 }
66 neighbor 192.0.2.2 {
67 multipath {
68 disable;
69 }
70 }
71 }
72 }
73}
74"#;
75
76 #[test]
77 fn reads_the_bare_and_block_forms_at_every_level() {
78 let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
79 assert_eq!(bgp.options.multipath.as_ref().map(|m| m.multiple_as), Some(false));
80
81 let group = bgp.groups[0].options.multipath.as_ref().unwrap();
82 assert!(group.multiple_as);
83 assert_eq!(group.starving_age.as_deref(), Some("30"));
84 assert!(!group.disable);
85
86 let neighbor = bgp.groups[0].neighbors[0].options.multipath.as_ref().unwrap();
87 assert!(neighbor.disable);
88 }
89
90 #[test]
91 fn is_none_when_absent() {
92 let config = juniper::parse_braces(
93 "protocols {\n bgp {\n group G {\n type external;\n }\n }\n}\n",
94 );
95 assert_eq!(bgp(&config).unwrap().groups[0].options.multipath, None);
96 }
97
98 const PRIORITIZATION: &str = r#"
100protocols {
101 bgp {
102 multipath {
103 prioritization {
104 high-priority-inline;
105 policy [ CRITICAL-PREFIXES OTHER ];
106 }
107 }
108 }
109}
110"#;
111
112 #[test]
113 fn reads_the_prioritization_block() {
114 let bgp = bgp(&juniper::parse_braces(PRIORITIZATION)).unwrap();
115 let multipath = bgp.options.multipath.unwrap();
116 assert!(multipath.prioritization);
117 assert!(multipath.prioritization_high_priority_inline);
118 assert_eq!(multipath.prioritization_policies, ["CRITICAL-PREFIXES", "OTHER"]);
119 }
120}