asterust-junos 0.5.6

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `tunnel`
//! `interfaces <name> { unit <n> { tunnel { encapsulation <type> { ... } } } }`.
//!
//! # See also
//!
//! - [tunnel (FTI)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tunnel-edit-interfaces-fti.html)
//! - [udp (tunnel encapsulation)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/udp-edit-interfaces-unit-tunnel-encapsulation.html)

use crate::{active_children, arg_of, child_block, has_leaf};
use asterust::config::ConfigLine;

/// The `tunnel` of a logical unit, as written under `encapsulation`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnitTunnel {
    /// `"gre"`, `"ipip"` or `"udp"`.
    pub encapsulation: Option<String>,
    pub source_address: Option<String>,
    pub source_interface: Option<String>,
    pub destination_address: Option<String>,
    pub key: Option<String>,
    pub routing_instance: Option<String>,
    pub tunnel_termination: bool,
    pub virtual_loop: bool,
}

pub(super) fn parse_tunnel(unit: &ConfigLine) -> Option<UnitTunnel> {
    let tunnel = child_block(unit, "tunnel")?;
    let encapsulation = active_children(tunnel).find(|c| c.arg("encapsulation").is_some());
    let mut parsed = UnitTunnel {
        encapsulation: encapsulation
            .and_then(|c| c.arg("encapsulation"))
            .map(|kind| kind.split_whitespace().next().unwrap_or(kind).to_string()),
        ..UnitTunnel::default()
    };
    if let Some(block) = encapsulation {
        let source = child_block(block, "source");
        parsed.source_address = source.and_then(|s| arg_of(s, "address")).map(str::to_string);
        parsed.source_interface = source.and_then(|s| arg_of(s, "interface")).map(str::to_string);
        parsed.destination_address = child_block(block, "destination")
            .and_then(|d| arg_of(d, "address"))
            .map(str::to_string);
        parsed.key = arg_of(block, "key").map(str::to_string);
        parsed.routing_instance = arg_of(block, "routing-instance").map(str::to_string);
        parsed.tunnel_termination = has_leaf(block, "tunnel-termination");
        parsed.virtual_loop = has_leaf(block, "virtual-loop");
    }
    Some(parsed)
}

#[cfg(test)]
mod tests {
    use crate::interfaces::interfaces;
    use asterust::config::juniper;

    // Adapted from the IS-IS User Guide, How to Configure IS-IS Flood Reflectors: the `fti0` interface of Device R0
    // (the `mpls` port is from the guide's `set` commands, written in brace form); see the README "Sources".
    #[test]
    fn reads_the_guide_fti_unit() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    fti0 {
        unit 0 {
            tunnel {
                encapsulation udp {
                    source {
                        address 192.168.255.10;
                    }
                    destination {
                        address 192.168.255.6;
                    }
                }
            }
            family inet {
                address 10.10.6.1/30;
                destination-udp-port 10000;
            }
            family iso {
                destination-udp-port 10030;
            }
            family inet6 {
                destination-udp-port 10010;
            }
            family mpls {
                destination-udp-port 10020;
            }
        }
    }
}
"#,
        ));
        let unit = &ifaces[0].units[0];
        let tunnel = unit.tunnel.as_ref().unwrap();
        assert_eq!(tunnel.encapsulation.as_deref(), Some("udp"));
        assert_eq!(tunnel.source_address.as_deref(), Some("192.168.255.10"));
        assert_eq!(tunnel.destination_address.as_deref(), Some("192.168.255.6"));
        assert_eq!(tunnel.source_interface, None);

        let ports: Vec<_> = unit
            .family_settings
            .iter()
            .map(|f| (f.name.as_str(), f.destination_udp_port.as_deref()))
            .collect();
        assert_eq!(
            ports,
            [
                ("inet", Some("10000")),
                ("iso", Some("10030")),
                ("inet6", Some("10010")),
                ("mpls", Some("10020"))
            ]
        );
        assert_eq!(unit.inet_addresses, ["10.10.6.1/30"]);
    }

    // Not from documentation: the other tunnel statements of the fti CLI Reference page.
    #[test]
    fn reads_the_other_tunnel_statements() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    fti1 {
        unit 0 {
            tunnel {
                encapsulation gre {
                    source {
                        address 192.0.2.1;
                        interface lo0.0;
                    }
                    destination {
                        address 192.0.2.2;
                    }
                    key 5;
                    routing-instance VRF-A;
                    tunnel-termination;
                    virtual-loop;
                }
            }
        }
        unit 1 {
            tunnel {
                encapsulation ipip;
            }
        }
        unit 2;
    }
}
"#,
        ));
        let units = &ifaces[0].units;
        let gre = units[0].tunnel.as_ref().unwrap();
        assert_eq!(gre.encapsulation.as_deref(), Some("gre"));
        assert_eq!(gre.source_interface.as_deref(), Some("lo0.0"));
        assert_eq!(gre.key.as_deref(), Some("5"));
        assert_eq!(gre.routing_instance.as_deref(), Some("VRF-A"));
        assert!(gre.tunnel_termination);
        assert!(gre.virtual_loop);

        let ipip = units[1].tunnel.as_ref().unwrap();
        assert_eq!(ipip.encapsulation.as_deref(), Some("ipip"));
        assert_eq!(ipip.source_address, None);
        assert!(units[2].tunnel.is_none());
    }
}