Skip to main content

asterust_junos/
routing_options.rs

1//! `routing-options { ... }`.
2
3use crate::{arg_of, first_word, top_level};
4use asterust::config::Config;
5
6/// Global routing options.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct RoutingOptions {
10    pub router_id: Option<String>,
11    pub autonomous_system: Option<String>,
12}
13
14/// The top-level `routing-options` block, or `None` if it is absent.
15pub fn routing_options(config: &Config) -> Option<RoutingOptions> {
16    let root = top_level(config, "routing-options")?;
17    Some(RoutingOptions {
18        router_id: arg_of(root, "router-id").map(str::to_string),
19        autonomous_system: arg_of(root, "autonomous-system")
20            .and_then(first_word)
21            .map(str::to_string),
22    })
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use asterust::config::juniper;
29
30    #[test]
31    fn extracts_router_id_and_asn() {
32        // Written from JunOS hierarchy knowledge, not adapted from documentation; see the README "Sources".
33        let config = juniper::parse_braces(
34            "routing-options {\n    router-id 10.255.0.1;\n    autonomous-system 65000 loops 2;\n}\n",
35        );
36        let options = routing_options(&config).unwrap();
37        assert_eq!(options.router_id.as_deref(), Some("10.255.0.1"));
38        assert_eq!(options.autonomous_system.as_deref(), Some("65000"));
39    }
40
41    #[test]
42    fn missing_block_is_none() {
43        assert!(routing_options(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_none());
44    }
45
46    #[test]
47    fn doc_router_id_and_autonomous_system() {
48        // Adapted from Juniper's BGP User Guide, "BGP Peering Sessions", "Example: Configuring Internal BGP Peer
49        // Sessions", Device A `show routing-options`; see the README "Sources".
50        let config =
51            juniper::parse_braces("routing-options {\n    router-id 192.168.6.5;\n    autonomous-system 17;\n}\n");
52        let options = routing_options(&config).unwrap();
53        assert_eq!(options.router_id.as_deref(), Some("192.168.6.5"));
54        assert_eq!(options.autonomous_system.as_deref(), Some("17"));
55    }
56}