Skip to main content

asterust_junos/interfaces/unit/
demux.rs

1//! `demux`
2//! `interfaces { <name> { unit <n> { demux { inet | inet6 { ... } } } } }`.
3//!
4//! # See also
5//!
6//! - [demux (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-edit-interfaces.html)
7//! - [address-ranges (Demux)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/address-range-edit-interfaces.html)
8//! - [username-include (Demux)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/username-include-edit-interfaces.html)
9//! - [dynamic-profile (Demux)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dynamic-profile-demux-edit-interfaces.html)
10
11use crate::{arg_of, child_block, has_leaf, named_children, secret_value};
12use asterust::config::ConfigLine;
13
14/// A `range <name>` of a demux dynamic profile network.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct DemuxRange {
18    pub name: String,
19    pub low: Option<String>,
20    pub high: Option<String>,
21}
22
23/// A `network <address>` of a demux dynamic profile.
24#[derive(Clone, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct DemuxNetwork {
27    pub address: String,
28    pub ranges: Vec<DemuxRange>,
29}
30
31/// The `dynamic-profile` of the demux address ranges.
32#[derive(Clone, Debug, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct DemuxDynamicProfile {
35    pub name: String,
36    pub networks: Vec<DemuxNetwork>,
37}
38
39/// The `username-include` of the demux address ranges.
40#[derive(Clone, Debug, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct DemuxUsernameInclude {
43    pub auth_server_realm: Option<String>,
44    pub delimiter: Option<String>,
45    pub domain_name: Option<String>,
46    pub interface_name: bool,
47    pub source_address: bool,
48    pub user_prefix: Option<String>,
49}
50
51/// The `address-ranges` of a demux family's `auto-configure`.
52#[derive(Clone, Debug, PartialEq, Eq)]
53#[non_exhaustive]
54pub struct DemuxAddressRanges {
55    /// An `authentication { password }` is configured.
56    pub has_authentication_password: bool,
57    /// The authentication password; `None` unless the `secrets` feature is enabled.
58    pub authentication_password: Option<crate::Secret>,
59    pub username_include: Option<DemuxUsernameInclude>,
60    pub dynamic_profile: Option<DemuxDynamicProfile>,
61}
62
63/// The `inet` or `inet6` block of `demux`.
64#[derive(Clone, Debug, PartialEq, Eq)]
65#[non_exhaustive]
66pub struct DemuxFamily {
67    /// `source` or `destination`, from `address`.
68    pub address: Option<String>,
69    pub address_ranges: Option<DemuxAddressRanges>,
70}
71
72/// The `demux` block of a unit.
73#[derive(Clone, Debug, PartialEq, Eq)]
74#[non_exhaustive]
75pub struct Demux {
76    pub inet: Option<DemuxFamily>,
77    pub inet6: Option<DemuxFamily>,
78}
79
80fn parse_address_ranges(ranges: &ConfigLine) -> DemuxAddressRanges {
81    let authentication = child_block(ranges, "authentication");
82    let password = authentication.and_then(|a| arg_of(a, "password"));
83    DemuxAddressRanges {
84        has_authentication_password: password.is_some(),
85        authentication_password: secret_value(password),
86        username_include: authentication
87            .and_then(|a| child_block(a, "username-include"))
88            .map(|include| DemuxUsernameInclude {
89                auth_server_realm: arg_of(include, "auth-server-realm").map(str::to_string),
90                delimiter: arg_of(include, "delimiter").map(str::to_string),
91                domain_name: arg_of(include, "domain-name").map(str::to_string),
92                interface_name: has_leaf(include, "interface-name"),
93                source_address: has_leaf(include, "source-address"),
94                user_prefix: arg_of(include, "user-prefix").map(str::to_string),
95            }),
96        dynamic_profile: named_children(ranges, "dynamic-profile")
97            .next()
98            .map(|(name, profile)| DemuxDynamicProfile {
99                name: name.to_string(),
100                networks: named_children(profile, "network")
101                    .map(|(address, network)| DemuxNetwork {
102                        address: address.to_string(),
103                        ranges: named_children(network, "range")
104                            .map(|(name, range)| DemuxRange {
105                                name: name.to_string(),
106                                low: arg_of(range, "low").map(str::to_string),
107                                high: arg_of(range, "high").map(str::to_string),
108                            })
109                            .collect(),
110                    })
111                    .collect(),
112            }),
113    }
114}
115
116fn parse_demux_family(family: &ConfigLine) -> DemuxFamily {
117    DemuxFamily {
118        address: arg_of(family, "address").map(str::to_string),
119        address_ranges: child_block(family, "auto-configure")
120            .and_then(|auto| child_block(auto, "address-ranges"))
121            .map(parse_address_ranges),
122    }
123}
124
125pub(super) fn parse_demux(demux: &ConfigLine) -> Demux {
126    Demux {
127        inet: child_block(demux, "inet").map(parse_demux_family),
128        inet6: child_block(demux, "inet6").map(parse_demux_family),
129    }
130}
131
132#[cfg(test)]
133mod tests {
134    use crate::interfaces::interfaces;
135    use crate::secret::expected;
136    use asterust::config::juniper;
137
138    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
139    #[test]
140    fn extracts_demux() {
141        let ifaces = interfaces(&juniper::parse_braces(
142            r#"
143interfaces {
144    ge-0/0/0 {
145        unit 0 {
146            demux {
147                inet {
148                    address source;
149                    auto-configure {
150                        address-ranges {
151                            authentication {
152                                password "$9$pw";
153                                username-include {
154                                    auth-server-realm realm1;
155                                    delimiter "@";
156                                    domain-name example.net;
157                                    interface-name;
158                                    source-address;
159                                    user-prefix pre;
160                                }
161                            }
162                            dynamic-profile DP {
163                                network 10.0.0.0/8 {
164                                    range r1 {
165                                        low 10.0.0.1;
166                                        high 10.0.0.9;
167                                    }
168                                    range r2;
169                                }
170                            }
171                        }
172                    }
173                }
174                inet6 {
175                    address destination;
176                }
177            }
178        }
179        unit 1 {
180            description plain;
181        }
182    }
183}
184"#,
185        ));
186        let demux = ifaces[0].units[0].demux.as_ref().unwrap();
187        let inet = demux.inet.as_ref().unwrap();
188        assert_eq!(inet.address.as_deref(), Some("source"));
189        let ranges = inet.address_ranges.as_ref().unwrap();
190        assert!(ranges.has_authentication_password);
191        assert_eq!(
192            ranges.authentication_password.as_ref().map(crate::Secret::expose),
193            expected("$9$pw")
194        );
195        let include = ranges.username_include.as_ref().unwrap();
196        assert_eq!(include.auth_server_realm.as_deref(), Some("realm1"));
197        assert_eq!(include.delimiter.as_deref(), Some("@"));
198        assert_eq!(include.domain_name.as_deref(), Some("example.net"));
199        assert!(include.interface_name && include.source_address);
200        assert_eq!(include.user_prefix.as_deref(), Some("pre"));
201        let profile = ranges.dynamic_profile.as_ref().unwrap();
202        assert_eq!(profile.name, "DP");
203        assert_eq!(profile.networks[0].address, "10.0.0.0/8");
204        assert_eq!(profile.networks[0].ranges[0].name, "r1");
205        assert_eq!(profile.networks[0].ranges[0].low.as_deref(), Some("10.0.0.1"));
206        assert_eq!(profile.networks[0].ranges[0].high.as_deref(), Some("10.0.0.9"));
207        assert!(profile.networks[0].ranges[1].low.is_none());
208        let inet6 = demux.inet6.as_ref().unwrap();
209        assert_eq!(inet6.address.as_deref(), Some("destination"));
210        assert!(inet6.address_ranges.is_none());
211        assert!(ifaces[0].units[1].demux.is_none());
212    }
213}