| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package l2topology |
| 4 | |
| 5 | import "strings" |
| 6 | |
| 7 | func buildTopologyDevicePortStatusAttributes(st topologyDevicePortStatus) map[string]any { |
| 8 | portStatus := map[string]any{ |
| 9 | "if_index": st.IfIndex, |
| 10 | "if_name": strings.TrimSpace(st.IfName), |
| 11 | "if_descr": strings.TrimSpace(st.IfDescr), |
| 12 | "if_alias": strings.TrimSpace(st.IfAlias), |
| 13 | "mac": strings.TrimSpace(st.MAC), |
| 14 | "duplex": strings.TrimSpace(st.Duplex), |
| 15 | "link_mode": st.LinkMode, |
| 16 | "link_mode_confidence": st.ModeConfidence, |
| 17 | "topology_role": st.TopologyRole, |
| 18 | "topology_role_confidence": st.RoleConfidence, |
| 19 | } |
| 20 | if st.SpeedBps > 0 { |
| 21 | portStatus["speed"] = st.SpeedBps |
| 22 | } |
| 23 | if st.LastChange > 0 { |
| 24 | portStatus["last_change"] = st.LastChange |
| 25 | } |
| 26 | if len(st.ModeSources) > 0 { |
| 27 | portStatus["link_mode_sources"] = st.ModeSources |
| 28 | } |
| 29 | if len(st.RoleSources) > 0 { |
| 30 | portStatus["topology_role_sources"] = st.RoleSources |
| 31 | } |
| 32 | if len(st.VLANIDs) > 0 { |
| 33 | portStatus["vlan_ids"] = st.VLANIDs |
| 34 | } |
| 35 | if len(st.VLANs) > 0 { |
| 36 | portStatus["vlans"] = st.VLANs |
| 37 | } |
| 38 | if st.FDBMACCount > 0 { |
| 39 | portStatus["fdb_mac_count"] = st.FDBMACCount |
| 40 | } |
| 41 | if st.STPState != "" { |
| 42 | portStatus["stp_state"] = st.STPState |
| 43 | } |
| 44 | if len(st.Neighbors) > 0 { |
| 45 | neighbors := make([]map[string]any, 0, len(st.Neighbors)) |
| 46 | for _, neighbor := range st.Neighbors { |
| 47 | if attrs := topologyPortNeighborStatusToAttributes(neighbor); len(attrs) > 0 { |
| 48 | neighbors = append(neighbors, attrs) |
| 49 | } |
| 50 | } |
| 51 | if len(neighbors) > 0 { |
| 52 | portStatus["neighbors"] = neighbors |
| 53 | } |
| 54 | } |
| 55 | if st.AdminStatus != "" { |
| 56 | portStatus["admin_status"] = st.AdminStatus |
| 57 | } |
| 58 | if st.OperStatus != "" { |
| 59 | portStatus["oper_status"] = st.OperStatus |
| 60 | } |
| 61 | if st.InterfaceType != "" { |
| 62 | portStatus["if_type"] = st.InterfaceType |
| 63 | } |
| 64 | return pruneTopologyAttributes(portStatus) |
| 65 | } |