Skip to main content

asterust_junos/protocols/bgp/
path_selection.rs

1//! `path-selection`
2//! `protocols { bgp { path-selection { ... } } }`.
3//!
4//! # See also
5//!
6//! - [path-selection](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/path-selection-edit-protocols-bgp.html)
7
8use crate::{arg_of, child_block, statement_words};
9use asterust::config::ConfigLine;
10
11/// The BGP `path-selection` settings, valid only directly under `bgp`.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct PathSelection {
15    pub always_compare_med: bool,
16    pub cisco_non_deterministic: bool,
17    pub external_router_id: bool,
18    pub as_path_ignore: bool,
19    pub l2vpn_use_bgp_rules: bool,
20    /// `med-plus-igp { igp-multiplier ...; }`.
21    pub med_plus_igp: bool,
22    pub igp_multiplier: Option<String>,
23    pub med_multiplier: Option<String>,
24}
25
26pub(super) fn parse_path_selection(line: &ConfigLine) -> Option<PathSelection> {
27    let words = statement_words(line, "path-selection")?;
28    let med_plus_igp = child_block(child_block(line, "path-selection")?, "med-plus-igp");
29    Some(PathSelection {
30        always_compare_med: words.contains(&"always-compare-med"),
31        cisco_non_deterministic: words.contains(&"cisco-non-deterministic"),
32        external_router_id: words.contains(&"external-router-id"),
33        as_path_ignore: words.contains(&"as-path-ignore"),
34        l2vpn_use_bgp_rules: words.contains(&"l2vpn-use-bgp-rules"),
35        med_plus_igp: words.contains(&"med-plus-igp"),
36        igp_multiplier: med_plus_igp
37            .and_then(|block| arg_of(block, "igp-multiplier"))
38            .map(str::to_string),
39        med_multiplier: med_plus_igp
40            .and_then(|block| arg_of(block, "med-multiplier"))
41            .map(str::to_string),
42    })
43}
44
45#[cfg(test)]
46mod tests {
47    use crate::protocols::bgp::bgp;
48    use asterust::config::juniper;
49
50    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
51    const CONFIG: &str = r#"
52protocols {
53    bgp {
54        path-selection {
55            always-compare-med;
56            as-path-ignore;
57            med-plus-igp {
58                igp-multiplier 2;
59                med-multiplier 3;
60            }
61        }
62        multipath-build-priority {
63            low;
64        }
65    }
66}
67"#;
68
69    #[test]
70    fn reads_selection_rules_and_med_plus_igp() {
71        let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
72        let selection = bgp.path_selection.unwrap();
73        assert!(selection.always_compare_med);
74        assert!(selection.as_path_ignore);
75        assert!(!selection.cisco_non_deterministic);
76        assert!(selection.med_plus_igp);
77        assert_eq!(selection.igp_multiplier.as_deref(), Some("2"));
78        assert_eq!(selection.med_multiplier.as_deref(), Some("3"));
79        assert_eq!(bgp.multipath_build_priority.as_deref(), Some("low"));
80    }
81}