Skip to main content

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//! - [ipv6-router-id](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ipv6-router-id-edit-routing-options.html)
10//! - [nonstop-routing](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/nonstop-routing-edit-routing-options.html)
11//! - [nsr-phantom-holdtime](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/nsr-phantom-holdtime-edit-routing-options.html)
12//! - [options (Routing Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/options-edit-routing-options.html)
13//! - [static (Routing Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-edit-routing-options.html)
14//! - [aggregate (Routing)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/aggregate-edit-routing-options.html)
15//! - [generate](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/generate-edit-routing-options.html)
16//! - [martians](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/martians-edit-routing-options.html)
17//! - [rib (General)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rib-edit-routing-options.html)
18//! - [instance-import](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/instance-import-edit-routing-options.html)
19//! - [instance-export](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/instance-export-edit-routing-options.html)
20//! - [flex-algorithm](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/flex-algorithm-edit-routing-options.html)
21//! - [definition](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/definition-edit-routing-options-flex-algorithm.html)
22//! - [flex-algorithm (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/flex-algorithm-ospf-edit-routing-options.html)
23//! - [definition (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/definition-ospf-edit-routing-options.html)
24//! - [Configuring Flexible Algorithm for Segment Routing](https://networkingdocs.hpe.com/documentation/us/en/software/junos/is-is/segment-routing/topics/task/isis-configuring-flex-algo-sr.html)
25//! - [Flexible Algorithms in IS-IS for Segment Routing Traffic Engineering](https://networkingdocs.hpe.com/documentation/us/en/software/junos/is-is/topics/topic-map/infocus-isis-flex-algo-sr.html)
26//! - [How to Configure Flexible Algorithms in OSPF for Segment Routing Traffic Engineering](https://networkingdocs.hpe.com/documentation/us/en/software/junos/ospf/topics/topic-map/ospf-flex-algo-sr.html)
27//! - [srv6 (Routing Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/srv6-edit-routing-options-source-packet-routing.html)
28//! - [locator](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/locator-edit-routing-options-source-packet-routing-srv6.html)
29//! - [micro-sid](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/routing-options-source-packet-routing-srv6-locator-micro-sid.html)
30//! - [block](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/routing-options-source-packet-routing-srv6-block.html)
31
32mod aggregate;
33mod auto_export;
34mod autonomous_system;
35mod confederation;
36mod fate_sharing;
37mod flex_algorithm;
38mod forwarding_table;
39mod generate;
40mod graceful_restart;
41mod interface_routes;
42mod martians;
43mod maximum_paths;
44mod resolution;
45mod rib;
46mod rib_groups;
47mod route_attributes;
48mod srv6;
49mod static_routes;
50
51pub use aggregate::{AggregateRoute, AggregateRoutes};
52pub use auto_export::{AutoExport, AutoExportFamily, AutoExportScope};
53pub use autonomous_system::AutonomousSystemOptions;
54pub use confederation::Confederation;
55pub use fate_sharing::{FateSharingEntry, FateSharingGroup};
56pub use flex_algorithm::{FlexAlgorithm, FlexAlgorithmDefinition};
57pub use forwarding_table::ForwardingTable;
58pub use generate::{GenerateRoute, GenerateRoutes};
59pub use graceful_restart::GracefulRestart;
60pub use interface_routes::{InterfaceRoutes, InterfaceRoutesFamily};
61pub use martians::Martian;
62pub use maximum_paths::MaximumPaths;
63pub use resolution::{Resolution, ResolutionRib};
64pub use rib::Rib;
65pub use rib_groups::RibGroup;
66pub use route_attributes::RouteAttributes;
67pub(crate) use srv6::{flavors_of, sid_with_flavors};
68pub use srv6::{Srv6, Srv6Block, Srv6Locator, Srv6MicroSid};
69pub use static_routes::{QualifiedNextHop, StaticRoute, StaticRoutes};
70
71use crate::{arg_of, args_of, child_block, first_word, has_leaf, list_values, statement_words, top_level};
72use aggregate::parse_aggregate_routes;
73use asterust::config::Config;
74use auto_export::parse_auto_export;
75use autonomous_system::parse_autonomous_system_options;
76use confederation::parse_confederation;
77use fate_sharing::parse_fate_sharing;
78use flex_algorithm::parse_flex_algorithms;
79use forwarding_table::parse_forwarding_table;
80use generate::parse_generate_routes;
81use graceful_restart::parse_graceful_restart;
82use interface_routes::parse_interface_routes;
83use martians::parse_martians;
84use maximum_paths::parse_maximum_paths;
85use resolution::parse_resolution;
86use rib::parse_ribs;
87use rib_groups::parse_rib_groups;
88use srv6::parse_srv6;
89use static_routes::parse_static_routes;
90
91/// Global routing options.
92#[derive(Clone, Debug, PartialEq, Eq)]
93#[non_exhaustive]
94pub struct RoutingOptions {
95    pub router_id: Option<String>,
96    pub ipv6_router_id: Option<String>,
97    /// The AS number as written, e.g. `"65000"` or `"1.10"`.
98    pub autonomous_system: Option<String>,
99    /// The options of the `autonomous-system` statement, if it is present.
100    pub autonomous_system_options: Option<AutonomousSystemOptions>,
101    pub confederation: Option<Confederation>,
102    pub graceful_restart: Option<GracefulRestart>,
103    pub nonstop_routing: bool,
104    /// Seconds.
105    pub nsr_phantom_holdtime: Option<String>,
106    pub maximum_paths: Option<MaximumPaths>,
107    pub forwarding_table: Option<ForwardingTable>,
108    pub interface_routes: Option<InterfaceRoutes>,
109    pub resolution: Option<Resolution>,
110    pub static_routes: Option<StaticRoutes>,
111    pub aggregate: Option<AggregateRoutes>,
112    pub generate: Option<GenerateRoutes>,
113    pub martians: Vec<Martian>,
114    pub ribs: Vec<Rib>,
115    pub rib_groups: Vec<RibGroup>,
116    pub instance_import: Vec<String>,
117    pub instance_export: Vec<String>,
118    pub auto_export: Option<AutoExport>,
119    pub fate_sharing: Vec<FateSharingGroup>,
120    pub flex_algorithms: Vec<FlexAlgorithm>,
121    /// `source-packet-routing srv6`.
122    pub srv6: Option<Srv6>,
123    /// The `syslog` setting of `options`, as written, e.g. `"level info"` or `"upto notice"`.
124    pub options_syslog: Option<String>,
125}
126
127/// Returns the top-level `routing-options` block, or `None` if it is absent.
128///
129/// # Examples
130///
131/// ```
132/// use asterust::config::juniper;
133/// use asterust_junos::routing_options::routing_options;
134///
135/// let config = juniper::parse_braces("routing-options {\n    router-id 192.0.2.1;\n    autonomous-system 65000;\n}\n");
136/// let options = routing_options(&config).unwrap();
137/// assert_eq!(options.router_id.as_deref(), Some("192.0.2.1"));
138/// assert_eq!(options.autonomous_system.as_deref(), Some("65000"));
139/// ```
140pub fn routing_options(config: &Config) -> Option<RoutingOptions> {
141    let root = top_level(config, "routing-options")?;
142    Some(RoutingOptions {
143        router_id: arg_of(root, "router-id").map(str::to_string),
144        ipv6_router_id: arg_of(root, "ipv6-router-id").map(str::to_string),
145        autonomous_system: arg_of(root, "autonomous-system")
146            .and_then(first_word)
147            .map(str::to_string),
148        autonomous_system_options: parse_autonomous_system_options(root),
149        confederation: parse_confederation(root),
150        graceful_restart: parse_graceful_restart(root),
151        nonstop_routing: has_leaf(root, "nonstop-routing"),
152        nsr_phantom_holdtime: arg_of(root, "nsr-phantom-holdtime").map(str::to_string),
153        maximum_paths: parse_maximum_paths(root),
154        forwarding_table: child_block(root, "forwarding-table").map(parse_forwarding_table),
155        interface_routes: child_block(root, "interface-routes").map(parse_interface_routes),
156        resolution: child_block(root, "resolution").map(parse_resolution),
157        static_routes: child_block(root, "static").map(parse_static_routes),
158        aggregate: child_block(root, "aggregate").map(parse_aggregate_routes),
159        generate: child_block(root, "generate").map(parse_generate_routes),
160        martians: parse_martians(root),
161        ribs: parse_ribs(root),
162        rib_groups: parse_rib_groups(root),
163        instance_import: args_of(root, "instance-import")
164            .into_iter()
165            .flat_map(list_values)
166            .collect(),
167        instance_export: args_of(root, "instance-export")
168            .into_iter()
169            .flat_map(list_values)
170            .collect(),
171        auto_export: child_block(root, "auto-export").map(parse_auto_export),
172        fate_sharing: child_block(root, "fate-sharing")
173            .map(parse_fate_sharing)
174            .unwrap_or_default(),
175        flex_algorithms: parse_flex_algorithms(root),
176        srv6: parse_srv6(root),
177        options_syslog: statement_words(root, "options").and_then(|words| {
178            let start = words.iter().position(|word| *word == "syslog")?;
179            Some(words[start + 1..].join(" "))
180        }),
181    })
182}
183
184#[cfg(test)]
185mod tests {
186    use super::*;
187    use asterust::config::juniper;
188
189    #[test]
190    fn extracts_router_id_and_asn() {
191        // Written from JunOS hierarchy knowledge, not adapted from documentation; see the README "Sources".
192        let config = juniper::parse_braces(
193            "routing-options {\n    router-id 10.255.0.1;\n    autonomous-system 65000 loops 2;\n}\n",
194        );
195        let options = routing_options(&config).unwrap();
196        assert_eq!(options.router_id.as_deref(), Some("10.255.0.1"));
197        assert_eq!(options.autonomous_system.as_deref(), Some("65000"));
198    }
199
200    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
201    #[test]
202    fn extracts_ipv6_router_id_nsr_and_options_syslog() {
203        let config = juniper::parse_braces(
204            "routing-options {\n    ipv6-router-id 2001:db8::1;\n    nonstop-routing;\n    nsr-phantom-holdtime 60;\n    options {\n        syslog upto notice;\n    }\n}\n",
205        );
206        let options = routing_options(&config).unwrap();
207        assert_eq!(options.ipv6_router_id.as_deref(), Some("2001:db8::1"));
208        assert!(options.nonstop_routing);
209        assert_eq!(options.nsr_phantom_holdtime.as_deref(), Some("60"));
210        assert_eq!(options.options_syslog.as_deref(), Some("upto notice"));
211    }
212
213    #[test]
214    fn global_settings_default_to_off() {
215        let options = routing_options(&juniper::parse_braces(
216            "routing-options {\n    router-id 192.0.2.1;\n}\n",
217        ))
218        .unwrap();
219        assert_eq!(options.ipv6_router_id, None);
220        assert!(!options.nonstop_routing);
221        assert_eq!(options.nsr_phantom_holdtime, None);
222        assert_eq!(options.options_syslog, None);
223    }
224
225    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
226    #[test]
227    fn extracts_instance_import_and_export_lists() {
228        let options = routing_options(&juniper::parse_braces(
229            "routing-options {\n    instance-import [ IMPORT-A IMPORT-B ];\n    instance-export EXPORT-A;\n}\n",
230        ))
231        .unwrap();
232        assert_eq!(options.instance_import, ["IMPORT-A", "IMPORT-B"]);
233        assert_eq!(options.instance_export, ["EXPORT-A"]);
234    }
235
236    #[test]
237    fn missing_block_is_none() {
238        assert!(routing_options(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_none());
239    }
240
241    #[test]
242    fn doc_router_id_and_autonomous_system() {
243        // Adapted from Juniper's BGP User Guide, "BGP Peering Sessions", "Example: Configuring Internal BGP Peer
244        // Sessions", Device A `show routing-options`; see the README "Sources".
245        let config =
246            juniper::parse_braces("routing-options {\n    router-id 192.168.6.5;\n    autonomous-system 17;\n}\n");
247        let options = routing_options(&config).unwrap();
248        assert_eq!(options.router_id.as_deref(), Some("192.168.6.5"));
249        assert_eq!(options.autonomous_system.as_deref(), Some("17"));
250    }
251}