asterust-junos 0.5.10

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
//! `arp`
//! `system { arp { ... } }`.
//!
//! # See also
//!
//! - [arp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/arp-edit-interfaces.html)
//! - [purging](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/purging-edit-system-arp.html)
//! - [non-subscriber-no-reply](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/non-subscriber-no-reply-edit-system-arp.html)
//! - [proactive-arp-detection](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-arp-proactive-arp-detection.html)

use crate::{active_children, arg_of, child_block, has_leaf, unquote};
use asterust::config::ConfigLine;

/// An `interfaces <name>` ARP aging timer override.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ArpInterface {
    pub name: String,
    /// Minutes between ARP updates.
    pub aging_timer: Option<String>,
}

/// The `system arp` options.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Arp {
    /// Minutes between ARP updates.
    pub aging_timer: Option<String>,
    pub arp_retries: Option<String>,
    /// Seconds to delay gratuitous ARP requests.
    pub gratuitous_arp_delay: Option<String>,
    pub gratuitous_arp_on_ifup: bool,
    /// Per-interface aging timers, from `interfaces <name> { aging-timer }`.
    pub interfaces: Vec<ArpInterface>,
    /// Drop ARP requests from non-subscribers when a user route is added for a subscriber.
    pub non_subscriber_no_reply: bool,
    pub passive_learning: bool,
    pub proactive_arp_detection: bool,
    pub proxy_arp_reply_for_default: bool,
    /// Purge obsolete ARP entries when an interface or link goes offline.
    pub purging: bool,
    pub respond_out_of_subnet: bool,
    pub unicast_mode_on_expire: bool,
}

pub(super) fn parse_arp(arp: &ConfigLine) -> Arp {
    Arp {
        aging_timer: arg_of(arp, "aging-timer").map(str::to_string),
        arp_retries: arg_of(arp, "arp-retries").map(str::to_string),
        gratuitous_arp_delay: arg_of(arp, "gratuitous-arp-delay").map(str::to_string),
        gratuitous_arp_on_ifup: has_leaf(arp, "gratuitous-arp-on-ifup"),
        interfaces: child_block(arp, "interfaces")
            .map(|interfaces| {
                active_children(interfaces)
                    .map(|interface| ArpInterface {
                        name: unquote(&interface.text).to_string(),
                        aging_timer: arg_of(interface, "aging-timer").map(str::to_string),
                    })
                    .collect()
            })
            .unwrap_or_default(),
        non_subscriber_no_reply: has_leaf(arp, "non-subscriber-no-reply"),
        passive_learning: has_leaf(arp, "passive-learning"),
        proactive_arp_detection: has_leaf(arp, "proactive-arp-detection"),
        proxy_arp_reply_for_default: has_leaf(arp, "proxy-arp-reply-for-default"),
        purging: has_leaf(arp, "purging"),
        respond_out_of_subnet: has_leaf(arp, "respond-out-of-subnet"),
        unicast_mode_on_expire: has_leaf(arp, "unicast-mode-on-expire"),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_arp_options() {
        let arp = system(&juniper::parse_braces(
            r#"
system {
    arp {
        aging-timer 10;
        arp-retries 6;
        gratuitous-arp-delay 4;
        gratuitous-arp-on-ifup;
        interfaces {
            ge-0/0/0.0 {
                aging-timer 30;
            }
            ge-0/0/1.0;
        }
        non-subscriber-no-reply;
        passive-learning;
        proactive-arp-detection;
        proxy-arp-reply-for-default;
        purging;
        respond-out-of-subnet;
        unicast-mode-on-expire;
    }
}
"#,
        ))
        .unwrap()
        .arp
        .unwrap();
        assert_eq!(arp.aging_timer.as_deref(), Some("10"));
        assert_eq!(arp.arp_retries.as_deref(), Some("6"));
        assert_eq!(arp.gratuitous_arp_delay.as_deref(), Some("4"));
        assert!(arp.gratuitous_arp_on_ifup);
        assert_eq!(arp.interfaces.len(), 2);
        assert_eq!(arp.interfaces[0].name, "ge-0/0/0.0");
        assert_eq!(arp.interfaces[0].aging_timer.as_deref(), Some("30"));
        assert_eq!(arp.interfaces[1].aging_timer, None);
        assert!(arp.non_subscriber_no_reply);
        assert!(arp.passive_learning);
        assert!(arp.proactive_arp_detection);
        assert!(arp.proxy_arp_reply_for_default);
        assert!(arp.purging);
        assert!(arp.respond_out_of_subnet);
        assert!(arp.unicast_mode_on_expire);
    }

    #[test]
    fn arp_options_default_to_off() {
        let arp = system(&juniper::parse_braces(
            "system {\n    arp {\n        purging;\n    }\n}\n",
        ))
        .unwrap()
        .arp
        .unwrap();
        assert!(!arp.non_subscriber_no_reply);
        assert!(!arp.passive_learning);
        assert_eq!(arp.aging_timer, None);
        assert!(arp.interfaces.is_empty());
    }

    #[test]
    fn no_arp_is_none() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.arp.is_none());
    }
}