Skip to main content

asterust_junos/routing_options/
aggregate.rs

1//! `aggregate`
2//! `routing-options { aggregate { route <prefix> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [aggregate (Routing)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/aggregate-edit-routing-options.html)
7//! - [policy (Aggregate and Generated Routes)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/policy-edit-routing-options.html)
8//! - [brief](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/brief-edit-routing-options.html)
9//! - [full](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/full-edit-routing-options.html)
10//! - [discard (routing-options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/discard-edit-routing-options.html)
11
12use super::route_attributes::{parse_route_attributes, route_statements, RouteAttributes};
13use crate::{active_children, child_block, split_keyword, split_name, unquote};
14use asterust::config::ConfigLine;
15
16/// An aggregate `route <prefix>`. `as-path` is not extracted.
17#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct AggregateRoute {
20    pub prefix: String,
21    /// The policy that selects the contributing routes.
22    pub policy: Option<String>,
23    pub brief: bool,
24    pub full: bool,
25    pub discard: bool,
26    pub next_table: Option<String>,
27    pub algorithm: Option<String>,
28    pub attributes: RouteAttributes,
29}
30
31/// The `aggregate` routes of a routing table.
32#[derive(Clone, Debug, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct AggregateRoutes {
35    /// The `defaults` attributes; `policy`, `brief`, `full` and `discard` inside `defaults` are not extracted.
36    pub defaults: Option<RouteAttributes>,
37    pub routes: Vec<AggregateRoute>,
38}
39
40pub(super) fn parse_aggregate_routes(aggregate: &ConfigLine) -> AggregateRoutes {
41    AggregateRoutes {
42        defaults: child_block(aggregate, "defaults")
43            .map(|defaults| parse_route_attributes(active_children(defaults).map(|child| child.text.as_str()))),
44        routes: active_children(aggregate)
45            .filter_map(|line| Some(parse_route(line.arg("route")?, line)))
46            .collect(),
47    }
48}
49
50fn parse_route(arg: &str, line: &ConfigLine) -> AggregateRoute {
51    let (prefix, inline) = split_name(arg);
52    let statements = route_statements(inline, line);
53    let mut route = AggregateRoute {
54        prefix: prefix.to_string(),
55        policy: None,
56        brief: false,
57        full: false,
58        discard: false,
59        next_table: None,
60        algorithm: None,
61        attributes: RouteAttributes::default(),
62    };
63    for statement in &statements {
64        let (keyword, arg) = split_keyword(statement);
65        match keyword {
66            "policy" => route.policy = Some(unquote(arg).to_string()),
67            "brief" => route.brief = true,
68            "full" => route.full = true,
69            "discard" => route.discard = true,
70            "next-table" => route.next_table = Some(unquote(arg).to_string()),
71            "algorithm" => route.algorithm = Some(arg.to_string()),
72            _ => {}
73        }
74    }
75    route.attributes = parse_route_attributes(statements);
76    route
77}
78
79#[cfg(test)]
80mod tests {
81    use crate::routing_options::routing_options;
82    use asterust::config::juniper;
83
84    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
85    const CONFIG: &str = r#"
86routing-options {
87    aggregate {
88        defaults {
89            preference 130;
90            community no-export;
91        }
92        route 10.0.0.0/8 {
93            policy CONTRIBUTORS;
94            brief;
95            discard;
96            metric 5;
97            tag 9;
98            passive;
99            description "summary";
100        }
101        route 192.0.2.0/24 discard;
102        route 198.51.100.0/24 next-table other.inet.0;
103        route 2001:db8::/32 {
104            algorithm 1;
105            full;
106        }
107        inactive: route 203.0.113.0/24 discard;
108    }
109}
110"#;
111
112    fn aggregate() -> crate::routing_options::AggregateRoutes {
113        routing_options(&juniper::parse_braces(CONFIG))
114            .unwrap()
115            .aggregate
116            .unwrap()
117    }
118
119    #[test]
120    fn extracts_defaults_and_block_route() {
121        let aggregate = aggregate();
122        let defaults = aggregate.defaults.unwrap();
123        assert_eq!(defaults.preference.as_deref(), Some("130"));
124        assert_eq!(defaults.community, ["no-export"]);
125
126        assert_eq!(aggregate.routes.len(), 4);
127        let route = &aggregate.routes[0];
128        assert_eq!(route.prefix, "10.0.0.0/8");
129        assert_eq!(route.policy.as_deref(), Some("CONTRIBUTORS"));
130        assert!(route.brief);
131        assert!(!route.full);
132        assert!(route.discard);
133        assert_eq!(route.attributes.metric.as_deref(), Some("5"));
134        assert_eq!(route.attributes.tag.as_deref(), Some("9"));
135        assert_eq!(route.attributes.active, Some(false));
136        assert_eq!(route.attributes.description.as_deref(), Some("summary"));
137    }
138
139    #[test]
140    fn extracts_one_line_routes_next_table_and_algorithm() {
141        let aggregate = aggregate();
142        assert!(aggregate.routes[1].discard);
143        assert_eq!(aggregate.routes[2].next_table.as_deref(), Some("other.inet.0"));
144        assert_eq!(aggregate.routes[3].algorithm.as_deref(), Some("1"));
145        assert!(aggregate.routes[3].full);
146    }
147
148    #[test]
149    fn no_aggregate_is_none() {
150        let options = routing_options(&juniper::parse_braces(
151            "routing-options {\n    router-id 192.0.2.1;\n}\n",
152        ))
153        .unwrap();
154        assert!(options.aggregate.is_none());
155    }
156}