asterust-junos 0.5.14

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)
//! - [allow-fragmentation](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/allow-fragmentation-edit-interfaces.html)
//! - [allow-fragmentation (fti)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/allow-fragmentation.html)
//! - [do-not-fragment](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/do-not-fragment-edit-interfaces.html)
//! - [path-mtu-discovery (Tunnel)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/path-mtu-discovery-edit-interfaces.html)
//! - [ttl](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ttl-edit-interfaces.html)
//! - [traffic-class (Tunnels)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traffic-class-tunnels-edit-interfaces.html)
//! - [backup-destination](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/backup-destination-edit-interfaces.html)
//! - [tunnel-routing-instance](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tunnel-routing-instance-edit-interfaces-interface-unit-tunnel-encapsulation-gre.html)
//! - [vxlan-gpe (FTI)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/fti-vxlan-gpe.html)
//! - [vni (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/vni-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, statement_words, toggle, value_after};
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,
    /// `allow-fragmentation`, written under `tunnel` or under its `encapsulation`.
    pub allow_fragmentation: bool,
    pub do_not_fragment: bool,
    /// `Some(true)` for `path-mtu-discovery`, `Some(false)` for `no-path-mtu-discovery`.
    pub path_mtu_discovery: Option<bool>,
    pub ttl: Option<String>,
    pub traffic_class: Option<String>,
    pub backup_destination: Option<String>,
    /// From `tunnel-routing-instance { routing-instance }` of a GRE encapsulation.
    pub tunnel_routing_instance: Option<String>,
    /// The `destination-udp-port` of a `vxlan-gpe` encapsulation.
    pub destination_udp_port: Option<String>,
    /// From `source-udp-port-range { min }`.
    pub source_udp_port_min: Option<String>,
    /// From `source-udp-port-range { max }`.
    pub source_udp_port_max: Option<String>,
    /// e.g. `vxlan`, from `tunnel-endpoint`.
    pub tunnel_endpoint: Option<String>,
    pub vni: Option<String>,
}

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()),
        allow_fragmentation: has_leaf(tunnel, "allow-fragmentation"),
        do_not_fragment: has_leaf(tunnel, "do-not-fragment"),
        path_mtu_discovery: toggle(tunnel, "path-mtu-discovery"),
        ttl: arg_of(tunnel, "ttl").map(str::to_string),
        traffic_class: arg_of(tunnel, "traffic-class").map(str::to_string),
        backup_destination: arg_of(tunnel, "backup-destination").map(str::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");
        parsed.allow_fragmentation |= has_leaf(block, "allow-fragmentation");
        parsed.tunnel_routing_instance = child_block(block, "tunnel-routing-instance")
            .and_then(|t| arg_of(t, "routing-instance"))
            .map(str::to_string);
        parsed.destination_udp_port = arg_of(block, "destination-udp-port").map(str::to_string);
        let ports = statement_words(block, "source-udp-port-range");
        parsed.source_udp_port_min = ports.as_ref().and_then(|w| value_after(w, "min"));
        parsed.source_udp_port_max = ports.as_ref().and_then(|w| value_after(w, "max"));
        parsed.tunnel_endpoint = arg_of(block, "tunnel-endpoint").map(str::to_string);
        parsed.vni = arg_of(block, "vni").map(str::to_string);
    }
    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());
    }
}