| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package l2topology |
| 4 | |
| 5 | import ( |
| 6 | "sort" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | func filterManagedDeviceHints(hints []string, managedDeviceIDs map[string]struct{}) []string { |
| 11 | if len(hints) == 0 || len(managedDeviceIDs) == 0 { |
| 12 | return hints |
| 13 | } |
| 14 | managed := make([]string, 0, len(hints)) |
| 15 | for _, hint := range hints { |
| 16 | hint = strings.TrimSpace(hint) |
| 17 | if hint == "" { |
| 18 | continue |
| 19 | } |
| 20 | if _, ok := managedDeviceIDs[hint]; ok { |
| 21 | managed = append(managed, hint) |
| 22 | } |
| 23 | } |
| 24 | if len(managed) == 0 { |
| 25 | return nil |
| 26 | } |
| 27 | managed = uniqueTopologyStrings(managed) |
| 28 | sort.Strings(managed) |
| 29 | return managed |
| 30 | } |
| 31 | |
| 32 | func topologyEndpointLabelDeviceIDs(labels map[string]string) []string { |
| 33 | out := labelsCSVToSlice(labels, "learned_device_ids") |
| 34 | if len(out) == 0 { |
| 35 | out = labelsCSVToSlice(labels, "device_ids") |
| 36 | } |
| 37 | out = uniqueTopologyStrings(out) |
| 38 | sort.Strings(out) |
| 39 | return out |
| 40 | } |
| 41 | |
| 42 | func resolveTopologyEndpointDeviceHints( |
| 43 | hints []string, |
| 44 | aliasOwnerIDs map[string]map[string]struct{}, |
| 45 | ) []string { |
| 46 | set := make(map[string]struct{}) |
| 47 | for _, hint := range hints { |
| 48 | hint = strings.TrimSpace(hint) |
| 49 | if hint == "" { |
| 50 | continue |
| 51 | } |
| 52 | if alias := normalizeTopologyEndpointDeviceAlias(hint); alias != "" { |
| 53 | if owners := aliasOwnerIDs[alias]; len(owners) > 0 { |
| 54 | for ownerID := range owners { |
| 55 | ownerID = strings.TrimSpace(ownerID) |
| 56 | if ownerID == "" { |
| 57 | continue |
| 58 | } |
| 59 | set[ownerID] = struct{}{} |
| 60 | } |
| 61 | continue |
| 62 | } |
| 63 | } |
| 64 | set[hint] = struct{}{} |
| 65 | } |
| 66 | if len(set) == 0 { |
| 67 | return nil |
| 68 | } |
| 69 | return sortedTopologySet(set) |
| 70 | } |
| 71 | |
| 72 | func normalizeTopologyEndpointDeviceAlias(hint string) string { |
| 73 | hint = strings.TrimSpace(hint) |
| 74 | if hint == "" { |
| 75 | return "" |
| 76 | } |
| 77 | if alias := normalizeFDBEndpointID(hint); alias != "" { |
| 78 | return alias |
| 79 | } |
| 80 | lower := strings.ToLower(hint) |
| 81 | if strings.HasPrefix(lower, "macaddress:") { |
| 82 | if mac := normalizeMAC(hint[len("macAddress:"):]); mac != "" { |
| 83 | return "mac:" + mac |
| 84 | } |
| 85 | } |
| 86 | return "" |
| 87 | } |