feat(access-point): declared writes for SSIDs, radios, VLAN names and the AP configuration, and a DHCP lease reader (NetOrk/netork#695)

WirelessWritesMixin declares push_ssid_config(ssid, SsidConfigDict), a
partial write, and set_radio_channels({radio: channel}), every radio in
one restart; AccessPointDriver and ResidentialGatewayDriver carry it.
AccessPointDriver declares set_vlan_name(vlan_id, name) and, through
ApConfigMixin, apply_ap_config(ApConfigDict, parts): the desired state
in vendor-neutral terms, of which the driver writes the parts the caller
found different, answered by ApConfigResultDict. The part names and
their I/O-free readers live in ap_config.

DhcpLeasesMixin declares get_dhcp_leases() with DhcpLeaseDict, apart
from DhcpServerMixin, so an access point can report the leases it serves.
This commit is contained in:
christianmanivong committed 2026-11-29 20:27:31 +01:00
1 parent 059d97f6de
commit 0db9c9ba65
12 files changed
+678 -2

No files matched your search

+32
View File
@@ -44,6 +44,7 @@ instead of being restated on every role that happens to need it:
* :class:`~napalm_device_types.container_engine.ContainerEngineMixin`
* :class:`~napalm_device_types.credential_expiry.CredentialExpiryMixin`
* :class:`~napalm_device_types.dhcp.DhcpServerMixin`
* :class:`~napalm_device_types.dhcp_leases.DhcpLeasesMixin`
* :class:`~napalm_device_types.firewall_rules.FirewallRuleMixin`
* :class:`~napalm_device_types.hardening.HardeningMixin`
* :class:`~napalm_device_types.health_metrics.HealthMetricsMixin`
@@ -58,6 +59,8 @@ instead of being restated on every role that happens to need it:
* :class:`~napalm_device_types.log_sources.LogSourcesMixin`
* :class:`~napalm_device_types.host_firewall.HostFirewallMixin`
* :class:`~napalm_device_types.mac_acl.MacAclMixin`
* :class:`~napalm_device_types.ap_config.ApConfigMixin`
* :class:`~napalm_device_types.wireless_writes.WirelessWritesMixin`
* :class:`~napalm_device_types.nat_vpn.NatVpnMixin`
* :class:`~napalm_device_types.packages.PackageManagementMixin`
* :class:`~napalm_device_types.ping_sweep.PingSweepMixin`
@@ -83,6 +86,14 @@ Introspection -- :func:`~napalm_device_types.roles.roles_of`,
"""
from napalm_device_types.access_point import AccessPointDriver
from napalm_device_types.ap_config import (
AP_CONFIG_PARTS,
AP_CONFIG_SSID_ATTRIBUTES,
ApConfigMixin,
ap_config_ssid_part,
ap_config_vlan_part,
is_ap_config_part,
)
from napalm_device_types.apt_history import (
APT_HISTORY_COMMAND,
AptHistoryMixin,
@@ -169,6 +180,7 @@ from napalm_device_types.credential_expiry import (
CredentialKind,
)
from napalm_device_types.dhcp import DhcpApplyError, DhcpServerMixin, normalize_cidr, normalize_mac
from napalm_device_types.dhcp_leases import DhcpLeasesMixin
from napalm_device_types.dns_forward import DnsForwardMixin
from napalm_device_types.firewall import FirewallDriver
from napalm_device_types.firewall_rules import FirewallRuleMixin
@@ -265,9 +277,13 @@ from napalm_device_types.management_controller import ManagementControllerDriver
from napalm_device_types.media import MediaDriver
from napalm_device_types.models import (
POWER_ACTIONS,
ApConfigDict,
ApConfigResultDict,
ApSsidConfigDict,
ConfigSyncDict,
DhcpHaDict,
DhcpHaPeerDict,
DhcpLeaseDict,
DnsForwardDict,
EventLogEntryDict,
FirmwareDict,
@@ -283,6 +299,7 @@ from napalm_device_types.models import (
PowerAction,
PowerStateDict,
SnmpAgentSettingsDict,
SsidConfigDict,
UpdateProgressDict,
)
from napalm_device_types.nat_vpn import NatVpnMixin
@@ -439,6 +456,7 @@ from napalm_device_types.virtual_ips import (
)
from napalm_device_types.vm_migration import MigrationPreflightDict
from napalm_device_types.vm_stop_request import VmStopRequestMixin
from napalm_device_types.wireless_writes import AUTO_CHANNEL, WirelessWritesMixin
from napalm_device_types.zabbix_agent import (
ZabbixAgentSettingsDict,
check_zabbix_agent_settings,
@@ -561,6 +579,20 @@ __all__ = [
"DeviceTypeDriver",
"DhcpApplyError",
"DhcpServerMixin",
"DhcpLeaseDict",
"DhcpLeasesMixin",
"AP_CONFIG_PARTS",
"AP_CONFIG_SSID_ATTRIBUTES",
"ApConfigDict",
"ApConfigMixin",
"ApConfigResultDict",
"ApSsidConfigDict",
"ap_config_ssid_part",
"ap_config_vlan_part",
"is_ap_config_part",
"AUTO_CHANNEL",
"SsidConfigDict",
"WirelessWritesMixin",
"FingerprintRule",
"rules_max_weight",
"FirewallDriver",
+18
View File
@@ -12,6 +12,7 @@ Usage::
from typing import TYPE_CHECKING
from napalm_device_types.ap_config import ApConfigMixin
from napalm_device_types.base import DeviceTypeDriver
from napalm_device_types.health_metrics import HealthMetricsMixin
from napalm_device_types.interface_filter import InterfaceFilterMixin
@@ -27,9 +28,12 @@ from napalm_device_types.models import (
from napalm_device_types.packages import PackageManagementMixin
from napalm_device_types.services import ServiceControlMixin
from napalm_device_types.updates import UpdateMixin
from napalm_device_types.wireless_writes import WirelessWritesMixin
class AccessPointDriver(
WirelessWritesMixin,
ApConfigMixin,
MacAclMixin,
UpdateMixin,
ServiceControlMixin,
@@ -61,6 +65,20 @@ class AccessPointDriver(
if TYPE_CHECKING:
def set_vlan_name(self, vlan_id: int, name: str) -> bool:
"""
Names the VLAN *vlan_id* the access point carries *name*.
Narrow on purpose: an access point carries the VLANs its SSIDs are
bridged into, and naming one is all it takes. It is no switch, and
does not create VLANs or set ports (``SwitchDriver.set_vlan``).
:returns: True when written; False when the device carries no VLAN
*vlan_id* that takes a name.
:raises Exception: when the device refuses the write.
"""
...
def get_wireless_config(self) -> WirelessConfigDict:
"""
Returns global wireless configuration parameters that apply across
@@ -113,3 +113,80 @@ class SSIDBridgeDict(TypedDict):
vlan_id: int
tagged: bool
client_isolation: bool
class SsidConfigDict(TypedDict, total=False):
"""What ``push_ssid_config`` writes to an SSID; a key left out is left as it is.
``encryption`` is the mode in hostapd's terms (``none``, ``psk2``, ``sae``,
``sae-mixed``, ...), ``key`` its passphrase. Which writes are safe -- never a
keyed mode without a key -- is the caller's policy, not the driver's.
"""
encryption: str
key: str
hidden: bool
enabled: bool
disassoc_low_ack: bool
max_inactivity: int # seconds
class ApSsidConfigDict(TypedDict):
"""One SSID of an access point's desired configuration (``ApConfigDict.ssids``)."""
name: str
vlan_id: NotRequired[int | None] # the VLAN its clients are bridged into
bands: NotRequired[list[str]] # "2.4 GHz", "5 GHz", "6 GHz"; empty: every band
encryption: NotRequired[str] # as in SsidConfigDict; "none" when open
key: NotRequired[str] # the passphrase; left out where none is on record
hidden: NotRequired[bool]
enabled: NotRequired[bool]
client_isolation: NotRequired[bool]
max_clients: NotRequired[int | None] # None: no limit
disassoc_low_ack: NotRequired[bool | None] # None: the device's default
max_inactivity: NotRequired[int | None] # seconds; None: the device's default
acl_mode: NotRequired[str] # "off" | "whitelist" | "blacklist"
acl_macs: NotRequired[list[str]] # the MACs of the list acl_mode names
fast_transition: NotRequired[bool] # 802.11r, in ApConfigDict.mobility_domain
class ApConfigDict(TypedDict, total=False):
"""An access point's desired configuration, as ``apply_ap_config`` writes it.
The caller decides which parts differ (``AP_CONFIG_PARTS``); the driver turns
the values those parts need into its device's commands. A value a part needs
and the dict lacks leaves that part unwritten (``ApConfigResultDict.skipped``).
"""
country_code: str # ISO 3166-1 alpha-2, for every radio
htmode_5g: str # the 5 GHz radio's channel width mode, e.g. "VHT80"
htmode_2g: str # the 2.4 GHz radio's, e.g. "HT20"
syslog_host: str # remote syslog target
syslog_port: int # UDP
web_ui: bool # whether the device serves its web interface
ssh_port: int
ssh_password_auth: bool # whether SSH takes a password login, root's included
management_vlan_id: int | None # the VLAN the device is managed in; None: no VLANs
management_untagged: bool # the management VLAN untagged (native) on the uplink
bridge_stp: bool # spanning tree on the bridge the SSIDs' VLANs share
mobility_domain: str # 802.11r mobility domain, 4 hex digits
ft_over_ds: bool # 802.11r over the distribution system rather than the air
ssids: list[ApSsidConfigDict]
class ApConfigResultDict(TypedDict):
"""What ``apply_ap_config`` did.
``applied`` and ``skipped`` are parts of those asked for: ``skipped`` the
ones the driver wrote nothing for -- a part it does not know, or one whose
values are missing, such as a keyed mode without a key. ``restarts`` says the
device restarts to apply them; the session is usually gone then.
``commands`` is what was sent, every passphrase masked; ``log`` where the
device keeps the output, "" when it keeps none.
"""
applied: list[str]
skipped: list[str]
restarts: bool
commands: str
log: str
+114
View File
@@ -0,0 +1,114 @@
"""An access point's configuration, applied as desired state.
The caller compares what it wants with what the device reports and names the
parts that differ; :meth:`ApConfigMixin.apply_ap_config` gets the whole desired
state and those parts, and writes them the way its device takes them. The
comparison is the caller's, the commands the driver's.
A part is a fixed name (``AP_CONFIG_PARTS``), ``network/vlan/<vid>`` for one VLAN
of the bridge, or ``wireless/<ssid>/<attribute>`` for one setting of an SSID
(``AP_CONFIG_SSID_ATTRIBUTES``). The helpers below read them; they do no I/O.
Declared under ``if TYPE_CHECKING``: only a driver that applies a configuration
defines ``apply_ap_config``.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from napalm_device_types.models import ApConfigDict, ApConfigResultDict
#: The parts without a subject, and the values of ``ApConfigDict`` each writes.
AP_CONFIG_PARTS = frozenset(
{
"radio/country", # country_code, on every radio
"radio/5g_htmode", # htmode_5g
"radio/2g_htmode", # htmode_2g
"system/syslog", # syslog_host, syslog_port
"system/web_ui", # web_ui
"system/ssh_port", # ssh_port
"system/password_auth", # ssh_password_auth
"network/bridge", # the bridge with every VLAN the SSIDs and management use
"network/bridge_stp", # bridge_stp
}
)
#: What ``wireless/<ssid>/<attribute>`` may name: "missing" creates the SSID,
#: "passphrase" writes its key, "acl" its MAC list, "ft" its 802.11r settings; the
#: rest write the value of ``ApSsidConfigDict`` of that name ("vlan" vlan_id).
AP_CONFIG_SSID_ATTRIBUTES = frozenset(
{
"missing",
"vlan",
"bands",
"encryption",
"passphrase",
"hidden",
"enabled",
"client_isolation",
"max_clients",
"disassoc_low_ack",
"max_inactivity",
"acl",
"ft",
}
)
_SSID_PREFIX = "wireless/"
_VLAN_PREFIX = "network/vlan/"
_VLAN_IDS = range(1, 4095)
def ap_config_ssid_part(part: str) -> tuple[str, str] | None:
"""``(ssid, attribute)`` of a ``wireless/<ssid>/<attribute>`` part; None for any other.
Split at the last slash: an SSID may hold one, an attribute never does.
"""
if not part.startswith(_SSID_PREFIX):
return None
ssid, _, attribute = part[len(_SSID_PREFIX) :].rpartition("/")
if not ssid or attribute not in AP_CONFIG_SSID_ATTRIBUTES:
return None
return ssid, attribute
def ap_config_vlan_part(part: str) -> int | None:
"""The VLAN id of a ``network/vlan/<vid>`` part; None for any other."""
if not part.startswith(_VLAN_PREFIX):
return None
vid = part[len(_VLAN_PREFIX) :]
if not vid.isdigit() or int(vid) not in _VLAN_IDS:
return None
return int(vid)
def is_ap_config_part(part: str) -> bool:
"""Whether *part* is one ``apply_ap_config`` knows."""
return (
part in AP_CONFIG_PARTS
or ap_config_vlan_part(part) is not None
or ap_config_ssid_part(part) is not None
)
class ApConfigMixin:
if TYPE_CHECKING:
def apply_ap_config(self, config: ApConfigDict, parts: list[str]) -> ApConfigResultDict:
"""
Writes the *parts* of *config* to the device.
Only the parts named are written, each from the values of *config* it
needs (see ``AP_CONFIG_PARTS``). A part the driver does not know, or
whose values *config* lacks -- a keyed SSID without its key -- is
written not at all and comes back in ``skipped``: never half of it.
A part that rebuilds the device's network (``network/bridge``) may
restart the device; ``restarts`` then says so, and the caller waits
before it reads the device again.
:raises Exception: when the device refuses the write, or the commands
cannot be handed to it.
"""
...
+46
View File
@@ -0,0 +1,46 @@
"""The leases a DHCP server handed out, wherever it runs.
Its own mixin, apart from :class:`~napalm_device_types.dhcp.DhcpServerMixin`:
an access point or a router often serves leases from a server nobody manages
through the driver, and reading them needs nothing of the reservations and
subnets that mixin declares. A driver that manages a DHCP server and reads its
leases mixes in both.
Declared under ``if TYPE_CHECKING``: a contract, not a placeholder. Only a driver
that reads leases defines ``get_dhcp_leases``, so
``callable(getattr(driver, "get_dhcp_leases", None))`` tells a caller whether to ask.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from napalm_device_types.models import DhcpLeaseDict
class DhcpLeasesMixin:
if TYPE_CHECKING:
def get_dhcp_leases(self) -> list[DhcpLeaseDict]:
"""
Returns the leases the device's DHCP server holds now.
A device that serves no leases -- an access point whose DHCP server is
off -- returns ``[]``: that is an answer, and it says there are none.
:raises Exception: when the leases cannot be read. An empty list for
"could not read" would make a broken server look like an idle one.
Example::
[
{
"mac": "aa:bb:cc:dd:ee:01",
"ip": "192.168.1.42",
"hostname": "laptop",
"lease_end": 1764432000,
"state": "active",
}
]
"""
...
+20
View File
@@ -8,6 +8,10 @@ from typing import Literal, NotRequired, get_args
from typing_extensions import TypedDict
from napalm_device_types.access_point_models import ApConfigDict as ApConfigDict
from napalm_device_types.access_point_models import ApConfigResultDict as ApConfigResultDict
from napalm_device_types.access_point_models import ApSsidConfigDict as ApSsidConfigDict
# The SNMP health profile shapes live in snmp_health_profile.py, those of the access
# point getters in access_point_models.py; re-exported here, where they were.
from napalm_device_types.access_point_models import ChannelScanEntryDict as ChannelScanEntryDict
@@ -18,6 +22,7 @@ from napalm_device_types.access_point_models import MeshConfigDict as MeshConfig
from napalm_device_types.access_point_models import MeshPeerDict as MeshPeerDict
from napalm_device_types.access_point_models import RadioStatusDict as RadioStatusDict
from napalm_device_types.access_point_models import SSIDBridgeDict as SSIDBridgeDict
from napalm_device_types.access_point_models import SsidConfigDict as SsidConfigDict
from napalm_device_types.access_point_models import SSIDDict as SSIDDict
from napalm_device_types.access_point_models import WirelessClientDict as WirelessClientDict
from napalm_device_types.access_point_models import WirelessConfigDict as WirelessConfigDict
@@ -535,6 +540,21 @@ class FirewallRuleDiffDict(TypedDict):
update: list[FirewallRuleUpdateDict]
class DhcpLeaseDict(TypedDict):
"""A lease a DHCP server handed out (``DhcpLeasesMixin.get_dhcp_leases``).
``mac`` lower-case and colon-separated; ``hostname`` "" when the client sent
none; ``lease_end`` the Unix time the lease ends, 0 when it does not end or is
not known; ``state`` the server's own word for it ("" when it has none).
"""
mac: str
ip: str
hostname: str
lease_end: int
state: str
class DhcpReservationDict(TypedDict):
"""A single static DHCP reservation (MAC -> IP), in vendor-neutral form.
+4 -1
View File
@@ -32,9 +32,12 @@ from napalm_device_types.models import (
WirelessClientDict,
)
from napalm_device_types.nat_vpn import NatVpnMixin
from napalm_device_types.wireless_writes import WirelessWritesMixin
class ResidentialGatewayDriver(NatVpnMixin, HealthMetricsMixin, DhcpServerMixin, DeviceTypeDriver):
class ResidentialGatewayDriver(
WirelessWritesMixin, NatVpnMixin, HealthMetricsMixin, DhcpServerMixin, DeviceTypeDriver
):
"""
Abstract intermediate driver for residential gateways (router + firewall + AP).
+59
View File
@@ -0,0 +1,59 @@
"""Writes to a device's SSIDs and radios: what an access point or a gateway's
built-in Wi-Fi takes.
Declared under ``if TYPE_CHECKING``: a contract, not a placeholder. Only a driver
that can write them defines these, so ``callable(getattr(driver, name, None))``
tells a caller whether to offer the write.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from napalm_device_types.models import SsidConfigDict
#: The channel that lets a radio choose its own (``set_radio_channels``).
AUTO_CHANNEL = 0
class WirelessWritesMixin:
if TYPE_CHECKING:
def push_ssid_config(self, ssid: str, config: SsidConfigDict) -> None:
"""
Writes *config* to every interface that serves the SSID *ssid*.
Partial: a key *config* leaves out is left as the device has it, and an
empty *config* writes nothing. The values are written as given; whether
a write is safe -- a keyed mode only together with its key -- is the
caller's decision.
:raises ValueError: for a value the device cannot take, before anything
is sent.
:raises Exception: when no interface serves *ssid*, or the device
refuses a step. What was staged before is not applied.
Example::
driver.push_ssid_config("Guest", {"hidden": True, "max_inactivity": 300})
"""
...
def set_radio_channels(self, channels: dict[str, int]) -> None:
"""
Sets each radio of *channels* (radio name -> channel) to its channel.
``AUTO_CHANNEL`` (0) lets the radio choose. Every radio changes in one
go, with one restart of the wireless stack, however many there are:
restarting per radio would take the Wi-Fi down once for each.
:raises ValueError: for a radio name or channel the device cannot take,
before anything is sent.
:raises Exception: when the device refuses a step; then no radio
changes and nothing restarts.
Example::
driver.set_radio_channels({"radio0": 36, "radio1": 0})
"""
...