use crate::{arg_of, child_block, has_leaf, statement_words, value_after};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct BgpBfd {
pub minimum_interval: Option<String>,
pub minimum_receive_interval: Option<String>,
pub multiplier: Option<String>,
pub version: Option<String>,
pub session_mode: Option<String>,
pub pdu_size: Option<String>,
pub holddown_interval: Option<String>,
pub no_adaptation: bool,
pub detection_time_threshold: Option<String>,
pub transmit_interval_minimum: Option<String>,
pub transmit_interval_threshold: Option<String>,
pub authentication_algorithm: Option<String>,
pub authentication_key_chain: Option<String>,
pub authentication_loose_check: bool,
pub strict_mode: bool,
pub strict_mode_wait_timeout: Option<String>,
}
pub(super) fn parse_bfd(line: &ConfigLine) -> Option<BgpBfd> {
let bfd = child_block(line, "bfd-liveness-detection")?;
let strict = statement_words(bfd, "strict-bfd").or_else(|| statement_words(bfd, "strict-mode"));
let field = |keyword: &str| arg_of(bfd, keyword).map(str::to_string);
let nested = |block: &str, keyword: &str| {
child_block(bfd, block)
.and_then(|block| arg_of(block, keyword))
.map(str::to_string)
};
Some(BgpBfd {
minimum_interval: field("minimum-interval"),
minimum_receive_interval: field("minimum-receive-interval"),
multiplier: field("multiplier"),
version: field("version"),
session_mode: field("session-mode"),
pdu_size: field("pdu-size"),
holddown_interval: field("holddown-interval"),
no_adaptation: has_leaf(bfd, "no-adaptation"),
detection_time_threshold: nested("detection-time", "threshold"),
transmit_interval_minimum: nested("transmit-interval", "minimum-interval"),
transmit_interval_threshold: nested("transmit-interval", "threshold"),
authentication_algorithm: nested("authentication", "algorithm"),
authentication_key_chain: nested("authentication", "key-chain"),
authentication_loose_check: child_block(bfd, "authentication")
.is_some_and(|auth| has_leaf(auth, "loose-check")),
strict_mode: strict.is_some(),
strict_mode_wait_timeout: strict.and_then(|words| {
value_after(&words, "bfd-up-wait-interval").or_else(|| value_after(&words, "bfd-wait-timeout"))
}),
})
}
#[cfg(test)]
mod tests {
use crate::protocols::bgp::bgp;
use asterust::config::juniper;
const CONFIG: &str = r#"
protocols {
bgp {
group EBGP {
bfd-liveness-detection {
minimum-interval 300;
multiplier 3;
session-mode single-hop;
no-adaptation;
detection-time {
threshold 900;
}
transmit-interval {
minimum-interval 100;
threshold 500;
}
authentication {
algorithm keyed-sha-1;
key-chain BFD-KEYS;
loose-check;
}
}
neighbor 192.0.2.2 {
bfd-liveness-detection {
minimum-interval 50;
}
}
neighbor 192.0.2.3;
}
}
}
"#;
#[test]
fn reads_the_session_and_its_nested_blocks() {
let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
let group = bgp.groups[0].options.bfd_liveness_detection.as_ref().unwrap();
assert_eq!(group.minimum_interval.as_deref(), Some("300"));
assert_eq!(group.multiplier.as_deref(), Some("3"));
assert_eq!(group.session_mode.as_deref(), Some("single-hop"));
assert!(group.no_adaptation);
assert_eq!(group.detection_time_threshold.as_deref(), Some("900"));
assert_eq!(group.transmit_interval_minimum.as_deref(), Some("100"));
assert_eq!(group.transmit_interval_threshold.as_deref(), Some("500"));
assert_eq!(group.authentication_algorithm.as_deref(), Some("keyed-sha-1"));
assert_eq!(group.authentication_key_chain.as_deref(), Some("BFD-KEYS"));
assert!(group.authentication_loose_check);
}
#[test]
fn reads_the_neighbor_level_and_leaves_others_unset() {
let bgp = bgp(&juniper::parse_braces(CONFIG)).unwrap();
let neighbors = &bgp.groups[0].neighbors;
let bfd = neighbors[0].options.bfd_liveness_detection.as_ref().unwrap();
assert_eq!(bfd.minimum_interval.as_deref(), Some("50"));
assert!(!bfd.no_adaptation);
assert_eq!(bfd.authentication_algorithm, None);
assert_eq!(neighbors[1].options.bfd_liveness_detection, None);
}
const STRICT: &str = r#"
protocols {
bgp {
group EBGP {
neighbor 192.0.2.2 {
bfd-liveness-detection {
strict-bfd {
bfd-up-wait-interval 30;
}
}
}
neighbor 192.0.2.3 {
bfd-liveness-detection {
strict-mode bfd-wait-timeout 20;
}
}
neighbor 192.0.2.4 {
bfd-liveness-detection {
strict-mode;
}
}
}
}
}
"#;
#[test]
fn reads_strict_mode_in_both_spellings() {
let bgp = bgp(&juniper::parse_braces(STRICT)).unwrap();
let bfd = |index: usize| {
bgp.groups[0].neighbors[index]
.options
.bfd_liveness_detection
.clone()
.unwrap()
};
assert!(bfd(0).strict_mode);
assert_eq!(bfd(0).strict_mode_wait_timeout.as_deref(), Some("30"));
assert!(bfd(1).strict_mode);
assert_eq!(bfd(1).strict_mode_wait_timeout.as_deref(), Some("20"));
assert!(bfd(2).strict_mode);
assert_eq!(bfd(2).strict_mode_wait_timeout, None);
}
}