Skip to main content

asterust_junos/system/
subscriber.rs

1//! Subscriber-management settings under `system`: `services subscriber-management`, `static-subscribers`,
2//! `dhcp-local-server`, `local-policy-decision-function` and `dynamic-profile-options`.
3
4use crate::{active_children, arg_of, args_of, child_block, has_leaf, named_children};
5use asterust::config::ConfigLine;
6
7/// An `interface <name>` of a subscriber-management `redundancy` block. The `shared-key` is not extracted.
8#[derive(Clone, Debug, PartialEq, Eq)]
9#[non_exhaustive]
10pub struct RedundancyInterface {
11    pub name: String,
12    pub local_inet_address: Option<String>,
13    pub local_inet6_address: Option<String>,
14    pub virtual_inet_address: Option<String>,
15    pub virtual_inet6_address: Option<String>,
16}
17
18/// The `redundancy` block of `subscriber-management`.
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct SubscriberRedundancy {
22    pub interfaces: Vec<RedundancyInterface>,
23    pub no_advertise_routes_on_backup: bool,
24    /// `pseudo-wire` and `vrrp`.
25    pub protocols: Vec<String>,
26}
27
28/// The `system services subscriber-management` settings. `overrides` and `traceoptions` are not extracted.
29#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct SubscriberManagement {
32    pub enable: bool,
33    /// `cgnat` is configured.
34    pub cgnat: bool,
35    pub enforce_strict_scale_limit_license: bool,
36    pub gres_route_flush_delay: Option<String>,
37    pub redundancy: Option<SubscriberRedundancy>,
38}
39
40/// A `dynamic-profile <name>` of a static-subscribers group.
41#[derive(Clone, Debug, PartialEq, Eq)]
42#[non_exhaustive]
43pub struct StaticSubscriberProfile {
44    pub name: String,
45    /// `merge` or `replace`.
46    pub aggregate_clients: Option<String>,
47}
48
49/// A `group <name>` under `static-subscribers`. Authentication settings are not extracted.
50#[derive(Clone, Debug, PartialEq, Eq)]
51#[non_exhaustive]
52pub struct StaticSubscriberGroup {
53    pub name: String,
54    pub access_profile: Option<String>,
55    pub dynamic_profiles: Vec<StaticSubscriberProfile>,
56    /// Each `interface` as written, e.g. `"ge-1/0/0.100 upto ge-1/0/0.200"` or `"ge-1/0/0.5 exclude"`.
57    pub interfaces: Vec<String>,
58}
59
60/// The `system services static-subscribers` settings. Authentication settings, which include a password, are not
61/// extracted.
62#[derive(Clone, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct StaticSubscribers {
65    pub access_profile: Option<String>,
66    pub service_profile: Option<String>,
67    pub groups: Vec<StaticSubscriberGroup>,
68}
69
70/// A `group <name>` of `dhcp-local-server`.
71#[derive(Clone, Debug, PartialEq, Eq)]
72#[non_exhaustive]
73pub struct DhcpLocalServerGroup {
74    pub name: String,
75    /// Each `interface` as written.
76    pub interfaces: Vec<String>,
77}
78
79/// The `system services dhcp-local-server` groups. The many per-group options are not extracted.
80#[derive(Clone, Debug, PartialEq, Eq)]
81#[non_exhaustive]
82pub struct DhcpLocalServer {
83    pub groups: Vec<DhcpLocalServerGroup>,
84    /// Groups under `dhcpv6`.
85    pub dhcpv6_groups: Vec<DhcpLocalServerGroup>,
86}
87
88/// An `aacl-statistics-profile <name>`.
89#[derive(Clone, Debug, PartialEq, Eq)]
90#[non_exhaustive]
91pub struct AaclStatisticsProfile {
92    pub name: String,
93    pub aacl_fields: Vec<String>,
94    pub file: Option<String>,
95    /// `interim-active-only` or `interim-full`.
96    pub record_mode: Option<String>,
97    /// Minutes.
98    pub report_interval: Option<String>,
99}
100
101/// The `system services local-policy-decision-function` settings.
102#[derive(Clone, Debug, PartialEq, Eq)]
103#[non_exhaustive]
104pub struct LocalPolicyDecisionFunction {
105    /// Profiles from `statistics`.
106    pub statistics: Vec<AaclStatisticsProfile>,
107}
108
109/// The `system dynamic-profile-options` settings.
110#[derive(Clone, Debug, PartialEq, Eq)]
111#[non_exhaustive]
112pub struct DynamicProfileOptions {
113    pub versioning: bool,
114}
115
116pub(super) fn parse_subscriber_management(management: &ConfigLine) -> SubscriberManagement {
117    SubscriberManagement {
118        enable: has_leaf(management, "enable"),
119        cgnat: child_block(management, "cgnat").is_some(),
120        enforce_strict_scale_limit_license: has_leaf(management, "enforce-strict-scale-limit-license"),
121        gres_route_flush_delay: arg_of(management, "gres-route-flush-delay").map(str::to_string),
122        redundancy: child_block(management, "redundancy").map(|redundancy| SubscriberRedundancy {
123            interfaces: named_children(redundancy, "interface")
124                .map(|(name, interface)| RedundancyInterface {
125                    name: name.to_string(),
126                    local_inet_address: arg_of(interface, "local-inet-address").map(str::to_string),
127                    local_inet6_address: arg_of(interface, "local-inet6-address").map(str::to_string),
128                    virtual_inet_address: arg_of(interface, "virtual-inet-address").map(str::to_string),
129                    virtual_inet6_address: arg_of(interface, "virtual-inet6-address").map(str::to_string),
130                })
131                .collect(),
132            no_advertise_routes_on_backup: has_leaf(redundancy, "no-advertise-routes-on-backup"),
133            protocols: child_block(redundancy, "protocol")
134                .map(|protocol| active_children(protocol).map(|p| p.text.clone()).collect())
135                .unwrap_or_default(),
136        }),
137    }
138}
139
140pub(super) fn parse_static_subscribers(subscribers: &ConfigLine) -> StaticSubscribers {
141    StaticSubscribers {
142        access_profile: arg_of(subscribers, "access-profile").map(str::to_string),
143        service_profile: arg_of(subscribers, "service-profile").map(str::to_string),
144        groups: named_children(subscribers, "group")
145            .map(|(name, group)| StaticSubscriberGroup {
146                name: name.to_string(),
147                access_profile: arg_of(group, "access-profile").map(str::to_string),
148                dynamic_profiles: named_children(group, "dynamic-profile")
149                    .map(|(name, profile)| StaticSubscriberProfile {
150                        name: name.to_string(),
151                        aggregate_clients: arg_of(profile, "aggregate-clients").map(str::to_string),
152                    })
153                    .collect(),
154                interfaces: args_of(group, "interface").into_iter().map(str::to_string).collect(),
155            })
156            .collect(),
157    }
158}
159
160pub(super) fn parse_dhcp_local_server(server: &ConfigLine) -> DhcpLocalServer {
161    let groups = |parent: &ConfigLine| -> Vec<DhcpLocalServerGroup> {
162        named_children(parent, "group")
163            .map(|(name, group)| DhcpLocalServerGroup {
164                name: name.to_string(),
165                interfaces: args_of(group, "interface").into_iter().map(str::to_string).collect(),
166            })
167            .collect()
168    };
169    DhcpLocalServer {
170        groups: groups(server),
171        dhcpv6_groups: child_block(server, "dhcpv6").map(groups).unwrap_or_default(),
172    }
173}
174
175pub(super) fn parse_local_policy_decision_function(function: &ConfigLine) -> LocalPolicyDecisionFunction {
176    LocalPolicyDecisionFunction {
177        statistics: child_block(function, "statistics")
178            .map(|statistics| {
179                named_children(statistics, "aacl-statistics-profile")
180                    .map(|(name, profile)| AaclStatisticsProfile {
181                        name: name.to_string(),
182                        aacl_fields: child_block(profile, "aacl-fields")
183                            .map(|fields| active_children(fields).map(|f| f.text.clone()).collect())
184                            .unwrap_or_default(),
185                        file: arg_of(profile, "file").map(str::to_string),
186                        record_mode: arg_of(profile, "record-mode").map(str::to_string),
187                        report_interval: arg_of(profile, "report-interval").map(str::to_string),
188                    })
189                    .collect()
190            })
191            .unwrap_or_default(),
192    }
193}
194
195pub(super) fn parse_dynamic_profile_options(options: &ConfigLine) -> DynamicProfileOptions {
196    DynamicProfileOptions {
197        versioning: has_leaf(options, "versioning"),
198    }
199}
200
201#[cfg(test)]
202mod tests {
203    use crate::system::system;
204    use asterust::config::juniper;
205
206    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
207    const SUBSCRIBERS: &str = r#"
208system {
209    dynamic-profile-options {
210        versioning;
211    }
212    services {
213        subscriber-management {
214            enable;
215            enforce-strict-scale-limit-license;
216            gres-route-flush-delay 60;
217            cgnat {
218                enable;
219            }
220            redundancy {
221                interface ae0.100 {
222                    local-inet-address 192.0.2.1;
223                    local-inet6-address 2001:db8::1;
224                    shared-key "$9$secret"; ## SECRET-DATA
225                    virtual-inet-address 192.0.2.254;
226                    virtual-inet6-address 2001:db8::fe;
227                }
228                no-advertise-routes-on-backup;
229                protocol {
230                    vrrp;
231                }
232            }
233        }
234        static-subscribers {
235            access-profile AP;
236            service-profile SP;
237            authentication {
238                password "$9$secret"; ## SECRET-DATA
239            }
240            group STATIC1 {
241                access-profile AP1;
242                authentication {
243                    password "$9$other"; ## SECRET-DATA
244                }
245                dynamic-profile PROFILE1 {
246                    aggregate-clients merge;
247                }
248                interface ge-1/0/0.100 upto ge-1/0/0.200;
249                interface ge-1/0/0.150 exclude;
250            }
251        }
252        dhcp-local-server {
253            group G1 {
254                interface ge-1/0/0.0;
255                interface ge-1/0/1.0 upto ge-1/0/1.9;
256            }
257            dhcpv6 {
258                group V6 {
259                    interface ge-2/0/0.0;
260                }
261            }
262        }
263        local-policy-decision-function {
264            statistics {
265                aacl-statistics-profile AACL1 {
266                    aacl-fields {
267                        input-bytes;
268                        output-bytes;
269                    }
270                    file aacl.stats;
271                    record-mode interim-full;
272                    report-interval 30;
273                }
274            }
275        }
276    }
277}
278"#;
279
280    #[test]
281    fn extracts_subscriber_management() {
282        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
283        let management = system.services.unwrap().subscriber_management.unwrap();
284        assert!(management.enable);
285        assert!(management.cgnat);
286        assert!(management.enforce_strict_scale_limit_license);
287        assert_eq!(management.gres_route_flush_delay.as_deref(), Some("60"));
288
289        let redundancy = management.redundancy.unwrap();
290        assert!(redundancy.no_advertise_routes_on_backup);
291        assert_eq!(redundancy.protocols, ["vrrp"]);
292        let interface = &redundancy.interfaces[0];
293        assert_eq!(interface.name, "ae0.100");
294        assert_eq!(interface.local_inet_address.as_deref(), Some("192.0.2.1"));
295        assert_eq!(interface.local_inet6_address.as_deref(), Some("2001:db8::1"));
296        assert_eq!(interface.virtual_inet_address.as_deref(), Some("192.0.2.254"));
297        assert_eq!(interface.virtual_inet6_address.as_deref(), Some("2001:db8::fe"));
298    }
299
300    #[test]
301    fn extracts_static_subscribers() {
302        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
303        let subscribers = system.services.unwrap().static_subscribers.unwrap();
304        assert_eq!(subscribers.access_profile.as_deref(), Some("AP"));
305        assert_eq!(subscribers.service_profile.as_deref(), Some("SP"));
306
307        let group = &subscribers.groups[0];
308        assert_eq!(group.name, "STATIC1");
309        assert_eq!(group.access_profile.as_deref(), Some("AP1"));
310        assert_eq!(group.dynamic_profiles[0].name, "PROFILE1");
311        assert_eq!(group.dynamic_profiles[0].aggregate_clients.as_deref(), Some("merge"));
312        assert_eq!(
313            group.interfaces,
314            ["ge-1/0/0.100 upto ge-1/0/0.200", "ge-1/0/0.150 exclude"]
315        );
316    }
317
318    #[test]
319    fn extracts_dhcp_local_server_groups() {
320        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
321        let server = system.services.unwrap().dhcp_local_server.unwrap();
322        assert_eq!(server.groups.len(), 1);
323        assert_eq!(server.groups[0].name, "G1");
324        assert_eq!(
325            server.groups[0].interfaces,
326            ["ge-1/0/0.0", "ge-1/0/1.0 upto ge-1/0/1.9"]
327        );
328        assert_eq!(server.dhcpv6_groups[0].name, "V6");
329        assert_eq!(server.dhcpv6_groups[0].interfaces, ["ge-2/0/0.0"]);
330    }
331
332    #[test]
333    fn extracts_aacl_statistics_profiles() {
334        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
335        let function = system.services.unwrap().local_policy_decision_function.unwrap();
336        let profile = &function.statistics[0];
337        assert_eq!(profile.name, "AACL1");
338        assert_eq!(profile.aacl_fields, ["input-bytes", "output-bytes"]);
339        assert_eq!(profile.file.as_deref(), Some("aacl.stats"));
340        assert_eq!(profile.record_mode.as_deref(), Some("interim-full"));
341        assert_eq!(profile.report_interval.as_deref(), Some("30"));
342    }
343
344    #[test]
345    fn extracts_dynamic_profile_options() {
346        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
347        assert!(system.dynamic_profile_options.unwrap().versioning);
348    }
349
350    #[test]
351    fn subscriber_settings_are_optional() {
352        let system = system(&juniper::parse_braces(
353            "system {\n    services {\n        ssh;\n    }\n}\n",
354        ))
355        .unwrap();
356        assert!(system.dynamic_profile_options.is_none());
357        let services = system.services.unwrap();
358        assert!(services.subscriber_management.is_none());
359        assert!(services.static_subscribers.is_none());
360        assert!(services.dhcp_local_server.is_none());
361        assert!(services.local_policy_decision_function.is_none());
362    }
363}