asterust_junos/policy_options/
condition.rs1use crate::{active_children, arg_of, child_block, has_leaf, split_keyword};
10use asterust::config::ConfigLine;
11
12#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct PolicyCondition {
16 pub name: String,
17 pub if_route_exists: Option<IfRouteExists>,
18 pub route_active_on: Option<String>,
20 pub dynamic_db: bool,
21}
22
23#[derive(Clone, Debug, Default, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct IfRouteExists {
27 pub address: Option<String>,
28 pub table: Option<String>,
29 pub inet: Option<IfRouteExistsFamily>,
31 pub ccc: Option<IfRouteExistsFamily>,
33}
34
35#[derive(Clone, Debug, Default, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct IfRouteExistsFamily {
39 pub address: Option<String>,
41 pub peer_unit: Option<String>,
42 pub standby: bool,
43 pub table: Option<String>,
44}
45
46pub(super) fn parse_condition(name: &str, line: &ConfigLine) -> PolicyCondition {
47 let mut condition = PolicyCondition {
48 name: name.to_string(),
49 if_route_exists: None,
50 route_active_on: arg_of(line, "route-active-on").map(str::to_string),
51 dynamic_db: has_leaf(line, "dynamic-db"),
52 };
53 for child in active_children(line) {
54 if child.text == "if-route-exists" {
55 condition.if_route_exists = Some(parse_if_route_exists(child));
56 } else if let Some(inline) = child.arg("if-route-exists") {
57 condition.if_route_exists = Some(parse_inline(inline));
58 }
59 }
60 condition
61}
62
63fn parse_if_route_exists(line: &ConfigLine) -> IfRouteExists {
65 let mut exists = IfRouteExists::default();
66 for child in active_children(line) {
67 if child.text == "address-family" {
68 exists.inet = child_block(child, "inet").map(parse_family);
69 exists.ccc = child_block(child, "ccc").map(parse_family);
70 } else if let Some(table) = child.arg("table") {
71 exists.table = Some(table.to_string());
72 } else if !child.text.contains(' ') {
73 exists.address = Some(child.text.clone());
74 } else {
75 let inline = parse_inline(&child.text);
76 exists.address = inline.address.or(exists.address);
77 exists.table = inline.table.or(exists.table);
78 }
79 }
80 exists
81}
82
83fn parse_inline(text: &str) -> IfRouteExists {
85 let (first, rest) = split_keyword(text);
86 if first == "table" {
87 return IfRouteExists {
88 table: Some(rest.to_string()),
89 ..IfRouteExists::default()
90 };
91 }
92 IfRouteExists {
93 address: Some(first.to_string()),
94 table: rest.strip_prefix("table").map(|t| t.trim().to_string()),
95 ..IfRouteExists::default()
96 }
97}
98
99fn parse_family(line: &ConfigLine) -> IfRouteExistsFamily {
100 let mut family = IfRouteExistsFamily::default();
101 for child in active_children(line) {
102 if let Some(unit) = child.arg("peer-unit") {
103 family.peer_unit = Some(unit.to_string());
104 } else if let Some(table) = child.arg("table") {
105 family.table = Some(table.to_string());
106 } else if child.text == "standby" {
107 family.standby = true;
108 } else if !child.text.contains(' ') {
109 family.address = Some(child.text.clone());
110 }
111 }
112 family
113}
114
115#[cfg(test)]
116mod tests {
117 use crate::policy_options::{policy_options, PolicyOptions};
118 use asterust::config::juniper;
119
120 fn parse(text: &str) -> PolicyOptions {
121 policy_options(&juniper::parse_braces(text)).expect("fixture has a policy-options block")
122 }
123
124 #[test]
127 fn reads_the_guide_condition() {
128 let options = parse(
129 "policy-options {\n policy-statement conditional-export-bgp {\n term conditional-default {\n from {\n route-filter 0.0.0.0/0 exact;\n condition prefix_11;\n }\n then accept;\n }\n }\n condition prefix_11 {\n if-route-exists {\n 172.16.11.1/32;\n table inet.0;\n }\n }\n}\n",
130 );
131 assert_eq!(options.conditions.len(), 1);
132 let condition = &options.conditions[0];
133 assert_eq!(condition.name, "prefix_11");
134 let exists = condition.if_route_exists.as_ref().unwrap();
135 assert_eq!(exists.address.as_deref(), Some("172.16.11.1/32"));
136 assert_eq!(exists.table.as_deref(), Some("inet.0"));
137 assert_eq!(options.policy_statements[0].terms[0].from.conditions, ["prefix_11"]);
138 }
139
140 #[test]
143 fn reads_the_address_families_and_one_line_form() {
144 let options = parse(
145 r#"
146policy-options {
147 condition c1 {
148 if-route-exists {
149 address-family {
150 inet {
151 192.0.2.1/32;
152 table inet.0;
153 }
154 ccc {
155 ge-0/0/0.0;
156 peer-unit 5;
157 standby;
158 table mpls.0;
159 }
160 }
161 }
162 route-active-on node1;
163 dynamic-db;
164 }
165 condition c2 {
166 if-route-exists 192.0.2.2/32 table inet.0;
167 }
168 condition c3 {
169 if-route-exists table inet.0;
170 }
171}
172"#,
173 );
174 let c1 = &options.conditions[0];
175 let exists = c1.if_route_exists.as_ref().unwrap();
176 assert_eq!(exists.address, None);
177 let inet = exists.inet.as_ref().unwrap();
178 assert_eq!(inet.address.as_deref(), Some("192.0.2.1/32"));
179 assert_eq!(inet.table.as_deref(), Some("inet.0"));
180 let ccc = exists.ccc.as_ref().unwrap();
181 assert_eq!(ccc.address.as_deref(), Some("ge-0/0/0.0"));
182 assert_eq!(ccc.peer_unit.as_deref(), Some("5"));
183 assert!(ccc.standby);
184 assert_eq!(ccc.table.as_deref(), Some("mpls.0"));
185 assert_eq!(c1.route_active_on.as_deref(), Some("node1"));
186 assert!(c1.dynamic_db);
187
188 let c2 = options.conditions[1].if_route_exists.as_ref().unwrap();
189 assert_eq!(c2.address.as_deref(), Some("192.0.2.2/32"));
190 assert_eq!(c2.table.as_deref(), Some("inet.0"));
191 let c3 = options.conditions[2].if_route_exists.as_ref().unwrap();
192 assert_eq!(c3.address, None);
193 assert_eq!(c3.table.as_deref(), Some("inet.0"));
194 }
195}