"""NAPALM ``get_lldp_neighbors`` from ``HostNetworkSystem.QueryNetworkHint``. ESXi listens for CDP on standard vSwitches by default and for LLDP where a vDS enables it. Both answers come back per vmnic in one hint; LLDP is preferred because it names the switch port the way the switch itself does. """ from __future__ import annotations from typing import Any def _lldp(info: dict[str, Any]) -> dict[str, str] | None: if not info.get("portId"): return None params = {p.get("key"): p.get("value") for p in info.get("parameter", [])} hostname = params.get("System Name") or info.get("chassisId", "") return {"hostname": str(hostname), "port": str(info["portId"])} def _cdp(info: dict[str, Any]) -> dict[str, str] | None: if not info.get("portId"): return None return {"hostname": str(info.get("devId", "")), "port": str(info["portId"])} def lldp_neighbors(hints: list[dict[str, Any]]) -> dict[str, list[dict[str, str]]]: result: dict[str, list[dict[str, str]]] = {} for hint in hints: neighbor = _lldp(hint.get("lldpInfo") or {}) or _cdp(hint.get("connectedSwitchPort") or {}) if neighbor: result[hint["device"]] = [neighbor] return result