blob: 87d5036f937e75de1314552733279cbc11c40264 [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 Huntb1ce2602016-07-23 14:04:31 -070032import static org.onosproject.net.region.RegionId.regionId;
Simon Hunt23fb1352016-04-11 12:15:19 -070033
Simon Hunt5f6dbf82016-03-30 08:53:33 -070034/**
35 * Represents a region.
36 */
37public class UiRegion extends UiNode {
Simon Hunt23fb1352016-04-11 12:15:19 -070038
Simon Huntb1ce2602016-07-23 14:04:31 -070039 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 Huntc0f20c12016-05-09 09:30:20 -070053 // loose bindings to things in this region
54 private final Set<DeviceId> deviceIds = new HashSet<>();
55 private final Set<HostId> hostIds = new HashSet<>();
Simon Hunt23fb1352016-04-11 12:15:19 -070056
Simon Huntd5b96732016-07-08 13:22:27 -070057 private final List<String> layerOrder = new ArrayList<>();
58
Simon Huntc0f20c12016-05-09 09:30:20 -070059 private final UiTopology topology;
Simon Hunt23fb1352016-04-11 12:15:19 -070060
Simon Huntc0f20c12016-05-09 09:30:20 -070061 private final Region region;
62
Simon Hunt4f4ffc32016-08-03 18:30:47 -070063 // keep track of hierarchy (inferred from UiTopoLayoutService)
64 private RegionId parent;
65 private final Set<RegionId> kids = new HashSet<>();
66
Simon Huntc0f20c12016-05-09 09:30:20 -070067 /**
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 Huntb1ce2602016-07-23 14:04:31 -070074 // 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 Huntc0f20c12016-05-09 09:30:20 -070076 this.topology = topology;
77 this.region = region;
Simon Huntb1ce2602016-07-23 14:04:31 -070078
79 setLayerOrder(DEFAULT_LAYER_TAGS);
Simon Huntc0f20c12016-05-09 09:30:20 -070080 }
Simon Hunt23fb1352016-04-11 12:15:19 -070081
82 @Override
83 protected void destroy() {
Simon Huntc0f20c12016-05-09 09:30:20 -070084 deviceIds.clear();
85 hostIds.clear();
Simon Hunt23fb1352016-04-11 12:15:19 -070086 }
87
Simon Hunt642bc452016-05-04 19:34:45 -070088 /**
Simon Huntd5b96732016-07-08 13:22:27 -070089 * 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 Hunt642bc452016-05-04 19:34:45 -0700100 * Returns the identity of the region.
101 *
102 * @return region ID
103 */
104 public RegionId id() {
Simon Huntb1ce2602016-07-23 14:04:31 -0700105 return region == null ? NULL_ID : region.id();
Simon Hunt642bc452016-05-04 19:34:45 -0700106 }
107
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700108 /**
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 Huntc13082f2016-08-03 21:20:23 -0700136 * 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 Hunt4f4ffc32016-08-03 18:30:47 -0700145 * 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 Hunt642bc452016-05-04 19:34:45 -0700163 @Override
164 public String idAsString() {
165 return id().toString();
166 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700167
168 @Override
169 public String name() {
Simon Huntb1ce2602016-07-23 14:04:31 -0700170 return region == null ? NULL_NAME : region.name();
Simon Huntc0f20c12016-05-09 09:30:20 -0700171 }
172
173 /**
Simon Huntb1ce2602016-07-23 14:04:31 -0700174 * Returns the region instance backing this UI region. If this instance
175 * represents the "null-region", the value returned will be null.
Simon Huntc0f20c12016-05-09 09:30:20 -0700176 *
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 Hunt4f4ffc32016-08-03 18:30:47 -0700198 .add("parent", parent)
199 .add("kids", kids)
Simon Huntc0f20c12016-05-09 09:30:20 -0700200 .add("devices", deviceIds)
201 .add("#hosts", hostIds.size())
Simon Huntc0f20c12016-05-09 09:30:20 -0700202 .toString();
203 }
204
205 /**
206 * Returns the region's type.
207 *
208 * @return region type
209 */
210 public Region.Type type() {
Simon Huntb1ce2602016-07-23 14:04:31 -0700211 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 Huntc0f20c12016-05-09 09:30:20 -0700222 }
223
224 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700225 * 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 Huntc0f20c12016-05-09 09:30:20 -0700234 * 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 Hunt58a0dd02016-05-17 11:54:23 -0700243 * 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 Huntc0f20c12016-05-09 09:30:20 -0700252 * 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 Huntd5b96732016-07-08 13:22:27 -0700261 * 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 Huntb1ce2602016-07-23 14:04:31 -0700266 * [UiNode.LAYER_OPTICAL, UiNode.LAYER_PACKET, UiNode.LAYER_DEFAULT]
Simon Huntd5b96732016-07-08 13:22:27 -0700267 * </pre>
268 *
269 * @return layer ordering
270 */
271 public List<String> layerOrder() {
272 return Collections.unmodifiableList(layerOrder);
273 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700274}