From 071beeb68afce12222779e906c3e26c39b4ab3c0 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:51:27 +0100 Subject: [PATCH] fix(lldp): report the neighbour's chassis MAC lldpd tells the chassis MAC, but get_lldp_neighbors reported only name and port, so netOrk, which matches a neighbour by MAC first, could match OpenWrt's neighbours by name only. napalm-device-types' lldp_neighbors projection now carries mac (lower case) when the chassis id is a MAC address, as napalm-linux reports it. Closes #31 --- CHANGELOG.md | 3 +++ napalm_openwrt/lldp_mixin.py | 7 +++++-- tests/unit/test_driver.py | 19 +++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e76840..96d398c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,9 @@ recorded here; their history is the git log. 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_lldp_neighbors()` reports a neighbour's chassis MAC as `mac` (lower case) when + the neighbour names its chassis by one, as napalm-linux does, so netOrk can match the + neighbour by MAC rather than by name only (#31). - `get_lldp_neighbors()` and `get_lldp_neighbors_detail()` raise when lldpctl fails (not installed, lldpd not running or unreachable), with lldpctl's message; they used to return no neighbours, the same answer as a device without any (#27). An empty diff --git a/napalm_openwrt/lldp_mixin.py b/napalm_openwrt/lldp_mixin.py index 11364cd..a04af1d 100644 --- a/napalm_openwrt/lldp_mixin.py +++ b/napalm_openwrt/lldp_mixin.py @@ -14,9 +14,10 @@ from __future__ import annotations from typing import TYPE_CHECKING -from napalm.base.models import LLDPNeighborDetailDict, LLDPNeighborDict +from napalm.base.models import LLDPNeighborDetailDict from napalm_device_types import ( LLDPCTL_KEYVALUE_COMMAND, + LldpNeighborDict, LldpNeighborRow, lldp_neighbors, lldp_neighbors_detail, @@ -27,9 +28,11 @@ from napalm_device_types import ( class OpenWrtLLDPMixin: """Mixin providing LLDP NAPALM getters.""" - def get_lldp_neighbors(self) -> dict[str, list[LLDPNeighborDict]]: + def get_lldp_neighbors(self) -> dict[str, list[LldpNeighborDict]]: """Return LLDP neighbors (requires ``lldpd`` installed and running on the device). + A neighbour that names its chassis by a MAC address carries it as ``mac``. + Uses ``lldpctl -f keyvalue`` output:: lldp.eth0.port.ifname=eth1 diff --git a/tests/unit/test_driver.py b/tests/unit/test_driver.py index 4fcdfef..abdc083 100644 --- a/tests/unit/test_driver.py +++ b/tests/unit/test_driver.py @@ -336,6 +336,17 @@ class TestGetLldpNeighbors: assert result["eth0"][0]["hostname"] == "core-router" assert result["eth0"][0]["port"] == "ether1" + def test_the_chassis_mac_is_reported(self, driver): + # netOrk matches a neighbour by this MAC first (#31). + driver._send_command = lambda cmd, **kw: LLDPCTL_KV + + assert driver.get_lldp_neighbors()["eth0"][0]["mac"] == "00:aa:bb:cc:dd:ee" + + def test_a_chassis_named_by_a_local_id_has_no_mac(self, driver): + driver._send_command = lambda cmd, **kw: LLDPCTL_KV_DOTTED_AND_ID_TYPES + + assert driver.get_lldp_neighbors()["eth1"] == [{"hostname": "access-sw", "port": "gi1/0/7"}] + # Two neighbours on one port, laid out the way lldpd's keyvalue writer prints them # (src/client/display.c, kv_writer.c): every neighbour opens with an optional @@ -402,11 +413,11 @@ class TestLldpSeveralNeighboursOnOnePort: result = driver.get_lldp_neighbors() - assert result["lan1"] == [ - {"hostname": "desk-phone", "port": "lan"}, - {"hostname": "desk-pc", "port": "eno1"}, + assert [(n["hostname"], n["port"]) for n in result["lan1"]] == [ + ("desk-phone", "lan"), + ("desk-pc", "eno1"), ] - assert result["eth0"] == [{"hostname": "core-router", "port": "ether1"}] + assert [(n["hostname"], n["port"]) for n in result["eth0"]] == [("core-router", "ether1")] def test_a_neighbour_keeps_only_its_own_fields(self, driver): driver._send_command = lambda cmd, **kw: LLDPCTL_KV_TWO_ON_ONE_PORT