Przeglądaj źródła

fix(topology): fill link speeds matching polled ports by ID, Iface and Label (#1512)

Closes #1510.

Match polled ports against link/dist-node names by ID and Iface (stripLANPrefix + EqualFold). SNMP ports carry the human name in Iface (snmp-<ifIndex> IDs) and the agent can put the role in the ID with the physical interface in Iface, which is what LLDP/FDB/overrides report as childPort. Label stays out of the match on purpose (display-only).

Co-authored-by: Bann-DEV <bann.dev@gmail.com>
Bann-DEV 1 miesiąc temu
rodzic
commit
aa77b4bca1

+ 30 - 4
server-go/internal/adapters/topology.go

@@ -775,7 +775,7 @@ func fillNodeSpeeds(polled map[string]*routerPolled, dists []DistributionNode) {
 			continue
 		}
 		for _, ep := range p.ports {
-			if ep.ID != dists[i].Port || !ep.Up {
+			if !ep.Up || !portMatchesName(ep, dists[i].Port) {
 				continue
 			}
 			if mbps := parseSpeedMbps(ep.Speed); mbps > 0 {
@@ -797,6 +797,32 @@ func stripLANPrefix(s string) string {
 	return s
 }
 
+// portMatchesName casa una boca sondeada (EthPort) con el nombre de puerto
+// que trae un enlace o un distnode. Comparar solo ep.ID se queda corto en
+// dos casos reales: los puertos SNMP viven con ID "snmp-<ifIndex>" y el
+// nombre humano ("xg2", "1/g9") en Iface, y el agente puede rotular el ID
+// con el rol ("wan") dejando la interfaz física ("eth0") en Iface, que es
+// justo la que reportan LLDP/FDB/override como childPort. Se prueban ID e
+// Iface con la misma normalización lan* y sin distinguir mayúsculas
+// (EqualFold cubre también la igualdad exacta); Label queda fuera porque es
+// campo de display ("LAN 1") y nunca sobrevive a la normalización. Sin dato
+// en el enlace, false para no afirmar velocidad.
+func portMatchesName(ep EthPort, name string) bool {
+	if name == "" {
+		return false
+	}
+	n := stripLANPrefix(name)
+	for _, cand := range []string{ep.ID, ep.Iface} {
+		if cand == "" {
+			continue
+		}
+		if strings.EqualFold(stripLANPrefix(cand), n) {
+			return true
+		}
+	}
+	return false
+}
+
 // fillUplinkSpeeds (#1321): velocidad del uplink de una unidad cuando el
 // padre la reporta en esa boca (EthPort up + parseSpeedMbps, el mismo
 // criterio conservador de fillNodeSpeeds). El From del enlace puede ser el
@@ -832,7 +858,7 @@ func fillUplinkSpeeds(polled map[string]*routerPolled, dists []DistributionNode,
 			continue
 		}
 		for _, ep := range p.ports {
-			if ep.Up && (ep.ID == l.Port || stripLANPrefix(ep.ID) == stripLANPrefix(l.Port)) {
+			if ep.Up && portMatchesName(ep, l.Port) {
 				if mbps := parseSpeedMbps(ep.Speed); mbps > 0 {
 					l.SpeedMbps = mbps
 				}
@@ -874,7 +900,7 @@ func fillWiredSpeeds(polled map[string]*routerPolled, dists []DistributionNode,
 			continue
 		}
 		for _, ep := range p.ports {
-			if ep.Up && (ep.ID == l.Port || stripLANPrefix(ep.ID) == stripLANPrefix(l.Port)) {
+			if ep.Up && portMatchesName(ep, l.Port) {
 				if mbps := parseSpeedMbps(ep.Speed); mbps > 0 {
 					l.SpeedMbps = mbps
 				}
@@ -911,7 +937,7 @@ func fillUplinkSpeedsFromChild(polled map[string]*routerPolled, evidence map[str
 			continue
 		}
 		for _, ep := range p.ports {
-			if ep.Up && (ep.ID == ev.childPort || stripLANPrefix(ep.ID) == stripLANPrefix(ev.childPort)) {
+			if ep.Up && portMatchesName(ep, ev.childPort) {
 				if mbps := parseSpeedMbps(ep.Speed); mbps > 0 {
 					l.SpeedMbps = mbps
 				}

+ 37 - 0
server-go/internal/adapters/topology_child_speed_test.go

@@ -115,3 +115,40 @@ func TestLldpUplinkSpeedParentWins(t *testing.T) {
 		t.Fatalf("velocidad %d Mbps, want 1000 (la del padre no se pisa)", links[0].SpeedMbps)
 	}
 }
+
+// Los puertos SNMP viven con ID "snmp-<ifIndex>" y el nombre humano en
+// Iface/Label; comparar solo el ID dejaba todos los uplinks de switch sin
+// velocidad ("Cable 1G" genérico en el mapa y "—" en la tabla).
+func TestFillUplinkSpeedsSnmpPortName(t *testing.T) {
+	polled := map[string]*routerPolled{
+		"sw1": {ports: []EthPort{
+			{ID: "snmp-2", Label: "xg2", Iface: "xg2", Up: true, Speed: "10 Gbps"},
+			{ID: "snmp-9", Label: "1/g9", Iface: "1/g9", Up: true, Speed: "1 Gbps"},
+		}},
+	}
+	links := []TopoLink{
+		{From: "sw1", To: "ap1", Kind: "uplink", Port: "xg2"},
+		{From: "sw1", To: "ap2", Kind: "uplink", Port: "1/g9"},
+	}
+	fillUplinkSpeeds(polled, nil, links)
+	if links[0].SpeedMbps != 10000 {
+		t.Fatalf("xg2 vía Iface SNMP: %d, want 10000", links[0].SpeedMbps)
+	}
+	if links[1].SpeedMbps != 1000 {
+		t.Fatalf("1/g9 vía Label SNMP: %d, want 1000", links[1].SpeedMbps)
+	}
+}
+
+// El agente puede rotular el ID con el rol ("wan") dejando la interfaz
+// física en Iface; el childPort de LLDP/FDB es la interfaz, no el rol.
+func TestFillUplinkSpeedsFromChildIface(t *testing.T) {
+	polled := map[string]*routerPolled{
+		"ap1": {ports: []EthPort{{ID: "wan", Iface: "eth0", Label: "WAN", Up: true, Speed: "10 Gbps"}}},
+	}
+	evidence := map[string]topoParent{"ap1": {parent: "sw1", port: "xg2", childPort: "eth0"}}
+	links := []TopoLink{{From: "sw1", To: "ap1", Kind: "uplink", Port: "xg2"}}
+	fillUplinkSpeedsFromChild(polled, evidence, links)
+	if links[0].SpeedMbps != 10000 {
+		t.Fatalf("childPort eth0 contra Iface: %d, want 10000", links[0].SpeedMbps)
+	}
+}