fix(lldp): report the neighbour's chassis MAC
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 28s
driver / types (pull_request) Failing after 30s
driver / test (3.13) (pull_request) Failing after 15s
driver / test (3.12) (pull_request) Failing after 17s
driver / lowest (pull_request) Failing after 18s
driver / build (pull_request) Successful in 35s
driver / security (pull_request) Failing after 40s
CI / driver (pull_request) Failing after 1m41s
driver / publish (pull_request) Skipped

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
This commit is contained in:
Christian Manivong committed 2026-11-02 08:51:27 +01:00
1 parent aafb5636a9
commit 071beeb68a
3 files changed
+23 -6

No files matched your search

+3
View File
@@ -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 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 carries the neighbour's system description and capabilities, which were always
empty. lldpd's output is parsed by napalm-device-types' shared parser. 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 - `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 (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 to return no neighbours, the same answer as a device without any (#27). An empty
+5 -2
View File
@@ -14,9 +14,10 @@ from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from napalm.base.models import LLDPNeighborDetailDict, LLDPNeighborDict from napalm.base.models import LLDPNeighborDetailDict
from napalm_device_types import ( from napalm_device_types import (
LLDPCTL_KEYVALUE_COMMAND, LLDPCTL_KEYVALUE_COMMAND,
LldpNeighborDict,
LldpNeighborRow, LldpNeighborRow,
lldp_neighbors, lldp_neighbors,
lldp_neighbors_detail, lldp_neighbors_detail,
@@ -27,9 +28,11 @@ from napalm_device_types import (
class OpenWrtLLDPMixin: class OpenWrtLLDPMixin:
"""Mixin providing LLDP NAPALM getters.""" """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). """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:: Uses ``lldpctl -f keyvalue`` output::
lldp.eth0.port.ifname=eth1 lldp.eth0.port.ifname=eth1
+15 -4
View File
@@ -336,6 +336,17 @@ class TestGetLldpNeighbors:
assert result["eth0"][0]["hostname"] == "core-router" assert result["eth0"][0]["hostname"] == "core-router"
assert result["eth0"][0]["port"] == "ether1" 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 # 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 # (src/client/display.c, kv_writer.c): every neighbour opens with an optional
@@ -402,11 +413,11 @@ class TestLldpSeveralNeighboursOnOnePort:
result = driver.get_lldp_neighbors() result = driver.get_lldp_neighbors()
assert result["lan1"] == [ assert [(n["hostname"], n["port"]) for n in result["lan1"]] == [
{"hostname": "desk-phone", "port": "lan"}, ("desk-phone", "lan"),
{"hostname": "desk-pc", "port": "eno1"}, ("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): def test_a_neighbour_keeps_only_its_own_fields(self, driver):
driver._send_command = lambda cmd, **kw: LLDPCTL_KV_TWO_ON_ONE_PORT driver._send_command = lambda cmd, **kw: LLDPCTL_KV_TWO_ON_ONE_PORT