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 | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 38 | private static final String E_UNMAPPED = |
| 39 | "Attempting to retrieve unmapped {}: {}"; |
| 40 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 41 | private static final String DEFAULT_TOPOLOGY_ID = "TOPOLOGY-0"; |
| 42 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 43 | private static final Logger log = LoggerFactory.getLogger(UiTopology.class); |
| 44 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 45 | |
| 46 | // top level mappings of topology elements by ID |
| 47 | private final Map<NodeId, UiClusterMember> cnodeLookup = new HashMap<>(); |
| 48 | private final Map<RegionId, UiRegion> regionLookup = new HashMap<>(); |
| 49 | private final Map<DeviceId, UiDevice> deviceLookup = new HashMap<>(); |
| 50 | private final Map<HostId, UiHost> hostLookup = new HashMap<>(); |
| 51 | private final Map<UiLinkId, UiLink> linkLookup = new HashMap<>(); |
| 52 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 53 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 54 | @Override |
| 55 | public String toString() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 56 | return toStringHelper(this) |
| 57 | .add("#cnodes", clusterMemberCount()) |
| 58 | .add("#regions", regionCount()) |
| 59 | .add("#devices", deviceLookup.size()) |
| 60 | .add("#hosts", hostLookup.size()) |
| 61 | .add("#links", linkLookup.size()) |
| 62 | .toString(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public String idAsString() { |
| 67 | return DEFAULT_TOPOLOGY_ID; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Clears the topology state; that is, drops all regions, devices, hosts, |
| 72 | * links, and cluster members. |
| 73 | */ |
| 74 | public void clear() { |
| 75 | log.debug("clearing topology model"); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 76 | cnodeLookup.clear(); |
| 77 | regionLookup.clear(); |
| 78 | deviceLookup.clear(); |
| 79 | hostLookup.clear(); |
| 80 | linkLookup.clear(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 81 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Returns the cluster member with the given identifier, or null if no |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 85 | * such member exists. |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 86 | * |
| 87 | * @param id cluster node identifier |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 88 | * @return corresponding UI cluster member |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 89 | */ |
| 90 | public UiClusterMember findClusterMember(NodeId id) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 91 | return cnodeLookup.get(id); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Adds the given cluster member to the topology model. |
| 96 | * |
| 97 | * @param member cluster member to add |
| 98 | */ |
| 99 | public void add(UiClusterMember member) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 100 | cnodeLookup.put(member.id(), member); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 104 | * Removes the given cluster member from the topology model. |
| 105 | * |
| 106 | * @param member cluster member to remove |
| 107 | */ |
| 108 | public void remove(UiClusterMember member) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 109 | UiClusterMember m = cnodeLookup.remove(member.id()); |
| 110 | if (m != null) { |
| 111 | m.destroy(); |
| 112 | } |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 116 | * Returns the number of members in the cluster. |
| 117 | * |
| 118 | * @return number of cluster members |
| 119 | */ |
| 120 | public int clusterMemberCount() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 121 | return cnodeLookup.size(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the region with the specified identifier, or null if |
| 126 | * no such region exists. |
| 127 | * |
| 128 | * @param id region identifier |
| 129 | * @return corresponding UI region |
| 130 | */ |
| 131 | public UiRegion findRegion(RegionId id) { |
| 132 | return regionLookup.get(id); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the number of regions configured in the topology. |
| 137 | * |
| 138 | * @return number of regions |
| 139 | */ |
| 140 | public int regionCount() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 141 | return regionLookup.size(); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 142 | } |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 143 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Adds the given region to the topology model. |
| 146 | * |
| 147 | * @param uiRegion region to add |
| 148 | */ |
| 149 | public void add(UiRegion uiRegion) { |
| 150 | regionLookup.put(uiRegion.id(), uiRegion); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 151 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Removes the given region from the topology model. |
| 155 | * |
| 156 | * @param uiRegion region to remove |
| 157 | */ |
| 158 | public void remove(UiRegion uiRegion) { |
| 159 | regionLookup.remove(uiRegion.id()); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns the device with the specified identifier, or null if |
| 164 | * no such device exists. |
| 165 | * |
| 166 | * @param id device identifier |
| 167 | * @return corresponding UI device |
| 168 | */ |
| 169 | public UiDevice findDevice(DeviceId id) { |
| 170 | return deviceLookup.get(id); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Adds the given device to the topology model. |
| 175 | * |
| 176 | * @param uiDevice device to add |
| 177 | */ |
| 178 | public void add(UiDevice uiDevice) { |
| 179 | deviceLookup.put(uiDevice.id(), uiDevice); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Removes the given device from the topology model. |
| 184 | * |
| 185 | * @param uiDevice device to remove |
| 186 | */ |
| 187 | public void remove(UiDevice uiDevice) { |
| 188 | UiDevice d = deviceLookup.remove(uiDevice.id()); |
| 189 | if (d != null) { |
| 190 | d.destroy(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the link with the specified identifier, or null if no such |
| 196 | * link exists. |
| 197 | * |
| 198 | * @param id the canonicalized link identifier |
| 199 | * @return corresponding UI link |
| 200 | */ |
| 201 | public UiLink findLink(UiLinkId id) { |
| 202 | return linkLookup.get(id); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Adds the given UI link to the topology model. |
| 207 | * |
| 208 | * @param uiLink link to add |
| 209 | */ |
| 210 | public void add(UiLink uiLink) { |
| 211 | linkLookup.put(uiLink.id(), uiLink); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Removes the given UI link from the model. |
| 216 | * |
| 217 | * @param uiLink link to remove |
| 218 | */ |
| 219 | public void remove(UiLink uiLink) { |
| 220 | UiLink link = linkLookup.get(uiLink.id()); |
| 221 | if (link != null) { |
| 222 | link.destroy(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Returns the host with the specified identifier, or null if no such |
| 228 | * host exists. |
| 229 | * |
| 230 | * @param id host identifier |
| 231 | * @return corresponding UI host |
| 232 | */ |
| 233 | public UiHost findHost(HostId id) { |
| 234 | return hostLookup.get(id); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Adds the given host to the topology model. |
| 239 | * |
| 240 | * @param uiHost host to add |
| 241 | */ |
| 242 | public void add(UiHost uiHost) { |
| 243 | hostLookup.put(uiHost.id(), uiHost); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Removes the given host from the topology model. |
| 248 | * |
| 249 | * @param uiHost host to remove |
| 250 | */ |
| 251 | public void remove(UiHost uiHost) { |
| 252 | UiHost h = hostLookup.remove(uiHost.id()); |
| 253 | if (h != null) { |
| 254 | h.destroy(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // == |
| 259 | // package private methods for supporting linkage amongst topology entities |
| 260 | // == |
| 261 | |
| 262 | /** |
| 263 | * Returns the set of UI devices with the given identifiers. |
| 264 | * |
| 265 | * @param deviceIds device identifiers |
| 266 | * @return set of matching UI device instances |
| 267 | */ |
| 268 | Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) { |
| 269 | Set<UiDevice> uiDevices = new HashSet<>(); |
| 270 | for (DeviceId id : deviceIds) { |
| 271 | UiDevice d = deviceLookup.get(id); |
| 272 | if (d != null) { |
| 273 | uiDevices.add(d); |
| 274 | } else { |
| 275 | log.warn(E_UNMAPPED, "device", id); |
| 276 | } |
| 277 | } |
| 278 | return uiDevices; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Returns the set of UI hosts with the given identifiers. |
| 283 | * |
| 284 | * @param hostIds host identifiers |
| 285 | * @return set of matching UI host instances |
| 286 | */ |
| 287 | Set<UiHost> hostSet(Set<HostId> hostIds) { |
| 288 | Set<UiHost> uiHosts = new HashSet<>(); |
| 289 | for (HostId id : hostIds) { |
| 290 | UiHost h = hostLookup.get(id); |
| 291 | if (h != null) { |
| 292 | uiHosts.add(h); |
| 293 | } else { |
| 294 | log.warn(E_UNMAPPED, "host", id); |
| 295 | } |
| 296 | } |
| 297 | return uiHosts; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Returns the set of UI links with the given identifiers. |
| 302 | * |
| 303 | * @param uiLinkIds link identifiers |
| 304 | * @return set of matching UI link instances |
| 305 | */ |
| 306 | Set<UiLink> linkSet(Set<UiLinkId> uiLinkIds) { |
| 307 | Set<UiLink> uiLinks = new HashSet<>(); |
| 308 | for (UiLinkId id : uiLinkIds) { |
| 309 | UiLink link = linkLookup.get(id); |
| 310 | if (link != null) { |
| 311 | uiLinks.add(link); |
| 312 | } else { |
| 313 | log.warn(E_UNMAPPED, "link", id); |
| 314 | } |
| 315 | } |
| 316 | return uiLinks; |
| 317 | } |
| 318 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 319 | } |