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