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