Skip to main content

asterust_junos/interfaces/
redundancy_options.rs

1//! `redundancy-options`
2//! `interfaces { <name> { redundancy-options { ... } } }`.
3//!
4//! # See also
5//!
6//! - [redundancy-options (Stateful Synchronization)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/nat-redundancy-options-msmic-msmpc-edit-interfaces.html)
7//! - [redundancy-options (Adaptive Services Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/redundancy-options-edit-interfaces-rsp.html)
8//! - [redundancy-options (Link Services IQ PIC Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/redundancy-options-edit-interfaces-rlsq.html)
9//! - [redundancy-options (Aggregated Multiservices)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/redundancy-option-edit-interfaces-ams.html)
10
11use crate::{arg_of, child_block, has_leaf};
12use asterust::config::ConfigLine;
13
14/// The `redundancy-options` of a services interface.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct RedundancyOptions {
18    pub primary: Option<String>,
19    pub secondary: Option<String>,
20    pub hot_standby: bool,
21    pub warm_standby: bool,
22    /// From `redundancy-local { data-address }`.
23    pub redundancy_local_data_address: Option<String>,
24    /// From `redundancy-peer { ipaddress }`.
25    pub redundancy_peer_ipaddress: Option<String>,
26    pub replication_threshold: Option<String>,
27    pub routing_instance: Option<String>,
28}
29
30pub(super) fn parse_redundancy_options(options: &ConfigLine) -> RedundancyOptions {
31    let value = |keyword| arg_of(options, keyword).map(str::to_string);
32    RedundancyOptions {
33        primary: value("primary"),
34        secondary: value("secondary"),
35        hot_standby: has_leaf(options, "hot-standby"),
36        warm_standby: has_leaf(options, "warm-standby"),
37        redundancy_local_data_address: child_block(options, "redundancy-local")
38            .and_then(|local| arg_of(local, "data-address"))
39            .map(str::to_string),
40        redundancy_peer_ipaddress: child_block(options, "redundancy-peer")
41            .and_then(|peer| arg_of(peer, "ipaddress"))
42            .map(str::to_string),
43        replication_threshold: value("replication-threshold"),
44        routing_instance: value("routing-instance"),
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use crate::interfaces::interfaces;
51    use asterust::config::juniper;
52
53    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
54    #[test]
55    fn extracts_redundancy_options() {
56        let ifaces = interfaces(&juniper::parse_braces(
57            r#"
58interfaces {
59    rsp0 {
60        redundancy-options {
61            primary sp-1/0/0;
62            secondary sp-2/0/0;
63            hot-standby;
64        }
65    }
66    rlsq0 {
67        redundancy-options {
68            primary lsq-1/0/0;
69            secondary lsq-2/0/0;
70            warm-standby;
71        }
72    }
73    ms-1/0/0 {
74        redundancy-options {
75            redundancy-local {
76                data-address 10.0.0.1;
77            }
78            redundancy-peer {
79                ipaddress 10.0.0.2;
80            }
81            replication-threshold 30;
82            routing-instance sync;
83        }
84    }
85}
86"#,
87        ));
88        let rsp = ifaces[0].redundancy_options.as_ref().unwrap();
89        assert_eq!(rsp.primary.as_deref(), Some("sp-1/0/0"));
90        assert_eq!(rsp.secondary.as_deref(), Some("sp-2/0/0"));
91        assert!(rsp.hot_standby && !rsp.warm_standby);
92        let rlsq = ifaces[1].redundancy_options.as_ref().unwrap();
93        assert!(rlsq.warm_standby && !rlsq.hot_standby);
94        let nat = ifaces[2].redundancy_options.as_ref().unwrap();
95        assert_eq!(nat.redundancy_local_data_address.as_deref(), Some("10.0.0.1"));
96        assert_eq!(nat.redundancy_peer_ipaddress.as_deref(), Some("10.0.0.2"));
97        assert_eq!(nat.replication_threshold.as_deref(), Some("30"));
98        assert_eq!(nat.routing_instance.as_deref(), Some("sync"));
99        assert!(nat.primary.is_none());
100    }
101}