Преглед изворни кода

feat(topology): anchor fleet uplinks to the FDB-learned parent (#1047) (#1049)

Without LLDP, every non-gateway router was drawn hanging from the
gateway. The managed switch FDB already knows which port learned each
fleet unit's MAC: use that attribution to anchor each uplink to its real
parent (ap1 under sw1, sw1 under the gateway), with the switch port on
the link.

Fleet units also stop duplicating as client chips: their leased bridge
MAC used to show up as an extra device next to the router node.
Nacho пре 2 месеци
родитељ
комит
15db1e037e

+ 3 - 1
app/src/components/topology/model.ts

@@ -1425,7 +1425,9 @@ export function buildTopologyModel({ routers, devices, wan, wireguard, distribut
           if (!node) return null
           const isWifi = node.router.backhaul === 'wifi'
           const label = isWifi ? 'WiFi uplink' : `Cable 1G${node.router.lldp ? ' · LLDP' : ''}`
-          const from = gatewayNode ?? null
+          // #1047: el padre lo decide la semántica (FDB del switch cuando no
+          // hay LLDP); solo se cae al gateway si el server no lo resolvió.
+          const from = routerById.get(sl.from) ?? gatewayNode ?? null
           const d = from ? curve(from, node) : ''
           return {
             id: `uplink-${node.id}`, kind: 'uplink', wifi: isWifi,

+ 39 - 3
server-go/internal/adapters/topo_semantics.go

@@ -78,6 +78,32 @@ func BuildTopoSemantics(routers []Router, devices []Device, wg WireGuardStats, d
 		}
 		online = append(online, d)
 	}
+	// #1047: las unidades de la flota (routers/switches gestionados) también
+	// aparecen como Device porque su brMac tiene lease, pero el nodo del
+	// router ya las representa: se excluyen de los chips y su atribución
+	// FDB (dónde se vio la MAC y en qué puerto) queda como evidencia para
+	// anclar el uplink al padre REAL (p. ej. ap1 cuelga de sw1, no del
+	// gateway) aunque no haya LLDP.
+	routerMacs := map[string]string{} // MAC(upper) → router id
+	for _, r := range routers {
+		if r.MAC != "" {
+			routerMacs[strings.ToUpper(r.MAC)] = r.ID
+		}
+	}
+	uplinkEvidence := map[string]struct{ parent, port string }{}
+	filtered := online[:0]
+	for _, d := range online {
+		if rid, ok := routerMacs[strings.ToUpper(d.MAC)]; ok {
+			if rid != d.RouterID {
+				if _, seen := uplinkEvidence[rid]; !seen {
+					uplinkEvidence[rid] = struct{ parent, port string }{d.RouterID, d.Port}
+				}
+			}
+			continue
+		}
+		filtered = append(filtered, d)
+	}
+	online = filtered
 	// #1042: el equipo aguas arriba del gateway (módem/ONT del ISP) se
 	// descubre por ARP como un cliente más, pero vive en el lado WAN: si su
 	// IP coincide con la puerta de enlace WAN se representa bajo el nodo
@@ -162,11 +188,21 @@ func BuildTopoSemantics(routers []Router, devices []Device, wg WireGuardStats, d
 		sem.Links = append(sem.Links, TopoLink{From: "internet", To: wanPeerID, Kind: "wan-peer"})
 	}
 	for _, ap := range nonGateway {
-		l := TopoLink{From: gateway.ID, To: ap.ID, Kind: "uplink"}
+		parent := gateway.ID
+		port := ""
 		if ap.Lldp != nil {
-			l.Port = ap.Lldp.PortDesc // puerto del uplink en el gateway (C2)
+			port = ap.Lldp.PortDesc
+		}
+		// #1047: la evidencia FDB (MAC del equipo aprendida en el puerto de
+		// otro miembro de la flota) manda sobre el fallback al gateway; el
+		// puerto reportado es el del padre (switch), no del gateway.
+		if ev, ok := uplinkEvidence[ap.ID]; ok && ev.parent != ap.ID {
+			parent = ev.parent
+			if ev.port != "" {
+				port = ev.port
+			}
 		}
-		sem.Links = append(sem.Links, l)
+		sem.Links = append(sem.Links, TopoLink{From: parent, To: ap.ID, Kind: "uplink", Port: port})
 	}
 	// router → distnode (solo inferred|managed son hubs propios en el mapa).
 	// En una cadena LLDP switch→switch (issue #300) el distnode cuelga de su

+ 38 - 0
server-go/internal/adapters/topo_semantics_test.go

@@ -228,3 +228,41 @@ func TestTopoSemanticsWanPeer(t *testing.T) {
 		t.Fatalf("WanPeer sin gateway WAN = %q, want vacío", sem2.WanPeer)
 	}
 }
+
+// #1047: la MAC de una unidad de flota aprendida en el FDB de otro miembro
+// ancla su uplink al padre real (ap1 cuelga de sw1, no del gateway), aunque
+// no haya LLDP. La unidad deja de duplicarse como chip de cliente.
+func TestTopoSemanticsFdbUplinkParent(t *testing.T) {
+	routers := []Router{
+		{ID: "gw", Name: "gateway", RoleBadge: "Principal", MAC: "AA:BB:CC:00:00:01"},
+		{ID: "sw1", Name: "sw1", MAC: "AA:BB:CC:00:00:02"},
+		{ID: "ap1", Name: "ap1", MAC: "AA:BB:CC:00:00:03"},
+	}
+	devices := []Device{
+		// sw1 visto en el puerto 1 del gateway; ap1 en el puerto 5 de sw1.
+		{ID: "dev-sw1", MAC: "AA:BB:CC:00:00:02", RouterID: "gw", Band: "cable", Online: true, Port: "1"},
+		{ID: "dev-ap1", MAC: "AA:BB:CC:00:00:03", RouterID: "sw1", Band: "cable", Online: true, Port: "5"},
+		{ID: "nas", MAC: "AA:BB:CC:00:00:09", RouterID: "gw", Band: "cable", Online: true, Port: "2"},
+	}
+	sem := BuildTopoSemantics(routers, devices, WireGuardStats{}, nil, "")
+	links := map[string]TopoLink{}
+	for _, l := range sem.Links {
+		if l.Kind == "uplink" {
+			links[l.To] = l
+		}
+	}
+	if got := links["sw1"]; got.From != "gw" || got.Port != "1" {
+		t.Fatalf("uplink sw1: %+v, want from gw puerto 1", got)
+	}
+	if got := links["ap1"]; got.From != "sw1" || got.Port != "5" {
+		t.Fatalf("uplink ap1: %+v, want from sw1 puerto 5", got)
+	}
+	// Las unidades de flota no duplican como chips de cliente.
+	for _, ring := range sem.Rings {
+		for _, id := range ring {
+			if id == "dev-sw1" || id == "dev-ap1" {
+				t.Fatalf("unidad de flota en anillo: %s", id)
+			}
+		}
+	}
+}