fix(lldp): shared lldpctl parser; raise on failed LLDP and environment reads #30
No files matched your search
+9
-1
@@ -15,7 +15,8 @@ recorded here; their history is the git log.
|
||||
to do on every poll (#8). Needs napalm-device-types with `LldpAgentMixin`.
|
||||
|
||||
### Changed
|
||||
- The floor on napalm-device-types is 4.23.0, the first release with what this driver uses.
|
||||
- The floor on napalm-device-types is 4.24.0, the first release with what this driver
|
||||
uses (its shared `lldpctl` parser).
|
||||
- Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver
|
||||
on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6).
|
||||
- The floor on napalm is 5.1: older releases import `pkg_resources`, which a Python
|
||||
@@ -27,6 +28,13 @@ recorded here; their history is the git log.
|
||||
`manage_service("lldpd", "enable")` and `manage_service("lldpd", "start")` (#8).
|
||||
|
||||
### Fixed
|
||||
- `get_lldp_neighbors()` and `get_lldp_neighbors_detail()` keep an interface name with
|
||||
a dot whole: the neighbours of a VLAN interface such as `eth0.1` used to come back
|
||||
under `eth0` with every field empty (#26). A neighbour that names its port by MAC
|
||||
address or a local id is reported with that port instead of an empty one, and a
|
||||
chassis named by a local id or an address with that id. The detail now also
|
||||
carries the neighbour's system description and capabilities, which were always
|
||||
empty. lldpd's output is parsed by napalm-device-types' shared parser.
|
||||
- `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types'
|
||||
`ARP_AGE_UNKNOWN`) instead of `0.0`, which means "confirmed just now": neither
|
||||
`ip neigh` nor `/proc/net/arp` has an age (NetOrk/netork#1327).
|
||||
|
||||
@@ -12,40 +12,24 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
#: The keys a neighbour opens with in ``lldpctl -f keyvalue``: an optional
|
||||
#: ``alias``, then ``via`` (lldpd's ``display_interface``).
|
||||
_NEIGHBOUR_START = ("alias", "via")
|
||||
from napalm.base.models import LLDPNeighborDetailDict, LLDPNeighborDict
|
||||
from napalm_device_types import (
|
||||
LldpNeighborRow,
|
||||
lldp_neighbors,
|
||||
lldp_neighbors_detail,
|
||||
parse_lldpctl_keyvalue,
|
||||
)
|
||||
|
||||
|
||||
def _keyvalue_neighbours(output: str) -> list[tuple[str, dict[str, str]]]:
|
||||
"""``lldpctl -f keyvalue`` as ``(local interface, {subkey: value})``, one per neighbour.
|
||||
|
||||
Every neighbour on a port prints the same keys, so a port is no key: a
|
||||
neighbour ends where the next one opens. A key may repeat inside one
|
||||
neighbour (``chassis.mgmt-ip``, one per address); the last value stays.
|
||||
"""
|
||||
neighbours: list[tuple[str, dict[str, str]]] = []
|
||||
for line in output.splitlines():
|
||||
key, sep, value = line.strip().partition("=")
|
||||
parts = key.split(".")
|
||||
# parts: ['lldp', '<iface>', <category>, <field>, ...]
|
||||
if not sep or len(parts) < 3 or parts[0] != "lldp":
|
||||
continue
|
||||
iface, subkey = parts[1], ".".join(parts[2:])
|
||||
current = neighbours[-1][1] if neighbours and neighbours[-1][0] == iface else None
|
||||
if current is None or (subkey in _NEIGHBOUR_START and set(current) - {"alias"}):
|
||||
current = {}
|
||||
neighbours.append((iface, current))
|
||||
current[subkey] = value
|
||||
return neighbours
|
||||
#: lldpd's neighbours, parsed by napalm-device-types' ``parse_lldpctl_keyvalue``.
|
||||
_LLDPCTL_COMMAND = "lldpctl -f keyvalue"
|
||||
|
||||
|
||||
class OpenWrtLLDPMixin:
|
||||
"""Mixin providing LLDP NAPALM getters."""
|
||||
|
||||
def get_lldp_neighbors(self) -> dict[str, list[dict[str, Any]]]:
|
||||
def get_lldp_neighbors(self) -> dict[str, list[LLDPNeighborDict]]:
|
||||
"""Return LLDP neighbors (requires ``lldpd`` installed and running on the device).
|
||||
|
||||
Uses ``lldpctl -f keyvalue`` output::
|
||||
@@ -53,55 +37,23 @@ class OpenWrtLLDPMixin:
|
||||
lldp.eth0.port.ifname=eth1
|
||||
lldp.eth0.chassis.name=router-core
|
||||
"""
|
||||
neighbors: dict[str, list[dict[str, Any]]] = {}
|
||||
for row in self._get_lldp_table():
|
||||
neighbors.setdefault(row["local_port"], []).append(
|
||||
{"hostname": row["system_name"], "port": row["port_id"]}
|
||||
)
|
||||
return neighbors
|
||||
return lldp_neighbors(self._get_lldp_table())
|
||||
|
||||
def _get_lldp_table(self) -> list[dict[str, Any]]:
|
||||
"""Parse ``lldpctl -f keyvalue`` into a list of row dicts.
|
||||
def _get_lldp_table(self) -> list[LldpNeighborRow]:
|
||||
"""``lldpctl -f keyvalue`` as one row per neighbour.
|
||||
|
||||
Reads only: lldpd is neither started nor reconfigured here. Installing,
|
||||
starting and configuring it are the caller's (``ensure_package``,
|
||||
``manage_service``).
|
||||
"""
|
||||
output = self._send_command("lldpctl -f keyvalue")
|
||||
rows: list[dict[str, Any]] = []
|
||||
return parse_lldpctl_keyvalue(self._send_command(_LLDPCTL_COMMAND))
|
||||
|
||||
for iface, data in _keyvalue_neighbours(output):
|
||||
rows.append(
|
||||
{
|
||||
"local_port": iface,
|
||||
"remote_chassis_id": data.get("chassis.mac", data.get("chassis.id.value", "")),
|
||||
"port_id": data.get("port.ifname", data.get("port.id.value", "")),
|
||||
"mgmt_address": data.get("chassis.mgmt-ip", ""),
|
||||
"port_description": data.get("port.descr", ""),
|
||||
"system_name": data.get("chassis.name", ""),
|
||||
}
|
||||
)
|
||||
|
||||
return rows
|
||||
|
||||
def get_lldp_neighbors_detail(self, interface: str = "") -> dict[str, list[dict[str, Any]]]:
|
||||
def get_lldp_neighbors_detail(
|
||||
self, interface: str = ""
|
||||
) -> dict[str, list[LLDPNeighborDetailDict]]:
|
||||
"""Return detailed LLDP neighbor info."""
|
||||
details: dict[str, list[dict[str, Any]]] = {}
|
||||
return lldp_neighbors_detail(self._get_lldp_table(), interface)
|
||||
|
||||
for row in self._get_lldp_table():
|
||||
if interface and row["local_port"] != interface:
|
||||
continue
|
||||
details.setdefault(row["local_port"], []).append(
|
||||
{
|
||||
"parent_interface": "",
|
||||
"remote_port": row["port_id"],
|
||||
"remote_port_description": row["port_description"],
|
||||
"remote_chassis_id": row["remote_chassis_id"],
|
||||
"remote_system_name": row["system_name"],
|
||||
"remote_system_description": "",
|
||||
"remote_system_capab": [],
|
||||
"remote_system_enable_capab": [],
|
||||
}
|
||||
)
|
||||
if TYPE_CHECKING: # pragma: no cover - the driver's
|
||||
|
||||
return details
|
||||
def _send_command(self, command: str | list[str]) -> str: ...
|
||||
+1
-2
@@ -21,7 +21,7 @@ classifiers = [
|
||||
]
|
||||
dependencies = [
|
||||
"napalm>=5.1", # older releases import pkg_resources, which a 3.12 venv lacks
|
||||
"napalm_device_types>=4.23.0",
|
||||
"napalm_device_types>=4.24.0",
|
||||
"netmiko>=4.0.0",
|
||||
"paramiko>=5.0.0", # CVE-2026-44405
|
||||
"netaddr>=0.7.19", # IPAddress; below it the package has none (CI lowest job)
|
||||
@@ -71,7 +71,6 @@ ignore_missing_imports = true
|
||||
module = [
|
||||
"napalm_openwrt.config_mixin",
|
||||
"napalm_openwrt.interfaces_mixin",
|
||||
"napalm_openwrt.lldp_mixin",
|
||||
"napalm_openwrt.openwrt",
|
||||
"napalm_openwrt.packages_mixin",
|
||||
"napalm_openwrt.routing_mixin",
|
||||
|
||||
@@ -40,7 +40,6 @@
|
||||
"mypy_relaxed": [
|
||||
"napalm_openwrt.config_mixin",
|
||||
"napalm_openwrt.interfaces_mixin",
|
||||
"napalm_openwrt.lldp_mixin",
|
||||
"napalm_openwrt.openwrt",
|
||||
"napalm_openwrt.packages_mixin",
|
||||
"napalm_openwrt.routing_mixin",
|
||||
|
||||
@@ -421,6 +421,67 @@ class TestLldpSeveralNeighboursOnOnePort:
|
||||
assert len(driver.get_lldp_neighbors_detail("lan1")["lan1"]) == 2
|
||||
|
||||
|
||||
# A swconfig VLAN interface (``eth0.1``) with a neighbour that names its port by
|
||||
# MAC, and a switch on ``eth1`` that names itself and its port by local ids.
|
||||
# Laid out the way lldpd's keyvalue writer prints them (src/client/display.c):
|
||||
# an id is printed under its type, never as ``id.value``. Not recorded from a device.
|
||||
LLDPCTL_KV_DOTTED_AND_ID_TYPES = """\
|
||||
lldp.eth0.1.via=LLDP
|
||||
lldp.eth0.1.rid=1
|
||||
lldp.eth0.1.chassis.mac=00:11:22:33:44:01
|
||||
lldp.eth0.1.chassis.name=desk-phone
|
||||
lldp.eth0.1.chassis.descr=SIP phone
|
||||
lldp.eth0.1.chassis.Tel.enabled=on
|
||||
lldp.eth0.1.port.mac=00:11:22:33:44:01
|
||||
lldp.eth0.1.port.descr=WAN
|
||||
lldp.eth1.via=LLDP
|
||||
lldp.eth1.rid=2
|
||||
lldp.eth1.chassis.local=access-sw-01
|
||||
lldp.eth1.chassis.name=access-sw
|
||||
lldp.eth1.chassis.Bridge.enabled=on
|
||||
lldp.eth1.chassis.Router.enabled=off
|
||||
lldp.eth1.port.local=gi1/0/7
|
||||
"""
|
||||
|
||||
|
||||
class TestLldpDottedInterfaceAndPortIds:
|
||||
"""``eth0.1`` keeps its name, and a port id given as MAC or local id is read (#26)."""
|
||||
|
||||
def test_a_dotted_interface_keeps_its_name(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: LLDPCTL_KV_DOTTED_AND_ID_TYPES
|
||||
|
||||
result = driver.get_lldp_neighbors()
|
||||
|
||||
assert sorted(result) == ["eth0.1", "eth1"]
|
||||
assert result["eth0.1"][0]["hostname"] == "desk-phone"
|
||||
|
||||
def test_a_port_given_by_mac_or_local_id_is_read(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: LLDPCTL_KV_DOTTED_AND_ID_TYPES
|
||||
|
||||
result = driver.get_lldp_neighbors()
|
||||
|
||||
assert result["eth0.1"][0]["port"] == "00:11:22:33:44:01"
|
||||
assert result["eth1"][0]["port"] == "gi1/0/7"
|
||||
|
||||
def test_a_chassis_given_by_a_local_id_is_read(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: LLDPCTL_KV_DOTTED_AND_ID_TYPES
|
||||
|
||||
switch = driver.get_lldp_neighbors_detail("eth1")["eth1"][0]
|
||||
|
||||
assert switch["remote_chassis_id"] == "access-sw-01"
|
||||
|
||||
def test_the_detail_carries_description_and_capabilities(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: LLDPCTL_KV_DOTTED_AND_ID_TYPES
|
||||
|
||||
detail = driver.get_lldp_neighbors_detail()
|
||||
|
||||
phone = detail["eth0.1"][0]
|
||||
assert phone["remote_port_description"] == "WAN"
|
||||
assert phone["remote_system_description"] == "SIP phone"
|
||||
assert phone["remote_system_enable_capab"] == ["telephone"]
|
||||
assert detail["eth1"][0]["remote_system_capab"] == ["bridge", "router"]
|
||||
|
||||
|
||||
BRIDGE_VLAN_SHOW = """\
|
||||
port vlan-id
|
||||
eth0 1 PVID Egress Untagged
|
||||
|
||||
Reference in new issue
Block a user