asterust-junos 0.5.12

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `netconf`
//! `system { services { netconf { ... } } }`.
//!
//! # See also
//!
//! - [netconf](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/netconf-edit-system-services.html)
//! - [ssh (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssh-edit-system-services-netconf.html)
//! - [hello-message (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/hello-message-edit-system-services-netconf.html)
//! - [netconf-monitoring (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/netconf-monitoring-edit-system-services-netconf.html)
//! - [tls (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tls-edit-system-services-netconf.html)
//! - [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)
//! - [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)
//! - [yang-compliant (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/yang-compliant-edit-system-services-netconf.html)
//! - [yang-modules (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/yang-modules-edit-system-services-netconf.html)
//! - [notification (NETCONF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/notification-edit-system-services-netconf.html)
//! - [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)

use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{arg_of, child_block, has_leaf, named_children};
use asterust::config::ConfigLine;

/// The `hello-message` block: the YANG modules the server advertises in the capabilities exchange.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NetconfHelloMessage {
    pub advertise_native_yang_modules: bool,
    pub advertise_custom_yang_modules: bool,
    pub advertise_standard_yang_modules: bool,
}

/// The `netconf-monitoring` block: the schemas returned for a list of supported schemas.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NetconfMonitoring {
    pub retrieve_custom_yang_modules: bool,
    pub retrieve_standard_yang_modules: bool,
}

/// The `yang-modules` block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NetconfYangModules {
    pub device_specific: bool,
    pub emit_anyxml_in_rpc_output: bool,
    pub emit_extensions: bool,
    pub emit_family_ns_and_module_name: bool,
}

/// A `client-identity <id>` of NETCONF over TLS.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NetconfClientIdentity {
    pub id: String,
    /// The fingerprint of the client certificate.
    pub fingerprint: Option<String>,
    /// `san-dirname-cn` or `specified`.
    pub map_type: Option<String>,
    pub username: Option<String>,
}

/// The `default-client-identity` of NETCONF over TLS.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NetconfDefaultClientIdentity {
    pub map_type: Option<String>,
    pub username: Option<String>,
}

/// The `tls` block of `netconf`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NetconfTls {
    pub client_identities: Vec<NetconfClientIdentity>,
    pub default_client_identity: Option<NetconfDefaultClientIdentity>,
    pub local_certificate: Option<String>,
    pub traceoptions: Option<Traceoptions>,
}

/// The `netconf ssh` block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NetconfSsh {
    pub port: Option<String>,
    pub connection_limit: Option<String>,
    pub rate_limit: Option<String>,
}

/// The `system services netconf` settings.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Netconf {
    pub ssh: Option<NetconfSsh>,
    pub rfc_compliant: bool,
    pub hello_message: Option<NetconfHelloMessage>,
    pub netconf_monitoring: Option<NetconfMonitoring>,
    pub tls: Option<NetconfTls>,
    pub yang_compliant: bool,
    pub yang_modules: Option<NetconfYangModules>,
    /// `notification { interleave; }`.
    pub notification_interleave: bool,
    pub traceoptions: Option<Traceoptions>,
}

pub(super) fn parse_netconf(netconf: &ConfigLine) -> Netconf {
    Netconf {
        ssh: child_block(netconf, "ssh").map(|ssh| NetconfSsh {
            port: arg_of(ssh, "port").map(str::to_string),
            connection_limit: arg_of(ssh, "connection-limit").map(str::to_string),
            rate_limit: arg_of(ssh, "rate-limit").map(str::to_string),
        }),
        rfc_compliant: has_leaf(netconf, "rfc-compliant"),
        hello_message: child_block(netconf, "hello-message").map(|hello| {
            let capabilities = child_block(hello, "yang-module-capabilities");
            let advertises = |name: &str| capabilities.is_some_and(|c| has_leaf(c, name));
            NetconfHelloMessage {
                advertise_native_yang_modules: advertises("advertise-native-yang-modules"),
                advertise_custom_yang_modules: advertises("advertise-custom-yang-modules"),
                advertise_standard_yang_modules: advertises("advertise-standard-yang-modules"),
            }
        }),
        netconf_monitoring: child_block(netconf, "netconf-monitoring").map(|monitoring| {
            let schemas = child_block(monitoring, "netconf-state-schemas");
            let retrieves = |name: &str| schemas.is_some_and(|s| has_leaf(s, name));
            NetconfMonitoring {
                retrieve_custom_yang_modules: retrieves("retrieve-custom-yang-modules"),
                retrieve_standard_yang_modules: retrieves("retrieve-standard-yang-modules"),
            }
        }),
        tls: child_block(netconf, "tls").map(|tls| NetconfTls {
            client_identities: named_children(tls, "client-identity")
                .map(|(id, identity)| NetconfClientIdentity {
                    id: id.to_string(),
                    fingerprint: arg_of(identity, "fingerprint").map(str::to_string),
                    map_type: arg_of(identity, "map-type").map(str::to_string),
                    username: arg_of(identity, "username").map(str::to_string),
                })
                .collect(),
            default_client_identity: child_block(tls, "default-client-identity").map(|identity| {
                NetconfDefaultClientIdentity {
                    map_type: arg_of(identity, "map-type").map(str::to_string),
                    username: arg_of(identity, "username").map(str::to_string),
                }
            }),
            local_certificate: arg_of(tls, "local-certificate").map(str::to_string),
            traceoptions: parse_traceoptions(tls),
        }),
        yang_compliant: has_leaf(netconf, "yang-compliant"),
        yang_modules: child_block(netconf, "yang-modules").map(|modules| NetconfYangModules {
            device_specific: has_leaf(modules, "device-specific"),
            emit_anyxml_in_rpc_output: has_leaf(modules, "emit-anyxml-in-rpc-output"),
            emit_extensions: has_leaf(modules, "emit-extensions"),
            emit_family_ns_and_module_name: has_leaf(modules, "emit-family-ns-and-module-name"),
        }),
        notification_interleave: child_block(netconf, "notification")
            .is_some_and(|notification| has_leaf(notification, "interleave")),
        traceoptions: parse_traceoptions(netconf),
    }
}

#[cfg(test)]
mod tests {
    use crate::system::system;
    use asterust::config::juniper;

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_the_yang_and_monitoring_options() {
        let netconf = system(&juniper::parse_braces(
            r#"
system {
    services {
        netconf {
            yang-compliant;
            hello-message {
                yang-module-capabilities {
                    advertise-native-yang-modules;
                    advertise-standard-yang-modules;
                }
            }
            netconf-monitoring {
                netconf-state-schemas {
                    retrieve-custom-yang-modules;
                }
            }
            yang-modules {
                device-specific;
                emit-extensions;
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .netconf
        .unwrap();
        assert!(netconf.yang_compliant);
        let hello = netconf.hello_message.unwrap();
        assert!(hello.advertise_native_yang_modules);
        assert!(!hello.advertise_custom_yang_modules);
        assert!(hello.advertise_standard_yang_modules);
        let monitoring = netconf.netconf_monitoring.unwrap();
        assert!(monitoring.retrieve_custom_yang_modules);
        assert!(!monitoring.retrieve_standard_yang_modules);
        let modules = netconf.yang_modules.unwrap();
        assert!(modules.device_specific);
        assert!(modules.emit_extensions);
        assert!(!modules.emit_anyxml_in_rpc_output);
        assert!(!modules.emit_family_ns_and_module_name);
    }

    #[test]
    fn extracts_notification_and_traceoptions() {
        let netconf = system(&juniper::parse_braces(
            "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",
        ))
        .unwrap()
        .services
        .unwrap()
        .netconf
        .unwrap();
        assert!(netconf.notification_interleave);
        assert_eq!(netconf.traceoptions.unwrap().flags[0].name, "incoming-xml");
    }

    #[test]
    fn extracts_netconf_over_tls() {
        let tls = system(&juniper::parse_braces(
            r#"
system {
    services {
        netconf {
            tls {
                client-identity mgr1 {
                    fingerprint "AB:CD:EF";
                    map-type specified;
                    username netconf-user;
                }
                default-client-identity {
                    map-type san-dirname-cn;
                }
                local-certificate nc-cert;
                traceoptions {
                    file nc-tls.log;
                    level info;
                }
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .netconf
        .unwrap()
        .tls
        .unwrap();
        assert_eq!(tls.client_identities.len(), 1);
        assert_eq!(tls.client_identities[0].id, "mgr1");
        assert_eq!(tls.client_identities[0].fingerprint.as_deref(), Some("AB:CD:EF"));
        assert_eq!(tls.client_identities[0].map_type.as_deref(), Some("specified"));
        assert_eq!(tls.client_identities[0].username.as_deref(), Some("netconf-user"));
        let default = tls.default_client_identity.unwrap();
        assert_eq!(default.map_type.as_deref(), Some("san-dirname-cn"));
        assert_eq!(default.username, None);
        assert_eq!(tls.local_certificate.as_deref(), Some("nc-cert"));
        assert_eq!(tls.traceoptions.unwrap().level.as_deref(), Some("info"));
    }

    #[test]
    fn bare_netconf_ssh_has_no_settings() {
        let netconf = system(&juniper::parse_braces(
            "system {\n    services {\n        netconf {\n            ssh;\n        }\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap()
        .netconf
        .unwrap();
        let ssh = netconf.ssh.unwrap();
        assert_eq!(ssh.port, None);
        assert!(!netconf.rfc_compliant);
    }
}