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
//! `multiservice-options`
//! `interfaces { <name> { multiservice-options { ... } } }`.
//!
//! # See also
//!
//! - [multiservice-options (Flow Monitoring)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/multiservice-options-edit-interfaces.html)
//! - [multiservice-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/multiservice-options-edit-interfaces-ni.html)
//! - [flow-control-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/flow-control-options-edit-interfaces.html)
//! - [dump-on-flow-control](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dump-on-flow-control-edit-interfaces.html)
//! - [pic-boot-timeout](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pic-boot-timeout-edit-interfaces-multiservice-options.html)

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

/// The `multiservice-options` of a monitoring services interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MultiserviceOptions {
    /// `Some(true)` for `core-dump`, `Some(false)` for `no-core-dump`.
    pub core_dump: Option<bool>,
    /// `Some(true)` for `syslog`, `Some(false)` for `no-syslog`.
    pub syslog: Option<bool>,
    /// Seconds, `240` or `300`.
    pub pic_boot_timeout: Option<String>,
    /// From the `flow-control-options` block or written directly under `multiservice-options`.
    pub down_on_flow_control: bool,
    pub dump_on_flow_control: bool,
    pub reset_on_flow_control: bool,
    pub up_on_flow_control: bool,
}

pub(super) fn parse_multiservice_options(options: &ConfigLine) -> MultiserviceOptions {
    let flow_control = |name| {
        has_leaf(options, name)
            || child_block(options, "flow-control-options").is_some_and(|block| has_leaf(block, name))
    };
    MultiserviceOptions {
        core_dump: toggle(options, "core-dump"),
        syslog: toggle(options, "syslog"),
        pic_boot_timeout: arg_of(options, "pic-boot-timeout").map(str::to_string),
        down_on_flow_control: flow_control("down-on-flow-control"),
        dump_on_flow_control: flow_control("dump-on-flow-control"),
        reset_on_flow_control: flow_control("reset-on-flow-control"),
        up_on_flow_control: flow_control("up-on-flow-control"),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_multiservice_options() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    mo-1/0/0 {
        multiservice-options {
            core-dump;
            no-syslog;
            pic-boot-timeout 300;
            flow-control-options {
                down-on-flow-control;
                reset-on-flow-control;
            }
        }
    }
    mo-1/0/1 {
        multiservice-options {
            no-core-dump;
            syslog;
            dump-on-flow-control;
        }
    }
}
"#,
        ));
        let first = ifaces[0].multiservice_options.as_ref().unwrap();
        assert_eq!(first.core_dump, Some(true));
        assert_eq!(first.syslog, Some(false));
        assert_eq!(first.pic_boot_timeout.as_deref(), Some("300"));
        assert!(first.down_on_flow_control && first.reset_on_flow_control);
        assert!(!first.dump_on_flow_control && !first.up_on_flow_control);
        let second = ifaces[1].multiservice_options.as_ref().unwrap();
        assert_eq!(second.core_dump, Some(false));
        assert_eq!(second.syslog, Some(true));
        assert!(second.dump_on_flow_control);
    }
}