Skip to main content

asterust_junos/policy_options/policy_statement/
term.rs

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