blob: c47842418524450f370f1b20aee417e93db65663 [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<>();
56 private final Set<UiLinkId> uiLinkIds = new HashSet<>();
Simon Hunt23fb1352016-04-11 12:15:19 -070057
Simon Huntd5b96732016-07-08 13:22:27 -070058 private final List<String> layerOrder = new ArrayList<>();
59
Simon Huntc0f20c12016-05-09 09:30:20 -070060 private final UiTopology topology;
Simon Hunt23fb1352016-04-11 12:15:19 -070061
Simon Huntc0f20c12016-05-09 09:30:20 -070062 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 Huntb1ce2602016-07-23 14:04:31 -070071 // 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 Huntc0f20c12016-05-09 09:30:20 -070073 this.topology = topology;
74 this.region = region;
Simon Huntb1ce2602016-07-23 14:04:31 -070075
76 setLayerOrder(DEFAULT_LAYER_TAGS);
Simon Huntc0f20c12016-05-09 09:30:20 -070077 }
Simon Hunt23fb1352016-04-11 12:15:19 -070078
79 @Override
80 protected void destroy() {
Simon Huntc0f20c12016-05-09 09:30:20 -070081 deviceIds.clear();
82 hostIds.clear();
83 uiLinkIds.clear();
Simon Hunt23fb1352016-04-11 12:15:19 -070084 }
85
Simon Hunt642bc452016-05-04 19:34:45 -070086 /**
Simon Huntd5b96732016-07-08 13:22:27 -070087 * 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 Hunt642bc452016-05-04 19:34:45 -070098 * Returns the identity of the region.
99 *
100 * @return region ID
101 */
102 public RegionId id() {
Simon Huntb1ce2602016-07-23 14:04:31 -0700103 return region == null ? NULL_ID : region.id();
Simon Hunt642bc452016-05-04 19:34:45 -0700104 }
105
106 @Override
107 public String idAsString() {
108 return id().toString();
109 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700110
111 @Override
112 public String name() {
Simon Huntb1ce2602016-07-23 14:04:31 -0700113 return region == null ? NULL_NAME : region.name();
Simon Huntc0f20c12016-05-09 09:30:20 -0700114 }
115
116 /**
Simon Huntb1ce2602016-07-23 14:04:31 -0700117 * Returns the region instance backing this UI region. If this instance
118 * represents the "null-region", the value returned will be null.
Simon Huntc0f20c12016-05-09 09:30:20 -0700119 *
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 Huntb1ce2602016-07-23 14:04:31 -0700153 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 Huntc0f20c12016-05-09 09:30:20 -0700164 }
165
166 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700167 * 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 Huntc0f20c12016-05-09 09:30:20 -0700176 * 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 Hunt58a0dd02016-05-17 11:54:23 -0700185 * 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 Huntc0f20c12016-05-09 09:30:20 -0700194 * 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 Hunt58a0dd02016-05-17 11:54:23 -0700203 * 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 Huntc0f20c12016-05-09 09:30:20 -0700212 * 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 Huntd5b96732016-07-08 13:22:27 -0700219
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 Huntb1ce2602016-07-23 14:04:31 -0700226 * [UiNode.LAYER_OPTICAL, UiNode.LAYER_PACKET, UiNode.LAYER_DEFAULT]
Simon Huntd5b96732016-07-08 13:22:27 -0700227 * </pre>
228 *
229 * @return layer ordering
230 */
231 public List<String> layerOrder() {
232 return Collections.unmodifiableList(layerOrder);
233 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700234}