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