use crate::{active_children, arg_of, child_block, has_leaf, split_keyword};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PolicyCondition {
pub name: String,
pub if_route_exists: Option<IfRouteExists>,
pub route_active_on: Option<String>,
pub dynamic_db: bool,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct IfRouteExists {
pub address: Option<String>,
pub table: Option<String>,
pub inet: Option<IfRouteExistsFamily>,
pub ccc: Option<IfRouteExistsFamily>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct IfRouteExistsFamily {
pub address: Option<String>,
pub peer_unit: Option<String>,
pub standby: bool,
pub table: Option<String>,
}
pub(super) fn parse_condition(name: &str, line: &ConfigLine) -> PolicyCondition {
let mut condition = PolicyCondition {
name: name.to_string(),
if_route_exists: None,
route_active_on: arg_of(line, "route-active-on").map(str::to_string),
dynamic_db: has_leaf(line, "dynamic-db"),
};
for child in active_children(line) {
if child.text == "if-route-exists" {
condition.if_route_exists = Some(parse_if_route_exists(child));
} else if let Some(inline) = child.arg("if-route-exists") {
condition.if_route_exists = Some(parse_inline(inline));
}
}
condition
}
fn parse_if_route_exists(line: &ConfigLine) -> IfRouteExists {
let mut exists = IfRouteExists::default();
for child in active_children(line) {
if child.text == "address-family" {
exists.inet = child_block(child, "inet").map(parse_family);
exists.ccc = child_block(child, "ccc").map(parse_family);
} else if let Some(table) = child.arg("table") {
exists.table = Some(table.to_string());
} else if !child.text.contains(' ') {
exists.address = Some(child.text.clone());
} else {
let inline = parse_inline(&child.text);
exists.address = inline.address.or(exists.address);
exists.table = inline.table.or(exists.table);
}
}
exists
}
fn parse_inline(text: &str) -> IfRouteExists {
let (first, rest) = split_keyword(text);
if first == "table" {
return IfRouteExists {
table: Some(rest.to_string()),
..IfRouteExists::default()
};
}
IfRouteExists {
address: Some(first.to_string()),
table: rest.strip_prefix("table").map(|t| t.trim().to_string()),
..IfRouteExists::default()
}
}
fn parse_family(line: &ConfigLine) -> IfRouteExistsFamily {
let mut family = IfRouteExistsFamily::default();
for child in active_children(line) {
if let Some(unit) = child.arg("peer-unit") {
family.peer_unit = Some(unit.to_string());
} else if let Some(table) = child.arg("table") {
family.table = Some(table.to_string());
} else if child.text == "standby" {
family.standby = true;
} else if !child.text.contains(' ') {
family.address = Some(child.text.clone());
}
}
family
}
#[cfg(test)]
mod tests {
use crate::policy_options::{policy_options, PolicyOptions};
use asterust::config::juniper;
fn parse(text: &str) -> PolicyOptions {
policy_options(&juniper::parse_braces(text)).expect("fixture has a policy-options block")
}
#[test]
fn reads_the_guide_condition() {
let options = parse(
"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",
);
assert_eq!(options.conditions.len(), 1);
let condition = &options.conditions[0];
assert_eq!(condition.name, "prefix_11");
let exists = condition.if_route_exists.as_ref().unwrap();
assert_eq!(exists.address.as_deref(), Some("172.16.11.1/32"));
assert_eq!(exists.table.as_deref(), Some("inet.0"));
assert_eq!(options.policy_statements[0].terms[0].from.conditions, ["prefix_11"]);
}
#[test]
fn reads_the_address_families_and_one_line_form() {
let options = parse(
r#"
policy-options {
condition c1 {
if-route-exists {
address-family {
inet {
192.0.2.1/32;
table inet.0;
}
ccc {
ge-0/0/0.0;
peer-unit 5;
standby;
table mpls.0;
}
}
}
route-active-on node1;
dynamic-db;
}
condition c2 {
if-route-exists 192.0.2.2/32 table inet.0;
}
condition c3 {
if-route-exists table inet.0;
}
}
"#,
);
let c1 = &options.conditions[0];
let exists = c1.if_route_exists.as_ref().unwrap();
assert_eq!(exists.address, None);
let inet = exists.inet.as_ref().unwrap();
assert_eq!(inet.address.as_deref(), Some("192.0.2.1/32"));
assert_eq!(inet.table.as_deref(), Some("inet.0"));
let ccc = exists.ccc.as_ref().unwrap();
assert_eq!(ccc.address.as_deref(), Some("ge-0/0/0.0"));
assert_eq!(ccc.peer_unit.as_deref(), Some("5"));
assert!(ccc.standby);
assert_eq!(ccc.table.as_deref(), Some("mpls.0"));
assert_eq!(c1.route_active_on.as_deref(), Some("node1"));
assert!(c1.dynamic_db);
let c2 = options.conditions[1].if_route_exists.as_ref().unwrap();
assert_eq!(c2.address.as_deref(), Some("192.0.2.2/32"));
assert_eq!(c2.table.as_deref(), Some("inet.0"));
let c3 = options.conditions[2].if_route_exists.as_ref().unwrap();
assert_eq!(c3.address, None);
assert_eq!(c3.table.as_deref(), Some("inet.0"));
}
}