Skip to main content

asterust_junos/interfaces/
logical_tunnel_options.rs

1//! `logical-tunnel-options`
2//! `interfaces { logical-tunnel-options { ... } }`.
3//!
4//! # See also
5//!
6//! - [logical-tunnel-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interfaces-logical-tunnel-options.html)
7
8use super::aggregated_ether_options::{parse_link_protection, parse_load_balance};
9use super::aggregated_ether_options::{LinkProtection, LoadBalance};
10use crate::{child_block, has_leaf, top_level};
11use asterust::config::Config;
12
13/// The router-wide `logical-tunnel-options`.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct LogicalTunnelOptions {
17    pub link_protection: Option<LinkProtection>,
18    pub load_balance: Option<LoadBalance>,
19    pub per_unit_mac_disable: bool,
20}
21
22/// Returns the `logical-tunnel-options` of the top-level `interfaces` block.
23///
24/// # Examples
25///
26/// ```
27/// use asterust::config::juniper;
28/// use asterust_junos::interfaces::logical_tunnel_options;
29///
30/// let config = juniper::parse_braces(
31///     "interfaces {\n    logical-tunnel-options {\n        per-unit-mac-disable;\n    }\n}\n",
32/// );
33/// assert!(logical_tunnel_options(&config).unwrap().per_unit_mac_disable);
34/// ```
35pub fn logical_tunnel_options(config: &Config) -> Option<LogicalTunnelOptions> {
36    let options = child_block(top_level(config, "interfaces")?, "logical-tunnel-options")?;
37    Some(LogicalTunnelOptions {
38        link_protection: child_block(options, "link-protection").map(parse_link_protection),
39        load_balance: child_block(options, "load-balance").map(parse_load_balance),
40        per_unit_mac_disable: has_leaf(options, "per-unit-mac-disable"),
41    })
42}
43
44#[cfg(test)]
45mod tests {
46    use super::logical_tunnel_options;
47    use asterust::config::juniper;
48
49    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
50    #[test]
51    fn extracts_logical_tunnel_options() {
52        let config = juniper::parse_braces(
53            r#"
54interfaces {
55    logical-tunnel-options {
56        link-protection {
57            non-revertive;
58        }
59        load-balance {
60            adaptive {
61                scan-interval 20;
62            }
63            local-bias percent 50;
64        }
65        per-unit-mac-disable;
66    }
67}
68"#,
69        );
70        let options = logical_tunnel_options(&config).unwrap();
71        assert_eq!(options.link_protection.as_ref().unwrap().revertive, Some(false));
72        let balance = options.load_balance.as_ref().unwrap();
73        assert_eq!(balance.adaptive_scan_interval.as_deref(), Some("20"));
74        assert_eq!(balance.local_bias_percent.as_deref(), Some("50"));
75        assert!(options.per_unit_mac_disable);
76    }
77
78    #[test]
79    fn logical_tunnel_options_are_optional() {
80        let config = juniper::parse_braces("interfaces {\n    lt-0/0/0 {\n        disable;\n    }\n}\n");
81        assert!(logical_tunnel_options(&config).is_none());
82    }
83}