1mod accounting;
4mod addressing;
5mod archival;
6mod auth;
7mod commit;
8mod internet;
9mod license;
10mod location;
11mod login;
12mod ntp;
13mod ports;
14mod processes;
15mod scripts;
16mod services;
17mod subscriber;
18mod syslog;
19
20pub use accounting::{Accounting, AccountingDestination};
21pub use addressing::{BackupRouter, NameServer, StaticHostMapping};
22pub use archival::{Archival, ArchivalConfiguration};
23pub use auth::{MasterPassword, RadiusServer, RootAuthentication, TacplusServer};
24pub use commit::Commit;
25pub use internet::{IcmpRateLimit, InternetOptions};
26pub use license::{License, LicenseAutoupdate, LicenseRenew};
27pub use location::Location;
28pub use login::{Login, LoginClass, LoginPassword, LoginRetryOptions, LoginUser};
29pub use ntp::{Ntp, NtpAuthenticationKey, NtpHost};
30pub use ports::{Auxiliary, Console, Ports};
31pub use processes::Process;
32pub use scripts::Scripts;
33pub use services::{
34 Netconf, NetconfSsh, OutboundSshClient, OutboundSshServer, Services, SshService, WebManagement, WebManagementHttp,
35 WebManagementHttps, XnmClearText, XnmSsl,
36};
37pub use subscriber::{
38 AaclStatisticsProfile, DhcpLocalServer, DhcpLocalServerGroup, DynamicProfileOptions, LocalPolicyDecisionFunction,
39 RedundancyInterface, StaticSubscriberGroup, StaticSubscriberProfile, StaticSubscribers, SubscriberManagement,
40 SubscriberRedundancy,
41};
42pub use syslog::{Syslog, SyslogArchive, SyslogHost, SyslogRule, SyslogStructuredData, SyslogTarget};
43
44use crate::{arg_of, args_of, child_block, has_leaf, list_values, top_level};
45use accounting::parse_accounting;
46use addressing::{name_servers, parse_backup_router, static_host_mappings};
47use archival::parse_archival;
48use asterust::config::Config;
49use auth::{parse_master_password, password_methods, radius_servers, root_authentication, tacplus_servers};
50use commit::parse_commit;
51use internet::parse_internet_options;
52use license::parse_license;
53use location::parse_location;
54use login::parse_login;
55use ntp::parse_ntp;
56use ports::parse_ports;
57use processes::parse_processes;
58use scripts::parse_scripts;
59use services::parse_services;
60use subscriber::parse_dynamic_profile_options;
61use syslog::parse_syslog;
62
63#[derive(Clone, Debug, PartialEq, Eq)]
65#[non_exhaustive]
66pub struct System {
67 pub host_name: Option<String>,
68 pub domain_name: Option<String>,
69 pub time_zone: Option<String>,
71 pub domain_search: Vec<String>,
72 pub static_host_mappings: Vec<StaticHostMapping>,
73 pub backup_router: Option<BackupRouter>,
74 pub inet6_backup_router: Option<BackupRouter>,
75 pub authentication_order: Vec<String>,
77 pub radius_servers: Vec<RadiusServer>,
78 pub tacplus_servers: Vec<TacplusServer>,
79 pub location: Option<Location>,
80 pub ports: Option<Ports>,
81 pub max_configurations_on_flash: Option<String>,
82 pub max_configuration_rollbacks: Option<String>,
83 pub no_redirects: bool,
84 pub default_address_selection: bool,
86 pub internet_options: Option<InternetOptions>,
87 pub commit: Option<Commit>,
88 pub processes: Vec<Process>,
89 pub dynamic_profile_options: Option<DynamicProfileOptions>,
90 pub scripts: Option<Scripts>,
91 pub no_multicast_echo: bool,
92 pub no_ping_record_route: bool,
93 pub no_ping_time_stamp: bool,
94 pub auto_snapshot: bool,
95 pub compress_configuration_files: bool,
96 pub mirror_flash_on_disk: bool,
97 pub switchover_on_routing_crash: bool,
98 pub use_imported_time_zones: bool,
99 pub archival: Option<Archival>,
100 pub accounting: Option<Accounting>,
101 pub root_authentication: Option<RootAuthentication>,
102 pub master_password: Option<MasterPassword>,
103 pub diag_port_authentication: Vec<String>,
105 pub pic_console_authentication: Vec<String>,
107 pub license: Option<License>,
108 pub name_servers: Vec<NameServer>,
109 pub ntp: Option<Ntp>,
110 pub syslog: Option<Syslog>,
111 pub login: Option<Login>,
112 pub services: Option<Services>,
113}
114
115pub fn system(config: &Config) -> Option<System> {
117 let system = top_level(config, "system")?;
118 Some(System {
119 host_name: arg_of(system, "host-name").map(str::to_string),
120 domain_name: arg_of(system, "domain-name").map(str::to_string),
121 time_zone: arg_of(system, "time-zone").map(str::to_string),
122 domain_search: args_of(system, "domain-search")
123 .into_iter()
124 .flat_map(list_values)
125 .collect(),
126 static_host_mappings: static_host_mappings(system),
127 backup_router: arg_of(system, "backup-router").map(parse_backup_router),
128 inet6_backup_router: arg_of(system, "inet6-backup-router").map(parse_backup_router),
129 authentication_order: args_of(system, "authentication-order")
130 .into_iter()
131 .flat_map(list_values)
132 .collect(),
133 radius_servers: radius_servers(system, "radius-server"),
134 tacplus_servers: tacplus_servers(system, "tacplus-server"),
135 location: child_block(system, "location").map(parse_location),
136 ports: child_block(system, "ports").map(parse_ports),
137 max_configurations_on_flash: arg_of(system, "max-configurations-on-flash").map(str::to_string),
138 max_configuration_rollbacks: arg_of(system, "max-configuration-rollbacks").map(str::to_string),
139 no_redirects: has_leaf(system, "no-redirects"),
140 default_address_selection: has_leaf(system, "default-address-selection"),
141 internet_options: child_block(system, "internet-options").map(parse_internet_options),
142 commit: child_block(system, "commit").map(parse_commit),
143 processes: child_block(system, "processes")
144 .map(parse_processes)
145 .unwrap_or_default(),
146 dynamic_profile_options: child_block(system, "dynamic-profile-options").map(parse_dynamic_profile_options),
147 scripts: child_block(system, "scripts").map(parse_scripts),
148 no_multicast_echo: has_leaf(system, "no-multicast-echo"),
149 no_ping_record_route: has_leaf(system, "no-ping-record-route"),
150 no_ping_time_stamp: has_leaf(system, "no-ping-time-stamp"),
151 auto_snapshot: has_leaf(system, "auto-snapshot"),
152 compress_configuration_files: has_leaf(system, "compress-configuration-files"),
153 mirror_flash_on_disk: has_leaf(system, "mirror-flash-on-disk"),
154 switchover_on_routing_crash: has_leaf(system, "switchover-on-routing-crash"),
155 use_imported_time_zones: has_leaf(system, "use-imported-time-zones"),
156 archival: child_block(system, "archival").map(parse_archival),
157 accounting: child_block(system, "accounting").map(parse_accounting),
158 root_authentication: root_authentication(system),
159 master_password: child_block(system, "master-password").map(parse_master_password),
160 diag_port_authentication: password_methods(system, "diag-port-authentication"),
161 pic_console_authentication: password_methods(system, "pic-console-authentication"),
162 license: child_block(system, "license").map(parse_license),
163 name_servers: name_servers(system),
164 ntp: child_block(system, "ntp").map(parse_ntp),
165 syslog: child_block(system, "syslog").map(parse_syslog),
166 login: child_block(system, "login").map(parse_login),
167 services: child_block(system, "services").map(parse_services),
168 })
169}
170
171#[cfg(test)]
172mod tests {
173 use super::*;
174 use asterust::config::juniper;
175
176 #[test]
178 fn extracts_host_name() {
179 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
180 assert_eq!(system.host_name.as_deref(), Some("pe1"));
181 }
182
183 #[test]
184 fn host_name_is_optional() {
185 let system = system(&juniper::parse_braces("system {\n domain-name example.net;\n}\n")).unwrap();
186 assert_eq!(system.host_name, None);
187 }
188
189 #[test]
190 fn skips_inactive_host_name() {
191 let system = system(&juniper::parse_braces("system {\n inactive: host-name pe1;\n}\n")).unwrap();
192 assert_eq!(system.host_name, None);
193 }
194
195 #[test]
196 fn extracts_domain_name() {
197 let system = system(&juniper::parse_braces(
198 "system {\n host-name pe1;\n domain-name example.net;\n}\n",
199 ))
200 .unwrap();
201 assert_eq!(system.domain_name.as_deref(), Some("example.net"));
202 }
203
204 #[test]
205 fn domain_name_is_optional() {
206 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
207 assert_eq!(system.domain_name, None);
208 }
209
210 #[test]
211 fn extracts_time_zone() {
212 let system = system(&juniper::parse_braces("system {\n time-zone America/New_York;\n}\n")).unwrap();
213 assert_eq!(system.time_zone.as_deref(), Some("America/New_York"));
214 }
215
216 #[test]
217 fn time_zone_is_optional() {
218 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
219 assert_eq!(system.time_zone, None);
220 }
221
222 #[test]
223 fn extracts_configuration_limits_and_no_redirects() {
224 let system = system(&juniper::parse_braces(
225 "system {\n max-configurations-on-flash 5;\n max-configuration-rollbacks 20;\n no-redirects;\n}\n",
226 ))
227 .unwrap();
228 assert_eq!(system.max_configurations_on_flash.as_deref(), Some("5"));
229 assert_eq!(system.max_configuration_rollbacks.as_deref(), Some("20"));
230 assert!(system.no_redirects);
231 }
232
233 #[test]
234 fn configuration_limits_are_optional() {
235 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
236 assert_eq!(system.max_configurations_on_flash, None);
237 assert_eq!(system.max_configuration_rollbacks, None);
238 assert!(!system.no_redirects);
239 }
240
241 #[test]
242 fn extracts_system_flags() {
243 let system = system(&juniper::parse_braces(
244 "system {\n no-multicast-echo;\n no-ping-record-route;\n no-ping-time-stamp;\n auto-snapshot;\n compress-configuration-files;\n mirror-flash-on-disk;\n switchover-on-routing-crash;\n use-imported-time-zones;\n}\n",
245 ))
246 .unwrap();
247 assert!(system.no_multicast_echo);
248 assert!(system.no_ping_record_route);
249 assert!(system.no_ping_time_stamp);
250 assert!(system.auto_snapshot);
251 assert!(system.compress_configuration_files);
252 assert!(system.mirror_flash_on_disk);
253 assert!(system.switchover_on_routing_crash);
254 assert!(system.use_imported_time_zones);
255 }
256
257 #[test]
258 fn system_flags_default_to_off() {
259 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
260 assert!(!system.no_multicast_echo);
261 assert!(!system.auto_snapshot);
262 assert!(!system.use_imported_time_zones);
263 }
264
265 #[test]
266 fn no_system_is_none() {
267 assert!(system(&juniper::parse_braces("interfaces {\n lo0;\n}\n")).is_none());
268 }
269}