# 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 from napalm_device_types import ( LLDPCTL_KEYVALUE_COMMAND, LldpNeighborDict, LldpNeighborRow, lldp_neighbors, lldp_neighbors_detail, parse_lldpctl_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). A neighbour that names its chassis by a MAC address carries it as ``mac``. 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``). :raises ValueError: when lldpctl failed (not installed, lldpd not running): that is no answer, not a port without neighbours. """ return parse_lldpctl_keyvalue(self._send_command(LLDPCTL_KEYVALUE_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: ...