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 | 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 | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 32 | import static org.onosproject.net.region.RegionId.regionId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 33 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Represents a region. |
| 36 | */ |
| 37 | public class UiRegion extends UiNode { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 38 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 39 | private static final String NULL_NAME = "<null-region>"; |
| 40 | |
| 41 | /** |
| 42 | * The identifier for the null-region. That is, a container for devices, |
| 43 | * hosts, and links for those that belong to no region. |
| 44 | */ |
| 45 | public static final RegionId NULL_ID = regionId(NULL_NAME); |
| 46 | |
| 47 | private static final String[] DEFAULT_LAYER_TAGS = { |
| 48 | UiNode.LAYER_OPTICAL, |
| 49 | UiNode.LAYER_PACKET, |
| 50 | UiNode.LAYER_DEFAULT |
| 51 | }; |
| 52 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 53 | // loose bindings to things in this region |
| 54 | private final Set<DeviceId> deviceIds = new HashSet<>(); |
| 55 | private final Set<HostId> hostIds = new HashSet<>(); |
| 56 | private final Set<UiLinkId> uiLinkIds = new HashSet<>(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 57 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 58 | private final List<String> layerOrder = new ArrayList<>(); |
| 59 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 60 | private final UiTopology topology; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 61 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 62 | private final Region region; |
| 63 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 64 | // keep track of hierarchy (inferred from UiTopoLayoutService) |
| 65 | private RegionId parent; |
| 66 | private final Set<RegionId> kids = new HashSet<>(); |
| 67 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Constructs a UI region, with a reference to the specified backing region. |
| 70 | * |
| 71 | * @param topology parent topology |
| 72 | * @param region backing region |
| 73 | */ |
| 74 | public UiRegion(UiTopology topology, Region region) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 75 | // Implementation Note: if region is null, this UiRegion is being used |
| 76 | // as a container for devices, hosts, links that belong to no region. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 77 | this.topology = topology; |
| 78 | this.region = region; |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 79 | |
| 80 | setLayerOrder(DEFAULT_LAYER_TAGS); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 81 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 82 | |
| 83 | @Override |
| 84 | protected void destroy() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 85 | deviceIds.clear(); |
| 86 | hostIds.clear(); |
| 87 | uiLinkIds.clear(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 90 | /** |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 91 | * Sets the layer order for this region. |
| 92 | * Typically, the {@code UiNode.LAYER_*} constants will be used here. |
| 93 | * |
| 94 | * @param layers the layers |
| 95 | */ |
| 96 | public void setLayerOrder(String... layers) { |
| 97 | layerOrder.clear(); |
| 98 | Collections.addAll(layerOrder, layers); |
| 99 | } |
| 100 | |
| 101 | /** |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 102 | * Returns the identity of the region. |
| 103 | * |
| 104 | * @return region ID |
| 105 | */ |
| 106 | public RegionId id() { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 107 | return region == null ? NULL_ID : region.id(); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Returns the identity of the parent region. |
| 112 | * |
| 113 | * @return parent region ID |
| 114 | */ |
| 115 | public RegionId parent() { |
| 116 | return parent; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns true if this is the root (default) region. |
| 121 | * |
| 122 | * @return true if root region |
| 123 | */ |
| 124 | public boolean isRoot() { |
| 125 | return id().equals(parent); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns the identities of the child regions. |
| 130 | * |
| 131 | * @return child region IDs |
| 132 | */ |
| 133 | public Set<RegionId> children() { |
| 134 | return ImmutableSet.copyOf(kids); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Sets the parent ID for this region. |
| 139 | * |
| 140 | * @param parentId parent ID |
| 141 | */ |
| 142 | public void setParent(RegionId parentId) { |
| 143 | parent = parentId; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets the children IDs for this region. |
| 148 | * |
| 149 | * @param children children IDs |
| 150 | */ |
| 151 | public void setChildren(Set<RegionId> children) { |
| 152 | kids.clear(); |
| 153 | kids.addAll(children); |
| 154 | } |
| 155 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public String idAsString() { |
| 158 | return id().toString(); |
| 159 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 160 | |
| 161 | @Override |
| 162 | public String name() { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 163 | return region == null ? NULL_NAME : region.name(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 167 | * Returns the region instance backing this UI region. If this instance |
| 168 | * represents the "null-region", the value returned will be null. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 169 | * |
| 170 | * @return the backing region instance |
| 171 | */ |
| 172 | public Region backingRegion() { |
| 173 | return region; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Make sure we have only these devices in the region. |
| 178 | * |
| 179 | * @param devices devices in the region |
| 180 | */ |
| 181 | public void reconcileDevices(Set<DeviceId> devices) { |
| 182 | deviceIds.clear(); |
| 183 | deviceIds.addAll(devices); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public String toString() { |
| 188 | return toStringHelper(this) |
| 189 | .add("id", id()) |
| 190 | .add("name", name()) |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 191 | .add("parent", parent) |
| 192 | .add("kids", kids) |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 193 | .add("devices", deviceIds) |
| 194 | .add("#hosts", hostIds.size()) |
| 195 | .add("#links", uiLinkIds.size()) |
| 196 | .toString(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns the region's type. |
| 201 | * |
| 202 | * @return region type |
| 203 | */ |
| 204 | public Region.Type type() { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 205 | return region == null ? null : region.type(); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * Returns the count of devices in this region. |
| 211 | * |
| 212 | * @return the device count |
| 213 | */ |
| 214 | public int deviceCount() { |
| 215 | return deviceIds.size(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 219 | * Returns the set of device identifiers for this region. |
| 220 | * |
| 221 | * @return device identifiers for this region |
| 222 | */ |
| 223 | public Set<DeviceId> deviceIds() { |
| 224 | return ImmutableSet.copyOf(deviceIds); |
| 225 | } |
| 226 | |
| 227 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 228 | * Returns the devices in this region. |
| 229 | * |
| 230 | * @return the devices in this region |
| 231 | */ |
| 232 | public Set<UiDevice> devices() { |
| 233 | return topology.deviceSet(deviceIds); |
| 234 | } |
| 235 | |
| 236 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 237 | * Returns the set of host identifiers for this region. |
| 238 | * |
| 239 | * @return host identifiers for this region |
| 240 | */ |
| 241 | public Set<HostId> hostIds() { |
| 242 | return ImmutableSet.copyOf(hostIds); |
| 243 | } |
| 244 | |
| 245 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 246 | * Returns the hosts in this region. |
| 247 | * |
| 248 | * @return the hosts in this region |
| 249 | */ |
| 250 | public Set<UiHost> hosts() { |
| 251 | return topology.hostSet(hostIds); |
| 252 | } |
| 253 | |
| 254 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 255 | * Returns the set of link identifiers for this region. |
| 256 | * |
| 257 | * @return link identifiers for this region |
| 258 | */ |
| 259 | public Set<UiLinkId> linkIds() { |
| 260 | return ImmutableSet.copyOf(uiLinkIds); |
| 261 | } |
| 262 | |
| 263 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 264 | * Returns the links in this region. |
| 265 | * |
| 266 | * @return the links in this region |
| 267 | */ |
| 268 | public Set<UiLink> links() { |
| 269 | return topology.linkSet(uiLinkIds); |
| 270 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * Returns the order in which layers should be rendered. Lower layers |
| 274 | * come earlier in the list. For example, to indicate that nodes in the |
| 275 | * optical layer should be rendered "below" nodes in the packet layer, |
| 276 | * this method should return: |
| 277 | * <pre> |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 278 | * [UiNode.LAYER_OPTICAL, UiNode.LAYER_PACKET, UiNode.LAYER_DEFAULT] |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 279 | * </pre> |
| 280 | * |
| 281 | * @return layer ordering |
| 282 | */ |
| 283 | public List<String> layerOrder() { |
| 284 | return Collections.unmodifiableList(layerOrder); |
| 285 | } |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 286 | } |