Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions device-discovery/device_discovery/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def translate_interface(
if_name: str,
interface_info: dict,
defaults: Defaults,
parent: Interface | None = None,
parent: str | None = None,
) -> Interface:
"""
Translate interface information from NAPALM format to Diode SDK Interface entity.
Expand All @@ -104,7 +104,7 @@ def translate_interface(
if_name (str): The name of the interface.
interface_info (dict): Dictionary containing interface information.
defaults (Defaults): Default configuration.
parent (Interface | None): Parent interface, if any.
parent (str | None): Parent interface name, if any.

Returns:
-------
Expand Down Expand Up @@ -228,7 +228,7 @@ def translate_interface_ips(
Entity(
ip_address=IPAddress(
address=ip_address,
assigned_object_interface=interface,
assigned_object_interface=interface.name,
role=ip_role,
tenant=ip_tenant,
vrf=ip_vrf,
Expand Down Expand Up @@ -312,11 +312,13 @@ def interface_sort_key(name: str) -> tuple[int, str]:
separator_score = name.count(".") + name.count(":")
return (separator_score, name)

def resolve_parent(name: str) -> Interface | None:
def resolve_parent(name: str) -> str | None:
parent_name = extract_parent_interface_name(name)
if not parent_name or parent_name not in defined_interface_names:
if parent_name is None:
return None
return interface_entities.get(parent_name)
if parent_name in defined_interface_names or parent_name in interface_entities:
return parent_name
return None

for if_name, interface_info in sorted(
interfaces.items(), key=lambda item: interface_sort_key(item[0])
Expand Down
36 changes: 36 additions & 0 deletions device-discovery/tests/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,42 @@ def test_translate_data_creates_missing_subinterface_with_parent(
assert ip_entity.address == "10.0.0.1/30"
assert ip_entity.assigned_object_interface.name == "ethernet-1/1.0"


def test_translate_data_creates_ip_only_subinterface_parent_relationship(
sample_device_info, sample_defaults
):
"""Ensure subinterfaces derived solely from IP data still link to existing parents."""
interfaces: dict[str, dict] = {}
interfaces_ip = {
"Bundle1": {"ipv4": {"198.51.100.1": {"prefix_length": 30}}},
"Bundle1.100": {"ipv4": {"198.51.100.5": {"prefix_length": 30}}},
}
data = {
"device": sample_device_info,
"interface": interfaces,
"interface_ip": interfaces_ip,
"driver": "ios",
}

entities = list(translate_data(data))

bundle_parent = next(
entity.interface
for entity in entities
if entity.WhichOneof("entity") == "interface"
and entity.interface.name == "Bundle1"
)
bundle_subinterface = next(
entity.interface
for entity in entities
if entity.WhichOneof("entity") == "interface"
and entity.interface.name == "Bundle1.100"
)

assert bundle_subinterface.parent.name == "Bundle1"
assert bundle_subinterface.parent.name == bundle_parent.name


def test_translate_data_handles_none_defaults_and_options(
sample_device_info, sample_interface_info, sample_interfaces_ip
):
Expand Down