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