asterust-junos 0.5.13

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
//! `logical-tunnel-options`
//! `interfaces { logical-tunnel-options { ... } }`.
//!
//! # See also
//!
//! - [logical-tunnel-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-logical-tunnel-options.html)

use super::aggregated_ether_options::{parse_link_protection, parse_load_balance};
use super::aggregated_ether_options::{LinkProtection, LoadBalance};
use crate::{child_block, has_leaf, top_level};
use asterust::config::Config;

/// The router-wide `logical-tunnel-options`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LogicalTunnelOptions {
    pub link_protection: Option<LinkProtection>,
    pub load_balance: Option<LoadBalance>,
    pub per_unit_mac_disable: bool,
}

/// Returns the `logical-tunnel-options` of the top-level `interfaces` block.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::interfaces::logical_tunnel_options;
///
/// let config = juniper::parse_braces(
///     "interfaces {\n    logical-tunnel-options {\n        per-unit-mac-disable;\n    }\n}\n",
/// );
/// assert!(logical_tunnel_options(&config).unwrap().per_unit_mac_disable);
/// ```
pub fn logical_tunnel_options(config: &Config) -> Option<LogicalTunnelOptions> {
    let options = child_block(top_level(config, "interfaces")?, "logical-tunnel-options")?;
    Some(LogicalTunnelOptions {
        link_protection: child_block(options, "link-protection").map(parse_link_protection),
        load_balance: child_block(options, "load-balance").map(parse_load_balance),
        per_unit_mac_disable: has_leaf(options, "per-unit-mac-disable"),
    })
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_logical_tunnel_options() {
        let config = juniper::parse_braces(
            r#"
interfaces {
    logical-tunnel-options {
        link-protection {
            non-revertive;
        }
        load-balance {
            adaptive {
                scan-interval 20;
            }
            local-bias percent 50;
        }
        per-unit-mac-disable;
    }
}
"#,
        );
        let options = logical_tunnel_options(&config).unwrap();
        assert_eq!(options.link_protection.as_ref().unwrap().revertive, Some(false));
        let balance = options.load_balance.as_ref().unwrap();
        assert_eq!(balance.adaptive_scan_interval.as_deref(), Some("20"));
        assert_eq!(balance.local_bias_percent.as_deref(), Some("50"));
        assert!(options.per_unit_mac_disable);
    }

    #[test]
    fn logical_tunnel_options_are_optional() {
        let config = juniper::parse_braces("interfaces {\n    lt-0/0/0 {\n        disable;\n    }\n}\n");
        assert!(logical_tunnel_options(&config).is_none());
    }
}