# Copyright 2025 The NetOrk Authors # # 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. """LLDP-related NAPALM getters for Proxmox VE nodes. lldpd's neighbours come from ``lldpctl -f keyvalue``, parsed and projected by napalm-device-types (``parse_lldpctl_keyvalue``, ``lldp_neighbors``, ``lldp_neighbors_detail``), as on every host that runs lldpd. """ from __future__ import annotations from typing import TYPE_CHECKING from napalm.base.models import LLDPNeighborDetailDict from napalm_device_types import ( LLDPCTL_KEYVALUE_COMMAND, LldpNeighborDict, LldpNeighborRow, lldp_neighbors, lldp_neighbors_detail, parse_lldpctl_keyvalue, ) class ProxmoxLLDPMixin: """Mixin providing LLDP NAPALM methods.""" def get_lldp_neighbors(self) -> dict[str, list[LldpNeighborDict]]: """Return LLDP neighbours (requires lldpd on the Proxmox node). Every neighbour of a port is its own entry; one that names its chassis by a MAC address carries it as ``mac``. :raises ValueError: when lldpctl failed or gave no answer. """ return lldp_neighbors(self._get_lldp_table()) def get_lldp_neighbors_detail( self, interface: str = "" ) -> dict[str, list[LLDPNeighborDetailDict]]: """Return detailed LLDP neighbour information, of *interface* only when given. :raises ValueError: when lldpctl failed or gave no answer. """ return lldp_neighbors_detail(self._get_lldp_table(), interface) def _get_lldp_table(self) -> list[LldpNeighborRow]: """``lldpctl -f keyvalue`` on the node, one row per neighbour. The command always prints lldpctl's exit status, so an empty answer means it never ran: that is no answer, not a node without neighbours. """ raw = self._exec_ssh_command(LLDPCTL_KEYVALUE_COMMAND) if not raw.strip(): raise ValueError("lldpctl gave no answer on the node") return parse_lldpctl_keyvalue(raw) if TYPE_CHECKING: # pragma: no cover - the driver's def _exec_ssh_command(self, command: str) -> str: ...