fix(lldp): keep every neighbour on a port

The keyvalue output was grouped by local interface only, so with several
neighbours on one port their keys overwrote each other and one mixed
entry came back. A neighbour now ends where the next one opens (alias,
then via, as lldpd prints it); a key repeated inside one neighbour, such
as one mgmt-ip per address, does not start a new one.

Closes #9
This commit is contained in:
Christian Manivong committed 2026-11-01 21:47:34 +01:00
1 parent 097d9b9581
commit 5e71f0b471
3 files changed
+88 -16

No files matched your search

+28 -16
View File
@@ -14,6 +14,33 @@ from __future__ import annotations
from typing import Any
#: The keys a neighbour opens with in ``lldpctl -f keyvalue``: an optional
#: ``alias``, then ``via`` (lldpd's ``display_interface``).
_NEIGHBOUR_START = ("alias", "via")
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
class OpenWrtLLDPMixin:
"""Mixin providing LLDP NAPALM getters."""
@@ -78,22 +105,7 @@ class OpenWrtLLDPMixin:
output = self._send_command("lldpctl -f keyvalue")
rows: list[dict[str, Any]] = []
# Group by local interface prefix: lldp.<iface>.*
entries: dict[str, dict[str, str]] = {}
for line in output.splitlines():
line_s = line.strip()
if "=" not in line_s:
continue
key, _, value = line_s.partition("=")
parts = key.split(".")
# parts: ['lldp', '<iface>', <category>, <field>, ...]
if len(parts) < 3 or parts[0] != "lldp":
continue
iface = parts[1]
subkey = ".".join(parts[2:])
entries.setdefault(iface, {})[subkey] = value
for iface, data in entries.items():
for iface, data in _keyvalue_neighbours(output):
rows.append(
{
"local_port": iface,