Skip to main content

asterust_junos/policy_options/policy_statement/
term.rs

1//! `term`
2//! `policy-options { policy-statement <name> { term <name> { from { ... } to { ... } then { ... } } } }`.
3//!
4//! A policy written without `term` statements is a single unnamed term. Each term carries `from`,
5//! `to` and `then` statements.
6//!
7//! # See also
8//!
9//! - [policy-statement](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/policy-statement-edit-policy-options.html)
10//! - [route-filter](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/route-filter-edit-policy-options.html)
11
12use crate::{list_values, split_keyword, split_name, statements, unquote};
13use asterust::config::ConfigLine;
14
15/// A `route-filter <prefix> <match-type>` condition.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct RouteFilter {
19    pub prefix: String,
20    /// Everything after the prefix, e.g. `"exact"`, `"orlonger"`, `"upto /24"`.
21    pub match_type: Option<String>,
22}
23
24/// The match conditions of a term's `from` or `to` statement. Conditions not listed here are
25/// ignored.
26#[derive(Clone, Debug, Default, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct PolicyConditions {
29    pub protocols: Vec<String>,
30    pub families: Vec<String>,
31    pub communities: Vec<String>,
32    pub as_paths: Vec<String>,
33    pub prefix_lists: Vec<String>,
34    /// The raw `prefix-list-filter` argument, e.g. `"CUSTOMERS orlonger"`.
35    pub prefix_list_filters: Vec<String>,
36    pub route_filters: Vec<RouteFilter>,
37    pub neighbors: Vec<String>,
38    /// Subroutine policies from `policy`.
39    pub policies: Vec<String>,
40}
41
42/// The flow-control action ending a term.
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44#[non_exhaustive]
45pub enum PolicyAction {
46    Accept,
47    Reject,
48    NextTerm,
49    NextPolicy,
50}
51
52/// How a `then community` action changes the route's communities.
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54#[non_exhaustive]
55pub enum CommunityOperation {
56    Add,
57    Set,
58    Delete,
59}
60
61/// A `then community (add | set | delete) <name>` action.
62#[derive(Clone, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct CommunityAction {
65    pub operation: CommunityOperation,
66    pub community: String,
67}
68
69/// The actions of a term's `then` statement. Actions not listed here are ignored.
70#[derive(Clone, Debug, Default, PartialEq, Eq)]
71#[non_exhaustive]
72pub struct PolicyActions {
73    pub action: Option<PolicyAction>,
74    /// Everything after `metric`, e.g. `"63"` or `"add 10"`.
75    pub metric: Option<String>,
76    pub local_preference: Option<String>,
77    pub next_hop: Option<String>,
78    pub as_path_prepend: Option<String>,
79    pub communities: Vec<CommunityAction>,
80}
81
82/// One term of a policy statement.
83#[derive(Clone, Debug, PartialEq, Eq)]
84#[non_exhaustive]
85pub struct PolicyTerm {
86    /// `None` for the unnamed term, which is the only one in a policy written without `term`
87    /// statements.
88    pub name: Option<String>,
89    pub from: PolicyConditions,
90    pub to: PolicyConditions,
91    pub then: PolicyActions,
92}
93
94pub(super) fn parse_term(name: Option<&str>, line: &ConfigLine) -> PolicyTerm {
95    PolicyTerm {
96        name: name.map(str::to_string),
97        from: parse_conditions(line, "from"),
98        to: parse_conditions(line, "to"),
99        then: parse_actions(line),
100    }
101}
102
103fn parse_conditions(line: &ConfigLine, keyword: &str) -> PolicyConditions {
104    let mut conditions = PolicyConditions::default();
105    for statement in statements(line, keyword) {
106        let (condition, arg) = split_keyword(statement);
107        match condition {
108            "protocol" => conditions.protocols.extend(list_values(arg)),
109            "family" => conditions.families.push(unquote(arg).to_string()),
110            "community" => conditions.communities.extend(list_values(arg)),
111            "as-path" => conditions.as_paths.extend(list_values(arg)),
112            "prefix-list" => conditions.prefix_lists.extend(list_values(arg)),
113            "prefix-list-filter" => conditions.prefix_list_filters.push(arg.to_string()),
114            "route-filter" => conditions.route_filters.push(parse_route_filter(arg)),
115            "neighbor" => conditions.neighbors.push(unquote(arg).to_string()),
116            "policy" => conditions.policies.push(unquote(arg).to_string()),
117            _ => {}
118        }
119    }
120    conditions
121}
122
123fn parse_route_filter(arg: &str) -> RouteFilter {
124    let (prefix, match_type) = split_keyword(arg);
125    RouteFilter {
126        prefix: prefix.to_string(),
127        match_type: Some(match_type).filter(|m| !m.is_empty()).map(str::to_string),
128    }
129}
130
131fn parse_actions(line: &ConfigLine) -> PolicyActions {
132    let mut actions = PolicyActions::default();
133    for statement in statements(line, "then") {
134        let (action, arg) = split_keyword(statement);
135        let value = || Some(unquote(arg).to_string());
136        match (action, arg) {
137            ("accept", "") => actions.action = Some(PolicyAction::Accept),
138            ("reject", "") => actions.action = Some(PolicyAction::Reject),
139            ("next", "term") => actions.action = Some(PolicyAction::NextTerm),
140            ("next", "policy") => actions.action = Some(PolicyAction::NextPolicy),
141            ("metric", _) => actions.metric = value(),
142            ("local-preference", _) => actions.local_preference = value(),
143            ("next-hop", _) => actions.next_hop = value(),
144            ("as-path-prepend", _) => actions.as_path_prepend = value(),
145            ("community", _) => actions.communities.extend(parse_community_action(arg)),
146            _ => {}
147        }
148    }
149    actions
150}
151
152fn parse_community_action(arg: &str) -> Option<CommunityAction> {
153    let (operation, community) = split_name(arg);
154    let operation = match operation {
155        "add" => CommunityOperation::Add,
156        "set" => CommunityOperation::Set,
157        "delete" => CommunityOperation::Delete,
158        _ => return None,
159    };
160    Some(CommunityAction {
161        operation,
162        community: unquote(community).to_string(),
163    })
164}