feat(fixes): configure_lldp_agent(interfaces) template
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Successful in 1m7s
driver / test (3.13) (pull_request) Successful in 56s
driver / lowest (pull_request) Successful in 30s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 58s
CI / driver (pull_request) Successful in 2m32s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Successful in 1m7s
driver / test (3.13) (pull_request) Successful in 56s
driver / lowest (pull_request) Successful in 30s
driver / build (pull_request) Successful in 28s
driver / security (pull_request) Successful in 58s
CI / driver (pull_request) Successful in 2m32s
driver / publish (pull_request) Skipped
LLDP getters used to install, start and reconfigure lldpd on their own, on every poll. Getters only read now, so starting the agent needs an action the application calls. The template installs the agent through ensure_package, has the driver write the interfaces it runs on, then enables and restarts it through manage_service. The caller names the interfaces: an agent on every interface also advertises on a WAN port. Refs NAPALM/napalm-openwrt#8
This commit is contained in:
1 parent
a809e6c02f
commit
bc17862a44
5 files changed
+155
-3
No files matched your search
@@ -107,6 +107,7 @@ from napalm_device_types.firewall_rules import FirewallRuleMixin
|
||||
from napalm_device_types.firmware import FirmwareOfferMixin
|
||||
from napalm_device_types.fixes import (
|
||||
EnsurePackageMixin,
|
||||
LldpAgentMixin,
|
||||
PackageProxyMixin,
|
||||
SnmpAgentMixin,
|
||||
run_fix_steps,
|
||||
@@ -260,6 +261,7 @@ __all__ = [
|
||||
"FixResultDict",
|
||||
"FixStepDict",
|
||||
"FixStepOutcomeDict",
|
||||
"LldpAgentMixin",
|
||||
"PackageProxyMixin",
|
||||
"APT_PROXY_COMMAND",
|
||||
"AptProxyMixin",
|
||||
|
||||
@@ -22,7 +22,7 @@ They replace ``run_device_action`` (docs/STRUCTURE.md):
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Sequence
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING, Any, ClassVar
|
||||
|
||||
from napalm_device_types.models import (
|
||||
FixResultDict,
|
||||
@@ -133,6 +133,66 @@ class EnsurePackageMixin:
|
||||
def install_package(self, name: str, version: str = "") -> None: ...
|
||||
|
||||
|
||||
class LldpAgentMixin:
|
||||
"""Run an LLDP agent on the device, on the interfaces the caller names.
|
||||
|
||||
The steps: install the agent, write the interfaces it runs on, enable it at boot
|
||||
and restart it so it reads them. Installing and starting are generic, over
|
||||
``ensure_package`` and ``manage_service`` with ``LLDP_AGENT_PACKAGE`` and
|
||||
``LLDP_AGENT_SERVICE``; the driver writes the configuration. Which interfaces is
|
||||
the caller's choice -- an agent on every interface also talks to the uplink of a
|
||||
WAN port. Whether neighbours appear is the caller's to read with
|
||||
``get_lldp_neighbors``, after the agent's first advertisement interval.
|
||||
"""
|
||||
|
||||
#: The package that brings the agent, as ``ensure_package`` takes it.
|
||||
LLDP_AGENT_PACKAGE: ClassVar[str] = "lldpd"
|
||||
#: The agent's service, as ``manage_service`` takes it.
|
||||
LLDP_AGENT_SERVICE: ClassVar[str] = "lldpd"
|
||||
|
||||
def configure_lldp_agent(self, interfaces: Sequence[str]) -> FixResultDict:
|
||||
"""Install, configure for *interfaces*, enable and restart the agent.
|
||||
|
||||
:raises ValueError: for no interface, before anything changes.
|
||||
"""
|
||||
names = [str(name) for name in interfaces]
|
||||
if not names:
|
||||
raise ValueError("name at least one interface for the LLDP agent")
|
||||
return run_fix_steps(
|
||||
[
|
||||
("install", self._install_lldp_agent),
|
||||
("configure", lambda: self._write_lldp_agent_config(names)),
|
||||
("start", self._start_lldp_agent),
|
||||
]
|
||||
)
|
||||
|
||||
def _install_lldp_agent(self) -> FixStepOutcomeDict:
|
||||
result = self.ensure_package(self.LLDP_AGENT_PACKAGE)
|
||||
return {"ok": bool(result["success"]), "output": str(result.get("output", ""))}
|
||||
|
||||
def _start_lldp_agent(self) -> FixStepOutcomeDict:
|
||||
service = self.LLDP_AGENT_SERVICE
|
||||
for action in ("enable", "restart"):
|
||||
result = self.manage_service(service, action)
|
||||
if not result.get("success"):
|
||||
output = str(result.get("output", "")).strip()
|
||||
return {"ok": False, "output": f"{action} {service}: {output}".rstrip(": ")}
|
||||
return {"ok": True, "output": f"{service} enabled and restarted"}
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover - declared for type checkers only
|
||||
|
||||
def ensure_package(self, name: str) -> FixResultDict: ...
|
||||
|
||||
def manage_service(self, name: str, action: str) -> dict[str, Any]: ...
|
||||
|
||||
# The step a driver implements:
|
||||
|
||||
def _write_lldp_agent_config(self, interfaces: list[str]) -> FixStepOutcomeDict:
|
||||
"""Have the agent send and listen on exactly *interfaces* (names as the
|
||||
device's interface getters report them), replacing the list it had."""
|
||||
...
|
||||
|
||||
|
||||
class PackageProxyMixin:
|
||||
"""Point the device's package manager at a caching proxy."""
|
||||
|
||||
@@ -152,6 +212,7 @@ class PackageProxyMixin:
|
||||
__all__ = [
|
||||
"EnsurePackageMixin",
|
||||
"FixStep",
|
||||
"LldpAgentMixin",
|
||||
"PackageProxyMixin",
|
||||
"SnmpAgentMixin",
|
||||
"run_fix_steps",
|
||||
|
||||
Reference in new issue
Block a user