Files
napalm-openwrt/napalm_openwrt/lldp_mixin.py
T
Christian Manivong 231def768e 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
2026-11-02 08:45:51 +01:00

60 lines
2.1 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from typing import TYPE_CHECKING
from napalm.base.models import LLDPNeighborDetailDict, LLDPNeighborDict
from napalm_device_types import (
LldpNeighborRow,
lldp_neighbors,
lldp_neighbors_detail,
parse_lldpctl_keyvalue,
)
#: 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[LLDPNeighborDict]]:
"""Return LLDP neighbors (requires ``lldpd`` installed and running on the device).
Uses ``lldpctl -f keyvalue`` output::
lldp.eth0.port.ifname=eth1
lldp.eth0.chassis.name=router-core
"""
return lldp_neighbors(self._get_lldp_table())
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``).
"""
return parse_lldpctl_keyvalue(self._send_command(_LLDPCTL_COMMAND))
def get_lldp_neighbors_detail(
self, interface: str = ""
) -> dict[str, list[LLDPNeighborDetailDict]]:
"""Return detailed LLDP neighbor info."""
return lldp_neighbors_detail(self._get_lldp_table(), interface)
if TYPE_CHECKING: # pragma: no cover - the driver's
def _send_command(self, command: str | list[str]) -> str: ...