| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package l2topology |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | func adjacencySideToEndpoint(dev Device, port string, ifIndexByDeviceName map[string]int, ifaceByDeviceIndex map[string]Interface) LinkEndpoint { |
| 11 | match := buildDeviceActorMatch(dev, nil) |
| 12 | |
| 13 | port = strings.TrimSpace(port) |
| 14 | ifName := "" |
| 15 | ifDescr := "" |
| 16 | ifIndex := 0 |
| 17 | var iface Interface |
| 18 | hasIface := false |
| 19 | if port != "" { |
| 20 | ifIndex = resolveIfIndexByPortName(dev.ID, port, ifIndexByDeviceName) |
| 21 | } |
| 22 | if ifIndex > 0 { |
| 23 | if ifaceValue, ok := ifaceByDeviceIndex[deviceIfIndexKey(dev.ID, ifIndex)]; ok { |
| 24 | iface = ifaceValue |
| 25 | hasIface = true |
| 26 | ifName = strings.TrimSpace(iface.IfName) |
| 27 | ifDescr = strings.TrimSpace(iface.IfDescr) |
| 28 | } |
| 29 | } |
| 30 | if ifName == "" { |
| 31 | ifName = ifDescr |
| 32 | } |
| 33 | if ifName == "" { |
| 34 | ifName = port |
| 35 | } |
| 36 | if ifIndex > 0 && ifName == "" { |
| 37 | ifName = strconv.Itoa(ifIndex) |
| 38 | } |
| 39 | |
| 40 | attrs := map[string]any{ |
| 41 | "if_index": ifIndex, |
| 42 | "if_name": ifName, |
| 43 | "port_id": port, |
| 44 | "sys_name": strings.TrimSpace(dev.Hostname), |
| 45 | "management_ip": firstAddress(dev.Addresses), |
| 46 | } |
| 47 | if ifDescr != "" { |
| 48 | attrs["if_descr"] = ifDescr |
| 49 | } |
| 50 | if ifIndex > 0 && hasIface { |
| 51 | if admin := strings.TrimSpace(iface.Labels["admin_status"]); admin != "" { |
| 52 | attrs["if_admin_status"] = admin |
| 53 | } |
| 54 | if oper := strings.TrimSpace(iface.Labels["oper_status"]); oper != "" { |
| 55 | attrs["if_oper_status"] = oper |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return LinkEndpoint{ |
| 60 | Match: match, |
| 61 | Attributes: pruneTopologyAttributes(attrs), |
| 62 | } |
| 63 | } |