diff --git a/CHANGELOG.md b/CHANGELOG.md index 22258d8..9dc337e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,9 @@ recorded here; their history is the git log. lldpctl missing) raises instead of reading as no neighbours. - `lldp_neighbors(rows)` and `lldp_neighbors_detail(rows, interface="")`: NAPALM's `get_lldp_neighbors` and `get_lldp_neighbors_detail` from those rows, so a driver no - longer builds the two shapes itself. + longer builds the two shapes itself. A neighbour that names its chassis by a MAC + address carries it in `get_lldp_neighbors` as `mac`, in lower case + (`LldpNeighborDict`), so an application can match it by MAC. ### Fixed - `DNF_SECURITY_COMMAND` reads the cached metadata only (`dnf --cacheonly`), as diff --git a/README.md b/README.md index 09e5158..6cbf721 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,7 @@ LLDP neighbours are rows: `LldpNeighborRow` is one neighbour as its local port h (chassis and port id with their type, name, description, management addresses, capabilities in NAPALM's names), and `lldp_neighbors(rows)` / `lldp_neighbors_detail(rows, interface)` are NAPALM's two getters projected from them, a port with two neighbours -keeping both. A driver for a host that runs lldpd sends `LLDPCTL_KEYVALUE_COMMAND` and +keeping both; a neighbour named by its chassis MAC carries it as `mac` (lower case). A driver for a host that runs lldpd sends `LLDPCTL_KEYVALUE_COMMAND` and hands its output to `parse_lldpctl_keyvalue`: one row per neighbour block, an interface name with dots (`eth0.1`) kept whole, ids read under the type lldpd prints them with (`chassis.mac`, `port.local`, ...). When lldpctl fails (lldpd not running, no `lldpctl`) diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index c4afbb5..9c7de80 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -135,7 +135,12 @@ from napalm_device_types.listening import ( ListeningSocketsUnavailable, parse_listening_sockets, ) -from napalm_device_types.lldp import LldpNeighborRow, lldp_neighbors, lldp_neighbors_detail +from napalm_device_types.lldp import ( + LldpNeighborDict, + LldpNeighborRow, + lldp_neighbors, + lldp_neighbors_detail, +) from napalm_device_types.lldpctl import LLDPCTL_KEYVALUE_COMMAND, parse_lldpctl_keyvalue from napalm_device_types.local_accounts import ( LocalAccountChangeDict, @@ -351,6 +356,7 @@ __all__ = [ "ListeningSocketsUnavailable", "parse_listening_sockets", "LLDPCTL_KEYVALUE_COMMAND", + "LldpNeighborDict", "LldpNeighborRow", "lldp_neighbors", "lldp_neighbors_detail", diff --git a/napalm_device_types/lldp.py b/napalm_device_types/lldp.py index e796bf4..44b0e26 100644 --- a/napalm_device_types/lldp.py +++ b/napalm_device_types/lldp.py @@ -10,10 +10,14 @@ several entries in both shapes. from __future__ import annotations +import re from collections.abc import Iterable -from typing import TypedDict +from typing import NotRequired, TypedDict -from napalm.base.models import LLDPNeighborDetailDict, LLDPNeighborDict +from napalm.base.models import LLDPNeighborDetailDict + +#: A chassis id of type ``mac`` that really is a MAC address. +_MAC_RE = re.compile(r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$") class LldpNeighborRow(TypedDict): @@ -39,13 +43,27 @@ class LldpNeighborRow(TypedDict): enabled_capabilities: list[str] -def lldp_neighbors(rows: Iterable[LldpNeighborRow]) -> dict[str, list[LLDPNeighborDict]]: - """``get_lldp_neighbors``: per local port, every neighbour's name and port.""" - neighbors: dict[str, list[LLDPNeighborDict]] = {} +class LldpNeighborDict(TypedDict): + """NAPALM's ``get_lldp_neighbors`` entry (``hostname``, ``port``), plus the chassis MAC. + + ``mac`` (lower case) is there only when the neighbour names its chassis by a + MAC address; an application can match the neighbour by it, where a name + may be ambiguous. + """ + + hostname: str + port: str + mac: NotRequired[str] + + +def lldp_neighbors(rows: Iterable[LldpNeighborRow]) -> dict[str, list[LldpNeighborDict]]: + """``get_lldp_neighbors``: per local port, every neighbour's name, port and chassis MAC.""" + neighbors: dict[str, list[LldpNeighborDict]] = {} for row in rows: - neighbors.setdefault(row["local_port"], []).append( - {"hostname": row["system_name"], "port": row["port_id"]} - ) + entry: LldpNeighborDict = {"hostname": row["system_name"], "port": row["port_id"]} + if row["chassis_id_type"] == "mac" and _MAC_RE.match(row["chassis_id"]): + entry["mac"] = row["chassis_id"].lower() + neighbors.setdefault(row["local_port"], []).append(entry) return neighbors @@ -76,4 +94,4 @@ def lldp_neighbors_detail( return details -__all__ = ["LldpNeighborRow", "lldp_neighbors", "lldp_neighbors_detail"] +__all__ = ["LldpNeighborDict", "LldpNeighborRow", "lldp_neighbors", "lldp_neighbors_detail"] diff --git a/tests/test_lldp_rows.py b/tests/test_lldp_rows.py index 5fe25c2..49ce11d 100644 --- a/tests/test_lldp_rows.py +++ b/tests/test_lldp_rows.py @@ -33,7 +33,7 @@ def _row(local_port: str, name: str, **fields: object) -> LldpNeighborRow: PHONE = _row( "eth0.1", "desk-phone", - chassis_id="00:11:22:33:44:01", + chassis_id="00:11:22:33:44:AB", chassis_id_type="mac", port_id="00:11:22:33:44:01", port_id_type="mac", @@ -66,7 +66,7 @@ class TestNeighbors: def test_every_neighbour_of_a_port_is_listed(self): assert lldp_neighbors([PHONE, PC, SWITCH]) == { "eth0.1": [ - {"hostname": "desk-phone", "port": "00:11:22:33:44:01"}, + {"hostname": "desk-phone", "port": "00:11:22:33:44:01", "mac": "00:11:22:33:44:ab"}, {"hostname": "desk-pc", "port": "eno1"}, ], "eth1": [{"hostname": "access-sw", "port": "gi1/0/7"}], @@ -75,6 +75,20 @@ class TestNeighbors: def test_no_rows_is_no_neighbour(self): assert lldp_neighbors([]) == {} + def test_a_chassis_mac_is_reported_in_lower_case(self): + # netOrk matches a neighbour by this MAC first (NAPALM/napalm-openwrt#31). + assert lldp_neighbors([PHONE])["eth0.1"][0]["mac"] == "00:11:22:33:44:ab" + + def test_a_chassis_named_otherwise_has_no_mac(self): + switch = _row("eth1", "sw", chassis_id="00:11:22:33:44:55", chassis_id_type="local") + + assert "mac" not in lldp_neighbors([switch])["eth1"][0] + + def test_a_mac_id_that_is_no_mac_address_is_left_out(self): + odd = _row("eth1", "sw", chassis_id="00:11", chassis_id_type="mac") + + assert "mac" not in lldp_neighbors([odd])["eth1"][0] + class TestNeighborsDetail: def test_every_field_napalm_names(self): @@ -84,7 +98,7 @@ class TestNeighborsDetail: "parent_interface": "", "remote_port": "00:11:22:33:44:01", "remote_port_description": "WAN", - "remote_chassis_id": "00:11:22:33:44:01", + "remote_chassis_id": "00:11:22:33:44:AB", "remote_system_name": "desk-phone", "remote_system_description": "SIP phone", "remote_system_capab": ["bridge", "telephone"],