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

+264
View File
@@ -0,0 +1,264 @@
"""The access point's writes and the DHCP lease reader, as declared (NetOrk/netork#695).
netOrk wrote an OpenWrt AP's SSID settings, radio channels, VLAN names and AP
profile as UCI itself, and read leases from no AP at all. Each becomes a declared
method with a vendor-neutral shape; the driver turns it into its device's commands.
"""
from __future__ import annotations
import ast
import inspect
from typing import get_type_hints
import pytest
import napalm_device_types
from napalm_device_types import (
AP_CONFIG_PARTS,
AP_CONFIG_SSID_ATTRIBUTES,
AUTO_CHANNEL,
AccessPointDriver,
ApConfigDict,
ApConfigMixin,
ApConfigResultDict,
ApSsidConfigDict,
DhcpLeaseDict,
DhcpLeasesMixin,
ResidentialGatewayDriver,
SsidConfigDict,
WirelessWritesMixin,
access_point,
ap_config,
ap_config_ssid_part,
ap_config_vlan_part,
dhcp_leases,
is_ap_config_part,
wireless_writes,
)
def _declared(module) -> dict[str, ast.FunctionDef]:
"""Every function the module's source defines, by name (declarations included)."""
return {
node.name: node
for node in ast.walk(ast.parse(inspect.getsource(module)))
if isinstance(node, ast.FunctionDef)
}
def _args(node: ast.FunctionDef) -> tuple[list[str], list[str]]:
return [a.arg for a in node.args.args], [a.arg for a in node.args.kwonlyargs]
class TestShapes:
def test_a_lease_is_the_five_fields_every_server_reports(self):
assert set(get_type_hints(DhcpLeaseDict)) == {
"mac",
"ip",
"hostname",
"lease_end",
"state",
}
def test_an_ssid_push_is_a_partial_dict(self):
assert set(get_type_hints(SsidConfigDict)) == {
"encryption",
"key",
"hidden",
"enabled",
"disassoc_low_ack",
"max_inactivity",
}
assert SsidConfigDict.__required_keys__ == frozenset()
def test_the_ap_configuration_is_desired_state_in_neutral_terms(self):
assert set(get_type_hints(ApConfigDict)) == {
"country_code",
"htmode_5g",
"htmode_2g",
"syslog_host",
"syslog_port",
"web_ui",
"ssh_port",
"ssh_password_auth",
"management_vlan_id",
"management_untagged",
"bridge_stp",
"mobility_domain",
"ft_over_ds",
"ssids",
}
assert ApConfigDict.__required_keys__ == frozenset()
def test_an_ssid_of_the_configuration_is_named(self):
assert ApSsidConfigDict.__required_keys__ == frozenset({"name"})
assert set(get_type_hints(ApSsidConfigDict)) == {
"name",
"vlan_id",
"bands",
"encryption",
"key",
"hidden",
"enabled",
"client_isolation",
"max_clients",
"disassoc_low_ack",
"max_inactivity",
"acl_mode",
"acl_macs",
"fast_transition",
}
def test_an_apply_says_what_it_wrote_and_what_it_could_not(self):
assert set(get_type_hints(ApConfigResultDict)) == {
"applied",
"skipped",
"restarts",
"commands",
"log",
}
class TestDeclarations:
def test_the_lease_reader_has_its_own_mixin(self):
"""An access point serves leases without being a DHCP server to manage."""
assert _args(_declared(dhcp_leases)["get_dhcp_leases"]) == (["self"], [])
assert not hasattr(DhcpLeasesMixin, "get_dhcp_leases")
assert not issubclass(AccessPointDriver, DhcpLeasesMixin)
def test_ssid_and_channel_writes(self):
declared = _declared(wireless_writes)
assert _args(declared["push_ssid_config"]) == (["self", "ssid", "config"], [])
assert _args(declared["set_radio_channels"]) == (["self", "channels"], [])
for name in ("push_ssid_config", "set_radio_channels"):
assert not hasattr(WirelessWritesMixin, name), name
def test_the_ap_configuration_apply(self):
assert _args(_declared(ap_config)["apply_ap_config"]) == (
["self", "config", "parts"],
[],
)
assert not hasattr(ApConfigMixin, "apply_ap_config")
def test_the_vlan_name_is_a_narrow_write_of_the_access_point(self):
assert _args(_declared(access_point)["set_vlan_name"]) == (
["self", "vlan_id", "name"],
[],
)
assert not hasattr(AccessPointDriver, "set_vlan_name")
assert not hasattr(AccessPointDriver, "set_vlan")
@pytest.mark.parametrize(
("role", "mixin"),
[
(AccessPointDriver, WirelessWritesMixin),
(AccessPointDriver, ApConfigMixin),
(ResidentialGatewayDriver, WirelessWritesMixin),
],
)
def test_the_roles_that_carry_them(self, role, mixin):
assert issubclass(role, mixin)
def test_exported(self):
for name in (
"DhcpLeaseDict",
"DhcpLeasesMixin",
"SsidConfigDict",
"WirelessWritesMixin",
"AUTO_CHANNEL",
"ApConfigDict",
"ApSsidConfigDict",
"ApConfigResultDict",
"ApConfigMixin",
"AP_CONFIG_PARTS",
"AP_CONFIG_SSID_ATTRIBUTES",
"ap_config_ssid_part",
"ap_config_vlan_part",
"is_ap_config_part",
):
assert name in napalm_device_types.__all__, name
def test_models_re_exports_the_shapes(self):
from napalm_device_types import models
assert models.DhcpLeaseDict is DhcpLeaseDict
assert models.SsidConfigDict is SsidConfigDict
assert models.ApConfigDict is ApConfigDict
class TestParts:
def test_auto_is_channel_zero(self):
assert AUTO_CHANNEL == 0
def test_the_fixed_parts(self):
assert {
"radio/country",
"radio/5g_htmode",
"radio/2g_htmode",
"system/syslog",
"system/web_ui",
"system/ssh_port",
"system/password_auth",
"network/bridge",
"network/bridge_stp",
} == AP_CONFIG_PARTS
def test_an_ssid_part_is_split_at_its_last_slash(self):
"""An SSID may hold a slash; the attribute never does."""
assert ap_config_ssid_part("wireless/Home/hidden") == ("Home", "hidden")
assert ap_config_ssid_part("wireless/a/b/encryption") == ("a/b", "encryption")
@pytest.mark.parametrize(
"part",
[
"wireless/Home/colour",
"wireless/hidden",
"wireless//hidden",
"network/vlan/10",
"radio/country",
],
)
def test_no_ssid_part(self, part):
assert ap_config_ssid_part(part) is None
def test_a_vlan_part(self):
assert ap_config_vlan_part("network/vlan/10") == 10
@pytest.mark.parametrize(
"part", ["network/vlan/", "network/vlan/x", "network/vlan/0", "network/vlan/4095"]
)
def test_no_vlan_part(self, part):
assert ap_config_vlan_part(part) is None
@pytest.mark.parametrize(
("part", "known"),
[
("radio/country", True),
("network/vlan/20", True),
("wireless/Guest/acl", True),
("wireless/Guest/passphrase", True),
("system/luci", False),
("wireless/Guest/extra/x", False),
("", False),
],
)
def test_known_parts(self, part, known):
assert is_ap_config_part(part) is known
def test_every_attribute_an_ssid_part_names(self):
assert {
"missing",
"vlan",
"bands",
"encryption",
"passphrase",
"hidden",
"enabled",
"client_isolation",
"max_clients",
"disassoc_low_ack",
"max_inactivity",
"acl",
"ft",
} == AP_CONFIG_SSID_ATTRIBUTES