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 | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
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 | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 22 | import org.onosproject.net.region.Region; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 23 | import org.onosproject.net.region.RegionId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 24 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.Collections; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 27 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 28 | import java.util.List; |
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 | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 32 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 8c0b18a | 2017-04-14 16:27:33 -0700 | [diff] [blame] | 33 | import static org.onosproject.net.DeviceId.deviceId; |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 34 | import static org.onosproject.net.region.RegionId.regionId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 35 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Represents a region. |
| 38 | */ |
| 39 | public class UiRegion extends UiNode { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 40 | |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 41 | private static final String NULL_NAME = "(root)"; |
| 42 | private static final String NO_NAME = "???"; |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * The identifier for the null-region. That is, a container for devices, |
| 46 | * hosts, and links for those that belong to no region. |
| 47 | */ |
| 48 | public static final RegionId NULL_ID = regionId(NULL_NAME); |
| 49 | |
| 50 | private static final String[] DEFAULT_LAYER_TAGS = { |
| 51 | UiNode.LAYER_OPTICAL, |
| 52 | UiNode.LAYER_PACKET, |
| 53 | UiNode.LAYER_DEFAULT |
| 54 | }; |
| 55 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 56 | // loose bindings to things in this region |
| 57 | private final Set<DeviceId> deviceIds = new HashSet<>(); |
| 58 | private final Set<HostId> hostIds = new HashSet<>(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 59 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 60 | private final List<String> layerOrder = new ArrayList<>(); |
| 61 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 62 | private final UiTopology topology; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 63 | |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 64 | private final RegionId regionId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 65 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 66 | // keep track of hierarchy (inferred from UiTopoLayoutService) |
| 67 | private RegionId parent; |
| 68 | private final Set<RegionId> kids = new HashSet<>(); |
| 69 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Constructs a UI region, with a reference to the specified backing region. |
| 72 | * |
| 73 | * @param topology parent topology |
| 74 | * @param region backing region |
| 75 | */ |
| 76 | public UiRegion(UiTopology topology, Region region) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 77 | // Implementation Note: if region is null, this UiRegion is being used |
| 78 | // as a container for devices, hosts, links that belong to no region. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 79 | this.topology = topology; |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 80 | this.regionId = region == null ? NULL_ID : region.id(); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 81 | |
| 82 | setLayerOrder(DEFAULT_LAYER_TAGS); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 83 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | protected void destroy() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 87 | deviceIds.clear(); |
| 88 | hostIds.clear(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 91 | /** |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 92 | * Sets the layer order for this region. |
| 93 | * Typically, the {@code UiNode.LAYER_*} constants will be used here. |
| 94 | * |
| 95 | * @param layers the layers |
| 96 | */ |
| 97 | public void setLayerOrder(String... layers) { |
| 98 | layerOrder.clear(); |
| 99 | Collections.addAll(layerOrder, layers); |
| 100 | } |
| 101 | |
| 102 | /** |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 103 | * Returns the identity of the region. |
| 104 | * |
| 105 | * @return region ID |
| 106 | */ |
| 107 | public RegionId id() { |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 108 | return regionId; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Returns the identity of the parent region. |
| 113 | * |
| 114 | * @return parent region ID |
| 115 | */ |
| 116 | public RegionId parent() { |
| 117 | return parent; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns true if this is the root (default) region. |
| 122 | * |
| 123 | * @return true if root region |
| 124 | */ |
| 125 | public boolean isRoot() { |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 126 | return parent == null; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the identities of the child regions. |
| 131 | * |
| 132 | * @return child region IDs |
| 133 | */ |
| 134 | public Set<RegionId> children() { |
| 135 | return ImmutableSet.copyOf(kids); |
| 136 | } |
| 137 | |
| 138 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 139 | * Returns the UI region that is the parent of this region. |
| 140 | * |
| 141 | * @return the parent region |
| 142 | */ |
| 143 | public UiRegion parentRegion() { |
| 144 | return topology.findRegion(parent); |
| 145 | } |
| 146 | |
| 147 | /** |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 148 | * Sets the parent ID for this region. |
| 149 | * |
| 150 | * @param parentId parent ID |
| 151 | */ |
| 152 | public void setParent(RegionId parentId) { |
| 153 | parent = parentId; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets the children IDs for this region. |
| 158 | * |
| 159 | * @param children children IDs |
| 160 | */ |
| 161 | public void setChildren(Set<RegionId> children) { |
| 162 | kids.clear(); |
| 163 | kids.addAll(children); |
| 164 | } |
| 165 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 166 | @Override |
| 167 | public String idAsString() { |
| 168 | return id().toString(); |
| 169 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 170 | |
| 171 | @Override |
| 172 | public String name() { |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 173 | Region region = backingRegion(); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 174 | return region == null ? NULL_NAME : region.name(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 178 | * Returns the region instance backing this UI region. If this instance |
| 179 | * represents the "null-region", the value returned will be null. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 180 | * |
| 181 | * @return the backing region instance |
| 182 | */ |
| 183 | public Region backingRegion() { |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 184 | return isRoot() ? null : topology.services.region().getRegion(regionId); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Make sure we have only these devices in the region. |
| 189 | * |
| 190 | * @param devices devices in the region |
| 191 | */ |
| 192 | public void reconcileDevices(Set<DeviceId> devices) { |
| 193 | deviceIds.clear(); |
| 194 | deviceIds.addAll(devices); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public String toString() { |
| 199 | return toStringHelper(this) |
| 200 | .add("id", id()) |
| 201 | .add("name", name()) |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 202 | .add("parent", parent) |
| 203 | .add("kids", kids) |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 204 | .add("devices", deviceIds) |
| 205 | .add("#hosts", hostIds.size()) |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 206 | .toString(); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns the region's type. |
| 211 | * |
| 212 | * @return region type |
| 213 | */ |
| 214 | public Region.Type type() { |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 215 | Region region = backingRegion(); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 216 | return region == null ? null : region.type(); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /** |
| 221 | * Returns the count of devices in this region. |
| 222 | * |
| 223 | * @return the device count |
| 224 | */ |
| 225 | public int deviceCount() { |
| 226 | return deviceIds.size(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 230 | * Returns the set of device identifiers for this region. |
| 231 | * |
| 232 | * @return device identifiers for this region |
| 233 | */ |
| 234 | public Set<DeviceId> deviceIds() { |
| 235 | return ImmutableSet.copyOf(deviceIds); |
| 236 | } |
| 237 | |
| 238 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 239 | * Returns the devices in this region. |
| 240 | * |
| 241 | * @return the devices in this region |
| 242 | */ |
| 243 | public Set<UiDevice> devices() { |
| 244 | return topology.deviceSet(deviceIds); |
| 245 | } |
| 246 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 247 | |
| 248 | /** |
| 249 | * Make sure we have only these hosts in the region. |
| 250 | * |
| 251 | * @param hosts hosts in the region |
| 252 | */ |
| 253 | public void reconcileHosts(Set<HostId> hosts) { |
| 254 | hostIds.clear(); |
| 255 | hostIds.addAll(hosts); |
| 256 | } |
| 257 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 258 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 259 | * Returns the set of host identifiers for this region. |
| 260 | * |
| 261 | * @return host identifiers for this region |
| 262 | */ |
| 263 | public Set<HostId> hostIds() { |
| 264 | return ImmutableSet.copyOf(hostIds); |
| 265 | } |
| 266 | |
| 267 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 268 | * Returns the hosts in this region. |
| 269 | * |
| 270 | * @return the hosts in this region |
| 271 | */ |
| 272 | public Set<UiHost> hosts() { |
| 273 | return topology.hostSet(hostIds); |
| 274 | } |
| 275 | |
| 276 | /** |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 277 | * Returns the count of devices in this region. |
| 278 | * |
| 279 | * @return the device count |
| 280 | */ |
| 281 | public int hostCount() { |
| 282 | return hostIds.size(); |
| 283 | } |
| 284 | |
| 285 | /** |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 286 | * Returns the order in which layers should be rendered. Lower layers |
| 287 | * come earlier in the list. For example, to indicate that nodes in the |
| 288 | * optical layer should be rendered "below" nodes in the packet layer, |
| 289 | * this method should return: |
| 290 | * <pre> |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 291 | * [UiNode.LAYER_OPTICAL, UiNode.LAYER_PACKET, UiNode.LAYER_DEFAULT] |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 292 | * </pre> |
| 293 | * |
| 294 | * @return layer ordering |
| 295 | */ |
| 296 | public List<String> layerOrder() { |
| 297 | return Collections.unmodifiableList(layerOrder); |
| 298 | } |
Simon Hunt | f836a87 | 2016-08-10 17:37:36 -0700 | [diff] [blame] | 299 | |
| 300 | /** |
| 301 | * Guarantees to return a string for the name of the specified region. |
| 302 | * If region is null, we return the null region name, else we return |
| 303 | * the name as configured on the region. |
| 304 | * |
| 305 | * @param region the region whose name we require |
| 306 | * @return the region's name |
| 307 | */ |
| 308 | public static String safeName(Region region) { |
| 309 | if (region == null) { |
| 310 | return NULL_NAME; |
| 311 | } |
| 312 | String name = region.name(); |
| 313 | return isNullOrEmpty(name) ? NO_NAME : name; |
| 314 | } |
Thomas Vachuska | 8c0b18a | 2017-04-14 16:27:33 -0700 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * Determins whether the specified event is relevant to the view |
| 318 | * constrained to this region. |
| 319 | * |
| 320 | * @param event UI model event |
| 321 | * @return true if relevant |
| 322 | */ |
| 323 | public boolean isRelevant(UiModelEvent event) { |
| 324 | switch (event.type()) { |
| 325 | case CLUSTER_MEMBER_ADDED_OR_UPDATED: |
| 326 | case CLUSTER_MEMBER_REMOVED: |
| 327 | return true; |
| 328 | |
| 329 | case REGION_ADDED_OR_UPDATED: |
| 330 | case REGION_REMOVED: |
| 331 | return isRegionRelevant(((UiRegion) event.subject()).id()); |
| 332 | |
| 333 | case DEVICE_ADDED_OR_UPDATED: |
| 334 | case DEVICE_REMOVED: |
| 335 | return isDeviceRelevant(((UiDevice) event.subject()).id()); |
| 336 | |
| 337 | case LINK_ADDED_OR_UPDATED: |
| 338 | case LINK_REMOVED: |
| 339 | return isLinkRelevant((UiLink) event.subject()); |
| 340 | |
| 341 | case HOST_ADDED_OR_UPDATED: |
| 342 | case HOST_MOVED: |
| 343 | case HOST_REMOVED: |
| 344 | return isDeviceRelevant(((UiHost) event.subject()).locationDevice()); |
| 345 | |
| 346 | default: |
| 347 | return true; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | private boolean isDeviceRelevant(DeviceId deviceId) { |
| 352 | return deviceIds.contains(deviceId); |
| 353 | } |
| 354 | |
| 355 | private boolean isLinkRelevant(UiLink uiLink) { |
| 356 | if (uiLink instanceof UiDeviceLink) { |
| 357 | UiDeviceLink uiDeviceLink = (UiDeviceLink) uiLink; |
| 358 | return isDeviceRelevant(uiDeviceLink.deviceA()) || |
| 359 | isDeviceRelevant(uiDeviceLink.deviceB()); |
| 360 | |
| 361 | } else if (uiLink instanceof UiRegionLink) { |
| 362 | UiRegionLink uiRegionLink = (UiRegionLink) uiLink; |
| 363 | return isRegionRelevant(uiRegionLink.regionA()) || |
| 364 | isRegionRelevant(uiRegionLink.regionB()); |
| 365 | |
| 366 | } else if (uiLink instanceof UiRegionDeviceLink) { |
| 367 | UiRegionDeviceLink uiRegionDeviceLink = (UiRegionDeviceLink) uiLink; |
| 368 | return isRegionRelevant(uiRegionDeviceLink.region()) || |
| 369 | isDeviceRelevant(uiRegionDeviceLink.device()); |
| 370 | |
| 371 | } else if (uiLink instanceof UiEdgeLink) { |
| 372 | UiEdgeLink uiEdgeLink = (UiEdgeLink) uiLink; |
| 373 | return isDeviceRelevant(uiEdgeLink.deviceId()); |
| 374 | } |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | private boolean isRegionRelevant(RegionId regionId) { |
| 379 | return kids.contains(regionId); |
| 380 | } |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 381 | } |