fix(lldp): keep dotted interface names and read every id type
The local interface was the second dotted part of the key, so the neighbours of eth0.1 landed under eth0 with every field missed. The remote port was read from port.ifname or port.id.value, and lldpd prints a port given by MAC or local id as port.mac / port.local, never as id.value, so that port came back empty. lldpd's output now goes through napalm-device-types' parse_lldpctl_keyvalue and its two NAPALM projections, which keep the interface name up to the neighbour's first field and read the ids under whatever type lldpd prints. The detail gains the system description and capabilities on the way. The module is under strict mypy now; the floor on napalm-device-types rises to 4.24.0. Closes #26
This commit is contained in:
1 parent
d04a969eb1
commit
231def768e
5 files changed
+92
-73
No files matched your search
@@ -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: ...
|
||||
Reference in new issue
Block a user