asterust_junos/policy_options/
mod.rs1mod as_path;
4mod as_path_group;
5mod community;
6mod policy_statement;
7mod prefix_list;
8
9pub use as_path::AsPath;
10pub use as_path_group::AsPathGroup;
11pub use community::Community;
12pub use policy_statement::{
13 CommunityAction, CommunityOperation, PolicyAction, PolicyActions, PolicyConditions, PolicyStatement, PolicyTerm,
14 RouteFilter,
15};
16pub use prefix_list::PrefixList;
17
18use crate::{active_children, named_children, top_level};
19use as_path::parse_as_path;
20use as_path_group::parse_as_path_group;
21use asterust::config::Config;
22use community::parse_community;
23use policy_statement::parse_policy;
24use prefix_list::parse_prefix_list;
25
26#[derive(Clone, Debug, PartialEq, Eq)]
28#[non_exhaustive]
29pub struct PolicyOptions {
30 pub policy_statements: Vec<PolicyStatement>,
31 pub communities: Vec<Community>,
32 pub prefix_lists: Vec<PrefixList>,
33 pub as_paths: Vec<AsPath>,
34 pub as_path_groups: Vec<AsPathGroup>,
35}
36
37pub fn policy_options(config: &Config) -> Option<PolicyOptions> {
39 let root = top_level(config, "policy-options")?;
40 Some(PolicyOptions {
41 policy_statements: named_children(root, "policy-statement")
42 .map(|(name, line)| parse_policy(name, line))
43 .collect(),
44 communities: active_children(root).filter_map(parse_community).collect(),
45 prefix_lists: named_children(root, "prefix-list")
46 .map(|(name, line)| parse_prefix_list(name, line))
47 .collect(),
48 as_paths: active_children(root).filter_map(parse_as_path).collect(),
49 as_path_groups: named_children(root, "as-path-group")
50 .map(|(name, line)| parse_as_path_group(name, line))
51 .collect(),
52 })
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58 use asterust::config::juniper;
59
60 fn parse(text: &str) -> PolicyOptions {
61 policy_options(&juniper::parse_braces(text)).expect("fixture has a policy-options block")
62 }
63
64 const DOC_COMMUNITY_TAG: &str = r#"
66policy-options {
67 policy-statement Edu-to-isis {
68 term 1 {
69 from {
70 protocol bgp;
71 community Edu;
72 }
73 then {
74 metric 63;
75 accept;
76 }
77 }
78 }
79 policy-statement send-isis-and-direct {
80 term 1 {
81 from {
82 protocol [ isis direct ];
83 route-filter 10.0.0.0/16 orlonger;
84 route-filter 192.168.0.0/16 orlonger;
85 }
86 then accept;
87 }
88 }
89 community Edu members 2:5;
90}
91"#;
92
93 #[test]
94 fn doc_term_with_from_block_and_metric() {
95 let options = parse(DOC_COMMUNITY_TAG);
96 assert_eq!(options.policy_statements.len(), 2);
97
98 let policy = &options.policy_statements[0];
99 assert_eq!(policy.name, "Edu-to-isis");
100 let term = &policy.terms[0];
101 assert_eq!(term.name.as_deref(), Some("1"));
102 assert_eq!(term.from.protocols, ["bgp"]);
103 assert_eq!(term.from.communities, ["Edu"]);
104 assert_eq!(term.then.metric.as_deref(), Some("63"));
105 assert_eq!(term.then.action, Some(PolicyAction::Accept));
106 }
107
108 #[test]
109 fn doc_protocol_list_route_filters_and_leaf_then() {
110 let options = parse(DOC_COMMUNITY_TAG);
111 let term = &options.policy_statements[1].terms[0];
112 assert_eq!(term.from.protocols, ["isis", "direct"]);
113 assert_eq!(term.from.route_filters.len(), 2);
114 assert_eq!(term.from.route_filters[0].prefix, "10.0.0.0/16");
115 assert_eq!(term.from.route_filters[0].match_type.as_deref(), Some("orlonger"));
116 assert_eq!(term.then.action, Some(PolicyAction::Accept));
117 }
118
119 #[test]
120 fn doc_one_line_community() {
121 let options = parse(DOC_COMMUNITY_TAG);
122 assert_eq!(options.communities.len(), 1);
123 assert_eq!(options.communities[0].name, "Edu");
124 assert_eq!(options.communities[0].members, ["2:5"]);
125 assert!(!options.communities[0].invert_match);
126 }
127
128 const DOC_COMMUNITY_ACTIONS: &str = r#"
131policy-options {
132 policy-statement statics {
133 from protocol static;
134 then {
135 community add 1;
136 accept;
137 }
138 }
139 policy-statement remove-communities {
140 term 1 {
141 from protocol bgp;
142 then {
143 community delete wild;
144 accept;
145 }
146 }
147 term 2 {
148 then reject;
149 }
150 }
151 community wild members *:*;
152 community 1 members [ 2:1 2:2 2:3 ];
153}
154"#;
155
156 #[test]
157 fn doc_policy_without_terms_has_one_unnamed_term() {
158 let options = parse(DOC_COMMUNITY_ACTIONS);
159 let statics = &options.policy_statements[0];
160 assert_eq!(statics.terms.len(), 1);
161 let term = &statics.terms[0];
162 assert_eq!(term.name, None);
163 assert_eq!(term.from.protocols, ["static"]);
164 assert_eq!(
165 term.then.communities,
166 [CommunityAction {
167 operation: CommunityOperation::Add,
168 community: "1".to_string()
169 }]
170 );
171 assert_eq!(term.then.action, Some(PolicyAction::Accept));
172 }
173
174 #[test]
175 fn doc_community_delete_and_final_reject_term() {
176 let options = parse(DOC_COMMUNITY_ACTIONS);
177 let remove = &options.policy_statements[1];
178 assert_eq!(remove.terms.len(), 2);
179 assert_eq!(
180 remove.terms[0].then.communities[0].operation,
181 CommunityOperation::Delete
182 );
183 assert_eq!(remove.terms[0].then.communities[0].community, "wild");
184 assert_eq!(remove.terms[1].name.as_deref(), Some("2"));
185 assert_eq!(remove.terms[1].then.action, Some(PolicyAction::Reject));
186 }
187
188 #[test]
189 fn doc_community_members_list() {
190 let options = parse(DOC_COMMUNITY_ACTIONS);
191 assert_eq!(options.communities[0].members, ["*:*"]);
192 assert_eq!(options.communities[1].name, "1");
193 assert_eq!(options.communities[1].members, ["2:1", "2:2", "2:3"]);
194 }
195
196 const CONFIG: &str = r#"
198policy-options {
199 prefix-list CUSTOMERS {
200 192.0.2.0/24;
201 2001:db8::/32;
202 }
203 prefix-list DYNAMIC {
204 apply-path "protocols bgp group <*> neighbor <*>";
205 }
206 as-path FROM-AS65001 ".* 65001";
207 as-path-group TRANSIT {
208 as-path TIER1 "65100 .*";
209 as-path TIER2 "65200 .*";
210 }
211 community BLOCK {
212 invert-match;
213 members [ no-export 65000:666 ];
214 }
215 community WILD members "* : *";
216 policy-statement IMPORT {
217 term FROM-CUSTOMER {
218 from {
219 family inet;
220 neighbor 192.0.2.1;
221 prefix-list CUSTOMERS;
222 prefix-list-filter CUSTOMERS orlonger;
223 as-path FROM-AS65001;
224 route-filter 10.0.0.0/8 upto /24;
225 policy SUBROUTINE;
226 }
227 then {
228 local-preference 200;
229 next-hop self;
230 as-path-prepend "65000 65000";
231 next policy;
232 }
233 }
234 inactive: term DISABLED {
235 then reject;
236 }
237 term SKIP {
238 from protocol ospf;
239 then next term;
240 }
241 then reject;
242 }
243}
244"#;
245
246 #[test]
247 fn extracts_prefix_lists_and_as_paths() {
248 let options = parse(CONFIG);
249 assert_eq!(options.prefix_lists[0].name, "CUSTOMERS");
250 assert_eq!(options.prefix_lists[0].prefixes, ["192.0.2.0/24", "2001:db8::/32"]);
251 assert_eq!(options.prefix_lists[0].apply_path, None);
252 assert!(options.prefix_lists[1].prefixes.is_empty());
253 assert_eq!(
254 options.prefix_lists[1].apply_path.as_deref(),
255 Some("protocols bgp group <*> neighbor <*>")
256 );
257
258 assert_eq!(options.as_paths.len(), 1);
259 assert_eq!(options.as_paths[0].name, "FROM-AS65001");
260 assert_eq!(options.as_paths[0].regex, ".* 65001");
261 let group = &options.as_path_groups[0];
262 assert_eq!(group.name, "TRANSIT");
263 let names: Vec<_> = group.as_paths.iter().map(|p| p.name.as_str()).collect();
264 assert_eq!(names, ["TIER1", "TIER2"]);
265 assert_eq!(group.as_paths[1].regex, "65200 .*");
266 }
267
268 #[test]
269 fn extracts_block_and_quoted_communities() {
270 let options = parse(CONFIG);
271 assert_eq!(options.communities.len(), 2);
272 assert_eq!(options.communities[0].name, "BLOCK");
273 assert_eq!(options.communities[0].members, ["no-export", "65000:666"]);
274 assert!(options.communities[0].invert_match);
275 assert_eq!(options.communities[1].members, ["* : *"]);
276 }
277
278 #[test]
279 fn extracts_conditions_and_manipulation_actions() {
280 let options = parse(CONFIG);
281 let term = &options.policy_statements[0].terms[0];
282 assert_eq!(term.name.as_deref(), Some("FROM-CUSTOMER"));
283 assert_eq!(term.from.families, ["inet"]);
284 assert_eq!(term.from.neighbors, ["192.0.2.1"]);
285 assert_eq!(term.from.prefix_lists, ["CUSTOMERS"]);
286 assert_eq!(term.from.prefix_list_filters, ["CUSTOMERS orlonger"]);
287 assert_eq!(term.from.as_paths, ["FROM-AS65001"]);
288 assert_eq!(term.from.route_filters[0].match_type.as_deref(), Some("upto /24"));
289 assert_eq!(term.from.policies, ["SUBROUTINE"]);
290 assert_eq!(term.then.local_preference.as_deref(), Some("200"));
291 assert_eq!(term.then.next_hop.as_deref(), Some("self"));
292 assert_eq!(term.then.as_path_prepend.as_deref(), Some("65000 65000"));
293 assert_eq!(term.then.action, Some(PolicyAction::NextPolicy));
294 }
295
296 #[test]
297 fn skips_inactive_terms_and_keeps_unnamed_final_term_last() {
298 let options = parse(CONFIG);
299 let terms = &options.policy_statements[0].terms;
300 let names: Vec<_> = terms.iter().map(|t| t.name.as_deref()).collect();
301 assert_eq!(names, [Some("FROM-CUSTOMER"), Some("SKIP"), None]);
302 assert_eq!(terms[1].then.action, Some(PolicyAction::NextTerm));
303 assert_eq!(terms[2].then.action, Some(PolicyAction::Reject));
304 }
305
306 #[test]
307 fn no_policy_options_is_none() {
308 assert!(policy_options(&juniper::parse_braces("system {\n host-name r1;\n}\n")).is_none());
309 }
310}