feat(lldp): report the neighbour's chassis MAC in lldp_neighbors
driver / standard (pull_request) Successful in 20s
driver / lint (pull_request) Failing after 15s
driver / types (pull_request) Successful in 1m13s
driver / test (3.12) (pull_request) Successful in 1m19s
driver / test (3.13) (pull_request) Successful in 1m12s
driver / lowest (pull_request) Successful in 38s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m13s
CI / driver (pull_request) Failing after 3m1s
driver / publish (pull_request) Skipped

An application matches an LLDP neighbour by its chassis MAC first, and
napalm-linux and napalm-netgear report it as mac; the projection did
not, so every driver built on it would have reported neighbours by name
only (NAPALM/napalm-openwrt#31). lldp_neighbors now carries mac, lower
case, when the chassis id is a MAC address (LldpNeighborDict).
This commit is contained in:
christianmanivong committed 2026-11-02 08:50:50 +01:00
1 parent 4427be35cc
commit 2542081c6f
5 files changed
+55 -15

No files matched your search

+3 -1
View File
@@ -17,7 +17,9 @@ recorded here; their history is the git log.
lldpctl missing) raises instead of reading as no neighbours.
- `lldp_neighbors(rows)` and `lldp_neighbors_detail(rows, interface="")`: NAPALM's
`get_lldp_neighbors` and `get_lldp_neighbors_detail` from those rows, so a driver no
longer builds the two shapes itself.
longer builds the two shapes itself. A neighbour that names its chassis by a MAC
address carries it in `get_lldp_neighbors` as `mac`, in lower case
(`LldpNeighborDict`), so an application can match it by MAC.
### Fixed
- `DNF_SECURITY_COMMAND` reads the cached metadata only (`dnf --cacheonly`), as
+1 -1
View File
@@ -183,7 +183,7 @@ LLDP neighbours are rows: `LldpNeighborRow` is one neighbour as its local port h
(chassis and port id with their type, name, description, management addresses,
capabilities in NAPALM's names), and `lldp_neighbors(rows)` / `lldp_neighbors_detail(rows,
interface)` are NAPALM's two getters projected from them, a port with two neighbours
keeping both. A driver for a host that runs lldpd sends `LLDPCTL_KEYVALUE_COMMAND` and
keeping both; a neighbour named by its chassis MAC carries it as `mac` (lower case). A driver for a host that runs lldpd sends `LLDPCTL_KEYVALUE_COMMAND` and
hands its output to `parse_lldpctl_keyvalue`: one row per neighbour block, an interface
name with dots (`eth0.1`) kept whole, ids read under the type lldpd prints them with
(`chassis.mac`, `port.local`, ...). When lldpctl fails (lldpd not running, no `lldpctl`)
+7 -1
View File
@@ -135,7 +135,12 @@ from napalm_device_types.listening import (
ListeningSocketsUnavailable,
parse_listening_sockets,
)
from napalm_device_types.lldp import LldpNeighborRow, lldp_neighbors, lldp_neighbors_detail
from napalm_device_types.lldp import (
LldpNeighborDict,
LldpNeighborRow,
lldp_neighbors,
lldp_neighbors_detail,
)
from napalm_device_types.lldpctl import LLDPCTL_KEYVALUE_COMMAND, parse_lldpctl_keyvalue
from napalm_device_types.local_accounts import (
LocalAccountChangeDict,
@@ -351,6 +356,7 @@ __all__ = [
"ListeningSocketsUnavailable",
"parse_listening_sockets",
"LLDPCTL_KEYVALUE_COMMAND",
"LldpNeighborDict",
"LldpNeighborRow",
"lldp_neighbors",
"lldp_neighbors_detail",
+27 -9
View File
@@ -10,10 +10,14 @@ several entries in both shapes.
from __future__ import annotations
import re
from collections.abc import Iterable
from typing import TypedDict
from typing import NotRequired, TypedDict
from napalm.base.models import LLDPNeighborDetailDict, LLDPNeighborDict
from napalm.base.models import LLDPNeighborDetailDict
#: A chassis id of type ``mac`` that really is a MAC address.
_MAC_RE = re.compile(r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$")
class LldpNeighborRow(TypedDict):
@@ -39,13 +43,27 @@ class LldpNeighborRow(TypedDict):
enabled_capabilities: list[str]
def lldp_neighbors(rows: Iterable[LldpNeighborRow]) -> dict[str, list[LLDPNeighborDict]]:
"""``get_lldp_neighbors``: per local port, every neighbour's name and port."""
neighbors: dict[str, list[LLDPNeighborDict]] = {}
class LldpNeighborDict(TypedDict):
"""NAPALM's ``get_lldp_neighbors`` entry (``hostname``, ``port``), plus the chassis MAC.
``mac`` (lower case) is there only when the neighbour names its chassis by a
MAC address; an application can match the neighbour by it, where a name
may be ambiguous.
"""
hostname: str
port: str
mac: NotRequired[str]
def lldp_neighbors(rows: Iterable[LldpNeighborRow]) -> dict[str, list[LldpNeighborDict]]:
"""``get_lldp_neighbors``: per local port, every neighbour's name, port and chassis MAC."""
neighbors: dict[str, list[LldpNeighborDict]] = {}
for row in rows:
neighbors.setdefault(row["local_port"], []).append(
{"hostname": row["system_name"], "port": row["port_id"]}
)
entry: LldpNeighborDict = {"hostname": row["system_name"], "port": row["port_id"]}
if row["chassis_id_type"] == "mac" and _MAC_RE.match(row["chassis_id"]):
entry["mac"] = row["chassis_id"].lower()
neighbors.setdefault(row["local_port"], []).append(entry)
return neighbors
@@ -76,4 +94,4 @@ def lldp_neighbors_detail(
return details
__all__ = ["LldpNeighborRow", "lldp_neighbors", "lldp_neighbors_detail"]
__all__ = ["LldpNeighborDict", "LldpNeighborRow", "lldp_neighbors", "lldp_neighbors_detail"]
+17 -3
View File
@@ -33,7 +33,7 @@ def _row(local_port: str, name: str, **fields: object) -> LldpNeighborRow:
PHONE = _row(
"eth0.1",
"desk-phone",
chassis_id="00:11:22:33:44:01",
chassis_id="00:11:22:33:44:AB",
chassis_id_type="mac",
port_id="00:11:22:33:44:01",
port_id_type="mac",
@@ -66,7 +66,7 @@ class TestNeighbors:
def test_every_neighbour_of_a_port_is_listed(self):
assert lldp_neighbors([PHONE, PC, SWITCH]) == {
"eth0.1": [
{"hostname": "desk-phone", "port": "00:11:22:33:44:01"},
{"hostname": "desk-phone", "port": "00:11:22:33:44:01", "mac": "00:11:22:33:44:ab"},
{"hostname": "desk-pc", "port": "eno1"},
],
"eth1": [{"hostname": "access-sw", "port": "gi1/0/7"}],
@@ -75,6 +75,20 @@ class TestNeighbors:
def test_no_rows_is_no_neighbour(self):
assert lldp_neighbors([]) == {}
def test_a_chassis_mac_is_reported_in_lower_case(self):
# netOrk matches a neighbour by this MAC first (NAPALM/napalm-openwrt#31).
assert lldp_neighbors([PHONE])["eth0.1"][0]["mac"] == "00:11:22:33:44:ab"
def test_a_chassis_named_otherwise_has_no_mac(self):
switch = _row("eth1", "sw", chassis_id="00:11:22:33:44:55", chassis_id_type="local")
assert "mac" not in lldp_neighbors([switch])["eth1"][0]
def test_a_mac_id_that_is_no_mac_address_is_left_out(self):
odd = _row("eth1", "sw", chassis_id="00:11", chassis_id_type="mac")
assert "mac" not in lldp_neighbors([odd])["eth1"][0]
class TestNeighborsDetail:
def test_every_field_napalm_names(self):
@@ -84,7 +98,7 @@ class TestNeighborsDetail:
"parent_interface": "",
"remote_port": "00:11:22:33:44:01",
"remote_port_description": "WAN",
"remote_chassis_id": "00:11:22:33:44:01",
"remote_chassis_id": "00:11:22:33:44:AB",
"remote_system_name": "desk-phone",
"remote_system_description": "SIP phone",
"remote_system_capab": ["bridge", "telephone"],