Skip to main content

asterust_junos/protocols/bgp/family/
add_path.rs

1//! `add-path`
2//! `protocols { bgp { group <name> { family <afi> { <safi> { add-path { receive; send { ... } } } } } } }`.
3//!
4//! # See also
5//!
6//! - [add-path](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/add-path-edit-protocols-bgp.html)
7//! - [send (add-path)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/send-edit-protocols-bgp.html)
8
9use crate::{args_of, child_block, has_leaf, list_values, statement_words, value_after};
10use asterust::config::ConfigLine;
11
12/// The `add-path` statement of an address family.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct AddPath {
16    pub receive: bool,
17    pub send: Option<AddPathSend>,
18}
19
20/// The `send` block of `add-path`.
21#[derive(Clone, Debug, Default, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct AddPathSend {
24    pub path_count: Option<String>,
25    pub include_backup_path: Option<String>,
26    pub multipath: bool,
27    /// `all-paths` or `equal-cost-paths`.
28    pub path_selection_mode: Option<String>,
29    pub prefix_policies: Vec<String>,
30}
31
32pub(super) fn parse_add_path(node: &ConfigLine) -> Option<AddPath> {
33    let add_path = child_block(node, "add-path")?;
34    let send = child_block(add_path, "send").map(|send| {
35        let words = statement_words(add_path, "send").unwrap_or_default();
36        AddPathSend {
37            path_count: value_after(&words, "path-count"),
38            include_backup_path: value_after(&words, "include-backup-path"),
39            multipath: words.contains(&"multipath"),
40            path_selection_mode: statement_words(send, "path-selection-mode")
41                .and_then(|words| words.first().map(|word| word.to_string())),
42            prefix_policies: args_of(send, "prefix-policy")
43                .into_iter()
44                .flat_map(list_values)
45                .collect(),
46        }
47    });
48    Some(AddPath {
49        receive: has_leaf(add_path, "receive"),
50        send,
51    })
52}
53
54#[cfg(test)]
55mod tests {
56    use crate::protocols::bgp::bgp;
57    use asterust::config::juniper;
58
59    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
60    const CONFIG: &str = r#"
61protocols {
62    bgp {
63        group RR {
64            family inet {
65                unicast {
66                    add-path {
67                        receive;
68                        send {
69                            path-count 4;
70                            include-backup-path 2;
71                            path-selection-mode {
72                                equal-cost-paths;
73                            }
74                            prefix-policy [ ADD-PATH-A ADD-PATH-B ];
75                        }
76                    }
77                }
78            }
79            neighbor 10.0.0.2 {
80                family inet {
81                    unicast {
82                        add-path {
83                            send {
84                                multipath;
85                            }
86                        }
87                    }
88                }
89            }
90        }
91    }
92}
93"#;
94
95    #[test]
96    fn reads_receive_and_the_send_block() {
97        let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
98        let add_path = bgp.groups[0].options.address_families[0].add_path.as_ref().unwrap();
99        assert!(add_path.receive);
100        let send = add_path.send.as_ref().unwrap();
101        assert_eq!(send.path_count.as_deref(), Some("4"));
102        assert_eq!(send.include_backup_path.as_deref(), Some("2"));
103        assert_eq!(send.path_selection_mode.as_deref(), Some("equal-cost-paths"));
104        assert_eq!(send.prefix_policies, ["ADD-PATH-A", "ADD-PATH-B"]);
105        assert!(!send.multipath);
106    }
107
108    #[test]
109    fn reads_a_send_only_neighbor() {
110        let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
111        let add_path = bgp.groups[0].neighbors[0].options.address_families[0]
112            .add_path
113            .as_ref()
114            .unwrap();
115        assert!(!add_path.receive);
116        assert!(add_path.send.as_ref().unwrap().multipath);
117    }
118}