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
//! `load-balancing-options`
//! `interfaces { <name> { load-balancing-options { ... } } }`.
//!
//! # See also
//!
//! - [load-balancing-options (Aggregated Multiservices)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/load-balancing-options-edit-interfaces-ams.html)
//! - [hash-keys](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/hash-keys-edit-interfaces-ams.html)

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

/// The `load-balancing-options` of an aggregated multiservices interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct LoadBalancingOptions {
    /// `many-to-one` or `one-to-one`, from `high-availability-options`.
    pub high_availability_mode: Option<String>,
    pub preferred_backup: Option<String>,
    pub drop_member_traffic: bool,
    pub rejoin_timeout: Option<String>,
    pub redistribute_all_traffic: bool,
    pub enable_rejoin: bool,
    /// `source-ip` or `destination-ip`.
    pub egress_key: Option<String>,
    pub ingress_key: Option<String>,
    pub ipv6_source_prefix_length: Option<String>,
    pub member_interfaces: Vec<String>,
}

pub(super) fn parse_load_balancing_options(options: &ConfigLine) -> LoadBalancingOptions {
    let availability = child_block(options, "high-availability-options");
    let mode = availability.and_then(|block| {
        ["many-to-one", "one-to-one"]
            .into_iter()
            .find(|mode| child_block(block, mode).is_some() || has_leaf(block, mode))
    });
    let failure = child_block(options, "member-failure-options");
    let drop = failure.and_then(|block| child_block(block, "drop-member-traffic"));
    let redistribute = failure.and_then(|block| child_block(block, "redistribute-all-traffic"));
    let hash_keys = child_block(options, "hash-keys");
    let hash_key = |keyword| hash_keys.and_then(|keys| arg_of(keys, keyword)).map(str::to_string);
    LoadBalancingOptions {
        high_availability_mode: mode.map(str::to_string),
        preferred_backup: availability
            .and_then(|block| mode.and_then(|mode| child_block(block, mode)))
            .and_then(|block| arg_of(block, "preferred-backup"))
            .map(str::to_string),
        drop_member_traffic: drop.is_some() || failure.is_some_and(|block| has_leaf(block, "drop-member-traffic")),
        rejoin_timeout: drop
            .and_then(|block| arg_of(block, "rejoin-timeout"))
            .map(str::to_string),
        redistribute_all_traffic: redistribute.is_some()
            || failure.is_some_and(|block| has_leaf(block, "redistribute-all-traffic")),
        enable_rejoin: redistribute.is_some_and(|block| has_leaf(block, "enable-rejoin")),
        egress_key: hash_key("egress-key"),
        ingress_key: hash_key("ingress-key"),
        ipv6_source_prefix_length: hash_key("ipv6-source-prefix-length"),
        member_interfaces: args_of(options, "member-interface")
            .into_iter()
            .map(str::to_string)
            .collect(),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_load_balancing_options() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ams0 {
        load-balancing-options {
            member-interface ms-1/0/0;
            member-interface ms-1/1/0;
            high-availability-options {
                many-to-one {
                    preferred-backup ms-2/0/0;
                }
            }
            member-failure-options {
                drop-member-traffic {
                    rejoin-timeout 120;
                }
                redistribute-all-traffic {
                    enable-rejoin;
                }
            }
            hash-keys {
                egress-key destination-ip;
                ingress-key source-ip;
                ipv6-source-prefix-length 64;
            }
        }
    }
    ams1 {
        load-balancing-options {
            member-interface ms-3/0/0;
        }
    }
}
"#,
        ));
        let options = ifaces[0].load_balancing_options.as_ref().unwrap();
        assert_eq!(options.member_interfaces, ["ms-1/0/0", "ms-1/1/0"]);
        assert_eq!(options.high_availability_mode.as_deref(), Some("many-to-one"));
        assert_eq!(options.preferred_backup.as_deref(), Some("ms-2/0/0"));
        assert!(options.drop_member_traffic && options.redistribute_all_traffic && options.enable_rejoin);
        assert_eq!(options.rejoin_timeout.as_deref(), Some("120"));
        assert_eq!(options.egress_key.as_deref(), Some("destination-ip"));
        assert_eq!(options.ingress_key.as_deref(), Some("source-ip"));
        assert_eq!(options.ipv6_source_prefix_length.as_deref(), Some("64"));

        let plain = ifaces[1].load_balancing_options.as_ref().unwrap();
        assert_eq!(plain.member_interfaces, ["ms-3/0/0"]);
        assert!(plain.high_availability_mode.is_none() && !plain.drop_member_traffic && plain.egress_key.is_none());
    }
}