asterust_junos/system/services/
netconf.rs1use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
19use crate::{arg_of, child_block, has_leaf, named_children};
20use asterust::config::ConfigLine;
21
22#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct NetconfHelloMessage {
26 pub advertise_native_yang_modules: bool,
27 pub advertise_custom_yang_modules: bool,
28 pub advertise_standard_yang_modules: bool,
29}
30
31#[derive(Clone, Debug, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct NetconfMonitoring {
35 pub retrieve_custom_yang_modules: bool,
36 pub retrieve_standard_yang_modules: bool,
37}
38
39#[derive(Clone, Debug, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct NetconfYangModules {
43 pub device_specific: bool,
44 pub emit_anyxml_in_rpc_output: bool,
45 pub emit_extensions: bool,
46 pub emit_family_ns_and_module_name: bool,
47}
48
49#[derive(Clone, Debug, PartialEq, Eq)]
51#[non_exhaustive]
52pub struct NetconfClientIdentity {
53 pub id: String,
54 pub fingerprint: Option<String>,
56 pub map_type: Option<String>,
58 pub username: Option<String>,
59}
60
61#[derive(Clone, Debug, PartialEq, Eq)]
63#[non_exhaustive]
64pub struct NetconfDefaultClientIdentity {
65 pub map_type: Option<String>,
66 pub username: Option<String>,
67}
68
69#[derive(Clone, Debug, PartialEq, Eq)]
71#[non_exhaustive]
72pub struct NetconfTls {
73 pub client_identities: Vec<NetconfClientIdentity>,
74 pub default_client_identity: Option<NetconfDefaultClientIdentity>,
75 pub local_certificate: Option<String>,
76 pub traceoptions: Option<Traceoptions>,
77}
78
79#[derive(Clone, Debug, PartialEq, Eq)]
81#[non_exhaustive]
82pub struct NetconfSsh {
83 pub port: Option<String>,
84 pub connection_limit: Option<String>,
85 pub rate_limit: Option<String>,
86}
87
88#[derive(Clone, Debug, PartialEq, Eq)]
90#[non_exhaustive]
91pub struct Netconf {
92 pub ssh: Option<NetconfSsh>,
93 pub rfc_compliant: bool,
94 pub hello_message: Option<NetconfHelloMessage>,
95 pub netconf_monitoring: Option<NetconfMonitoring>,
96 pub tls: Option<NetconfTls>,
97 pub yang_compliant: bool,
98 pub yang_modules: Option<NetconfYangModules>,
99 pub notification_interleave: bool,
101 pub traceoptions: Option<Traceoptions>,
102}
103
104pub(super) fn parse_netconf(netconf: &ConfigLine) -> Netconf {
105 Netconf {
106 ssh: child_block(netconf, "ssh").map(|ssh| NetconfSsh {
107 port: arg_of(ssh, "port").map(str::to_string),
108 connection_limit: arg_of(ssh, "connection-limit").map(str::to_string),
109 rate_limit: arg_of(ssh, "rate-limit").map(str::to_string),
110 }),
111 rfc_compliant: has_leaf(netconf, "rfc-compliant"),
112 hello_message: child_block(netconf, "hello-message").map(|hello| {
113 let capabilities = child_block(hello, "yang-module-capabilities");
114 let advertises = |name: &str| capabilities.is_some_and(|c| has_leaf(c, name));
115 NetconfHelloMessage {
116 advertise_native_yang_modules: advertises("advertise-native-yang-modules"),
117 advertise_custom_yang_modules: advertises("advertise-custom-yang-modules"),
118 advertise_standard_yang_modules: advertises("advertise-standard-yang-modules"),
119 }
120 }),
121 netconf_monitoring: child_block(netconf, "netconf-monitoring").map(|monitoring| {
122 let schemas = child_block(monitoring, "netconf-state-schemas");
123 let retrieves = |name: &str| schemas.is_some_and(|s| has_leaf(s, name));
124 NetconfMonitoring {
125 retrieve_custom_yang_modules: retrieves("retrieve-custom-yang-modules"),
126 retrieve_standard_yang_modules: retrieves("retrieve-standard-yang-modules"),
127 }
128 }),
129 tls: child_block(netconf, "tls").map(|tls| NetconfTls {
130 client_identities: named_children(tls, "client-identity")
131 .map(|(id, identity)| NetconfClientIdentity {
132 id: id.to_string(),
133 fingerprint: arg_of(identity, "fingerprint").map(str::to_string),
134 map_type: arg_of(identity, "map-type").map(str::to_string),
135 username: arg_of(identity, "username").map(str::to_string),
136 })
137 .collect(),
138 default_client_identity: child_block(tls, "default-client-identity").map(|identity| {
139 NetconfDefaultClientIdentity {
140 map_type: arg_of(identity, "map-type").map(str::to_string),
141 username: arg_of(identity, "username").map(str::to_string),
142 }
143 }),
144 local_certificate: arg_of(tls, "local-certificate").map(str::to_string),
145 traceoptions: parse_traceoptions(tls),
146 }),
147 yang_compliant: has_leaf(netconf, "yang-compliant"),
148 yang_modules: child_block(netconf, "yang-modules").map(|modules| NetconfYangModules {
149 device_specific: has_leaf(modules, "device-specific"),
150 emit_anyxml_in_rpc_output: has_leaf(modules, "emit-anyxml-in-rpc-output"),
151 emit_extensions: has_leaf(modules, "emit-extensions"),
152 emit_family_ns_and_module_name: has_leaf(modules, "emit-family-ns-and-module-name"),
153 }),
154 notification_interleave: child_block(netconf, "notification")
155 .is_some_and(|notification| has_leaf(notification, "interleave")),
156 traceoptions: parse_traceoptions(netconf),
157 }
158}
159
160#[cfg(test)]
161mod tests {
162 use crate::system::system;
163 use asterust::config::juniper;
164
165 #[test]
167 fn extracts_the_yang_and_monitoring_options() {
168 let netconf = system(&juniper::parse_braces(
169 r#"
170system {
171 services {
172 netconf {
173 yang-compliant;
174 hello-message {
175 yang-module-capabilities {
176 advertise-native-yang-modules;
177 advertise-standard-yang-modules;
178 }
179 }
180 netconf-monitoring {
181 netconf-state-schemas {
182 retrieve-custom-yang-modules;
183 }
184 }
185 yang-modules {
186 device-specific;
187 emit-extensions;
188 }
189 }
190 }
191}
192"#,
193 ))
194 .unwrap()
195 .services
196 .unwrap()
197 .netconf
198 .unwrap();
199 assert!(netconf.yang_compliant);
200 let hello = netconf.hello_message.unwrap();
201 assert!(hello.advertise_native_yang_modules);
202 assert!(!hello.advertise_custom_yang_modules);
203 assert!(hello.advertise_standard_yang_modules);
204 let monitoring = netconf.netconf_monitoring.unwrap();
205 assert!(monitoring.retrieve_custom_yang_modules);
206 assert!(!monitoring.retrieve_standard_yang_modules);
207 let modules = netconf.yang_modules.unwrap();
208 assert!(modules.device_specific);
209 assert!(modules.emit_extensions);
210 assert!(!modules.emit_anyxml_in_rpc_output);
211 assert!(!modules.emit_family_ns_and_module_name);
212 }
213
214 #[test]
215 fn extracts_notification_and_traceoptions() {
216 let netconf = system(&juniper::parse_braces(
217 "system {\n services {\n netconf {\n notification {\n interleave;\n }\n traceoptions {\n file netconf.log;\n flag incoming-xml;\n }\n }\n }\n}\n",
218 ))
219 .unwrap()
220 .services
221 .unwrap()
222 .netconf
223 .unwrap();
224 assert!(netconf.notification_interleave);
225 assert_eq!(netconf.traceoptions.unwrap().flags[0].name, "incoming-xml");
226 }
227
228 #[test]
229 fn extracts_netconf_over_tls() {
230 let tls = system(&juniper::parse_braces(
231 r#"
232system {
233 services {
234 netconf {
235 tls {
236 client-identity mgr1 {
237 fingerprint "AB:CD:EF";
238 map-type specified;
239 username netconf-user;
240 }
241 default-client-identity {
242 map-type san-dirname-cn;
243 }
244 local-certificate nc-cert;
245 traceoptions {
246 file nc-tls.log;
247 level info;
248 }
249 }
250 }
251 }
252}
253"#,
254 ))
255 .unwrap()
256 .services
257 .unwrap()
258 .netconf
259 .unwrap()
260 .tls
261 .unwrap();
262 assert_eq!(tls.client_identities.len(), 1);
263 assert_eq!(tls.client_identities[0].id, "mgr1");
264 assert_eq!(tls.client_identities[0].fingerprint.as_deref(), Some("AB:CD:EF"));
265 assert_eq!(tls.client_identities[0].map_type.as_deref(), Some("specified"));
266 assert_eq!(tls.client_identities[0].username.as_deref(), Some("netconf-user"));
267 let default = tls.default_client_identity.unwrap();
268 assert_eq!(default.map_type.as_deref(), Some("san-dirname-cn"));
269 assert_eq!(default.username, None);
270 assert_eq!(tls.local_certificate.as_deref(), Some("nc-cert"));
271 assert_eq!(tls.traceoptions.unwrap().level.as_deref(), Some("info"));
272 }
273
274 #[test]
275 fn bare_netconf_ssh_has_no_settings() {
276 let netconf = system(&juniper::parse_braces(
277 "system {\n services {\n netconf {\n ssh;\n }\n }\n}\n",
278 ))
279 .unwrap()
280 .services
281 .unwrap()
282 .netconf
283 .unwrap();
284 let ssh = netconf.ssh.unwrap();
285 assert_eq!(ssh.port, None);
286 assert!(!netconf.rfc_compliant);
287 }
288}