feat: continue cable trace across wireless links

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-11-05 10:04:04 +01:00
1 parent d49c4c6ee7
commit 102b161356
5 files changed
+222 -2

No files matched your search

+1
View File
@@ -225,6 +225,7 @@ sudo systemctl restart netbox netbox-rq
| `hardware_vcpu_ratio_warning` | `4` | Warn when a host or cluster has more vCPUs per physical core |
| `switch_ports_enabled` | `True` | Front panel and **Export switch port usage** button for switches |
| `switch_device_roles` | `["Switch"]` | Device roles (case-insensitive) that get the front panel and export button |
| `wireless_trace_enabled` | `True` | Cable trace continues across wireless links (radio bridges) to the cabled port of the peer radio |
## Usage
+6 -1
View File
@@ -1,6 +1,6 @@
from netbox.plugins import PluginConfig, get_plugin_config
__version__ = "0.25.0"
__version__ = "0.26.0"
class NetBoxUtilitiesConfig(PluginConfig):
@@ -31,6 +31,7 @@ class NetBoxUtilitiesConfig(PluginConfig):
"hardware_vcpu_ratio_warning": 4,
"switch_ports_enabled": True,
"switch_device_roles": ["Switch"],
"wireless_trace_enabled": True,
}
middleware = [
"netbox_utilities.middleware.GlobalTenantFilterMiddleware",
@@ -67,6 +68,10 @@ class NetBoxUtilitiesConfig(PluginConfig):
from .connection_scope import install_connection_scope_filter
install_connection_scope_filter()
if get_plugin_config("netbox_utilities", "wireless_trace_enabled"):
from .wireless_trace import install_wireless_trace
install_wireless_trace()
if self.apps.is_installed("netbox_export"):
from .netbox_export import install_netbox_export_rack_width_support
@@ -0,0 +1,71 @@
from types import SimpleNamespace
from unittest.mock import patch
from django.test import SimpleTestCase
from netbox_utilities import wireless_trace
class Interface(SimpleNamespace):
pass
def iface(pk, device_id, wireless_link_id=None, trace=()):
return Interface(pk=pk, device_id=device_id, wireless_link_id=wireless_link_id, trace=lambda: list(trace))
class WirelessTraceTest(SimpleTestCase):
def setUp(self):
# Switch port 1 --cable--> TG ether1 (10); TG wlan60 (11) ~~ Funk wlan60 (21);
# Funk ether1 (20) --cable--> Switch2 port (30)
self.switch_port = iface(1, 1)
self.tg_ether = iface(10, 2)
self.tg_wlan = iface(11, 2, wireless_link_id=99)
self.funk_wlan = iface(21, 3, wireless_link_id=99)
self.switch2_port = iface(30, 4)
self.funk_ether = iface(20, 3, trace=[([iface(20, 3)], ["cable34"], [self.switch2_port])])
self.link = SimpleNamespace(pk=99)
def hop(device_id, visited):
if device_id == 2 and 11 not in visited:
return self.tg_wlan, self.link, self.funk_wlan
if device_id == 3 and 21 not in visited:
return self.funk_wlan, self.link, self.tg_wlan
return None
def cabled(device_id, visited):
if device_id == 3 and 20 not in visited:
return self.funk_ether
return None
patches = [
patch.object(wireless_trace, "_wireless_hop", side_effect=hop),
patch.object(wireless_trace, "_cabled_interface", side_effect=cabled),
patch("dcim.models.Interface", Interface),
]
for p in patches:
p.start()
self.addCleanup(p.stop)
def test_appends_radio_hop_and_peer_cable(self):
trace = [([self.switch_port], ["cable48"], [self.tg_ether])]
result = wireless_trace.extended_trace(self.switch_port, trace)
self.assertEqual(len(result), 3)
self.assertEqual(result[1], ([self.tg_wlan], [self.link], [self.funk_wlan]))
self.assertEqual(result[2][1], ["cable34"])
self.assertIs(result[2][2][0], self.switch2_port)
def test_prepends_reversed_chain_for_radio_origin_side(self):
trace = [([self.tg_ether], ["cable48"], [self.switch_port])]
result = wireless_trace.extended_trace(self.tg_ether, trace)
self.assertEqual(len(result), 3)
self.assertIs(result[0][0][0], self.switch2_port)
self.assertEqual(result[1], ([self.funk_wlan], [self.link], [self.tg_wlan]))
self.assertEqual(result[2][1], ["cable48"])
def test_empty_trace_is_left_alone(self):
self.assertEqual(wireless_trace.extended_trace(self.tg_ether, []), [])
+143
View File
@@ -0,0 +1,143 @@
"""Continue NetBox cable traces across wireless links (radio bridges).
NetBox stops a trace at the interface a cable ends on. For a point-to-point
radio such as a MikroTik Cube, the interesting part continues on the far side:
the device's wireless interface is linked to a peer radio, whose cabled
interface leads on to the next switch. This module extends the cable trace
SVG at both ends with these hops:
... cable -> ether1 [Radio A] wlan60 ~~ wlan60 [Radio B] ether1 -> cable ...
"""
MAX_HOPS = 8
def _single_interface(terminations):
from dcim.models import Interface
if len(terminations) == 1 and isinstance(terminations[0], Interface):
return terminations[0]
return None
def _wireless_hop(device_id, visited):
"""Return (local, link, peer) for a not yet visited wireless link of the device."""
from dcim.models import Interface
candidates = (
Interface.objects.filter(device_id=device_id, wireless_link__isnull=False)
.exclude(pk__in=visited)
.select_related("wireless_link")
)
for local in candidates:
link = local.wireless_link
peer_id = link.interface_b_id if link.interface_a_id == local.pk else link.interface_a_id
if peer_id in visited:
continue
peer = Interface.objects.select_related("device").filter(pk=peer_id).first()
if peer is not None:
return local, link, peer
return None
def _cabled_interface(device_id, visited):
"""Return the device's first not yet visited interface that carries a cable."""
from dcim.models import Interface
return (
Interface.objects.filter(device_id=device_id, cable__isnull=False, wireless_link__isnull=True)
.exclude(pk__in=visited)
.first()
)
def _collect_ids(segments):
ids = set()
for near_ends, _links, far_ends in segments:
for termination in (*near_ends, *far_ends):
if termination.__class__.__name__ == "Interface":
ids.add(termination.pk)
return ids
def extend_outwards(interface, visited, max_hops=MAX_HOPS):
"""Segments continuing from `interface` through radio links, away from it.
`interface` is the outermost interface of a trace; the returned segments
start on another interface of the same device. `visited` (interface pks)
is updated in place and prevents loops.
"""
segments = []
current = interface
arrived_by_radio = bool(getattr(interface, "wireless_link_id", None))
for _ in range(max_hops):
if arrived_by_radio:
# Leave the radio through its cabled port.
cabled = _cabled_interface(current.device_id, visited)
if cabled is None:
break
trace = [tuple(list(part) for part in segment) for segment in cabled.trace()]
visited.add(cabled.pk)
if not trace:
break
visited |= _collect_ids(trace)
segments.extend(trace)
current = _single_interface(trace[-1][2])
if current is None:
break
arrived_by_radio = bool(current.wireless_link_id)
else:
hop = _wireless_hop(current.device_id, visited)
if hop is None:
break
local, link, peer = hop
visited.update({local.pk, peer.pk})
segments.append(([local], [link], [peer]))
current = peer
arrived_by_radio = True
return segments
def reverse_segments(segments):
return [(list(far_ends), list(links), list(near_ends)) for near_ends, links, far_ends in reversed(segments)]
def extended_trace(origin, trace, max_hops=MAX_HOPS):
"""Return `trace` with radio hops prepended and appended where possible."""
from dcim.models import Interface
trace = [tuple(list(part) for part in segment) for segment in trace]
if not trace or not isinstance(origin, Interface):
return trace
visited = {origin.pk} | _collect_ids(trace)
tail = []
last = _single_interface(trace[-1][2])
if last is not None:
tail = extend_outwards(last, visited, max_hops)
head = reverse_segments(extend_outwards(origin, visited, max_hops))
return head + trace + tail
def install_wireless_trace():
from dcim.svg import CableTraceSVG
if getattr(CableTraceSVG, "_netbox_utilities_wireless_trace", False):
return
original_render = CableTraceSVG.render
def render(self, *args, **kwargs):
origin = self.origin
native_trace = origin.trace
try:
origin.trace = lambda: extended_trace(origin, native_trace())
except AttributeError:
return original_render(self, *args, **kwargs)
try:
return original_render(self, *args, **kwargs)
finally:
del origin.trace
CableTraceSVG.render = render
CableTraceSVG._netbox_utilities_wireless_trace = True
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "netbox-utilities"
version = "0.25.0"
version = "0.26.0"
description = "Navigation, tenant utilities, connection VLANs, partial-width racks, disks, CPU & RAM, switch port export and bulk operations for NetBox 4.6/4.7"
readme = "README.md"
requires-python = ">=3.12"