blob: 40dec559f758a8a5cdd7ef006193e0121f2a973e [file] [log] [blame]
Simon Hunt5f6dbf82016-03-30 08:53:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Simon Hunt5f6dbf82016-03-30 08:53:33 -07003 *
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
17package org.onosproject.ui.model.topo;
18
Simon Hunt58a0dd02016-05-17 11:54:23 -070019import com.google.common.collect.ImmutableSet;
Simon Huntc0f20c12016-05-09 09:30:20 -070020import org.onosproject.net.DeviceId;
21import org.onosproject.net.HostId;
Simon Hunt23fb1352016-04-11 12:15:19 -070022import org.onosproject.net.region.Region;
Simon Hunt642bc452016-05-04 19:34:45 -070023import org.onosproject.net.region.RegionId;
Simon Hunt23fb1352016-04-11 12:15:19 -070024
Simon Huntd5b96732016-07-08 13:22:27 -070025import java.util.ArrayList;
26import java.util.Collections;
Simon Huntc0f20c12016-05-09 09:30:20 -070027import java.util.HashSet;
Simon Huntd5b96732016-07-08 13:22:27 -070028import java.util.List;
Simon Hunt23fb1352016-04-11 12:15:19 -070029import java.util.Set;
Simon Huntc0f20c12016-05-09 09:30:20 -070030
31import static com.google.common.base.MoreObjects.toStringHelper;
Simon Huntf836a872016-08-10 17:37:36 -070032import static com.google.common.base.Strings.isNullOrEmpty;
Simon Huntb1ce2602016-07-23 14:04:31 -070033import static org.onosproject.net.region.RegionId.regionId;
Simon Hunt23fb1352016-04-11 12:15:19 -070034
Simon Hunt5f6dbf82016-03-30 08:53:33 -070035/**
36 * Represents a region.
37 */
38public class UiRegion extends UiNode {
Simon Hunt23fb1352016-04-11 12:15:19 -070039
Simon Huntf836a872016-08-10 17:37:36 -070040 private static final String NULL_NAME = "(root)";
41 private static final String NO_NAME = "???";
Simon Huntb1ce2602016-07-23 14:04:31 -070042
43 /**
44 * The identifier for the null-region. That is, a container for devices,
45 * hosts, and links for those that belong to no region.
46 */
47 public static final RegionId NULL_ID = regionId(NULL_NAME);
48
49 private static final String[] DEFAULT_LAYER_TAGS = {
50 UiNode.LAYER_OPTICAL,
51 UiNode.LAYER_PACKET,
52 UiNode.LAYER_DEFAULT
53 };
54
Simon Huntc0f20c12016-05-09 09:30:20 -070055 // loose bindings to things in this region
56 private final Set<DeviceId> deviceIds = new HashSet<>();
57 private final Set<HostId> hostIds = new HashSet<>();
Simon Hunt23fb1352016-04-11 12:15:19 -070058
Simon Huntd5b96732016-07-08 13:22:27 -070059 private final List<String> layerOrder = new ArrayList<>();
60
Simon Huntc0f20c12016-05-09 09:30:20 -070061 private final UiTopology topology;
Simon Hunt23fb1352016-04-11 12:15:19 -070062
Simon Huntc0f20c12016-05-09 09:30:20 -070063 private final Region region;
64
Simon Hunt4f4ffc32016-08-03 18:30:47 -070065 // keep track of hierarchy (inferred from UiTopoLayoutService)
66 private RegionId parent;
67 private final Set<RegionId> kids = new HashSet<>();
68
Simon Huntc0f20c12016-05-09 09:30:20 -070069 /**
70 * Constructs a UI region, with a reference to the specified backing region.
71 *
72 * @param topology parent topology
73 * @param region backing region
74 */
75 public UiRegion(UiTopology topology, Region region) {
Simon Huntb1ce2602016-07-23 14:04:31 -070076 // Implementation Note: if region is null, this UiRegion is being used
77 // as a container for devices, hosts, links that belong to no region.
Simon Huntc0f20c12016-05-09 09:30:20 -070078 this.topology = topology;
79 this.region = region;
Simon Huntb1ce2602016-07-23 14:04:31 -070080
81 setLayerOrder(DEFAULT_LAYER_TAGS);
Simon Huntc0f20c12016-05-09 09:30:20 -070082 }
Simon Hunt23fb1352016-04-11 12:15:19 -070083
84 @Override
85 protected void destroy() {
Simon Huntc0f20c12016-05-09 09:30:20 -070086 deviceIds.clear();
87 hostIds.clear();
Simon Hunt23fb1352016-04-11 12:15:19 -070088 }
89
Simon Hunt642bc452016-05-04 19:34:45 -070090 /**
Simon Huntd5b96732016-07-08 13:22:27 -070091 * 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 Hunt642bc452016-05-04 19:34:45 -0700102 * Returns the identity of the region.
103 *
104 * @return region ID
105 */
106 public RegionId id() {
Simon Huntb1ce2602016-07-23 14:04:31 -0700107 return region == null ? NULL_ID : region.id();
Simon Hunt642bc452016-05-04 19:34:45 -0700108 }
109
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700110 /**
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 /**
Simon Huntc13082f2016-08-03 21:20:23 -0700138 * Returns the UI region that is the parent of this region.
139 *
140 * @return the parent region
141 */
142 public UiRegion parentRegion() {
143 return topology.findRegion(parent);
144 }
145
146 /**
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700147 * Sets the parent ID for this region.
148 *
149 * @param parentId parent ID
150 */
151 public void setParent(RegionId parentId) {
152 parent = parentId;
153 }
154
155 /**
156 * Sets the children IDs for this region.
157 *
158 * @param children children IDs
159 */
160 public void setChildren(Set<RegionId> children) {
161 kids.clear();
162 kids.addAll(children);
163 }
164
Simon Hunt642bc452016-05-04 19:34:45 -0700165 @Override
166 public String idAsString() {
167 return id().toString();
168 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700169
170 @Override
171 public String name() {
Simon Huntb1ce2602016-07-23 14:04:31 -0700172 return region == null ? NULL_NAME : region.name();
Simon Huntc0f20c12016-05-09 09:30:20 -0700173 }
174
175 /**
Simon Huntb1ce2602016-07-23 14:04:31 -0700176 * Returns the region instance backing this UI region. If this instance
177 * represents the "null-region", the value returned will be null.
Simon Huntc0f20c12016-05-09 09:30:20 -0700178 *
179 * @return the backing region instance
180 */
181 public Region backingRegion() {
182 return region;
183 }
184
185 /**
186 * Make sure we have only these devices in the region.
187 *
188 * @param devices devices in the region
189 */
190 public void reconcileDevices(Set<DeviceId> devices) {
191 deviceIds.clear();
192 deviceIds.addAll(devices);
193 }
194
195 @Override
196 public String toString() {
197 return toStringHelper(this)
198 .add("id", id())
199 .add("name", name())
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700200 .add("parent", parent)
201 .add("kids", kids)
Simon Huntc0f20c12016-05-09 09:30:20 -0700202 .add("devices", deviceIds)
203 .add("#hosts", hostIds.size())
Simon Huntc0f20c12016-05-09 09:30:20 -0700204 .toString();
205 }
206
207 /**
208 * Returns the region's type.
209 *
210 * @return region type
211 */
212 public Region.Type type() {
Simon Huntb1ce2602016-07-23 14:04:31 -0700213 return region == null ? null : region.type();
214 }
215
216
217 /**
218 * Returns the count of devices in this region.
219 *
220 * @return the device count
221 */
222 public int deviceCount() {
223 return deviceIds.size();
Simon Huntc0f20c12016-05-09 09:30:20 -0700224 }
225
226 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700227 * Returns the set of device identifiers for this region.
228 *
229 * @return device identifiers for this region
230 */
231 public Set<DeviceId> deviceIds() {
232 return ImmutableSet.copyOf(deviceIds);
233 }
234
235 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700236 * Returns the devices in this region.
237 *
238 * @return the devices in this region
239 */
240 public Set<UiDevice> devices() {
241 return topology.deviceSet(deviceIds);
242 }
243
244 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700245 * Returns the set of host identifiers for this region.
246 *
247 * @return host identifiers for this region
248 */
249 public Set<HostId> hostIds() {
250 return ImmutableSet.copyOf(hostIds);
251 }
252
253 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700254 * Returns the hosts in this region.
255 *
256 * @return the hosts in this region
257 */
258 public Set<UiHost> hosts() {
259 return topology.hostSet(hostIds);
260 }
261
262 /**
Simon Huntd5b96732016-07-08 13:22:27 -0700263 * Returns the order in which layers should be rendered. Lower layers
264 * come earlier in the list. For example, to indicate that nodes in the
265 * optical layer should be rendered "below" nodes in the packet layer,
266 * this method should return:
267 * <pre>
Simon Huntb1ce2602016-07-23 14:04:31 -0700268 * [UiNode.LAYER_OPTICAL, UiNode.LAYER_PACKET, UiNode.LAYER_DEFAULT]
Simon Huntd5b96732016-07-08 13:22:27 -0700269 * </pre>
270 *
271 * @return layer ordering
272 */
273 public List<String> layerOrder() {
274 return Collections.unmodifiableList(layerOrder);
275 }
Simon Huntf836a872016-08-10 17:37:36 -0700276
277 /**
278 * Guarantees to return a string for the name of the specified region.
279 * If region is null, we return the null region name, else we return
280 * the name as configured on the region.
281 *
282 * @param region the region whose name we require
283 * @return the region's name
284 */
285 public static String safeName(Region region) {
286 if (region == null) {
287 return NULL_NAME;
288 }
289 String name = region.name();
290 return isNullOrEmpty(name) ? NO_NAME : name;
291 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700292}