asterust-junos 0.5.4

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
//! `igmp`
//! `protocols { igmp { ... } }`.
//!
//! The configuration is shared with [`mld`](super::mld), see [`GroupMembership`].
//!
//! # See also
//!
//! - [igmp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/igmp-edit-protocols.html)
//! - [accounting (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accounting-edit-protocols-igmp.html)
//! - [maximum-transmit-rate (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/maximum-transmit-rate-edit-protocols-igmp.html)
//! - [query-interval (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/query-interval-edit-protocols-igmp.html)
//! - [query-last-member-interval (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/query-last-member-interval-edit-protocols-igmp.html)
//! - [query-response-interval (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/query-response-interval-edit-protocols-igmp.html)
//! - [robust-count (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/robust-count-edit-protocols-igmp.html)
//! - [traceoptions (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-protocols-igmp.html)
//! - [interface (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-protocols-igmp.html)
//! - [disable (Protocols)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/disable-edit-protocols-igmp.html)
//! - [accounting (IGMP interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accounting-edit-protocols-igmp-interface.html)
//! - [distributed](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/distributed-edit-protocols-igmp-interface.html)
//! - [group-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-limit-edit-protocols-igmp-interface.html)
//! - [group-policy (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-policy-edit-protocols-igmp.html)
//! - [group-threshold](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-threshold-edit-protocols-igmp-interface.html)
//! - [log-interval](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/log-interval-edit-protocols-igmp-interface.html)
//! - [oif-map](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/oif-map-edit-protocols-igmp-interface.html)
//! - [passive](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/passive-edit-protocols-igmp-interface.html)
//! - [ssm-map (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssm-map-edit-protocols-igmp.html)
//! - [ssm-map-policy (IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssm-map-policy-edit-protocols-igmp.html)
//! - [version (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/version-edit-protocols-igmp.html)
//! - [static (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-edit-protocols-igmp.html)
//! - [group (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-edit-protocols-igmp.html)
//! - [exclude (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/exclude-edit-protocols-igmp.html)
//! - [source (Protocols IGMP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/source-edit-protocols-igmp.html)
//! - [Configuring IGMP](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-igmp.html), for `immediate-leave` and `promiscuous-mode`

use super::group_membership::{parse_group_membership, GroupMembership, MembershipInterface};
use crate::{child_block, top_level};
use asterust::config::Config;

/// The `protocols igmp` configuration.
pub type Igmp = GroupMembership;

/// An `interface` entry of IGMP.
pub type IgmpInterface = MembershipInterface;

/// Returns the `protocols igmp` block, or `None` if IGMP is not configured.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::protocols::igmp::igmp;
///
/// let config = juniper::parse_braces("protocols {\n    igmp {\n        interface fe-0/1/2 {\n            version 3;\n        }\n    }\n}\n");
/// let igmp = igmp(&config).unwrap();
/// assert_eq!(igmp.interfaces[0].name, "fe-0/1/2");
/// assert_eq!(igmp.interfaces[0].version.as_deref(), Some("3"));
/// ```
pub fn igmp(config: &Config) -> Option<Igmp> {
    child_block(top_level(config, "protocols")?, "igmp").map(parse_group_membership)
}

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

    // Blocks of the Example: Configuring Source-Specific Multicast page of the Multicast Protocols User Guide; see the
    // README "Sources".
    const DOC_SSM_VERSION: &str = r#"
protocols {
    igmp {
        interface fe-0/1/2 {
            version 3;
        }
    }
}
"#;

    const DOC_SSM_MAP: &str = r#"
protocols {
    igmp {
        interface fe-0/1/0.0 {
            ssm-map ssm-map-ipv4-example;
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference pages of the statements. See the README "Sources".
    const EXTRA: &str = r#"
protocols {
    igmp {
        accounting;
        maximum-transmit-rate 300;
        query-interval 125;
        query-last-member-interval 1;
        query-response-interval 10;
        robust-count 3;
        traceoptions {
            file igmp.log size 1m;
            flag packets;
        }
        interface all {
            version 2;
        }
        interface ge-0/0/0.0 {
            no-accounting;
            distributed;
            group-limit 100;
            group-policy [ gp1 gp2 ];
            group-threshold 80;
            log-interval 60;
            immediate-leave;
            oif-map om1;
            passive;
            promiscuous-mode;
            ssm-map ssm1;
            ssm-map-policy ssmp;
            version 3;
            static {
                group 224.1.1.1 {
                    exclude;
                    group-count 4;
                    group-increment 0.0.0.2;
                    source 10.0.0.1 {
                        source-count 2;
                        source-increment 0.0.0.1;
                    }
                    source 10.0.0.9;
                }
                group 224.2.2.2;
            }
        }
        interface ge-0/0/1.0 {
            disable;
        }
    }
}
"#;

    fn igmp_of(text: &str) -> Igmp {
        igmp(&juniper::parse_braces(text)).unwrap()
    }

    #[test]
    fn reads_the_guide_version_and_ssm_map() {
        let igmp = igmp_of(DOC_SSM_VERSION);
        assert_eq!(igmp.interfaces.len(), 1);
        assert_eq!(igmp.interfaces[0].name, "fe-0/1/2");
        assert_eq!(igmp.interfaces[0].version.as_deref(), Some("3"));
        assert_eq!(igmp.interfaces[0].ssm_map, None);

        let igmp = igmp_of(DOC_SSM_MAP);
        assert_eq!(igmp.interfaces[0].name, "fe-0/1/0.0");
        assert_eq!(igmp.interfaces[0].ssm_map.as_deref(), Some("ssm-map-ipv4-example"));
    }

    #[test]
    fn reads_the_global_options() {
        let igmp = igmp_of(EXTRA);
        assert!(igmp.accounting);
        assert_eq!(igmp.maximum_transmit_rate.as_deref(), Some("300"));
        assert_eq!(igmp.query_interval.as_deref(), Some("125"));
        assert_eq!(igmp.query_last_member_interval.as_deref(), Some("1"));
        assert_eq!(igmp.query_response_interval.as_deref(), Some("10"));
        assert_eq!(igmp.robust_count.as_deref(), Some("3"));
        let trace = igmp.traceoptions.unwrap();
        assert_eq!(trace.file.as_deref(), Some("igmp.log"));
        assert_eq!(trace.flags[0].name, "packets");
    }

    #[test]
    fn reads_the_interface_options() {
        let igmp = igmp_of(EXTRA);
        assert_eq!(igmp.interfaces.len(), 3);
        assert_eq!(igmp.interfaces[0].name, "all");
        assert_eq!(igmp.interfaces[0].version.as_deref(), Some("2"));
        assert_eq!(igmp.interfaces[0].accounting, None);

        let interface = &igmp.interfaces[1];
        assert_eq!(interface.accounting, Some(false));
        assert!(interface.distributed);
        assert_eq!(interface.group_limit.as_deref(), Some("100"));
        assert_eq!(interface.group_policies, ["gp1", "gp2"]);
        assert_eq!(interface.group_threshold.as_deref(), Some("80"));
        assert_eq!(interface.log_interval.as_deref(), Some("60"));
        assert!(interface.immediate_leave);
        assert_eq!(interface.oif_maps, ["om1"]);
        assert!(interface.passive);
        assert!(interface.promiscuous_mode);
        assert_eq!(interface.ssm_map.as_deref(), Some("ssm1"));
        assert_eq!(interface.ssm_map_policy.as_deref(), Some("ssmp"));
        assert_eq!(interface.version.as_deref(), Some("3"));
        assert!(!interface.disable);
        assert!(igmp.interfaces[2].disable);
    }

    #[test]
    fn reads_the_static_groups() {
        let igmp = igmp_of(EXTRA);
        let groups = &igmp.interfaces[1].static_groups;
        assert_eq!(groups.len(), 2);
        assert_eq!(groups[0].group, "224.1.1.1");
        assert!(groups[0].exclude);
        assert_eq!(groups[0].group_count.as_deref(), Some("4"));
        assert_eq!(groups[0].group_increment.as_deref(), Some("0.0.0.2"));
        assert_eq!(groups[0].sources.len(), 2);
        assert_eq!(groups[0].sources[0].address, "10.0.0.1");
        assert_eq!(groups[0].sources[0].source_count.as_deref(), Some("2"));
        assert_eq!(groups[0].sources[0].source_increment.as_deref(), Some("0.0.0.1"));
        assert_eq!(groups[0].sources[1].address, "10.0.0.9");
        assert_eq!(groups[0].sources[1].source_count, None);
        assert_eq!(groups[1].group, "224.2.2.2");
        assert!(!groups[1].exclude);
        assert!(groups[1].sources.is_empty());
    }

    #[test]
    fn no_igmp_is_none() {
        let config = juniper::parse_braces("protocols {\n    ospf {\n        area 0.0.0.0;\n    }\n}\n");
        assert_eq!(igmp(&config), None);
    }
}