Skip to main content

asterust_junos/routing_options/
route_attributes.rs

1//! `active`, `install`, `metric`, `preference`, `tag`, `community` and the other route attributes
2//! `routing-options { static { route <prefix> { ... } } }`; shared with `defaults` and, later, aggregate routes.
3//!
4//! # See also
5//!
6//! - [static (Routing Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-edit-routing-options.html)
7//! - [active](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/active-edit-routing-options.html)
8//! - [install (Routing Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/install-edit-routing-options.html)
9//! - [readvertise](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/readvertise-edit-routing-options.html)
10//! - [resolve](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resolve-edit-routing-options.html)
11//! - [retain](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/retain-edit-routing-options.html)
12//! - [preference (Routing Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/preference-edit-routing-options.html)
13//! - [metric (Aggregate, Generated, or Static Route)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/metric-edit-routing-options.html)
14//! - [tag (Routing Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tag-edit-routing-options.html)
15//! - [community (Routing Options)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/community-edit-routing-options.html)
16
17use crate::{active_children, list_values, split_keyword, unquote};
18use asterust::config::ConfigLine;
19
20/// Returns the statements of a route written as `route <prefix> <statement>;` or as a block: the inline one
21/// first, then each child.
22pub(super) fn route_statements<'a>(inline: &'a str, line: &'a ConfigLine) -> Vec<&'a str> {
23    let mut statements = Vec::new();
24    if !inline.is_empty() {
25        statements.push(inline);
26    }
27    statements.extend(active_children(line).map(|child| child.text.as_str()));
28    statements
29}
30
31/// The attributes a route can carry, from the route itself or from the `defaults` of its block. `as-path`,
32/// `bfd-liveness-detection` and the LSP next-hop statements are not extracted.
33#[derive(Clone, Debug, Default, PartialEq, Eq)]
34#[non_exhaustive]
35pub struct RouteAttributes {
36    /// `Some(true)` for `active`, `Some(false)` for `passive`.
37    pub active: Option<bool>,
38    /// `Some(true)` for `install`, `Some(false)` for `no-install`.
39    pub install: Option<bool>,
40    pub readvertise: Option<bool>,
41    pub resolve: Option<bool>,
42    pub retain: Option<bool>,
43    pub longest_match: Option<bool>,
44    /// The value as written, with any `type` option, e.g. `"5"` or `"5 type 2"`.
45    pub preference: Option<String>,
46    pub preference2: Option<String>,
47    pub metric: Option<String>,
48    pub metric2: Option<String>,
49    pub tag: Option<String>,
50    pub tag2: Option<String>,
51    pub color: Option<String>,
52    pub color2: Option<String>,
53    /// Community IDs and well-known names.
54    pub community: Vec<String>,
55    pub description: Option<String>,
56    /// `high`, `medium` or `low`.
57    pub priority: Option<String>,
58    pub backup_pe_group: Option<String>,
59}
60
61/// Reads the attributes from statements such as `preference 5`, `no-readvertise` or `community [ a b ]`. Statements
62/// that are not attributes are ignored.
63pub(super) fn parse_route_attributes<'a>(statements: impl IntoIterator<Item = &'a str>) -> RouteAttributes {
64    let mut attributes = RouteAttributes::default();
65    for statement in statements {
66        let (keyword, arg) = split_keyword(statement);
67        let value = || Some(arg.to_string());
68        match keyword {
69            "active" => attributes.active = Some(true),
70            "passive" => attributes.active = Some(false),
71            "install" => attributes.install = Some(true),
72            "no-install" => attributes.install = Some(false),
73            "readvertise" => attributes.readvertise = Some(true),
74            "no-readvertise" => attributes.readvertise = Some(false),
75            "resolve" => attributes.resolve = Some(true),
76            "no-resolve" => attributes.resolve = Some(false),
77            "retain" => attributes.retain = Some(true),
78            "no-retain" => attributes.retain = Some(false),
79            "longest-match" => attributes.longest_match = Some(true),
80            "no-longest-match" => attributes.longest_match = Some(false),
81            "preference" => attributes.preference = value(),
82            "preference2" => attributes.preference2 = value(),
83            "metric" => attributes.metric = value(),
84            "metric2" => attributes.metric2 = value(),
85            "tag" => attributes.tag = value(),
86            "tag2" => attributes.tag2 = value(),
87            "color" => attributes.color = value(),
88            "color2" => attributes.color2 = value(),
89            "community" => attributes.community.extend(list_values(arg)),
90            "description" => attributes.description = Some(unquote(arg).to_string()),
91            "priority" => attributes.priority = value(),
92            "backup-pe-group" => attributes.backup_pe_group = value(),
93            _ => {}
94        }
95    }
96    attributes
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
104    #[test]
105    fn reads_flags_pairs_and_values() {
106        let attributes = parse_route_attributes([
107            "passive",
108            "no-install",
109            "readvertise",
110            "no-resolve",
111            "retain",
112            "no-longest-match",
113            "preference 5 type 2",
114            "metric 3",
115            "tag 7",
116            "color 100",
117            "community [ 65000:1 no-export ]",
118            "description \"to branch\"",
119            "priority high",
120        ]);
121        assert_eq!(attributes.active, Some(false));
122        assert_eq!(attributes.install, Some(false));
123        assert_eq!(attributes.readvertise, Some(true));
124        assert_eq!(attributes.resolve, Some(false));
125        assert_eq!(attributes.retain, Some(true));
126        assert_eq!(attributes.longest_match, Some(false));
127        assert_eq!(attributes.preference.as_deref(), Some("5 type 2"));
128        assert_eq!(attributes.metric.as_deref(), Some("3"));
129        assert_eq!(attributes.tag.as_deref(), Some("7"));
130        assert_eq!(attributes.color.as_deref(), Some("100"));
131        assert_eq!(attributes.community, ["65000:1", "no-export"]);
132        assert_eq!(attributes.description.as_deref(), Some("to branch"));
133        assert_eq!(attributes.priority.as_deref(), Some("high"));
134    }
135
136    #[test]
137    fn ignores_statements_that_are_not_attributes() {
138        let attributes = parse_route_attributes(["next-hop 192.0.2.1", "discard"]);
139        assert_eq!(attributes, RouteAttributes::default());
140    }
141}