asterust-junos 0.4.4

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `mld`
//! `protocols { mld { ... } }`.
//!
//! The configuration is shared with [`igmp`](super::igmp), see [`GroupMembership`].
//!
//! # See also
//!
//! - [mld](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mld-edit-protocols.html)
//! - [accounting (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accounting-edit-protocols-mld.html)
//! - [maximum-transmit-rate (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/maximum-transmit-rate-edit-protocols-mld.html)
//! - [query-interval (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/query-interval-edit-protocols-mld.html)
//! - [query-last-member-interval (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/query-last-member-interval-edit-protocols-mld.html)
//! - [query-response-interval (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/query-response-interval-edit-protocols-mld.html)
//! - [robust-count (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/robust-count-edit-protocols-mld.html)
//! - [interface (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-edit-protocols-mld.html)
//! - [disable (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/disable-edit-protocols-mld.html)
//! - [group-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-limit-edit-protocols-mld-interface.html)
//! - [group-policy (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-policy-edit-protocols-mld.html)
//! - [group-threshold](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-threshold-edit-protocols-mld-interface.html)
//! - [log-interval](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/log-interval-edit-protocols-mld-interface.html)
//! - [oif-map](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/oif-map-edit-protocols-mld-interface.html)
//! - [passive](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/passive-edit-protocols-mld-interface.html)
//! - [ssm-map (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssm-map-edit-protocols-mld.html)
//! - [ssm-map-policy (MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssm-map-policy-edit-protocols-mld.html)
//! - [version (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/version-edit-protocols-mld.html)
//! - [static (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-edit-protocols-mld.html)
//! - [group (Protocols MLD)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/group-edit-protocols-mld.html)
//! - [Configuring MLD](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-mld.html), for `accounting` and `immediate-leave`

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

/// The `protocols mld` configuration.
pub type Mld = GroupMembership;

/// An `interface` entry of MLD.
pub type MldInterface = MembershipInterface;

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

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

    // Blocks of the Examples: Configuring PIM Sparse Mode and Example: Configuring Source-Specific Multicast pages of
    // the Multicast Protocols User Guide; see the README "Sources".
    const DOC_STATIC_GROUPS: &str = r#"
protocols {
    mld {
        interface ge-0/1/0.0 {
            static {
                group ff0e::10;
            }
        }
        interface ge-0/1/1.0 {
            static {
                group ff0e::10;
            }
        }
    }
}
"#;

    const DOC_SSM_MAP: &str = r#"
protocols {
    mld {
        interface fe-0/1/1.0 {
            ssm-map ssm-map-ipv6-example;
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference pages of the statements. See the README "Sources".
    const EXTRA: &str = r#"
protocols {
    mld {
        accounting;
        maximum-transmit-rate 400;
        query-interval 125;
        query-last-member-interval 1;
        query-response-interval 10;
        robust-count 2;
        interface all {
            version 2;
            immediate-leave;
            oif-map [ om1 om2 ];
            passive;
            group-limit 50;
            group-policy gp;
            group-threshold 90;
            log-interval 30;
            ssm-map-policy ssmp;
            accounting;
            static {
                group ff0e::20 {
                    source 2001:db8::1 {
                        source-count 3;
                    }
                }
            }
        }
        interface fxp0.0 {
            disable;
        }
    }
}
"#;

    fn mld_of(text: &str) -> Mld {
        mld(&juniper::parse_braces(text)).unwrap()
    }

    #[test]
    fn reads_the_guide_static_groups_and_ssm_map() {
        let mld = mld_of(DOC_STATIC_GROUPS);
        assert_eq!(mld.interfaces.len(), 2);
        assert_eq!(mld.interfaces[0].name, "ge-0/1/0.0");
        assert_eq!(mld.interfaces[0].static_groups.len(), 1);
        assert_eq!(mld.interfaces[0].static_groups[0].group, "ff0e::10");
        assert!(mld.interfaces[0].static_groups[0].sources.is_empty());
        assert_eq!(mld.interfaces[1].static_groups[0].group, "ff0e::10");

        let mld = mld_of(DOC_SSM_MAP);
        assert_eq!(mld.interfaces[0].ssm_map.as_deref(), Some("ssm-map-ipv6-example"));
    }

    #[test]
    fn reads_the_global_and_interface_options() {
        let mld = mld_of(EXTRA);
        assert!(mld.accounting);
        assert_eq!(mld.maximum_transmit_rate.as_deref(), Some("400"));
        assert_eq!(mld.query_interval.as_deref(), Some("125"));
        assert_eq!(mld.query_last_member_interval.as_deref(), Some("1"));
        assert_eq!(mld.query_response_interval.as_deref(), Some("10"));
        assert_eq!(mld.robust_count.as_deref(), Some("2"));
        assert_eq!(mld.traceoptions, None);

        let all = &mld.interfaces[0];
        assert_eq!(all.name, "all");
        assert_eq!(all.version.as_deref(), Some("2"));
        assert!(all.immediate_leave);
        assert_eq!(all.oif_maps, ["om1", "om2"]);
        assert!(all.passive);
        assert!(!all.promiscuous_mode);
        assert_eq!(all.group_limit.as_deref(), Some("50"));
        assert_eq!(all.group_policies, ["gp"]);
        assert_eq!(all.group_threshold.as_deref(), Some("90"));
        assert_eq!(all.log_interval.as_deref(), Some("30"));
        assert_eq!(all.ssm_map_policy.as_deref(), Some("ssmp"));
        assert_eq!(all.accounting, Some(true));
        assert!(mld.interfaces[1].disable);
    }

    #[test]
    fn reads_a_static_group_source() {
        let mld = mld_of(EXTRA);
        let group = &mld.interfaces[0].static_groups[0];
        assert_eq!(group.group, "ff0e::20");
        assert_eq!(group.sources[0].address, "2001:db8::1");
        assert_eq!(group.sources[0].source_count.as_deref(), Some("3"));
    }

    #[test]
    fn no_mld_is_none() {
        let config = juniper::parse_braces("protocols {\n    igmp {\n        accounting;\n    }\n}\n");
        assert_eq!(mld(&config), None);
    }
}