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; |
| 22 | import org.onosproject.net.region.RegionId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 26 | import java.util.ArrayList; |
| 27 | import java.util.Collections; |
| 28 | import java.util.Comparator; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 29 | import java.util.HashMap; |
| 30 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 31 | import java.util.List; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 32 | import java.util.Map; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 33 | import java.util.Set; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 34 | |
| 35 | import static com.google.common.base.MoreObjects.toStringHelper; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 36 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Represents the overall network topology. |
| 39 | */ |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 40 | public class UiTopology extends UiElement { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 41 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 42 | private static final String INDENT_1 = " "; |
| 43 | private static final String INDENT_2 = " "; |
| 44 | private static final String EOL = String.format("%n"); |
| 45 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 46 | private static final String E_UNMAPPED = |
| 47 | "Attempting to retrieve unmapped {}: {}"; |
| 48 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 49 | private static final String DEFAULT_TOPOLOGY_ID = "TOPOLOGY-0"; |
| 50 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 51 | private static final Logger log = LoggerFactory.getLogger(UiTopology.class); |
| 52 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 53 | private static final Comparator<UiClusterMember> CLUSTER_MEMBER_COMPARATOR = |
| 54 | (o1, o2) -> o1.idAsString().compareTo(o2.idAsString()); |
| 55 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 56 | |
| 57 | // top level mappings of topology elements by ID |
| 58 | private final Map<NodeId, UiClusterMember> cnodeLookup = new HashMap<>(); |
| 59 | private final Map<RegionId, UiRegion> regionLookup = new HashMap<>(); |
| 60 | private final Map<DeviceId, UiDevice> deviceLookup = new HashMap<>(); |
| 61 | private final Map<HostId, UiHost> hostLookup = new HashMap<>(); |
| 62 | private final Map<UiLinkId, UiLink> linkLookup = new HashMap<>(); |
| 63 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 64 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 65 | @Override |
| 66 | public String toString() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 67 | return toStringHelper(this) |
| 68 | .add("#cnodes", clusterMemberCount()) |
| 69 | .add("#regions", regionCount()) |
| 70 | .add("#devices", deviceLookup.size()) |
| 71 | .add("#hosts", hostLookup.size()) |
| 72 | .add("#links", linkLookup.size()) |
| 73 | .toString(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String idAsString() { |
| 78 | return DEFAULT_TOPOLOGY_ID; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Clears the topology state; that is, drops all regions, devices, hosts, |
| 83 | * links, and cluster members. |
| 84 | */ |
| 85 | public void clear() { |
| 86 | log.debug("clearing topology model"); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 87 | cnodeLookup.clear(); |
| 88 | regionLookup.clear(); |
| 89 | deviceLookup.clear(); |
| 90 | hostLookup.clear(); |
| 91 | linkLookup.clear(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 92 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 93 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 94 | |
| 95 | /** |
| 96 | * Returns all the cluster members, sorted by their ID. |
| 97 | * |
| 98 | * @return all cluster members |
| 99 | */ |
| 100 | public List<UiClusterMember> allClusterMembers() { |
| 101 | List<UiClusterMember> members = new ArrayList<>(cnodeLookup.values()); |
| 102 | Collections.sort(members, CLUSTER_MEMBER_COMPARATOR); |
| 103 | return members; |
| 104 | } |
| 105 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Returns the cluster member with the given identifier, or null if no |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 108 | * such member exists. |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 109 | * |
| 110 | * @param id cluster node identifier |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 111 | * @return corresponding UI cluster member |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 112 | */ |
| 113 | public UiClusterMember findClusterMember(NodeId id) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 114 | return cnodeLookup.get(id); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Adds the given cluster member to the topology model. |
| 119 | * |
| 120 | * @param member cluster member to add |
| 121 | */ |
| 122 | public void add(UiClusterMember member) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 123 | cnodeLookup.put(member.id(), member); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 127 | * Removes the given cluster member from the topology model. |
| 128 | * |
| 129 | * @param member cluster member to remove |
| 130 | */ |
| 131 | public void remove(UiClusterMember member) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 132 | UiClusterMember m = cnodeLookup.remove(member.id()); |
| 133 | if (m != null) { |
| 134 | m.destroy(); |
| 135 | } |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 139 | * Returns the number of members in the cluster. |
| 140 | * |
| 141 | * @return number of cluster members |
| 142 | */ |
| 143 | public int clusterMemberCount() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 144 | return cnodeLookup.size(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the region with the specified identifier, or null if |
| 149 | * no such region exists. |
| 150 | * |
| 151 | * @param id region identifier |
| 152 | * @return corresponding UI region |
| 153 | */ |
| 154 | public UiRegion findRegion(RegionId id) { |
| 155 | return regionLookup.get(id); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 159 | * Adds the given region to the topology model. |
| 160 | * |
| 161 | * @param uiRegion region to add |
| 162 | */ |
| 163 | public void add(UiRegion uiRegion) { |
| 164 | regionLookup.put(uiRegion.id(), uiRegion); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 165 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * Removes the given region from the topology model. |
| 169 | * |
| 170 | * @param uiRegion region to remove |
| 171 | */ |
| 172 | public void remove(UiRegion uiRegion) { |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 173 | UiRegion r = regionLookup.remove(uiRegion.id()); |
| 174 | if (r != null) { |
| 175 | r.destroy(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Returns the number of regions configured in the topology. |
| 181 | * |
| 182 | * @return number of regions |
| 183 | */ |
| 184 | public int regionCount() { |
| 185 | return regionLookup.size(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Returns the device with the specified identifier, or null if |
| 190 | * no such device exists. |
| 191 | * |
| 192 | * @param id device identifier |
| 193 | * @return corresponding UI device |
| 194 | */ |
| 195 | public UiDevice findDevice(DeviceId id) { |
| 196 | return deviceLookup.get(id); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Adds the given device to the topology model. |
| 201 | * |
| 202 | * @param uiDevice device to add |
| 203 | */ |
| 204 | public void add(UiDevice uiDevice) { |
| 205 | deviceLookup.put(uiDevice.id(), uiDevice); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Removes the given device from the topology model. |
| 210 | * |
| 211 | * @param uiDevice device to remove |
| 212 | */ |
| 213 | public void remove(UiDevice uiDevice) { |
| 214 | UiDevice d = deviceLookup.remove(uiDevice.id()); |
| 215 | if (d != null) { |
| 216 | d.destroy(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 221 | * Returns the number of devices configured in the topology. |
| 222 | * |
| 223 | * @return number of devices |
| 224 | */ |
| 225 | public int deviceCount() { |
| 226 | return deviceLookup.size(); |
| 227 | } |
| 228 | |
| 229 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 230 | * Returns the link with the specified identifier, or null if no such |
| 231 | * link exists. |
| 232 | * |
| 233 | * @param id the canonicalized link identifier |
| 234 | * @return corresponding UI link |
| 235 | */ |
| 236 | public UiLink findLink(UiLinkId id) { |
| 237 | return linkLookup.get(id); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Adds the given UI link to the topology model. |
| 242 | * |
| 243 | * @param uiLink link to add |
| 244 | */ |
| 245 | public void add(UiLink uiLink) { |
| 246 | linkLookup.put(uiLink.id(), uiLink); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Removes the given UI link from the model. |
| 251 | * |
| 252 | * @param uiLink link to remove |
| 253 | */ |
| 254 | public void remove(UiLink uiLink) { |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 255 | UiLink link = linkLookup.remove(uiLink.id()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 256 | if (link != null) { |
| 257 | link.destroy(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 262 | * Returns the number of links configured in the topology. |
| 263 | * |
| 264 | * @return number of links |
| 265 | */ |
| 266 | public int linkCount() { |
| 267 | return linkLookup.size(); |
| 268 | } |
| 269 | |
| 270 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 271 | * Returns the host with the specified identifier, or null if no such |
| 272 | * host exists. |
| 273 | * |
| 274 | * @param id host identifier |
| 275 | * @return corresponding UI host |
| 276 | */ |
| 277 | public UiHost findHost(HostId id) { |
| 278 | return hostLookup.get(id); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Adds the given host to the topology model. |
| 283 | * |
| 284 | * @param uiHost host to add |
| 285 | */ |
| 286 | public void add(UiHost uiHost) { |
| 287 | hostLookup.put(uiHost.id(), uiHost); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Removes the given host from the topology model. |
| 292 | * |
| 293 | * @param uiHost host to remove |
| 294 | */ |
| 295 | public void remove(UiHost uiHost) { |
| 296 | UiHost h = hostLookup.remove(uiHost.id()); |
| 297 | if (h != null) { |
| 298 | h.destroy(); |
| 299 | } |
| 300 | } |
| 301 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 302 | /** |
| 303 | * Returns the number of hosts configured in the topology. |
| 304 | * |
| 305 | * @return number of hosts |
| 306 | */ |
| 307 | public int hostCount() { |
| 308 | return hostLookup.size(); |
| 309 | } |
| 310 | |
| 311 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 312 | // == |
| 313 | // package private methods for supporting linkage amongst topology entities |
| 314 | // == |
| 315 | |
| 316 | /** |
| 317 | * Returns the set of UI devices with the given identifiers. |
| 318 | * |
| 319 | * @param deviceIds device identifiers |
| 320 | * @return set of matching UI device instances |
| 321 | */ |
| 322 | Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) { |
| 323 | Set<UiDevice> uiDevices = new HashSet<>(); |
| 324 | for (DeviceId id : deviceIds) { |
| 325 | UiDevice d = deviceLookup.get(id); |
| 326 | if (d != null) { |
| 327 | uiDevices.add(d); |
| 328 | } else { |
| 329 | log.warn(E_UNMAPPED, "device", id); |
| 330 | } |
| 331 | } |
| 332 | return uiDevices; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Returns the set of UI hosts with the given identifiers. |
| 337 | * |
| 338 | * @param hostIds host identifiers |
| 339 | * @return set of matching UI host instances |
| 340 | */ |
| 341 | Set<UiHost> hostSet(Set<HostId> hostIds) { |
| 342 | Set<UiHost> uiHosts = new HashSet<>(); |
| 343 | for (HostId id : hostIds) { |
| 344 | UiHost h = hostLookup.get(id); |
| 345 | if (h != null) { |
| 346 | uiHosts.add(h); |
| 347 | } else { |
| 348 | log.warn(E_UNMAPPED, "host", id); |
| 349 | } |
| 350 | } |
| 351 | return uiHosts; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Returns the set of UI links with the given identifiers. |
| 356 | * |
| 357 | * @param uiLinkIds link identifiers |
| 358 | * @return set of matching UI link instances |
| 359 | */ |
| 360 | Set<UiLink> linkSet(Set<UiLinkId> uiLinkIds) { |
| 361 | Set<UiLink> uiLinks = new HashSet<>(); |
| 362 | for (UiLinkId id : uiLinkIds) { |
| 363 | UiLink link = linkLookup.get(id); |
| 364 | if (link != null) { |
| 365 | uiLinks.add(link); |
| 366 | } else { |
| 367 | log.warn(E_UNMAPPED, "link", id); |
| 368 | } |
| 369 | } |
| 370 | return uiLinks; |
| 371 | } |
| 372 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 373 | /** |
| 374 | * Returns a detailed (multi-line) string showing the contents of the |
| 375 | * topology. |
| 376 | * |
| 377 | * @return detailed string |
| 378 | */ |
| 379 | public String dumpString() { |
| 380 | StringBuilder sb = new StringBuilder("Topology:").append(EOL); |
| 381 | |
| 382 | sb.append(INDENT_1).append("Cluster Members").append(EOL); |
| 383 | for (UiClusterMember m : cnodeLookup.values()) { |
| 384 | sb.append(INDENT_2).append(m).append(EOL); |
| 385 | } |
| 386 | |
| 387 | sb.append(INDENT_1).append("Regions").append(EOL); |
| 388 | for (UiRegion r : regionLookup.values()) { |
| 389 | sb.append(INDENT_2).append(r).append(EOL); |
| 390 | } |
| 391 | |
| 392 | sb.append(INDENT_1).append("Devices").append(EOL); |
| 393 | for (UiDevice d : deviceLookup.values()) { |
| 394 | sb.append(INDENT_2).append(d).append(EOL); |
| 395 | } |
| 396 | |
| 397 | sb.append(INDENT_1).append("Hosts").append(EOL); |
| 398 | for (UiHost h : hostLookup.values()) { |
| 399 | sb.append(INDENT_2).append(h).append(EOL); |
| 400 | } |
| 401 | |
| 402 | sb.append(INDENT_1).append("Links").append(EOL); |
| 403 | for (UiLink link : linkLookup.values()) { |
| 404 | sb.append(INDENT_2).append(link).append(EOL); |
| 405 | } |
| 406 | sb.append("------").append(EOL); |
| 407 | |
| 408 | return sb.toString(); |
| 409 | } |
| 410 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 411 | } |