Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.model.topo; |
| 18 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 19 | import org.onosproject.cluster.NodeId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.HostId; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 22 | import org.onosproject.net.PortNumber; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 23 | import org.onosproject.net.region.RegionId; |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.model.ServiceBundle; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | import java.util.Collections; |
| 30 | import java.util.Comparator; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 31 | import java.util.HashMap; |
| 32 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 33 | import java.util.List; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 34 | import java.util.Map; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 35 | import java.util.Objects; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 36 | import java.util.Set; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 37 | import java.util.stream.Collectors; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 38 | |
| 39 | import static com.google.common.base.MoreObjects.toStringHelper; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 40 | import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 41 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Represents the overall network topology. |
| 44 | */ |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 45 | public class UiTopology extends UiElement { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 46 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 47 | private static final String INDENT_1 = " "; |
| 48 | private static final String INDENT_2 = " "; |
| 49 | private static final String EOL = String.format("%n"); |
| 50 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 51 | private static final String E_UNMAPPED = |
| 52 | "Attempting to retrieve unmapped {}: {}"; |
| 53 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 54 | private static final String DEFAULT_TOPOLOGY_ID = "TOPOLOGY-0"; |
| 55 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 56 | private static final Logger log = LoggerFactory.getLogger(UiTopology.class); |
| 57 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 58 | private static final Comparator<UiClusterMember> CLUSTER_MEMBER_COMPARATOR = |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 59 | Comparator.comparing(UiClusterMember::idAsString); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 60 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 61 | |
| 62 | // top level mappings of topology elements by ID |
| 63 | private final Map<NodeId, UiClusterMember> cnodeLookup = new HashMap<>(); |
| 64 | private final Map<RegionId, UiRegion> regionLookup = new HashMap<>(); |
| 65 | private final Map<DeviceId, UiDevice> deviceLookup = new HashMap<>(); |
| 66 | private final Map<HostId, UiHost> hostLookup = new HashMap<>(); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 67 | private final Map<UiLinkId, UiDeviceLink> devLinkLookup = new HashMap<>(); |
| 68 | private final Map<UiLinkId, UiEdgeLink> edgeLinkLookup = new HashMap<>(); |
| 69 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 70 | // a cache of the computed synthetic links, keyed by ID of original UiLink |
| 71 | private final Map<UiLinkId, UiSynthLink> synthMap = new HashMap<>(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 72 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 73 | // a container for devices, hosts, etc. belonging to no region |
| 74 | private final UiRegion nullRegion = new UiRegion(this, null); |
| 75 | |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 76 | final ServiceBundle services; |
| 77 | |
| 78 | /** |
| 79 | * Creates a new UI topology backed by the specified service bundle. |
| 80 | * |
| 81 | * @param services service bundle |
| 82 | */ |
| 83 | public UiTopology(ServiceBundle services) { |
| 84 | this.services = services; |
| 85 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 86 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public String toString() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 89 | return toStringHelper(this) |
| 90 | .add("#cnodes", clusterMemberCount()) |
| 91 | .add("#regions", regionCount()) |
| 92 | .add("#devices", deviceLookup.size()) |
| 93 | .add("#hosts", hostLookup.size()) |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 94 | .add("#dev-links", devLinkLookup.size()) |
| 95 | .add("#edge-links", edgeLinkLookup.size()) |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 96 | .add("#synth-links", synthMap.size()) |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 97 | .toString(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public String idAsString() { |
| 102 | return DEFAULT_TOPOLOGY_ID; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Clears the topology state; that is, drops all regions, devices, hosts, |
| 107 | * links, and cluster members. |
| 108 | */ |
| 109 | public void clear() { |
| 110 | log.debug("clearing topology model"); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 111 | cnodeLookup.clear(); |
| 112 | regionLookup.clear(); |
| 113 | deviceLookup.clear(); |
| 114 | hostLookup.clear(); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 115 | devLinkLookup.clear(); |
| 116 | edgeLinkLookup.clear(); |
| 117 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 118 | synthMap.clear(); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 119 | |
| 120 | nullRegion.destroy(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 121 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 122 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Returns all the cluster members, sorted by their ID. |
| 126 | * |
| 127 | * @return all cluster members |
| 128 | */ |
| 129 | public List<UiClusterMember> allClusterMembers() { |
| 130 | List<UiClusterMember> members = new ArrayList<>(cnodeLookup.values()); |
| 131 | Collections.sort(members, CLUSTER_MEMBER_COMPARATOR); |
| 132 | return members; |
| 133 | } |
| 134 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Returns the cluster member with the given identifier, or null if no |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 137 | * such member exists. |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 138 | * |
| 139 | * @param id cluster node identifier |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 140 | * @return corresponding UI cluster member |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 141 | */ |
| 142 | public UiClusterMember findClusterMember(NodeId id) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 143 | return cnodeLookup.get(id); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Adds the given cluster member to the topology model. |
| 148 | * |
| 149 | * @param member cluster member to add |
| 150 | */ |
| 151 | public void add(UiClusterMember member) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 152 | cnodeLookup.put(member.id(), member); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 156 | * Removes the given cluster member from the topology model. |
| 157 | * |
| 158 | * @param member cluster member to remove |
| 159 | */ |
| 160 | public void remove(UiClusterMember member) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 161 | UiClusterMember m = cnodeLookup.remove(member.id()); |
| 162 | if (m != null) { |
| 163 | m.destroy(); |
| 164 | } |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 168 | * Returns the number of members in the cluster. |
| 169 | * |
| 170 | * @return number of cluster members |
| 171 | */ |
| 172 | public int clusterMemberCount() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 173 | return cnodeLookup.size(); |
| 174 | } |
| 175 | |
Simon Hunt | 10973dd | 2016-08-01 15:50:35 -0700 | [diff] [blame] | 176 | |
| 177 | /** |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 178 | * Returns all regions in the model (except the |
| 179 | * {@link #nullRegion() null region}). |
Simon Hunt | 10973dd | 2016-08-01 15:50:35 -0700 | [diff] [blame] | 180 | * |
| 181 | * @return all regions |
| 182 | */ |
| 183 | public Set<UiRegion> allRegions() { |
| 184 | return new HashSet<>(regionLookup.values()); |
| 185 | } |
| 186 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 187 | /** |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 188 | * Returns a reference to the null-region. That is, the container for |
| 189 | * devices, hosts, and links that belong to no region. |
| 190 | * |
| 191 | * @return the null-region |
| 192 | */ |
| 193 | public UiRegion nullRegion() { |
| 194 | return nullRegion; |
| 195 | } |
| 196 | |
| 197 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 198 | * Returns the region with the specified identifier, or null if |
| 199 | * no such region exists. |
| 200 | * |
| 201 | * @param id region identifier |
| 202 | * @return corresponding UI region |
| 203 | */ |
| 204 | public UiRegion findRegion(RegionId id) { |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 205 | return UiRegion.NULL_ID.equals(id) ? nullRegion() : regionLookup.get(id); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 209 | * Adds the given region to the topology model. |
| 210 | * |
| 211 | * @param uiRegion region to add |
| 212 | */ |
| 213 | public void add(UiRegion uiRegion) { |
| 214 | regionLookup.put(uiRegion.id(), uiRegion); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 215 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * Removes the given region from the topology model. |
| 219 | * |
| 220 | * @param uiRegion region to remove |
| 221 | */ |
| 222 | public void remove(UiRegion uiRegion) { |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 223 | UiRegion r = regionLookup.remove(uiRegion.id()); |
| 224 | if (r != null) { |
| 225 | r.destroy(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Returns the number of regions configured in the topology. |
| 231 | * |
| 232 | * @return number of regions |
| 233 | */ |
| 234 | public int regionCount() { |
| 235 | return regionLookup.size(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 239 | * Returns all devices in the model. |
| 240 | * |
| 241 | * @return all devices |
| 242 | */ |
| 243 | public Set<UiDevice> allDevices() { |
| 244 | return new HashSet<>(deviceLookup.values()); |
| 245 | } |
| 246 | |
| 247 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 248 | * Returns the device with the specified identifier, or null if |
| 249 | * no such device exists. |
| 250 | * |
| 251 | * @param id device identifier |
| 252 | * @return corresponding UI device |
| 253 | */ |
| 254 | public UiDevice findDevice(DeviceId id) { |
| 255 | return deviceLookup.get(id); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Adds the given device to the topology model. |
| 260 | * |
| 261 | * @param uiDevice device to add |
| 262 | */ |
| 263 | public void add(UiDevice uiDevice) { |
| 264 | deviceLookup.put(uiDevice.id(), uiDevice); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Removes the given device from the topology model. |
| 269 | * |
| 270 | * @param uiDevice device to remove |
| 271 | */ |
| 272 | public void remove(UiDevice uiDevice) { |
| 273 | UiDevice d = deviceLookup.remove(uiDevice.id()); |
Thomas Vachuska | 8c0b18a | 2017-04-14 16:27:33 -0700 | [diff] [blame] | 274 | // TODO: Update the containing region |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 275 | if (d != null) { |
| 276 | d.destroy(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 281 | * Returns the number of devices configured in the topology. |
| 282 | * |
| 283 | * @return number of devices |
| 284 | */ |
| 285 | public int deviceCount() { |
| 286 | return deviceLookup.size(); |
| 287 | } |
| 288 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 289 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 290 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 291 | * Returns all device links in the model. |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 292 | * |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 293 | * @return all device links |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 294 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 295 | public Set<UiDeviceLink> allDeviceLinks() { |
| 296 | return new HashSet<>(devLinkLookup.values()); |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 300 | * Returns the device link with the specified identifier, or null if no |
| 301 | * such link exists. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 302 | * |
| 303 | * @param id the canonicalized link identifier |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 304 | * @return corresponding UI device link |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 305 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 306 | public UiDeviceLink findDeviceLink(UiLinkId id) { |
| 307 | return devLinkLookup.get(id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 311 | * Returns the edge link with the specified identifier, or null if no |
| 312 | * such link exists. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 313 | * |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 314 | * @param id the canonicalized link identifier |
| 315 | * @return corresponding UI edge link |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 316 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 317 | public UiEdgeLink findEdgeLink(UiLinkId id) { |
| 318 | return edgeLinkLookup.get(id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 322 | * Adds the given UI device link to the topology model. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 323 | * |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 324 | * @param uiDeviceLink link to add |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 325 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 326 | public void add(UiDeviceLink uiDeviceLink) { |
| 327 | devLinkLookup.put(uiDeviceLink.id(), uiDeviceLink); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Adds the given UI edge link to the topology model. |
| 332 | * |
| 333 | * @param uiEdgeLink link to add |
| 334 | */ |
| 335 | public void add(UiEdgeLink uiEdgeLink) { |
| 336 | edgeLinkLookup.put(uiEdgeLink.id(), uiEdgeLink); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Removes the given UI device link from the model. |
| 341 | * |
| 342 | * @param uiDeviceLink link to remove |
| 343 | */ |
| 344 | public void remove(UiDeviceLink uiDeviceLink) { |
| 345 | UiDeviceLink link = devLinkLookup.remove(uiDeviceLink.id()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 346 | if (link != null) { |
| 347 | link.destroy(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 352 | * Removes the given UI edge link from the model. |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 353 | * |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 354 | * @param uiEdgeLink link to remove |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 355 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 356 | public void remove(UiEdgeLink uiEdgeLink) { |
| 357 | UiEdgeLink link = edgeLinkLookup.remove(uiEdgeLink.id()); |
| 358 | if (link != null) { |
| 359 | link.destroy(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Returns the number of device links configured in the topology. |
| 365 | * |
| 366 | * @return number of device links |
| 367 | */ |
| 368 | public int deviceLinkCount() { |
| 369 | return devLinkLookup.size(); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Returns the number of edge links configured in the topology. |
| 374 | * |
| 375 | * @return number of edge links |
| 376 | */ |
| 377 | public int edgeLinkCount() { |
| 378 | return edgeLinkLookup.size(); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 382 | * Returns all hosts in the model. |
| 383 | * |
| 384 | * @return all hosts |
| 385 | */ |
| 386 | public Set<UiHost> allHosts() { |
| 387 | return new HashSet<>(hostLookup.values()); |
| 388 | } |
| 389 | |
| 390 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 391 | * Returns the host with the specified identifier, or null if no such |
| 392 | * host exists. |
| 393 | * |
| 394 | * @param id host identifier |
| 395 | * @return corresponding UI host |
| 396 | */ |
| 397 | public UiHost findHost(HostId id) { |
| 398 | return hostLookup.get(id); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Adds the given host to the topology model. |
| 403 | * |
| 404 | * @param uiHost host to add |
| 405 | */ |
| 406 | public void add(UiHost uiHost) { |
| 407 | hostLookup.put(uiHost.id(), uiHost); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Removes the given host from the topology model. |
| 412 | * |
| 413 | * @param uiHost host to remove |
| 414 | */ |
| 415 | public void remove(UiHost uiHost) { |
| 416 | UiHost h = hostLookup.remove(uiHost.id()); |
| 417 | if (h != null) { |
| 418 | h.destroy(); |
| 419 | } |
| 420 | } |
| 421 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 422 | /** |
| 423 | * Returns the number of hosts configured in the topology. |
| 424 | * |
| 425 | * @return number of hosts |
| 426 | */ |
| 427 | public int hostCount() { |
| 428 | return hostLookup.size(); |
| 429 | } |
| 430 | |
| 431 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 432 | // == |
| 433 | // package private methods for supporting linkage amongst topology entities |
| 434 | // == |
| 435 | |
| 436 | /** |
| 437 | * Returns the set of UI devices with the given identifiers. |
| 438 | * |
| 439 | * @param deviceIds device identifiers |
| 440 | * @return set of matching UI device instances |
| 441 | */ |
| 442 | Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) { |
| 443 | Set<UiDevice> uiDevices = new HashSet<>(); |
| 444 | for (DeviceId id : deviceIds) { |
| 445 | UiDevice d = deviceLookup.get(id); |
| 446 | if (d != null) { |
| 447 | uiDevices.add(d); |
| 448 | } else { |
| 449 | log.warn(E_UNMAPPED, "device", id); |
| 450 | } |
| 451 | } |
| 452 | return uiDevices; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Returns the set of UI hosts with the given identifiers. |
| 457 | * |
| 458 | * @param hostIds host identifiers |
| 459 | * @return set of matching UI host instances |
| 460 | */ |
| 461 | Set<UiHost> hostSet(Set<HostId> hostIds) { |
| 462 | Set<UiHost> uiHosts = new HashSet<>(); |
| 463 | for (HostId id : hostIds) { |
| 464 | UiHost h = hostLookup.get(id); |
| 465 | if (h != null) { |
| 466 | uiHosts.add(h); |
| 467 | } else { |
| 468 | log.warn(E_UNMAPPED, "host", id); |
| 469 | } |
| 470 | } |
| 471 | return uiHosts; |
| 472 | } |
| 473 | |
| 474 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 475 | * Returns the set of UI device links with the given identifiers. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 476 | * |
| 477 | * @param uiLinkIds link identifiers |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 478 | * @return set of matching UI device link instances |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 479 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 480 | Set<UiDeviceLink> linkSet(Set<UiLinkId> uiLinkIds) { |
| 481 | Set<UiDeviceLink> result = new HashSet<>(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 482 | for (UiLinkId id : uiLinkIds) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 483 | UiDeviceLink link = devLinkLookup.get(id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 484 | if (link != null) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 485 | result.add(link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 486 | } else { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 487 | log.warn(E_UNMAPPED, "device link", id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 490 | return result; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Uses the device-device links and data about the regions to compute the |
| 495 | * set of synthetic links that are required per region. |
| 496 | */ |
| 497 | public void computeSynthLinks() { |
| 498 | List<UiSynthLink> slinks = new ArrayList<>(); |
| 499 | allDeviceLinks().forEach((link) -> { |
| 500 | UiSynthLink synthetic = inferSyntheticLink(link); |
| 501 | slinks.add(synthetic); |
| 502 | log.debug("Synthetic link: {}", synthetic); |
| 503 | }); |
| 504 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 505 | slinks.addAll(wrapHostLinks(nullRegion())); |
| 506 | for (UiRegion r: allRegions()) { |
| 507 | slinks.addAll(wrapHostLinks(r)); |
| 508 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 509 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 510 | synthMap.clear(); |
| 511 | for (UiSynthLink sl : slinks) { |
| 512 | synthMap.put(sl.original().id(), sl); |
| 513 | } |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 514 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 515 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 516 | private Set<UiSynthLink> wrapHostLinks(UiRegion region) { |
| 517 | RegionId regionId = region.id(); |
| 518 | return region.hosts().stream().map(h -> wrapHostLink(regionId, h)) |
| 519 | .collect(Collectors.toSet()); |
| 520 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 521 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 522 | private UiSynthLink wrapHostLink(RegionId regionId, UiHost host) { |
| 523 | UiEdgeLink elink = new UiEdgeLink(this, host.edgeLinkId()); |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 524 | return new UiSynthLink(regionId, elink, elink); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | private UiSynthLink inferSyntheticLink(UiDeviceLink link) { |
| 528 | /* |
| 529 | Look at the containment hierarchy of each end of the link. Find the |
| 530 | common ancestor region R. A synthetic link will be added to R, based |
| 531 | on the "next" node back down the branch... |
| 532 | |
| 533 | S1 --- S2 * in the same region ... |
| 534 | : : |
| 535 | R R return S1 --- S2 (same link instance) |
| 536 | |
| 537 | |
| 538 | S1 --- S2 * in different regions (R1, R2) at same level |
| 539 | : : |
| 540 | R1 R2 return R1 --- R2 |
| 541 | : : |
| 542 | R R |
| 543 | |
| 544 | S1 --- S2 * in different regions at different levels |
| 545 | : : |
| 546 | R1 R2 return R1 --- R3 |
| 547 | : : |
| 548 | R R3 |
| 549 | : |
| 550 | R |
| 551 | |
| 552 | S1 --- S2 * in different regions at different levels |
| 553 | : : |
| 554 | R R2 return S1 --- R2 |
| 555 | : |
| 556 | R |
| 557 | |
| 558 | */ |
| 559 | DeviceId a = link.deviceA(); |
| 560 | DeviceId b = link.deviceB(); |
| 561 | List<RegionId> aBranch = ancestors(a); |
| 562 | List<RegionId> bBranch = ancestors(b); |
| 563 | if (aBranch == null || bBranch == null) { |
| 564 | return null; |
| 565 | } |
| 566 | |
| 567 | return makeSynthLink(link, aBranch, bBranch); |
| 568 | } |
| 569 | |
| 570 | // package private for unit testing |
| 571 | UiSynthLink makeSynthLink(UiDeviceLink orig, |
| 572 | List<RegionId> aBranch, |
| 573 | List<RegionId> bBranch) { |
| 574 | |
| 575 | final int aSize = aBranch.size(); |
| 576 | final int bSize = bBranch.size(); |
| 577 | final int min = Math.min(aSize, bSize); |
| 578 | |
| 579 | int index = 0; |
| 580 | RegionId commonRegion = aBranch.get(index); |
| 581 | |
| 582 | while (true) { |
| 583 | int next = index + 1; |
| 584 | if (next == min) { |
| 585 | // no more pairs of regions left to test |
| 586 | break; |
| 587 | } |
| 588 | RegionId rA = aBranch.get(next); |
| 589 | RegionId rB = bBranch.get(next); |
| 590 | if (rA.equals(rB)) { |
| 591 | commonRegion = rA; |
| 592 | index++; |
| 593 | } else { |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | |
| 599 | int endPointIndex = index + 1; |
| 600 | UiLinkId linkId; |
| 601 | UiLink link; |
| 602 | |
| 603 | if (endPointIndex < aSize) { |
| 604 | // the A endpoint is a subregion |
| 605 | RegionId aRegion = aBranch.get(endPointIndex); |
| 606 | |
| 607 | if (endPointIndex < bSize) { |
| 608 | // the B endpoint is a subregion |
| 609 | RegionId bRegion = bBranch.get(endPointIndex); |
| 610 | |
| 611 | linkId = uiLinkId(aRegion, bRegion); |
| 612 | link = new UiRegionLink(this, linkId); |
| 613 | |
| 614 | } else { |
| 615 | // the B endpoint is the device |
| 616 | DeviceId dB = orig.deviceB(); |
| 617 | PortNumber pB = orig.portB(); |
| 618 | |
| 619 | linkId = uiLinkId(aRegion, dB, pB); |
| 620 | link = new UiRegionDeviceLink(this, linkId); |
| 621 | } |
| 622 | |
| 623 | } else { |
| 624 | // the A endpoint is the device |
| 625 | DeviceId dA = orig.deviceA(); |
| 626 | PortNumber pA = orig.portA(); |
| 627 | |
| 628 | if (endPointIndex < bSize) { |
| 629 | // the B endpoint is a subregion |
| 630 | RegionId bRegion = bBranch.get(endPointIndex); |
| 631 | |
| 632 | linkId = uiLinkId(bRegion, dA, pA); |
| 633 | link = new UiRegionDeviceLink(this, linkId); |
| 634 | |
| 635 | } else { |
| 636 | // the B endpoint is the device |
| 637 | // (so, we can just use the original device-device link...) |
| 638 | |
| 639 | link = orig; |
| 640 | } |
| 641 | } |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 642 | return new UiSynthLink(commonRegion, link, orig); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | private List<RegionId> ancestors(DeviceId id) { |
| 646 | // return the ancestor chain from this device to root region |
| 647 | UiDevice dev = findDevice(id); |
| 648 | if (dev == null) { |
| 649 | log.warn("Unable to find cached device with ID %s", id); |
| 650 | return null; |
| 651 | } |
| 652 | |
| 653 | UiRegion r = dev.uiRegion(); |
| 654 | List<RegionId> result = new ArrayList<>(); |
| 655 | while (r != null && !r.isRoot()) { |
| 656 | result.add(0, r.id()); |
| 657 | r = r.parentRegion(); |
| 658 | } |
| 659 | // finally add root region, since this is the grand-daddy of them all |
| 660 | result.add(0, UiRegion.NULL_ID); |
| 661 | return result; |
| 662 | } |
| 663 | |
| 664 | |
| 665 | /** |
| 666 | * Returns the synthetic links associated with the specified region. |
| 667 | * |
| 668 | * @param regionId the region ID |
| 669 | * @return synthetic links for this region |
| 670 | */ |
| 671 | public List<UiSynthLink> findSynthLinks(RegionId regionId) { |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 672 | return synthMap.values().stream() |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 673 | .filter(s -> Objects.equals(regionId, s.regionId())) |
| 674 | .collect(Collectors.toList()); |
| 675 | } |
| 676 | |
| 677 | |
| 678 | /** |
| 679 | * Returns the number of synthetic links in the topology. |
| 680 | * |
| 681 | * @return the synthetic link count |
| 682 | */ |
| 683 | public int synthLinkCount() { |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 684 | return synthMap.size(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 687 | /** |
| 688 | * Returns a detailed (multi-line) string showing the contents of the |
| 689 | * topology. |
| 690 | * |
| 691 | * @return detailed string |
| 692 | */ |
| 693 | public String dumpString() { |
| 694 | StringBuilder sb = new StringBuilder("Topology:").append(EOL); |
| 695 | |
| 696 | sb.append(INDENT_1).append("Cluster Members").append(EOL); |
| 697 | for (UiClusterMember m : cnodeLookup.values()) { |
| 698 | sb.append(INDENT_2).append(m).append(EOL); |
| 699 | } |
| 700 | |
| 701 | sb.append(INDENT_1).append("Regions").append(EOL); |
| 702 | for (UiRegion r : regionLookup.values()) { |
| 703 | sb.append(INDENT_2).append(r).append(EOL); |
| 704 | } |
| 705 | |
| 706 | sb.append(INDENT_1).append("Devices").append(EOL); |
| 707 | for (UiDevice d : deviceLookup.values()) { |
| 708 | sb.append(INDENT_2).append(d).append(EOL); |
| 709 | } |
| 710 | |
| 711 | sb.append(INDENT_1).append("Hosts").append(EOL); |
| 712 | for (UiHost h : hostLookup.values()) { |
| 713 | sb.append(INDENT_2).append(h).append(EOL); |
| 714 | } |
| 715 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 716 | sb.append(INDENT_1).append("Device Links").append(EOL); |
| 717 | for (UiLink link : devLinkLookup.values()) { |
| 718 | sb.append(INDENT_2).append(link).append(EOL); |
| 719 | } |
| 720 | |
| 721 | sb.append(INDENT_1).append("Edge Links").append(EOL); |
| 722 | for (UiLink link : edgeLinkLookup.values()) { |
| 723 | sb.append(INDENT_2).append(link).append(EOL); |
| 724 | } |
| 725 | |
| 726 | sb.append(INDENT_1).append("Synth Links").append(EOL); |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 727 | for (UiSynthLink link : synthMap.values()) { |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 728 | sb.append(INDENT_2).append(link).append(EOL); |
| 729 | } |
| 730 | sb.append("------").append(EOL); |
| 731 | |
| 732 | return sb.toString(); |
| 733 | } |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 734 | } |