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