Skip to content

Commit

Permalink
Refactor(eos_designs): structured_config for underlay router_pim_spar…
Browse files Browse the repository at this point in the history
…se_mode (#5114)

Co-authored-by: Mahesh Kumar <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Guillaume Mulocher <[email protected]>
  • Loading branch information
4 people authored Feb 28, 2025
1 parent 1b015c7 commit 1a8d7e2
Showing 1 changed file with 15 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# that can be found in the LICENSE file.
from __future__ import annotations

from functools import cached_property
from typing import TYPE_CHECKING, Protocol

from pyavd._eos_cli_config_gen.schema import EosCliConfigGen
from pyavd._eos_designs.structured_config.structured_config_generator import structured_config_contributor
from pyavd._utils import get

if TYPE_CHECKING:
Expand All @@ -19,53 +20,33 @@ class RouterPimSparseModeMixin(Protocol):
Class should only be used as Mixin to a AvdStructuredConfig class.
"""

@cached_property
def router_pim_sparse_mode(self: AvdStructuredConfigUnderlayProtocol) -> dict | None:
@structured_config_contributor
def router_pim_sparse_mode(self: AvdStructuredConfigUnderlayProtocol) -> None:
"""
Return structured config for router_pim_sparse_mode.
Set the structured config for router_pim_sparse_mode.
Used for to configure multicast RPs for the underlay
"""
if not self.shared_utils.underlay_multicast or not self.inputs.underlay_multicast_rps:
return None
return

rp_addresses = []
anycast_rps = []
for rp_entry in self.inputs.underlay_multicast_rps:
rp_address = {"address": rp_entry.rp}
rp_address = EosCliConfigGen.RouterPimSparseMode.Ipv4.RpAddressesItem(address=rp_entry.rp)
if rp_entry.groups:
if rp_entry.access_list_name:
rp_address["access_lists"] = [rp_entry.access_list_name]
rp_address.access_lists.append(rp_entry.access_list_name)
else:
rp_address["groups"] = rp_entry.groups._as_list()
rp_address.groups.extend(rp_entry.groups)

rp_addresses.append(rp_address)
self.structured_config.router_pim_sparse_mode.ipv4.rp_addresses.append(rp_address)

if len(rp_entry.nodes) < 2 or self.shared_utils.hostname not in rp_entry.nodes or self.inputs.underlay_multicast_anycast_rp.mode != "pim":
continue

# Anycast-RP using PIM (default)
anycast_rps.append(
{
"address": rp_entry.rp,
"other_anycast_rp_addresses": [
{
"address": get(self.shared_utils.get_peer_facts(node.name), "router_id", required=True),
}
for node in rp_entry.nodes
],
},
other_anycast_rp_addresses = EosCliConfigGen.RouterPimSparseMode.Ipv4.AnycastRpsItem.OtherAnycastRpAddresses()
for node in rp_entry.nodes:
other_anycast_rp_addresses.append_new(address=get(self.shared_utils.get_peer_facts(node.name), "router_id", required=True))
self.structured_config.router_pim_sparse_mode.ipv4.anycast_rps.append_new(
address=rp_entry.rp, other_anycast_rp_addresses=other_anycast_rp_addresses
)

if rp_addresses:
router_pim_sparse_mode = {
"ipv4": {
"rp_addresses": rp_addresses,
},
}
if anycast_rps:
router_pim_sparse_mode["ipv4"]["anycast_rps"] = anycast_rps

return router_pim_sparse_mode

return None

0 comments on commit 1a8d7e2

Please sign in to comment.