use super::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{arg_of, args_of, child_block, has_leaf, named_children, statement_words, top_level, value_after};
use asterust::config::{Config, ConfigLine};
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkManagementTeLinkInterface {
pub name: String,
pub disable: bool,
pub local_address: Option<String>,
pub remote_address: Option<String>,
pub remote_id: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkManagementTeLink {
pub name: String,
pub disable: bool,
pub ethernet_vlan: bool,
pub vlan_id_range: Option<String>,
pub local_address: Option<String>,
pub remote_address: Option<String>,
pub remote_id: Option<String>,
pub interfaces: Vec<LinkManagementTeLinkInterface>,
pub label_switched_paths: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkManagementControlChannel {
pub interface: String,
pub remote_address: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkManagementLmpProtocol {
pub hello_dead_interval: Option<String>,
pub hello_interval: Option<String>,
pub passive: bool,
pub retransmission_interval: Option<String>,
pub retry_limit: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkManagementPeer {
pub name: String,
pub address: Option<String>,
pub control_channel: Option<String>,
pub lmp_control_channel: Option<LinkManagementControlChannel>,
pub lmp_protocol: Option<LinkManagementLmpProtocol>,
pub te_links: Vec<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct LinkManagement {
pub peers: Vec<LinkManagementPeer>,
pub te_links: Vec<LinkManagementTeLink>,
pub traceoptions: Option<Traceoptions>,
}
fn parse_te_link_interface(name: &str, interface: &ConfigLine) -> LinkManagementTeLinkInterface {
LinkManagementTeLinkInterface {
name: name.to_string(),
disable: has_leaf(interface, "disable"),
local_address: arg_of(interface, "local-address").map(str::to_string),
remote_address: arg_of(interface, "remote-address").map(str::to_string),
remote_id: arg_of(interface, "remote-id").map(str::to_string),
}
}
fn parse_te_link(name: &str, link: &ConfigLine) -> LinkManagementTeLink {
let vlan = statement_words(link, "ethernet-vlan").or_else(|| statement_words(link, "ethernet-label"));
LinkManagementTeLink {
name: name.to_string(),
disable: has_leaf(link, "disable"),
ethernet_vlan: vlan.is_some(),
vlan_id_range: vlan.as_ref().and_then(|words| value_after(words, "vlan-id-range")),
local_address: arg_of(link, "local-address").map(str::to_string),
remote_address: arg_of(link, "remote-address").map(str::to_string),
remote_id: arg_of(link, "remote-id").map(str::to_string),
interfaces: named_children(link, "interface")
.map(|(name, interface)| parse_te_link_interface(name, interface))
.collect(),
label_switched_paths: args_of(link, "label-switched-path")
.into_iter()
.map(str::to_string)
.collect(),
}
}
fn parse_peer(name: &str, peer: &ConfigLine) -> LinkManagementPeer {
let field = |block: &ConfigLine, keyword: &str| arg_of(block, keyword).map(str::to_string);
LinkManagementPeer {
name: name.to_string(),
address: field(peer, "address"),
control_channel: field(peer, "control-channel"),
lmp_control_channel: named_children(peer, "lmp-control-channel")
.next()
.map(|(interface, channel)| LinkManagementControlChannel {
interface: interface.to_string(),
remote_address: field(channel, "remote-address"),
}),
lmp_protocol: child_block(peer, "lmp-protocol").map(|protocol| LinkManagementLmpProtocol {
hello_dead_interval: field(protocol, "hello-dead-interval"),
hello_interval: field(protocol, "hello-interval"),
passive: has_leaf(protocol, "passive"),
retransmission_interval: field(protocol, "retransmission-interval"),
retry_limit: field(protocol, "retry-limit"),
}),
te_links: named_children(peer, "te-link")
.map(|(name, _)| name.to_string())
.collect(),
}
}
pub fn link_management(config: &Config) -> Option<LinkManagement> {
let lmp = child_block(top_level(config, "protocols")?, "link-management")?;
Some(LinkManagement {
peers: named_children(lmp, "peer")
.map(|(name, peer)| parse_peer(name, peer))
.collect(),
te_links: named_children(lmp, "te-link")
.map(|(name, link)| parse_te_link(name, link))
.collect(),
traceoptions: parse_traceoptions(lmp),
})
}
#[cfg(test)]
mod tests {
use super::*;
use asterust::config::juniper;
const GUIDE: &str = r#"
protocols {
link-management {
te-link link10 {
local-address 10.35.1.1;
remote-address 10.35.1.2;
ethernet-vlan;
interface ge-0/0/0;
}
te-link link1 {
local-address 10.35.1.2;
remote-address 10.35.1.1;
ethernet-vlan {
vlan-id-range 1-1000;
}
interface ge-0/0/0;
}
peer PE1 {
address 10.255.10.2;
control-channel gre.0;
te-link link10;
}
}
}
"#;
#[test]
fn reads_the_guide_te_links_and_peer() {
let lmp = link_management(&juniper::parse_braces(GUIDE)).unwrap();
assert_eq!(lmp.te_links.len(), 2);
let first = &lmp.te_links[0];
assert_eq!(first.name, "link10");
assert_eq!(first.local_address.as_deref(), Some("10.35.1.1"));
assert_eq!(first.remote_address.as_deref(), Some("10.35.1.2"));
assert!(first.ethernet_vlan);
assert_eq!(first.vlan_id_range, None);
assert_eq!(first.interfaces[0].name, "ge-0/0/0");
let second = &lmp.te_links[1];
assert!(second.ethernet_vlan);
assert_eq!(second.vlan_id_range.as_deref(), Some("1-1000"));
assert_eq!(lmp.peers.len(), 1);
let peer = &lmp.peers[0];
assert_eq!(peer.name, "PE1");
assert_eq!(peer.address.as_deref(), Some("10.255.10.2"));
assert_eq!(peer.control_channel.as_deref(), Some("gre.0"));
assert_eq!(peer.te_links, ["link10"]);
assert!(peer.lmp_control_channel.is_none() && peer.lmp_protocol.is_none());
}
#[test]
fn reads_the_remaining_statements() {
let lmp = link_management(&juniper::parse_braces(
r#"
protocols {
link-management {
traceoptions {
file lmp.log size 1m;
flag state;
}
te-link link2 {
disable;
ethernet-vlan vlan-id-range 10-20;
remote-id 7;
label-switched-path lsp1;
label-switched-path lsp2;
interface xe-0/0/0 {
disable;
local-address 10.1.1.1;
remote-address 10.1.1.2;
remote-id 9;
}
}
peer P2 {
lmp-control-channel ge-0/0/1.0 {
remote-address 192.0.2.5;
}
lmp-protocol {
hello-interval 150;
hello-dead-interval 500;
passive;
retransmission-interval 500;
retry-limit 3;
}
te-link link2;
te-link link3;
}
}
}
"#,
))
.unwrap();
assert_eq!(lmp.traceoptions.as_ref().unwrap().flags[0].name, "state");
let link = &lmp.te_links[0];
assert!(link.disable && link.ethernet_vlan);
assert_eq!(link.vlan_id_range.as_deref(), Some("10-20"));
assert_eq!(link.remote_id.as_deref(), Some("7"));
assert_eq!(link.label_switched_paths, ["lsp1", "lsp2"]);
let interface = &link.interfaces[0];
assert!(interface.disable);
assert_eq!(interface.local_address.as_deref(), Some("10.1.1.1"));
assert_eq!(interface.remote_address.as_deref(), Some("10.1.1.2"));
assert_eq!(interface.remote_id.as_deref(), Some("9"));
let peer = &lmp.peers[0];
let channel = peer.lmp_control_channel.as_ref().unwrap();
assert_eq!(channel.interface, "ge-0/0/1.0");
assert_eq!(channel.remote_address.as_deref(), Some("192.0.2.5"));
let protocol = peer.lmp_protocol.as_ref().unwrap();
assert_eq!(protocol.hello_interval.as_deref(), Some("150"));
assert_eq!(protocol.hello_dead_interval.as_deref(), Some("500"));
assert!(protocol.passive);
assert_eq!(protocol.retransmission_interval.as_deref(), Some("500"));
assert_eq!(protocol.retry_limit.as_deref(), Some("3"));
assert_eq!(peer.te_links, ["link2", "link3"]);
assert!(peer.address.is_none() && peer.control_channel.is_none());
}
#[test]
fn link_management_is_optional() {
assert!(link_management(&juniper::parse_braces(
"protocols {\n mpls {\n interface all;\n }\n}\n"
))
.is_none());
}
}