asterust-junos 0.5.13

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
//! `demux`
//! `interfaces { <name> { unit <n> { demux { inet | inet6 { ... } } } } }`.
//!
//! # See also
//!
//! - [demux (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/demux-edit-interfaces.html)
//! - [address-ranges (Demux)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/address-range-edit-interfaces.html)
//! - [username-include (Demux)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/username-include-edit-interfaces.html)
//! - [dynamic-profile (Demux)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dynamic-profile-demux-edit-interfaces.html)

use crate::{arg_of, child_block, has_leaf, named_children, secret_value};
use asterust::config::ConfigLine;

/// A `range <name>` of a demux dynamic profile network.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DemuxRange {
    pub name: String,
    pub low: Option<String>,
    pub high: Option<String>,
}

/// A `network <address>` of a demux dynamic profile.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DemuxNetwork {
    pub address: String,
    pub ranges: Vec<DemuxRange>,
}

/// The `dynamic-profile` of the demux address ranges.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DemuxDynamicProfile {
    pub name: String,
    pub networks: Vec<DemuxNetwork>,
}

/// The `username-include` of the demux address ranges.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DemuxUsernameInclude {
    pub auth_server_realm: Option<String>,
    pub delimiter: Option<String>,
    pub domain_name: Option<String>,
    pub interface_name: bool,
    pub source_address: bool,
    pub user_prefix: Option<String>,
}

/// The `address-ranges` of a demux family's `auto-configure`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DemuxAddressRanges {
    /// An `authentication { password }` is configured.
    pub has_authentication_password: bool,
    /// The authentication password; `None` unless the `secrets` feature is enabled.
    pub authentication_password: Option<crate::Secret>,
    pub username_include: Option<DemuxUsernameInclude>,
    pub dynamic_profile: Option<DemuxDynamicProfile>,
}

/// The `inet` or `inet6` block of `demux`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DemuxFamily {
    /// `source` or `destination`, from `address`.
    pub address: Option<String>,
    pub address_ranges: Option<DemuxAddressRanges>,
}

/// The `demux` block of a unit.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Demux {
    pub inet: Option<DemuxFamily>,
    pub inet6: Option<DemuxFamily>,
}

fn parse_address_ranges(ranges: &ConfigLine) -> DemuxAddressRanges {
    let authentication = child_block(ranges, "authentication");
    let password = authentication.and_then(|a| arg_of(a, "password"));
    DemuxAddressRanges {
        has_authentication_password: password.is_some(),
        authentication_password: secret_value(password),
        username_include: authentication
            .and_then(|a| child_block(a, "username-include"))
            .map(|include| DemuxUsernameInclude {
                auth_server_realm: arg_of(include, "auth-server-realm").map(str::to_string),
                delimiter: arg_of(include, "delimiter").map(str::to_string),
                domain_name: arg_of(include, "domain-name").map(str::to_string),
                interface_name: has_leaf(include, "interface-name"),
                source_address: has_leaf(include, "source-address"),
                user_prefix: arg_of(include, "user-prefix").map(str::to_string),
            }),
        dynamic_profile: named_children(ranges, "dynamic-profile")
            .next()
            .map(|(name, profile)| DemuxDynamicProfile {
                name: name.to_string(),
                networks: named_children(profile, "network")
                    .map(|(address, network)| DemuxNetwork {
                        address: address.to_string(),
                        ranges: named_children(network, "range")
                            .map(|(name, range)| DemuxRange {
                                name: name.to_string(),
                                low: arg_of(range, "low").map(str::to_string),
                                high: arg_of(range, "high").map(str::to_string),
                            })
                            .collect(),
                    })
                    .collect(),
            }),
    }
}

fn parse_demux_family(family: &ConfigLine) -> DemuxFamily {
    DemuxFamily {
        address: arg_of(family, "address").map(str::to_string),
        address_ranges: child_block(family, "auto-configure")
            .and_then(|auto| child_block(auto, "address-ranges"))
            .map(parse_address_ranges),
    }
}

pub(super) fn parse_demux(demux: &ConfigLine) -> Demux {
    Demux {
        inet: child_block(demux, "inet").map(parse_demux_family),
        inet6: child_block(demux, "inet6").map(parse_demux_family),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_demux() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ge-0/0/0 {
        unit 0 {
            demux {
                inet {
                    address source;
                    auto-configure {
                        address-ranges {
                            authentication {
                                password "$9$pw";
                                username-include {
                                    auth-server-realm realm1;
                                    delimiter "@";
                                    domain-name example.net;
                                    interface-name;
                                    source-address;
                                    user-prefix pre;
                                }
                            }
                            dynamic-profile DP {
                                network 10.0.0.0/8 {
                                    range r1 {
                                        low 10.0.0.1;
                                        high 10.0.0.9;
                                    }
                                    range r2;
                                }
                            }
                        }
                    }
                }
                inet6 {
                    address destination;
                }
            }
        }
        unit 1 {
            description plain;
        }
    }
}
"#,
        ));
        let demux = ifaces[0].units[0].demux.as_ref().unwrap();
        let inet = demux.inet.as_ref().unwrap();
        assert_eq!(inet.address.as_deref(), Some("source"));
        let ranges = inet.address_ranges.as_ref().unwrap();
        assert!(ranges.has_authentication_password);
        assert_eq!(
            ranges.authentication_password.as_ref().map(crate::Secret::expose),
            expected("$9$pw")
        );
        let include = ranges.username_include.as_ref().unwrap();
        assert_eq!(include.auth_server_realm.as_deref(), Some("realm1"));
        assert_eq!(include.delimiter.as_deref(), Some("@"));
        assert_eq!(include.domain_name.as_deref(), Some("example.net"));
        assert!(include.interface_name && include.source_address);
        assert_eq!(include.user_prefix.as_deref(), Some("pre"));
        let profile = ranges.dynamic_profile.as_ref().unwrap();
        assert_eq!(profile.name, "DP");
        assert_eq!(profile.networks[0].address, "10.0.0.0/8");
        assert_eq!(profile.networks[0].ranges[0].name, "r1");
        assert_eq!(profile.networks[0].ranges[0].low.as_deref(), Some("10.0.0.1"));
        assert_eq!(profile.networks[0].ranges[0].high.as_deref(), Some("10.0.0.9"));
        assert!(profile.networks[0].ranges[1].low.is_none());
        let inet6 = demux.inet6.as_ref().unwrap();
        assert_eq!(inet6.address.as_deref(), Some("destination"));
        assert!(inet6.address_ranges.is_none());
        assert!(ifaces[0].units[1].demux.is_none());
    }
}