Skip to main content

asterust_junos/routing_options/
autonomous_system.rs

1//! `autonomous-system`
2//! `routing-options { autonomous-system <as> [asdot-notation] [loops <n>] [independent-domain]; }`.
3//!
4//! # See also
5//!
6//! - [autonomous-system](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/autonomous-system-edit-routing-options.html)
7//! - [independent-domain](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/independent-domain-edit-routing-options.html)
8
9use crate::{statement_words, value_after};
10use asterust::config::ConfigLine;
11
12/// The options of the `autonomous-system` statement. The AS number itself is
13/// [`RoutingOptions::autonomous_system`](super::RoutingOptions::autonomous_system).
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct AutonomousSystemOptions {
17    /// How many times the local AS may appear in a received AS path.
18    pub loops: Option<String>,
19    /// Displays 4-byte AS numbers as `high.low`.
20    pub asdot_notation: bool,
21    pub independent_domain: bool,
22    /// The `no-attrset` option of `independent-domain`.
23    pub no_attrset: bool,
24}
25
26/// Reads the options written inline (`autonomous-system 65000 loops 2;`) or as child statements.
27pub(super) fn parse_autonomous_system_options(root: &ConfigLine) -> Option<AutonomousSystemOptions> {
28    let words = statement_words(root, "autonomous-system")?;
29    Some(AutonomousSystemOptions {
30        loops: value_after(&words, "loops"),
31        asdot_notation: words.contains(&"asdot-notation"),
32        independent_domain: words.contains(&"independent-domain"),
33        no_attrset: words.contains(&"no-attrset"),
34    })
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::routing_options::routing_options;
40    use asterust::config::juniper;
41
42    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
43    #[test]
44    fn extracts_inline_options() {
45        let options = routing_options(&juniper::parse_braces(
46            "routing-options {\n    autonomous-system 65000 asdot-notation loops 2 independent-domain no-attrset;\n}\n",
47        ))
48        .unwrap();
49        assert_eq!(options.autonomous_system.as_deref(), Some("65000"));
50        let settings = options.autonomous_system_options.unwrap();
51        assert_eq!(settings.loops.as_deref(), Some("2"));
52        assert!(settings.asdot_notation);
53        assert!(settings.independent_domain);
54        assert!(settings.no_attrset);
55    }
56
57    #[test]
58    fn extracts_block_form() {
59        let options = routing_options(&juniper::parse_braces(
60            "routing-options {\n    autonomous-system 65000 {\n        loops 3;\n        independent-domain;\n    }\n}\n",
61        ))
62        .unwrap();
63        let settings = options.autonomous_system_options.unwrap();
64        assert_eq!(settings.loops.as_deref(), Some("3"));
65        assert!(settings.independent_domain);
66        assert!(!settings.no_attrset);
67    }
68
69    #[test]
70    fn plain_autonomous_system_has_no_options() {
71        let options = routing_options(&juniper::parse_braces(
72            "routing-options {\n    autonomous-system 17;\n}\n",
73        ))
74        .unwrap();
75        let settings = options.autonomous_system_options.unwrap();
76        assert_eq!(settings.loops, None);
77        assert!(!settings.asdot_notation);
78        assert!(!settings.independent_domain);
79    }
80
81    #[test]
82    fn asdot_number_is_kept_as_written() {
83        let options = routing_options(&juniper::parse_braces(
84            "routing-options {\n    autonomous-system 1.10;\n}\n",
85        ))
86        .unwrap();
87        assert_eq!(options.autonomous_system.as_deref(), Some("1.10"));
88    }
89
90    #[test]
91    fn no_autonomous_system_has_no_options() {
92        let options = routing_options(&juniper::parse_braces(
93            "routing-options {\n    router-id 192.0.2.1;\n}\n",
94        ))
95        .unwrap();
96        assert!(options.autonomous_system_options.is_none());
97    }
98}