mod interface;
mod session;
pub use interface::{LdpInterface, LdpLinkProtection};
pub use session::LdpSession;
use super::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{
active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children, statement_words, toggle,
top_level, value_after,
};
use asterust::config::Config;
use interface::parse_interface;
use session::parse_session;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpGracefulRestart {
pub disable: bool,
pub helper_disable: bool,
pub maximum_neighbor_recovery_time: Option<String>,
pub reconnect_time: Option<String>,
pub recovery_time: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpDualTransport {
pub inet_lsr_id: Option<String>,
pub inet6_lsr_id: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpTargetedHello {
pub hello_interval: Option<String>,
pub hold_time: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpAutoTargetedSession {
pub maximum_sessions: Option<String>,
pub teardown_delay: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LdpMakeBeforeBreak {
pub timeout: Option<String>,
pub switchover_delay: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Ldp {
pub disable: bool,
pub interfaces: Vec<LdpInterface>,
pub sessions: Vec<LdpSession>,
pub session_groups: Vec<LdpSession>,
pub keepalive_interval: Option<String>,
pub keepalive_timeout: Option<String>,
pub preference: Option<String>,
pub families: Vec<String>,
pub dual_transport: Option<LdpDualTransport>,
pub transport_address: Option<String>,
pub transport_preference: Option<String>,
pub deaggregate: Option<bool>,
pub mtu_discovery: Option<bool>,
pub explicit_null: bool,
pub no_forwarding: bool,
pub track_igp_metric: bool,
pub l2_smart_policy: bool,
pub strict_targeted_hellos: bool,
pub upstream_label_assignment: bool,
pub p2mp: bool,
pub p2mp_no_rsvp_tunneling: bool,
pub longest_match: Option<Vec<String>>,
pub export_policies: Vec<String>,
pub import_policies: Vec<String>,
pub egress_policies: Vec<String>,
pub dod_request_policy: Option<String>,
pub igp_synchronization_holddown_interval: Option<String>,
pub graceful_restart: Option<LdpGracefulRestart>,
pub session_protection: Option<Option<String>>,
pub targeted_hello: Option<LdpTargetedHello>,
pub auto_targeted_session: Option<LdpAutoTargetedSession>,
pub make_before_break: Option<LdpMakeBeforeBreak>,
pub label_withdrawal_delay: Option<String>,
pub log_updown_trap_disabled: bool,
pub traceoptions: Option<Traceoptions>,
}
pub fn ldp(config: &Config) -> Option<Ldp> {
let ldp = child_block(top_level(config, "protocols")?, "ldp")?;
let field = |keyword: &str| arg_of(ldp, keyword).map(str::to_string);
let policies = |keyword: &str| -> Vec<String> { args_of(ldp, keyword).into_iter().flat_map(list_values).collect() };
let p2mp = child_block(ldp, "p2mp");
Some(Ldp {
disable: has_leaf(ldp, "disable"),
interfaces: named_children(ldp, "interface")
.map(|(name, interface)| parse_interface(name, interface))
.collect(),
sessions: named_children(ldp, "session")
.map(|(name, session)| parse_session(name, session))
.collect(),
session_groups: named_children(ldp, "session-group")
.map(|(name, group)| parse_session(name, group))
.collect(),
keepalive_interval: field("keepalive-interval"),
keepalive_timeout: field("keepalive-timeout"),
preference: field("preference"),
families: child_block(ldp, "family")
.map(|family| active_children(family).map(|c| c.text.clone()).collect())
.unwrap_or_default(),
dual_transport: child_block(ldp, "dual-transport").map(|block| LdpDualTransport {
inet_lsr_id: arg_of(block, "inet-lsr-id").map(str::to_string),
inet6_lsr_id: arg_of(block, "inet6-lsr-id").map(str::to_string),
}),
transport_address: field("transport-address"),
transport_preference: field("transport-preference"),
deaggregate: toggle(ldp, "deaggregate"),
mtu_discovery: toggle(ldp, "mtu-discovery"),
explicit_null: has_leaf(ldp, "explicit-null") || arg_of(ldp, "explicit-null").is_some(),
no_forwarding: has_leaf(ldp, "no-forwarding"),
track_igp_metric: has_leaf(ldp, "track-igp-metric"),
l2_smart_policy: has_leaf(ldp, "l2-smart-policy"),
strict_targeted_hellos: has_leaf(ldp, "strict-targeted-hellos"),
upstream_label_assignment: has_leaf(ldp, "upstream-label-assignment"),
p2mp: p2mp.is_some(),
p2mp_no_rsvp_tunneling: p2mp.is_some_and(|block| has_leaf(block, "no-rsvp-tunneling")),
longest_match: child_block(ldp, "longest-match").map(|block| {
let policy = child_block(block, "policy");
policy
.map(|p| active_children(p).flat_map(|c| list_values(&c.text)).collect())
.unwrap_or_else(|| args_of(block, "policy").into_iter().flat_map(list_values).collect())
}),
export_policies: policies("export"),
import_policies: policies("import"),
egress_policies: policies("egress-policy"),
dod_request_policy: field("dod-request-policy"),
igp_synchronization_holddown_interval: statement_words(ldp, "igp-synchronization")
.and_then(|words| value_after(&words, "holddown-interval")),
graceful_restart: child_block(ldp, "graceful-restart").map(|block| LdpGracefulRestart {
disable: has_leaf(block, "disable"),
helper_disable: has_leaf(block, "helper-disable"),
maximum_neighbor_recovery_time: arg_of(block, "maximum-neighbor-recovery-time").map(str::to_string),
reconnect_time: arg_of(block, "reconnect-time").map(str::to_string),
recovery_time: arg_of(block, "recovery-time").map(str::to_string),
}),
session_protection: statement_words(ldp, "session-protection").map(|words| value_after(&words, "timeout")),
targeted_hello: child_block(ldp, "targeted-hello").map(|block| LdpTargetedHello {
hello_interval: arg_of(block, "hello-interval").map(str::to_string),
hold_time: arg_of(block, "hold-time").map(str::to_string),
}),
auto_targeted_session: child_block(ldp, "auto-targeted-session").map(|block| LdpAutoTargetedSession {
maximum_sessions: arg_of(block, "maximum-sessions").map(str::to_string),
teardown_delay: arg_of(block, "teardown-delay").map(str::to_string),
}),
make_before_break: child_block(ldp, "make-before-break").map(|block| LdpMakeBeforeBreak {
timeout: arg_of(block, "timeout").map(str::to_string),
switchover_delay: arg_of(block, "switchover-delay").map(str::to_string),
}),
label_withdrawal_delay: field("label-withdrawal-delay"),
log_updown_trap_disabled: statement_words(ldp, "log-updown").is_some_and(|words| words.contains(&"disable")),
traceoptions: parse_traceoptions(ldp),
})
}
#[cfg(test)]
mod tests {
use super::*;
use asterust::config::juniper;
const DOC_LINK_PROTECTION: &str = r#"
protocols {
ldp {
interface all {
link-protection {
dynamic-rsvp-lsp;
}
}
interface fxp0.0 {
disable;
}
p2mp;
}
}
"#;
const DOC_IPV6: &str = r#"
protocols {
ldp {
deaggregate;
interface ge-1/0/0.0;
interface lo0.0;
family {
inet6;
inet;
}
transport-preference ipv4;
}
}
"#;
const DOC_DUAL_TRANSPORT: &str = r#"
protocols {
ldp {
deaggregate;
interface ge-1/0/0.0;
interface lo0.0;
family {
inet6;
inet;
}
dual-transport {
inet-lsr-id 10.255.0.1;
inet6-lsr-id 10.1.1.1;
}
}
}
"#;
const DOC_DOWNSTREAM_ON_DEMAND: &str = r#"
protocols {
ldp {
dod-request-policy DOD-Request-Loopbacks;
session 172.16.1.1 {
downstream-on-demand;
}
}
}
"#;
const DOC_LONGEST_MATCH: &str = r#"
protocols {
ldp {
longest-match;
interface ge-0/0/2.0;
interface lo0.0;
}
}
"#;
const DOC_TRACEOPTIONS: &str = r#"
protocols {
ldp {
traceoptions {
file ldp size 10m files 5 world-readable;
flag packets receive;
flag binding;
}
interface all {
}
}
}
"#;
const DOC_POLICIES: &str = r#"
protocols {
ldp {
egress-policy connected-only;
export block-one;
import only-32;
graceful-restart {
helper-disable;
}
}
}
"#;
const EXTRA: &str = r#"
protocols {
ldp {
keepalive-interval 10;
keepalive-timeout 30;
preference 9;
transport-address router-id;
no-deaggregate;
no-mtu-discovery;
explicit-null;
no-forwarding;
track-igp-metric;
l2-smart-policy;
strict-targeted-hellos;
upstream-label-assignment;
label-withdrawal-delay 60;
export [ a b ];
import [ c d ];
egress-policy [ e f ];
longest-match {
policy [ lm1 lm2 ];
}
igp-synchronization {
holddown-interval 20;
}
graceful-restart {
disable;
maximum-neighbor-recovery-time 140;
reconnect-time 60;
recovery-time 100;
}
session-protection {
timeout 120;
}
targeted-hello {
hello-interval 5;
hold-time 15;
}
auto-targeted-session {
maximum-sessions 200;
teardown-delay 30;
}
make-before-break {
timeout 60;
switchover-delay 5;
}
p2mp {
no-rsvp-tunneling;
}
log-updown {
trap disable;
}
interface ge-0/0/0.0 {
hello-interval 3;
hold-time 9;
transport-address loopback;
allow-subnet-mismatch;
link-protection {
disable;
dynamic-rsvp-lsp;
}
}
interface ge-0/0/1.0 {
link-protection;
}
session 10.0.0.2 {
authentication-algorithm hmac-sha-1-96;
authentication-key "$9$secret"; ## SECRET-DATA
authentication-key-chain chain1;
no-mtu-discovery;
transport-address 10.255.0.1;
}
session-group 10.1.0.0/16 {
downstream-on-demand;
mtu-discovery;
}
}
}
"#;
fn ldp_of(text: &str) -> Ldp {
ldp(&juniper::parse_braces(text)).unwrap()
}
#[test]
fn reads_the_guide_link_protection() {
let ldp = ldp_of(DOC_LINK_PROTECTION);
assert_eq!(ldp.interfaces.len(), 2);
assert_eq!(ldp.interfaces[0].name, "all");
let protection = ldp.interfaces[0].link_protection.as_ref().unwrap();
assert!(protection.dynamic_rsvp_lsp);
assert!(!protection.disable);
assert_eq!(ldp.interfaces[1].name, "fxp0.0");
assert!(ldp.interfaces[1].disable);
assert!(ldp.p2mp);
assert!(!ldp.p2mp_no_rsvp_tunneling);
}
#[test]
fn reads_the_guide_ipv6_transport() {
let ldp = ldp_of(DOC_IPV6);
assert_eq!(ldp.deaggregate, Some(true));
assert_eq!(ldp.families, ["inet6", "inet"]);
assert_eq!(ldp.transport_preference.as_deref(), Some("ipv4"));
assert_eq!(ldp.dual_transport, None);
let dual = ldp_of(DOC_DUAL_TRANSPORT).dual_transport.unwrap();
assert_eq!(dual.inet_lsr_id.as_deref(), Some("10.255.0.1"));
assert_eq!(dual.inet6_lsr_id.as_deref(), Some("10.1.1.1"));
}
#[test]
fn reads_the_guide_downstream_on_demand_session() {
let ldp = ldp_of(DOC_DOWNSTREAM_ON_DEMAND);
assert_eq!(ldp.dod_request_policy.as_deref(), Some("DOD-Request-Loopbacks"));
assert_eq!(ldp.sessions.len(), 1);
assert_eq!(ldp.sessions[0].name, "172.16.1.1");
assert!(ldp.sessions[0].downstream_on_demand);
assert!(ldp.session_groups.is_empty());
}
#[test]
fn reads_the_guide_longest_match_policies_and_tracing() {
let ldp = ldp_of(DOC_LONGEST_MATCH);
assert_eq!(ldp.longest_match, Some(Vec::new()));
assert_eq!(ldp.interfaces.len(), 2);
let ldp = ldp_of(DOC_POLICIES);
assert_eq!(ldp.egress_policies, ["connected-only"]);
assert_eq!(ldp.export_policies, ["block-one"]);
assert_eq!(ldp.import_policies, ["only-32"]);
let restart = ldp.graceful_restart.unwrap();
assert!(restart.helper_disable);
assert!(!restart.disable);
let ldp = ldp_of(DOC_TRACEOPTIONS);
let trace = ldp.traceoptions.unwrap();
assert_eq!(trace.file.as_deref(), Some("ldp"));
assert_eq!(trace.size.as_deref(), Some("10m"));
assert_eq!(trace.files.as_deref(), Some("5"));
assert_eq!(trace.world_readable, Some(true));
assert_eq!(trace.flags[0].name, "packets");
assert_eq!(trace.flags[0].modifiers, ["receive"]);
assert_eq!(trace.flags[1].name, "binding");
assert_eq!(ldp.interfaces[0].name, "all");
}
#[test]
fn reads_the_global_options() {
let ldp = ldp_of(EXTRA);
assert!(!ldp.disable);
assert_eq!(ldp.keepalive_interval.as_deref(), Some("10"));
assert_eq!(ldp.keepalive_timeout.as_deref(), Some("30"));
assert_eq!(ldp.preference.as_deref(), Some("9"));
assert_eq!(ldp.transport_address.as_deref(), Some("router-id"));
assert_eq!(ldp.deaggregate, Some(false));
assert_eq!(ldp.mtu_discovery, Some(false));
assert!(ldp.explicit_null);
assert!(ldp.no_forwarding);
assert!(ldp.track_igp_metric);
assert!(ldp.l2_smart_policy);
assert!(ldp.strict_targeted_hellos);
assert!(ldp.upstream_label_assignment);
assert_eq!(ldp.label_withdrawal_delay.as_deref(), Some("60"));
assert!(ldp.log_updown_trap_disabled);
assert!(ldp.p2mp);
assert!(ldp.p2mp_no_rsvp_tunneling);
assert_eq!(ldp.export_policies, ["a", "b"]);
assert_eq!(ldp.import_policies, ["c", "d"]);
assert_eq!(ldp.egress_policies, ["e", "f"]);
assert_eq!(ldp.longest_match, Some(vec!["lm1".to_string(), "lm2".to_string()]));
assert_eq!(ldp.igp_synchronization_holddown_interval.as_deref(), Some("20"));
}
#[test]
fn reads_the_grouped_options() {
let ldp = ldp_of(EXTRA);
let restart = ldp.graceful_restart.unwrap();
assert!(restart.disable);
assert_eq!(restart.maximum_neighbor_recovery_time.as_deref(), Some("140"));
assert_eq!(restart.reconnect_time.as_deref(), Some("60"));
assert_eq!(restart.recovery_time.as_deref(), Some("100"));
assert_eq!(ldp.session_protection, Some(Some("120".to_string())));
let hello = ldp.targeted_hello.unwrap();
assert_eq!(hello.hello_interval.as_deref(), Some("5"));
assert_eq!(hello.hold_time.as_deref(), Some("15"));
let auto = ldp.auto_targeted_session.unwrap();
assert_eq!(auto.maximum_sessions.as_deref(), Some("200"));
assert_eq!(auto.teardown_delay.as_deref(), Some("30"));
let mbb = ldp.make_before_break.unwrap();
assert_eq!(mbb.timeout.as_deref(), Some("60"));
assert_eq!(mbb.switchover_delay.as_deref(), Some("5"));
}
#[test]
fn reads_the_interface_options() {
let ldp = ldp_of(EXTRA);
let interface = &ldp.interfaces[0];
assert_eq!(interface.hello_interval.as_deref(), Some("3"));
assert_eq!(interface.hold_time.as_deref(), Some("9"));
assert_eq!(interface.transport_address.as_deref(), Some("loopback"));
assert!(interface.allow_subnet_mismatch);
let protection = interface.link_protection.as_ref().unwrap();
assert!(protection.disable);
assert!(protection.dynamic_rsvp_lsp);
assert_eq!(ldp.interfaces[1].link_protection, Some(LdpLinkProtection::default()));
}
#[test]
fn reads_sessions_without_the_key() {
let ldp = ldp_of(EXTRA);
let session = &ldp.sessions[0];
assert_eq!(session.name, "10.0.0.2");
assert_eq!(session.authentication_algorithm.as_deref(), Some("hmac-sha-1-96"));
assert!(session.has_authentication_key);
assert_eq!(session.authentication_key_chain.as_deref(), Some("chain1"));
assert_eq!(session.mtu_discovery, Some(false));
assert_eq!(session.transport_address.as_deref(), Some("10.255.0.1"));
assert!(!session.downstream_on_demand);
let group = &ldp.session_groups[0];
assert_eq!(group.name, "10.1.0.0/16");
assert!(group.downstream_on_demand);
assert_eq!(group.mtu_discovery, Some(true));
assert!(!group.has_authentication_key);
}
#[test]
fn bare_statements_and_absent_values() {
let ldp = ldp_of("protocols {\n ldp {\n session-protection;\n disable;\n }\n}\n");
assert!(ldp.disable);
assert_eq!(ldp.session_protection, Some(None));
assert_eq!(ldp.graceful_restart, None);
assert_eq!(ldp.longest_match, None);
assert!(!ldp.p2mp);
assert!(!ldp.log_updown_trap_disabled);
}
#[test]
fn no_ldp_is_none() {
let config = juniper::parse_braces("protocols {\n ospf {\n area 0.0.0.0;\n }\n}\n");
assert_eq!(ldp(&config), None);
}
}