Skip to main content

asterust_junos/interfaces/
load_balancing_options.rs

1//! `load-balancing-options`
2//! `interfaces { <name> { load-balancing-options { ... } } }`.
3//!
4//! # See also
5//!
6//! - [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)
7//! - [hash-keys](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/hash-keys-edit-interfaces-ams.html)
8
9use crate::{arg_of, args_of, child_block, has_leaf};
10use asterust::config::ConfigLine;
11
12/// The `load-balancing-options` of an aggregated multiservices interface.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct LoadBalancingOptions {
16    /// `many-to-one` or `one-to-one`, from `high-availability-options`.
17    pub high_availability_mode: Option<String>,
18    pub preferred_backup: Option<String>,
19    pub drop_member_traffic: bool,
20    pub rejoin_timeout: Option<String>,
21    pub redistribute_all_traffic: bool,
22    pub enable_rejoin: bool,
23    /// `source-ip` or `destination-ip`.
24    pub egress_key: Option<String>,
25    pub ingress_key: Option<String>,
26    pub ipv6_source_prefix_length: Option<String>,
27    pub member_interfaces: Vec<String>,
28}
29
30pub(super) fn parse_load_balancing_options(options: &ConfigLine) -> LoadBalancingOptions {
31    let availability = child_block(options, "high-availability-options");
32    let mode = availability.and_then(|block| {
33        ["many-to-one", "one-to-one"]
34            .into_iter()
35            .find(|mode| child_block(block, mode).is_some() || has_leaf(block, mode))
36    });
37    let failure = child_block(options, "member-failure-options");
38    let drop = failure.and_then(|block| child_block(block, "drop-member-traffic"));
39    let redistribute = failure.and_then(|block| child_block(block, "redistribute-all-traffic"));
40    let hash_keys = child_block(options, "hash-keys");
41    let hash_key = |keyword| hash_keys.and_then(|keys| arg_of(keys, keyword)).map(str::to_string);
42    LoadBalancingOptions {
43        high_availability_mode: mode.map(str::to_string),
44        preferred_backup: availability
45            .and_then(|block| mode.and_then(|mode| child_block(block, mode)))
46            .and_then(|block| arg_of(block, "preferred-backup"))
47            .map(str::to_string),
48        drop_member_traffic: drop.is_some() || failure.is_some_and(|block| has_leaf(block, "drop-member-traffic")),
49        rejoin_timeout: drop
50            .and_then(|block| arg_of(block, "rejoin-timeout"))
51            .map(str::to_string),
52        redistribute_all_traffic: redistribute.is_some()
53            || failure.is_some_and(|block| has_leaf(block, "redistribute-all-traffic")),
54        enable_rejoin: redistribute.is_some_and(|block| has_leaf(block, "enable-rejoin")),
55        egress_key: hash_key("egress-key"),
56        ingress_key: hash_key("ingress-key"),
57        ipv6_source_prefix_length: hash_key("ipv6-source-prefix-length"),
58        member_interfaces: args_of(options, "member-interface")
59            .into_iter()
60            .map(str::to_string)
61            .collect(),
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use crate::interfaces::interfaces;
68    use asterust::config::juniper;
69
70    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
71    #[test]
72    fn extracts_load_balancing_options() {
73        let ifaces = interfaces(&juniper::parse_braces(
74            r#"
75interfaces {
76    ams0 {
77        load-balancing-options {
78            member-interface ms-1/0/0;
79            member-interface ms-1/1/0;
80            high-availability-options {
81                many-to-one {
82                    preferred-backup ms-2/0/0;
83                }
84            }
85            member-failure-options {
86                drop-member-traffic {
87                    rejoin-timeout 120;
88                }
89                redistribute-all-traffic {
90                    enable-rejoin;
91                }
92            }
93            hash-keys {
94                egress-key destination-ip;
95                ingress-key source-ip;
96                ipv6-source-prefix-length 64;
97            }
98        }
99    }
100    ams1 {
101        load-balancing-options {
102            member-interface ms-3/0/0;
103        }
104    }
105}
106"#,
107        ));
108        let options = ifaces[0].load_balancing_options.as_ref().unwrap();
109        assert_eq!(options.member_interfaces, ["ms-1/0/0", "ms-1/1/0"]);
110        assert_eq!(options.high_availability_mode.as_deref(), Some("many-to-one"));
111        assert_eq!(options.preferred_backup.as_deref(), Some("ms-2/0/0"));
112        assert!(options.drop_member_traffic && options.redistribute_all_traffic && options.enable_rejoin);
113        assert_eq!(options.rejoin_timeout.as_deref(), Some("120"));
114        assert_eq!(options.egress_key.as_deref(), Some("destination-ip"));
115        assert_eq!(options.ingress_key.as_deref(), Some("source-ip"));
116        assert_eq!(options.ipv6_source_prefix_length.as_deref(), Some("64"));
117
118        let plain = ifaces[1].load_balancing_options.as_ref().unwrap();
119        assert_eq!(plain.member_interfaces, ["ms-3/0/0"]);
120        assert!(plain.high_availability_mode.is_none() && !plain.drop_member_traffic && plain.egress_key.is_none());
121    }
122}