fix(wan): read the uplink from the gateway's status page (#29)

get_wan_status built its answer from WanIp, WanAccessType,
SystemUpTime, MaxUpstream and MaxDownstream in getBasicInformation.
A VMG4005-B50A sends none of them there, so every gateway's uplink
read as down, with no address and no sync rate; get_nat_translations
built inside_global from the same WanIp and reported ":<port>".

Both now read the DAL's cardpage_status. A bridge has no WAN address
of its own, so it is connected when its DSL line is in sync (a
DslChannelInfo entry with Status "Up"); a router is connected when a
WAN interface that is not IP_Bridged is up with an address. The sync
rates are those of the channels in sync, added up for bonded lines;
a channel out of sync keeps its last rates, which are not reported.
A translation without a WAN address goes to 0.0.0.0, and without
enabled port forwards the status page is not read at all.

The tests read the recorded, anonymised status page of a
VMG4005-B50A bridge whose line was not in sync; the in-sync and
router states are derived from it.

Closes #29
This commit is contained in:
Christian Manivong committed 2026-11-12 19:05:48 +01:00
1 parent a47f9c3970
commit 3213dfaaaa
7 files changed
+672 -28

No files matched your search

+88 -1
View File
@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import Any
from napalm_device_types.models import HostDict, PortForwardDict
from napalm_device_types.models import HostDict, PortForwardDict, WANStatusDict
#: The name ``lanhosts`` gives a host the gateway has no name for.
_UNKNOWN_HOST = "Unknown"
@@ -60,3 +60,90 @@ def port_forwards(objects: list[dict[str, Any]]) -> list[PortForwardDict]:
forward["remote_host"] = str(rule["OriginatingIpAddress"])
forwards.append(forward)
return forwards
def _channels_in_sync(page: dict[str, Any]) -> list[dict[str, Any]]:
"""The DSL channels of a ``cardpage_status`` page that are in sync."""
return [
channel
for channel in page.get("DslChannelInfo") or []
if isinstance(channel, dict) and channel.get("Status") == "Up"
]
def _routed_wans(page: dict[str, Any]) -> list[dict[str, Any]]:
"""The enabled WAN interfaces of a ``cardpage_status`` page that are not bridges."""
return [
entry
for entry in page.get("WanLanInfo") or []
if isinstance(entry, dict)
and entry.get("X_ZYXEL_Type") == "WAN"
and entry.get("Enable")
and entry.get("X_ZYXEL_ConnectionType") != "IP_Bridged"
]
def _address(entry: dict[str, Any]) -> str:
"""The first enabled IPv4 address of a ``WanLanInfo`` entry, else ``""``."""
for addr in entry.get("IPv4Address") or []:
if isinstance(addr, dict) and addr.get("Enable") and addr.get("IPAddress"):
return str(addr["IPAddress"])
return ""
def _connected_wan(page: dict[str, Any]) -> dict[str, Any] | None:
"""The routed WAN interface that is up with an address, if there is one."""
for entry in _routed_wans(page):
if entry.get("Status") == "Up" and _address(entry):
return entry
return None
def wan_address(page: dict[str, Any]) -> str:
"""The gateway's own WAN address on a ``cardpage_status`` page; ``""`` for a bridge
or a router whose WAN is not up."""
wan = _connected_wan(page)
return _address(wan) if wan is not None else ""
def _seconds(value: Any) -> int:
"""*value* as a number of seconds; ``0`` (unknown) when it is not a number."""
text = str(value if value is not None else "").strip()
return int(text) if text.isdigit() else 0
def _uplink(page: dict[str, Any], channels: list[dict[str, Any]]) -> tuple[bool, str, int]:
"""Whether the uplink is connected, its type and how long it has been up."""
routed = _routed_wans(page)
if not routed: # a bridge: the DSL line is the uplink
uptime = _seconds(channels[0].get("DSLUpTime")) if channels else 0
return bool(channels), "DSL", uptime
wan = _connected_wan(page)
connection_type = str(routed[0].get("Encapsulation") or "")
if wan is None:
return False, connection_type, 0
return True, connection_type, _seconds(wan.get("X_ZYXEL_UpTime"))
def wan_status(page: dict[str, Any]) -> WANStatusDict:
"""The uplink of a ``cardpage_status`` page.
A router (a WAN interface that is not ``IP_Bridged``) is connected when such an
interface is up with an address. A bridge has no WAN address of its own: it is
connected when its DSL line is in sync. The sync rates (kbit/s) are those of the
channels in sync, added up for bonded lines; a channel out of sync keeps the rates
of its last sync, which are not reported.
"""
channels = _channels_in_sync(page)
connected, connection_type, uptime = _uplink(page, channels)
return {
"connection_type": connection_type,
"is_connected": connected,
"external_ip": wan_address(page),
"uptime": uptime,
"bytes_sent": 0,
"bytes_received": 0,
"max_bitrate_up": sum(int(c.get("UpstreamCurrRate") or 0) for c in channels),
"max_bitrate_down": sum(int(c.get("DownstreamCurrRate") or 0) for c in channels),
"link_status": "Up" if channels else "Down",
}