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
//! `services-options`
//! `interfaces { <name> { services-options { ... } } }`.
//!
//! # See also
//!
//! - [services-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/services-options-edit-interfaces.html)
//! - [services-options (Next Gen Services Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/services-options-edit-interfaces-usf.html)
//! - [ignore-errors](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ignore-errors-edit-interfaces.html)
//! - [trio-flow-offload](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/trio-flow-offload-edit-services.html)
//! - [utc-timestamp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/utc-timestamp-edit-services.html)
//! - [enable-subscriber-analysis](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/enable-subscriber-edit-interface-service-options-usf.html)
//! - [message-rate-limit (jflow-log)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/message-rate-limit-edit-interfaces-services-options.html)

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

/// The `session-limit` of the services options.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SessionLimit {
    pub maximum: Option<String>,
    /// New sessions per second.
    pub rate: Option<String>,
    /// Percentage.
    pub cpu_load_threshold: Option<String>,
}

/// A `host` of the services `syslog`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServicesSyslogHost {
    pub name: String,
    pub facility_override: Option<String>,
    pub log_prefix: Option<String>,
    pub port: Option<String>,
    pub services: Option<String>,
}

/// The `syslog` block of the services options.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServicesSyslog {
    pub message_rate_limit: Option<String>,
    pub hosts: Vec<ServicesSyslogHost>,
}

/// The `services-options` of a services interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServicesOptions {
    pub cgn_pic: bool,
    pub close_timeout: Option<String>,
    pub fragment_limit: Option<String>,
    pub disable_global_timeout_override: bool,
    pub enable_subscriber_analysis: bool,
    pub utc_timestamp: bool,
    /// The `<alg> <tcp>` words of `ignore-errors`, or none for a bare statement.
    pub ignore_errors: Option<Vec<String>>,
    pub inactivity_non_tcp_timeout: Option<String>,
    pub inactivity_tcp_timeout: Option<String>,
    pub inactivity_timeout: Option<String>,
    pub open_timeout: Option<String>,
    pub pba_interim_logging_interval: Option<String>,
    pub reassembly_timeout: Option<String>,
    pub session_timeout: Option<String>,
    pub tcp_tickles: Option<String>,
    /// From `trio-flow-offload minimum-bytes`.
    pub trio_flow_offload_minimum_bytes: Option<String>,
    pub session_limit: Option<SessionLimit>,
    /// From `jflow-log { message-rate-limit }`.
    pub jflow_log_message_rate_limit: Option<String>,
    pub syslog: Option<ServicesSyslog>,
    /// From `flow { traceoptions { ... } }`; its `packet-filter` blocks are not read.
    pub flow_traceoptions: Option<Traceoptions>,
    /// `brief`, `detail` or `error`.
    pub flow_trace_level: Option<String>,
    pub flow_trace_rate_limit: Option<String>,
}

pub(super) fn parse_services_options(options: &ConfigLine) -> ServicesOptions {
    let value = |keyword| arg_of(options, keyword).map(str::to_string);
    let flow = child_block(options, "flow");
    let flow_trace = flow.and_then(|f| child_block(f, "traceoptions"));
    ServicesOptions {
        cgn_pic: has_leaf(options, "cgn-pic"),
        close_timeout: value("close-timeout"),
        fragment_limit: value("fragment-limit"),
        disable_global_timeout_override: has_leaf(options, "disable-global-timeout-override"),
        enable_subscriber_analysis: has_leaf(options, "enable-subscriber-analysis"),
        utc_timestamp: has_leaf(options, "utc-timestamp"),
        ignore_errors: statement_words(options, "ignore-errors")
            .map(|words| words.into_iter().map(str::to_string).collect()),
        inactivity_non_tcp_timeout: value("inactivity-non-tcp-timeout"),
        inactivity_tcp_timeout: value("inactivity-tcp-timeout"),
        inactivity_timeout: value("inactivity-timeout"),
        open_timeout: value("open-timeout"),
        pba_interim_logging_interval: value("pba-interim-logging-interval"),
        reassembly_timeout: value("reassembly-timeout"),
        session_timeout: value("session-timeout"),
        tcp_tickles: value("tcp-tickles"),
        trio_flow_offload_minimum_bytes: statement_words(options, "trio-flow-offload")
            .and_then(|words| value_after(&words, "minimum-bytes")),
        session_limit: child_block(options, "session-limit").map(|limit| SessionLimit {
            maximum: arg_of(limit, "maximum").map(str::to_string),
            rate: arg_of(limit, "rate").map(str::to_string),
            cpu_load_threshold: arg_of(limit, "cpu-load-threshold").map(str::to_string),
        }),
        jflow_log_message_rate_limit: child_block(options, "jflow-log")
            .and_then(|log| arg_of(log, "message-rate-limit"))
            .map(str::to_string),
        syslog: child_block(options, "syslog").map(|syslog| ServicesSyslog {
            message_rate_limit: arg_of(syslog, "message-rate-limit").map(str::to_string),
            hosts: named_children(syslog, "host")
                .map(|(name, host)| ServicesSyslogHost {
                    name: name.to_string(),
                    facility_override: arg_of(host, "facility-override").map(str::to_string),
                    log_prefix: arg_of(host, "log-prefix").map(str::to_string),
                    port: arg_of(host, "port").map(str::to_string),
                    services: arg_of(host, "services").map(str::to_string),
                })
                .collect(),
        }),
        flow_traceoptions: flow.and_then(parse_traceoptions),
        flow_trace_level: flow_trace.and_then(|t| arg_of(t, "trace-level")).map(str::to_string),
        flow_trace_rate_limit: flow_trace.and_then(|t| arg_of(t, "rate-limit")).map(str::to_string),
    }
}

#[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_services_options() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ms-1/0/0 {
        services-options {
            cgn-pic;
            close-timeout 30;
            fragment-limit 5;
            disable-global-timeout-override;
            enable-subscriber-analysis;
            utc-timestamp;
            ignore-errors alg tcp;
            inactivity-non-tcp-timeout 60;
            inactivity-tcp-timeout 1800;
            inactivity-timeout 90;
            open-timeout 20;
            pba-interim-logging-interval 600;
            reassembly-timeout 5;
            session-timeout 300;
            tcp-tickles 4;
            trio-flow-offload minimum-bytes 1000;
            session-limit {
                maximum 100000;
                rate 500;
                cpu-load-threshold 80;
            }
            jflow-log {
                message-rate-limit 1000;
            }
            syslog {
                host 10.1.1.1 {
                    facility-override local3;
                    log-prefix nat;
                    port 514;
                    services info;
                }
                message-rate-limit 200;
            }
            flow {
                traceoptions {
                    file flow.log size 1m;
                    flag all;
                    trace-level detail;
                    rate-limit 100;
                }
            }
        }
    }
    ms-1/0/1 {
        services-options {
            ignore-errors;
        }
    }
    ms-1/0/2 {
        disable;
    }
}
"#,
        ));
        let options = ifaces[0].services_options.as_ref().unwrap();
        assert!(options.cgn_pic && options.disable_global_timeout_override);
        assert!(options.enable_subscriber_analysis && options.utc_timestamp);
        assert_eq!(options.close_timeout.as_deref(), Some("30"));
        assert_eq!(options.fragment_limit.as_deref(), Some("5"));
        assert_eq!(
            options.ignore_errors.as_deref(),
            Some(&["alg".to_string(), "tcp".to_string()][..])
        );
        assert_eq!(options.inactivity_non_tcp_timeout.as_deref(), Some("60"));
        assert_eq!(options.inactivity_tcp_timeout.as_deref(), Some("1800"));
        assert_eq!(options.inactivity_timeout.as_deref(), Some("90"));
        assert_eq!(options.open_timeout.as_deref(), Some("20"));
        assert_eq!(options.pba_interim_logging_interval.as_deref(), Some("600"));
        assert_eq!(options.reassembly_timeout.as_deref(), Some("5"));
        assert_eq!(options.session_timeout.as_deref(), Some("300"));
        assert_eq!(options.tcp_tickles.as_deref(), Some("4"));
        assert_eq!(options.trio_flow_offload_minimum_bytes.as_deref(), Some("1000"));
        let limit = options.session_limit.as_ref().unwrap();
        assert_eq!(limit.maximum.as_deref(), Some("100000"));
        assert_eq!(limit.rate.as_deref(), Some("500"));
        assert_eq!(limit.cpu_load_threshold.as_deref(), Some("80"));
        assert_eq!(options.jflow_log_message_rate_limit.as_deref(), Some("1000"));
        let syslog = options.syslog.as_ref().unwrap();
        assert_eq!(syslog.message_rate_limit.as_deref(), Some("200"));
        assert_eq!(syslog.hosts[0].name, "10.1.1.1");
        assert_eq!(syslog.hosts[0].facility_override.as_deref(), Some("local3"));
        assert_eq!(syslog.hosts[0].log_prefix.as_deref(), Some("nat"));
        assert_eq!(syslog.hosts[0].port.as_deref(), Some("514"));
        assert_eq!(syslog.hosts[0].services.as_deref(), Some("info"));
        let trace = options.flow_traceoptions.as_ref().unwrap();
        assert_eq!(trace.file.as_deref(), Some("flow.log"));
        assert_eq!(trace.flags[0].name, "all");
        assert_eq!(options.flow_trace_level.as_deref(), Some("detail"));
        assert_eq!(options.flow_trace_rate_limit.as_deref(), Some("100"));

        let bare = ifaces[1].services_options.as_ref().unwrap();
        assert_eq!(bare.ignore_errors, Some(Vec::new()));
        assert!(!bare.cgn_pic && bare.session_limit.is_none() && bare.syslog.is_none());
        assert!(bare.flow_traceoptions.is_none());
        assert!(ifaces[2].services_options.is_none());
    }
}