Skip to main content

asterust_junos/routing_options/
confederation.rs

1//! `confederation`
2//! `routing-options { confederation <as> members [ <as> ... ]; }`.
3//!
4//! # See also
5//!
6//! - [confederation](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/confederation-edit-routing-options.html)
7
8use crate::{arg_of, list_values, split_name};
9use asterust::config::ConfigLine;
10
11/// A BGP confederation: the confederation AS and its member ASes.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct Confederation {
15    pub autonomous_system: String,
16    pub members: Vec<String>,
17}
18
19pub(super) fn parse_confederation(root: &ConfigLine) -> Option<Confederation> {
20    let (autonomous_system, rest) = split_name(arg_of(root, "confederation")?);
21    Some(Confederation {
22        autonomous_system: autonomous_system.to_string(),
23        members: rest.strip_prefix("members").map(list_values).unwrap_or_default(),
24    })
25}
26
27#[cfg(test)]
28mod tests {
29    use crate::routing_options::routing_options;
30    use asterust::config::juniper;
31
32    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
33    #[test]
34    fn extracts_confederation_and_members() {
35        let options = routing_options(&juniper::parse_braces(
36            "routing-options {\n    confederation 65001 members [ 65002 65003 ];\n}\n",
37        ))
38        .unwrap();
39        let confederation = options.confederation.unwrap();
40        assert_eq!(confederation.autonomous_system, "65001");
41        assert_eq!(confederation.members, ["65002", "65003"]);
42    }
43
44    #[test]
45    fn single_member_needs_no_brackets() {
46        let options = routing_options(&juniper::parse_braces(
47            "routing-options {\n    confederation 65001 members 65002;\n}\n",
48        ))
49        .unwrap();
50        assert_eq!(options.confederation.unwrap().members, ["65002"]);
51    }
52
53    #[test]
54    fn no_confederation_is_none() {
55        let options = routing_options(&juniper::parse_braces(
56            "routing-options {\n    router-id 192.0.2.1;\n}\n",
57        ))
58        .unwrap();
59        assert!(options.confederation.is_none());
60    }
61}