Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update napalm agent dependencies and add overflow integer check #15

Merged
merged 2 commits into from
Oct 17, 2024
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
10 changes: 5 additions & 5 deletions diode-napalm-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ This is a basic set of instructions to get started using Diode NAPALM agent on a
## Requirements

The Diode NAPALM Agent requires a Python runtime environment and has the following requirements:
- importlib-metadata==8.5.0
- napalm==5.0.0
- netboxlabs-diode-sdk==0.4.0
- pydantic==2.9.2
- python-dotenv==1.0.1
- importlib-metadata~=8.5
- napalm~=5.0
- netboxlabs-diode-sdk~=0.4
- pydantic~=2.9
- python-dotenv~=1.0

Instructions on installing the Diode SDK Python can be found [here](https://github.com/netboxlabs/diode-sdk-python).

Expand Down
30 changes: 28 additions & 2 deletions diode-napalm-agent/diode_napalm/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@
)


def int32_overflows(number: int) -> bool:
"""
Check if an integer is overflowing the int32 range.

Args:
----
number (int): The integer to check.

Returns:
-------
bool: True if the integer is overflowing the int32 range, False otherwise.

"""
INT32_MIN = -2147483648
INT32_MAX = 2147483647
return not (INT32_MIN <= number <= INT32_MAX)


def translate_device(device_info: dict) -> Device:
"""
Translate device information from NAPALM format to Diode SDK Device entity.
Expand Down Expand Up @@ -65,11 +83,19 @@ def translate_interface(
device=device,
name=if_name,
enabled=interface_info.get("is_enabled"),
mtu=interface_info.get("mtu"),
mac_address=interface_info.get("mac_address"),
speed=int(interface_info.get("speed")),
description=interface_info.get("description"),
)

# Convert napalm interface speed from Mbps to Netbox Kbps
speed = int(interface_info.get("speed")) * 1000
if not int32_overflows(speed):
interface.speed = speed

mtu = interface_info.get("mtu")
if not int32_overflows(mtu):
interface.mtu = mtu

return interface


Expand Down
10 changes: 5 additions & 5 deletions diode-napalm-agent/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ classifiers = [
]

dependencies = [
"importlib-metadata==8.5.0",
"napalm==5.0.0",
"netboxlabs-diode-sdk==0.4.0",
"pydantic==2.9.2",
"python-dotenv==1.0.1",
"importlib-metadata~=8.5",
"napalm~=5.0",
"netboxlabs-diode-sdk~=0.4",
"pydantic~=2.9",
"python-dotenv~=1.0",
]

[project.optional-dependencies]
Expand Down
35 changes: 34 additions & 1 deletion diode-napalm-agent/tests/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ def sample_interface_info():
}


@pytest.fixture
def sample_interface_overflows_info():
"""Sample interface information for testing."""
return {
"GigabitEthernet0/0": {
"is_enabled": True,
"mtu": 150000000000,
"mac_address": "00:1C:58:29:4A:71",
"speed": 10000000000,
"description": "Uplink Interface",
}
}


@pytest.fixture
def sample_interfaces_ip():
"""Sample interface IPs for testing."""
Expand Down Expand Up @@ -67,7 +81,26 @@ def test_translate_interface(sample_device_info, sample_interface_info):
assert interface.enabled is True
assert interface.mtu == 1500
assert interface.mac_address == "00:1C:58:29:4A:71"
assert interface.speed == 1000
assert interface.speed == 1000000
assert interface.description == "Uplink Interface"


def test_translate_interface_with_overflow_data(
sample_device_info, sample_interface_overflows_info
):
"""Ensure interface translation is correct."""
device = translate_device(sample_device_info)
interface = translate_interface(
device,
"GigabitEthernet0/0",
sample_interface_overflows_info["GigabitEthernet0/0"],
)
assert interface.device.name == "router1"
assert interface.name == "GigabitEthernet0/0"
assert interface.enabled is True
assert interface.mtu == 0
assert interface.mac_address == "00:1C:58:29:4A:71"
assert interface.speed == 0
assert interface.description == "Uplink Interface"


Expand Down
Loading