asterust-junos 0.5.11

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
//! `esi`
//! `interfaces <name> { esi [<identifier>] { ... } }` and `interfaces <name> { unit <n> { esi { ... } } }`.
//!
//! The Ethernet segment of an EVPN multihomed interface.
//!
//! # See also
//!
//! - [esi (Interfaces)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/esi-edit-interfaces.html)
//! - [Understanding Automatically Generated ESIs in EVPN Networks](https://networkingdocs.hpe.com/documentation/us/en/software/junos/evpn/topics/concept/evpn-auto-esi.html)

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

/// The `type-3-system-mac` of `auto-derive`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EsiSystemMac {
    pub mac: Option<String>,
    pub local_discriminator: Option<String>,
}

/// The `auto-derive` of an ESI.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EsiAutoDerive {
    pub lacp: bool,
    pub type_1_lacp: bool,
    pub type_3_system_mac: Option<EsiSystemMac>,
}

/// The `df-election-granularity` of an ESI.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EsiDfGranularity {
    pub per_esi: bool,
    pub lacp_oos_on_ndf: bool,
    pub per_esi_vlan: bool,
    pub per_evi: bool,
}

/// The `preference` of `df-election-type`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EsiPreference {
    pub least: bool,
    pub non_revertive: bool,
    pub value: Option<String>,
}

/// The `df-election-type` of an ESI.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct EsiDfElectionType {
    /// `mod`: the default modulo election.
    pub mod_based: bool,
    pub preference: Option<EsiPreference>,
}

/// An `esi`, written `esi <identifier> { ... }` or, when auto-derived, `esi { ... }`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Esi {
    pub identifier: Option<String>,
    pub all_active: bool,
    pub single_active: bool,
    pub auto_derive: Option<EsiAutoDerive>,
    pub df_election_granularity: Option<EsiDfGranularity>,
    pub df_election_type: Option<EsiDfElectionType>,
    pub source_bmac: Option<String>,
}

/// The `esi` of an interface or unit, or `None` if it has none.
pub(super) fn parse_esi(parent: &ConfigLine) -> Option<Esi> {
    let line = active_children(parent).find(|c| c.text == "esi" || c.arg("esi").is_some())?;
    Some(Esi {
        identifier: line.arg("esi").map(str::to_string),
        all_active: has_leaf(line, "all-active"),
        single_active: has_leaf(line, "single-active"),
        auto_derive: child_block(line, "auto-derive").map(|derive| EsiAutoDerive {
            lacp: has_leaf(derive, "lacp"),
            type_1_lacp: has_leaf(derive, "type-1-lacp"),
            type_3_system_mac: child_block(derive, "type-3-system-mac").map(|mac| EsiSystemMac {
                mac: arg_of(mac, "mac").map(str::to_string),
                local_discriminator: arg_of(mac, "local-discriminator").map(str::to_string),
            }),
        }),
        df_election_granularity: child_block(line, "df-election-granularity").map(|granularity| {
            let per_esi = child_block(granularity, "per-esi");
            EsiDfGranularity {
                per_esi: per_esi.is_some() || has_leaf(granularity, "per-esi"),
                lacp_oos_on_ndf: per_esi.is_some_and(|block| has_leaf(block, "lacp-oos-on-ndf")),
                per_esi_vlan: has_leaf(granularity, "per-esi-vlan"),
                per_evi: has_leaf(granularity, "per-evi"),
            }
        }),
        df_election_type: child_block(line, "df-election-type").map(|election| EsiDfElectionType {
            mod_based: has_leaf(election, "mod"),
            preference: child_block(election, "preference").map(|preference| EsiPreference {
                least: has_leaf(preference, "least"),
                non_revertive: has_leaf(preference, "non-revertive"),
                value: arg_of(preference, "value").map(str::to_string),
            }),
        }),
        source_bmac: arg_of(line, "source-bmac").map(str::to_string),
    })
}

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

    // Adapted from the EVPN User Guide, Understanding Automatically Generated ESIs in EVPN Networks: the `show
    // interfaces ae0` output of the LACP and the system MAC examples (the `###` comments are removed); see the
    // README "Sources".
    const DOC_AUTO_ESI: &str = r#"
interfaces {
    ae0 {
        esi {
            auto-derive {
                type-1-lacp;
            }
            all-active;
        }
        aggregated-ether-options {
            minimum-links 1;
            lacp {
                active;
                system-id 00:11:22:33:44:55;
                admin-key 1234;
            }
        }
    }
    ae1 {
        esi {
            auto-derive {
                type-3-system-mac {
                    mac 00:aa:bb:cc:dd:ee;
                    local-discriminator 1000;
                }
            }
            all-active;
        }
    }
}
"#;

    #[test]
    fn reads_the_guide_auto_derived_esis() {
        let ifaces = interfaces(&juniper::parse_braces(DOC_AUTO_ESI));
        let lacp = ifaces[0].esi.as_ref().unwrap();
        assert_eq!(lacp.identifier, None);
        assert!(lacp.all_active && !lacp.single_active);
        let derive = lacp.auto_derive.as_ref().unwrap();
        assert!(derive.type_1_lacp && !derive.lacp);
        assert!(derive.type_3_system_mac.is_none());

        let mac = ifaces[1]
            .esi
            .as_ref()
            .unwrap()
            .auto_derive
            .as_ref()
            .unwrap()
            .type_3_system_mac
            .as_ref()
            .unwrap();
        assert_eq!(mac.mac.as_deref(), Some("00:aa:bb:cc:dd:ee"));
        assert_eq!(mac.local_discriminator.as_deref(), Some("1000"));
    }

    // Not from documentation: written from the esi CLI Reference pages. See the README "Sources".
    #[test]
    fn reads_the_other_statements_on_interfaces_and_units() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    ae2 {
        esi 00:11:22:33:44:55:66:77:88:99 {
            single-active;
            auto-derive {
                lacp;
            }
            df-election-granularity {
                per-esi {
                    lacp-oos-on-ndf;
                }
            }
            df-election-type {
                preference {
                    least;
                    non-revertive;
                    value 100;
                }
            }
            source-bmac 00:00:5e:00:53:01;
        }
        unit 10 {
            esi 00:11:22:33:44:55:66:77:88:aa {
                all-active;
                df-election-granularity {
                    per-evi;
                }
                df-election-type {
                    mod;
                }
            }
        }
        unit 11;
    }
}
"#,
        ));
        let esi = ifaces[0].esi.as_ref().unwrap();
        assert_eq!(esi.identifier.as_deref(), Some("00:11:22:33:44:55:66:77:88:99"));
        assert!(esi.single_active && !esi.all_active);
        assert!(esi.auto_derive.as_ref().unwrap().lacp);
        let granularity = esi.df_election_granularity.as_ref().unwrap();
        assert!(granularity.per_esi && granularity.lacp_oos_on_ndf && !granularity.per_evi);
        let preference = esi.df_election_type.as_ref().unwrap().preference.as_ref().unwrap();
        assert!(preference.least && preference.non_revertive);
        assert_eq!(preference.value.as_deref(), Some("100"));
        assert_eq!(esi.source_bmac.as_deref(), Some("00:00:5e:00:53:01"));

        let unit = ifaces[0].units[0].esi.as_ref().unwrap();
        assert_eq!(unit.identifier.as_deref(), Some("00:11:22:33:44:55:66:77:88:aa"));
        assert!(unit.all_active);
        assert!(unit.df_election_granularity.as_ref().unwrap().per_evi);
        assert!(unit.df_election_type.as_ref().unwrap().mod_based);
        assert!(ifaces[0].units[1].esi.is_none());
    }
}