asterust-junos 0.5.3

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
//! `statistics`
//! `protocols { mpls { statistics { ... } } }`.
//!
//! # See also
//!
//! - [statistics (Protocols MPLS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/statistics-edit-protocols-mpls.html)
//! - [Gather Statistics on MPLS Sessions](https://networkingdocs.hpe.com/documentation/us/en/software/junos/mpls/topics/topic-map/mpls-statistics.html)
//! - [Basic LSP Configuration](https://networkingdocs.hpe.com/documentation/us/en/software/junos/mpls/topics/topic-map/basic-lsp-configurtion.html), for the automatic bandwidth statistics

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

/// A `polling-profile <name>` of the automatic bandwidth statistics.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsPollingProfile {
    pub name: String,
    /// Seconds.
    pub interval: Option<String>,
}

/// The `statistics` block of MPLS.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct MplsStatistics {
    /// `auto-bandwidth` is enabled, written bare or as a block.
    pub auto_bandwidth: bool,
    /// The `polling-profile` entries of the `auto-bandwidth` block.
    pub auto_bandwidth_polling_profiles: Vec<MplsPollingProfile>,
    /// The file name of `file <name> [files <n>] [size <size>] [world-readable | no-world-readable]`.
    pub file: Option<String>,
    pub files: Option<String>,
    pub size: Option<String>,
    /// `Some(true)` for `world-readable`, `Some(false)` for `no-world-readable`.
    pub world_readable: Option<bool>,
    /// Seconds.
    pub interval: Option<String>,
    pub no_transit_statistics: bool,
    pub traffic_class_statistics: bool,
    pub transit_statistics_polling: bool,
    pub statistics_query_batch_size: Option<String>,
}

pub(super) fn parse_statistics(mpls: &ConfigLine) -> Option<MplsStatistics> {
    let statistics = child_block(mpls, "statistics")?;
    let (file, options) = arg_of(statistics, "file")
        .map(|arg| {
            let (name, rest) = split_name(arg);
            (Some(name.to_string()), rest)
        })
        .unwrap_or((None, ""));
    let words: Vec<&str> = options.split_whitespace().collect();
    let auto_bandwidth = child_block(statistics, "auto-bandwidth");
    Some(MplsStatistics {
        auto_bandwidth: auto_bandwidth.is_some(),
        auto_bandwidth_polling_profiles: auto_bandwidth
            .map(|block| {
                named_children(block, "polling-profile")
                    .map(|(name, profile)| MplsPollingProfile {
                        name: name.to_string(),
                        interval: arg_of(profile, "interval").map(str::to_string),
                    })
                    .collect()
            })
            .unwrap_or_default(),
        file,
        files: value_after(&words, "files"),
        size: value_after(&words, "size"),
        world_readable: if words.contains(&"world-readable") {
            Some(true)
        } else if words.contains(&"no-world-readable") {
            Some(false)
        } else {
            None
        },
        interval: arg_of(statistics, "interval").map(str::to_string),
        no_transit_statistics: has_leaf(statistics, "no-transit-statistics"),
        traffic_class_statistics: has_leaf(statistics, "traffic-class-statistics"),
        transit_statistics_polling: has_leaf(statistics, "transit-statistics-polling"),
        statistics_query_batch_size: arg_of(statistics, "statistics-query-batch-size").map(str::to_string),
    })
}

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

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    const CONFIG: &str = r#"
protocols {
    mpls {
        statistics {
            auto-bandwidth;
            file mpls.stats size 1m files 5 world-readable;
            interval 60;
            no-transit-statistics;
            traffic-class-statistics;
            transit-statistics-polling;
            statistics-query-batch-size 100;
        }
    }
}
"#;

    #[test]
    fn reads_the_statistics_block() {
        let statistics = mpls(&juniper::parse_braces(CONFIG)).unwrap().statistics.unwrap();
        assert!(statistics.auto_bandwidth);
        assert_eq!(statistics.file.as_deref(), Some("mpls.stats"));
        assert_eq!(statistics.files.as_deref(), Some("5"));
        assert_eq!(statistics.size.as_deref(), Some("1m"));
        assert_eq!(statistics.world_readable, Some(true));
        assert_eq!(statistics.interval.as_deref(), Some("60"));
        assert!(statistics.no_transit_statistics);
        assert!(statistics.traffic_class_statistics);
        assert!(statistics.transit_statistics_polling);
        assert_eq!(statistics.statistics_query_batch_size.as_deref(), Some("100"));
    }

    #[test]
    fn a_bare_statistics_block_has_nothing_set() {
        let statistics = mpls(&juniper::parse_braces(
            "protocols {\n    mpls {\n        statistics;\n    }\n}\n",
        ))
        .unwrap()
        .statistics
        .unwrap();
        assert_eq!(statistics, Default::default());
    }

    #[test]
    fn no_world_readable_is_read() {
        let statistics = mpls(&juniper::parse_braces(
            "protocols {\n    mpls {\n        statistics {\n            file s no-world-readable;\n        }\n    }\n}\n",
        ))
        .unwrap()
        .statistics
        .unwrap();
        assert_eq!(statistics.world_readable, Some(false));
    }

    // The `mpls` blocks of Gather Statistics on MPLS Sessions (R1) and of the automatic bandwidth sample of Basic LSP
    // Configuration (MPLS Applications User Guide), shortened; see the README "Sources".
    const DOC_TRAFFIC_CLASS: &str = r#"
protocols {
    mpls {
        statistics {
            traffic-class-statistics;
        }
        label-switched-path R1-R2 {
            to 10.20.0.1;
            oam mpls-tp-mode;
            ultimate-hop-popping;
            associate-lsp R2-R1;
        }
        interface ge-0/0/0.0;
        interface lo0.0;
        interface fxp0.0 {
            disable;
        }
    }
}
"#;

    const DOC_AUTO_BANDWIDTH: &str = r#"
protocols {
    mpls {
        statistics {
            interval 60;
            auto-bandwidth {
                polling-profile poll-prof-1 {
                    interval 30;
                }
            }
        }
        label-switched-path lsp1 {
            to 192.168.4.4;
            auto-bandwidth {
                adjust-threshold-options {
                    overflow-limit 3;
                    overflow-threshold 10;
                    threshold 20;
                    underflow-limit 6;
                    underflow-threshold 30;
                }
                maximum-bandwidth 10g;
                polling-profile poll-prof-1;
            }
        }
    }
}
"#;

    #[test]
    fn reads_the_guide_traffic_class_statistics() {
        let mpls = mpls(&juniper::parse_braces(DOC_TRAFFIC_CLASS)).unwrap();
        let statistics = mpls.statistics.unwrap();
        assert!(statistics.traffic_class_statistics);
        assert!(!statistics.auto_bandwidth);
        assert_eq!(mpls.label_switched_paths[0].name, "R1-R2");
        assert_eq!(mpls.label_switched_paths[0].to.as_deref(), Some("10.20.0.1"));
        assert_eq!(mpls.interfaces.len(), 3);
        assert!(mpls.interfaces[2].disable);
    }

    #[test]
    fn reads_the_guide_auto_bandwidth_statistics() {
        let mpls = mpls(&juniper::parse_braces(DOC_AUTO_BANDWIDTH)).unwrap();
        let statistics = mpls.statistics.unwrap();
        assert_eq!(statistics.interval.as_deref(), Some("60"));
        assert!(statistics.auto_bandwidth);
        assert_eq!(statistics.auto_bandwidth_polling_profiles.len(), 1);
        assert_eq!(statistics.auto_bandwidth_polling_profiles[0].name, "poll-prof-1");
        assert_eq!(
            statistics.auto_bandwidth_polling_profiles[0].interval.as_deref(),
            Some("30")
        );
        assert_eq!(mpls.label_switched_paths[0].to.as_deref(), Some("192.168.4.4"));
    }
}