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