Skip to main content

asterust_junos/routing_options/
rib.rs

1//! `rib`
2//! `routing-options { rib <table> { ... } }`.
3//!
4//! # See also
5//!
6//! - [rib (General)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rib-edit-routing-options.html)
7
8use super::aggregate::{parse_aggregate_routes, AggregateRoutes};
9use super::generate::{parse_generate_routes, GenerateRoutes};
10use super::martians::{parse_martians, Martian};
11use super::static_routes::{parse_static_routes, StaticRoutes};
12use crate::{child_block, has_leaf, named_children};
13use asterust::config::ConfigLine;
14
15/// A `rib <table>`: the routes and martians that belong to one routing table.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct Rib {
19    /// The table name, e.g. `"inet6.0"` or `"VRF.inet.0"`.
20    pub name: String,
21    pub static_routes: Option<StaticRoutes>,
22    pub aggregate: Option<AggregateRoutes>,
23    pub generate: Option<GenerateRoutes>,
24    pub martians: Vec<Martian>,
25    pub policy_multipath: bool,
26}
27
28pub(super) fn parse_ribs(root: &ConfigLine) -> Vec<Rib> {
29    named_children(root, "rib")
30        .map(|(name, rib)| Rib {
31            name: name.to_string(),
32            static_routes: child_block(rib, "static").map(parse_static_routes),
33            aggregate: child_block(rib, "aggregate").map(parse_aggregate_routes),
34            generate: child_block(rib, "generate").map(parse_generate_routes),
35            martians: parse_martians(rib),
36            policy_multipath: has_leaf(rib, "policy-multipath"),
37        })
38        .collect()
39}
40
41#[cfg(test)]
42mod tests {
43    use crate::routing_options::routing_options;
44    use asterust::config::juniper;
45
46    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
47    #[test]
48    fn extracts_routes_martians_and_flags_per_table() {
49        let options = routing_options(&juniper::parse_braces(
50            r#"
51routing-options {
52    rib inet6.0 {
53        static {
54            route ::/0 next-hop 2001:db8::1;
55        }
56        aggregate {
57            route 2001:db8::/32 discard;
58        }
59        martians {
60            ::1/128 exact;
61        }
62        policy-multipath;
63    }
64    rib CUSTOMER.inet.0 {
65        generate {
66            route 0.0.0.0/0 policy DEFAULT-ROUTE;
67        }
68    }
69}
70"#,
71        ))
72        .unwrap();
73        assert_eq!(options.ribs.len(), 2);
74
75        let v6 = &options.ribs[0];
76        assert_eq!(v6.name, "inet6.0");
77        assert_eq!(v6.static_routes.as_ref().unwrap().routes[0].next_hops, ["2001:db8::1"]);
78        assert_eq!(v6.aggregate.as_ref().unwrap().routes[0].prefix, "2001:db8::/32");
79        assert_eq!(v6.martians[0].prefix, "::1/128");
80        assert!(v6.policy_multipath);
81        assert!(v6.generate.is_none());
82
83        let customer = &options.ribs[1];
84        assert_eq!(customer.name, "CUSTOMER.inet.0");
85        assert_eq!(
86            customer.generate.as_ref().unwrap().routes[0].policy.as_deref(),
87            Some("DEFAULT-ROUTE")
88        );
89        assert!(customer.static_routes.is_none());
90        assert!(!customer.policy_multipath);
91    }
92
93    #[test]
94    fn no_ribs_is_empty() {
95        let options = routing_options(&juniper::parse_braces(
96            "routing-options {\n    router-id 192.0.2.1;\n}\n",
97        ))
98        .unwrap();
99        assert!(options.ribs.is_empty());
100    }
101}