Skip to main content

asterust_junos/system/services/
netconf.rs

1//! `netconf`
2//! `system { services { netconf { ... } } }`.
3//!
4//! # See also
5//!
6//! - [netconf](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/netconf-edit-system-services.html)
7//! - [ssh (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssh-edit-system-services-netconf.html)
8//! - [hello-message (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/hello-message-edit-system-services-netconf.html)
9//! - [netconf-monitoring (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/netconf-monitoring-edit-system-services-netconf.html)
10//! - [tls (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tls-edit-system-services-netconf.html)
11//! - [client-identity (NETCONF TLS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/client-identity-edit-system-services-netconf-tls.html)
12//! - [default-client-identity (NETCONF TLS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/default-client-identity-edit-system-services-netconf-tls.html)
13//! - [yang-compliant (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/yang-compliant-edit-system-services-netconf.html)
14//! - [yang-modules (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/yang-modules-edit-system-services-netconf.html)
15//! - [notification (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/notification-edit-system-services-netconf.html)
16//! - [traceoptions (NETCONF and Junos XML Protocol)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-system-services-netconf.html)
17
18use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
19use crate::{arg_of, child_block, has_leaf, named_children};
20use asterust::config::ConfigLine;
21
22/// The `hello-message` block: the YANG modules the server advertises in the capabilities exchange.
23#[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/// The `netconf-monitoring` block: the schemas returned for a list of supported schemas.
32#[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/// The `yang-modules` block.
40#[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/// A `client-identity <id>` of NETCONF over TLS.
50#[derive(Clone, Debug, PartialEq, Eq)]
51#[non_exhaustive]
52pub struct NetconfClientIdentity {
53    pub id: String,
54    /// The fingerprint of the client certificate.
55    pub fingerprint: Option<String>,
56    /// `san-dirname-cn` or `specified`.
57    pub map_type: Option<String>,
58    pub username: Option<String>,
59}
60
61/// The `default-client-identity` of NETCONF over TLS.
62#[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/// The `tls` block of `netconf`.
70#[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/// The `netconf ssh` block.
80#[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/// The `system services netconf` settings.
89#[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    /// `notification { interleave; }`.
100    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    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
166    #[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}