asterust-junos 0.5.6

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
//! `ospf3`
//! `protocols { ospf3 { ... } }`.
//!
//! OSPFv3 shares its statements with OSPFv2, so [`ospf3`] returns the same [`Ospf`] type as
//! [`ospf`](super::ospf::ospf). The statements that only exist for OSPFv3 are `realm` ([`Ospf::realms`]) and the
//! area-level `inter-area-prefix-export` and `inter-area-prefix-import`.
//!
//! # See also
//!
//! - [ospf3](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ospf3-edit-protocols.html)
//! - [realm](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/realm-edit-protocols-ospf3.html)
//! - [inter-area-prefix-export](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/inter-area-prefix-export-edit-protocols-ospf.html)
//! - [inter-area-prefix-import](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/inter-area-prefix-import-edit-protocols-ospf.html)
//! - [area](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/area-edit-protocols-ospf.html), which also documents `[edit protocols ospf3]`
//! - [Example: Configuring Nonstop Active Routing for PIM](https://networkingdocs.hpe.com/documentation/us/en/software/junos/multicast/topics/topic-map/mcast-pim-graceful-restart.html)

use super::ospf::{parse_ospf, Ospf};
use crate::{child_block, top_level};
use asterust::config::Config;

/// The `protocols ospf3` configuration.
pub type Ospf3 = Ospf;

/// Returns the `protocols ospf3` block, or `None` if OSPFv3 is not configured.
///
/// # Examples
///
/// ```
/// use asterust::config::juniper;
/// use asterust_junos::protocols::ospf3::ospf3;
///
/// let config = juniper::parse_braces("protocols {\n    ospf3 {\n        area 0.0.0.0 {\n            interface lo0.0 {\n                passive;\n            }\n        }\n    }\n}\n");
/// let ospf3 = ospf3(&config).unwrap();
/// assert_eq!(ospf3.areas[0].id, "0.0.0.0");
/// assert!(ospf3.areas[0].interfaces[0].passive);
/// ```
pub fn ospf3(config: &Config) -> Option<Ospf3> {
    child_block(top_level(config, "protocols")?, "ospf3").map(parse_ospf)
}

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

    // The `ospf3` block of Example: Configuring Nonstop Active Routing for PIM (Multicast Protocols User Guide); see the
    // README "Sources".
    const DOC_NSR: &str = r#"
protocols {
    ospf3 {
        traceoptions {
            file r1-nsr-ospf3 size 10m world-readable;
            flag lsa-update detail;
            flag flooding detail;
            flag lsa-request detail;
            flag state detail;
            flag event detail;
            flag hello detail;
            flag nsr-synchronization detail;
        }
        area 0.0.0.0 {
            interface fe-1/1/0.0 {
                passive;
                metric 1;
            }
            interface lo0.0 {
                passive;
            }
            interface so-0/0/1.0 {
                metric 1;
            }
            interface fe-0/1/3.0 {
                metric 1;
            }
        }
    }
}
"#;

    // Not from documentation: written from the CLI Reference pages of the statements. See the README "Sources".
    const EXTRA: &str = r#"
protocols {
    ospf3 {
        reference-bandwidth 100g;
        export OSPF3-EXPORT;
        area 0.0.0.1 {
            inter-area-prefix-export [ IAP-OUT-A IAP-OUT-B ];
            inter-area-prefix-import IAP-IN;
            interface ge-0/0/0.0;
        }
        realm ipv4-unicast {
            area 0.0.0.0 {
                interface ge-0/0/1.0 {
                    metric 20;
                }
                interface lo0.0 {
                    passive;
                }
            }
            area 0.0.0.2 {
                inter-area-prefix-export IAP-V4;
            }
        }
        realm ipv6-multicast {
            area 0.0.0.0 {
                interface ge-0/0/2.0;
            }
        }
    }
    ospf {
        area 0.0.0.0;
    }
}
"#;

    fn ospf3_of(text: &str) -> Ospf3 {
        ospf3(&juniper::parse_braces(text)).unwrap()
    }

    #[test]
    fn reads_the_guide_nonstop_routing_example() {
        let ospf3 = ospf3_of(DOC_NSR);
        let trace = ospf3.traceoptions.unwrap();
        assert_eq!(trace.file.as_deref(), Some("r1-nsr-ospf3"));
        assert_eq!(trace.size.as_deref(), Some("10m"));
        assert_eq!(trace.world_readable, Some(true));
        assert_eq!(trace.flags.len(), 7);
        assert_eq!(trace.flags[0].name, "lsa-update");
        assert_eq!(trace.flags[0].modifiers, ["detail"]);
        assert_eq!(ospf3.areas.len(), 1);
        let area = &ospf3.areas[0];
        assert_eq!(area.id, "0.0.0.0");
        assert_eq!(area.interfaces.len(), 4);
        assert_eq!(area.interfaces[0].name, "fe-1/1/0.0");
        assert!(area.interfaces[0].passive);
        assert_eq!(area.interfaces[0].metric, Some(1));
        assert!(area.interfaces[1].passive);
        assert_eq!(area.interfaces[1].metric, None);
        assert!(ospf3.realms.is_empty());
    }

    #[test]
    fn reads_the_inter_area_prefix_policies() {
        let ospf3 = ospf3_of(EXTRA);
        assert_eq!(ospf3.reference_bandwidth.as_deref(), Some("100g"));
        assert_eq!(ospf3.export_policies, ["OSPF3-EXPORT"]);
        let area = &ospf3.areas[0];
        assert_eq!(area.inter_area_prefix_export, ["IAP-OUT-A", "IAP-OUT-B"]);
        assert_eq!(area.inter_area_prefix_import, ["IAP-IN"]);
        assert_eq!(area.interfaces[0].name, "ge-0/0/0.0");
    }

    #[test]
    fn reads_the_realms() {
        let ospf3 = ospf3_of(EXTRA);
        assert_eq!(ospf3.realms.len(), 2);
        let unicast = &ospf3.realms[0];
        assert_eq!(unicast.name, "ipv4-unicast");
        assert_eq!(unicast.areas.len(), 2);
        assert_eq!(unicast.areas[0].interfaces[0].metric, Some(20));
        assert!(unicast.areas[0].interfaces[1].passive);
        assert_eq!(unicast.areas[1].inter_area_prefix_export, ["IAP-V4"]);
        assert_eq!(ospf3.realms[1].name, "ipv6-multicast");
        assert_eq!(ospf3.realms[1].areas[0].interfaces[0].name, "ge-0/0/2.0");
    }

    #[test]
    fn ospf3_does_not_read_ospf_and_the_reverse() {
        let config = juniper::parse_braces(EXTRA);
        assert_eq!(ospf3(&config).unwrap().areas[0].id, "0.0.0.1");
        let ospf = crate::protocols::ospf::ospf(&config).unwrap();
        assert_eq!(ospf.areas[0].id, "0.0.0.0");
        assert!(ospf.realms.is_empty());
    }

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