blob: 023300cfbc798fdebb320e42c40bdd1ab6f1d627 [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 Hunt23fb1352016-04-11 12:15:19 -070032
Simon Hunt5f6dbf82016-03-30 08:53:33 -070033/**
34 * Represents a region.
35 */
36public class UiRegion extends UiNode {
Simon Hunt23fb1352016-04-11 12:15:19 -070037
Simon Huntc0f20c12016-05-09 09:30:20 -070038 // loose bindings to things in this region
39 private final Set<DeviceId> deviceIds = new HashSet<>();
40 private final Set<HostId> hostIds = new HashSet<>();
41 private final Set<UiLinkId> uiLinkIds = new HashSet<>();
Simon Hunt23fb1352016-04-11 12:15:19 -070042
Simon Huntd5b96732016-07-08 13:22:27 -070043 private final List<String> layerOrder = new ArrayList<>();
44
Simon Huntc0f20c12016-05-09 09:30:20 -070045 private final UiTopology topology;
Simon Hunt23fb1352016-04-11 12:15:19 -070046
Simon Huntc0f20c12016-05-09 09:30:20 -070047 private final Region region;
48
49 /**
50 * Constructs a UI region, with a reference to the specified backing region.
51 *
52 * @param topology parent topology
53 * @param region backing region
54 */
55 public UiRegion(UiTopology topology, Region region) {
56 this.topology = topology;
57 this.region = region;
Simon Huntd5b96732016-07-08 13:22:27 -070058 // unless told otherwise, we'll use a single, default layer
59 layerOrder.add(UiNode.LAYER_DEFAULT);
Simon Huntc0f20c12016-05-09 09:30:20 -070060 }
Simon Hunt23fb1352016-04-11 12:15:19 -070061
62 @Override
63 protected void destroy() {
Simon Huntc0f20c12016-05-09 09:30:20 -070064 deviceIds.clear();
65 hostIds.clear();
66 uiLinkIds.clear();
Simon Hunt23fb1352016-04-11 12:15:19 -070067 }
68
Simon Hunt642bc452016-05-04 19:34:45 -070069 /**
Simon Huntd5b96732016-07-08 13:22:27 -070070 * Sets the layer order for this region.
71 * Typically, the {@code UiNode.LAYER_*} constants will be used here.
72 *
73 * @param layers the layers
74 */
75 public void setLayerOrder(String... layers) {
76 layerOrder.clear();
77 Collections.addAll(layerOrder, layers);
78 }
79
80 /**
Simon Hunt642bc452016-05-04 19:34:45 -070081 * Returns the identity of the region.
82 *
83 * @return region ID
84 */
85 public RegionId id() {
86 return region.id();
87 }
88
89 @Override
90 public String idAsString() {
91 return id().toString();
92 }
Simon Huntc0f20c12016-05-09 09:30:20 -070093
94 @Override
95 public String name() {
96 return region.name();
97 }
98
99 /**
100 * Returns the region instance backing this UI region.
101 *
102 * @return the backing region instance
103 */
104 public Region backingRegion() {
105 return region;
106 }
107
108 /**
109 * Make sure we have only these devices in the region.
110 *
111 * @param devices devices in the region
112 */
113 public void reconcileDevices(Set<DeviceId> devices) {
114 deviceIds.clear();
115 deviceIds.addAll(devices);
116 }
117
118 @Override
119 public String toString() {
120 return toStringHelper(this)
121 .add("id", id())
122 .add("name", name())
123 .add("devices", deviceIds)
124 .add("#hosts", hostIds.size())
125 .add("#links", uiLinkIds.size())
126 .toString();
127 }
128
129 /**
130 * Returns the region's type.
131 *
132 * @return region type
133 */
134 public Region.Type type() {
135 return region.type();
136 }
137
138 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700139 * Returns the set of device identifiers for this region.
140 *
141 * @return device identifiers for this region
142 */
143 public Set<DeviceId> deviceIds() {
144 return ImmutableSet.copyOf(deviceIds);
145 }
146
147 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700148 * Returns the devices in this region.
149 *
150 * @return the devices in this region
151 */
152 public Set<UiDevice> devices() {
153 return topology.deviceSet(deviceIds);
154 }
155
156 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700157 * Returns the set of host identifiers for this region.
158 *
159 * @return host identifiers for this region
160 */
161 public Set<HostId> hostIds() {
162 return ImmutableSet.copyOf(hostIds);
163 }
164
165 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700166 * Returns the hosts in this region.
167 *
168 * @return the hosts in this region
169 */
170 public Set<UiHost> hosts() {
171 return topology.hostSet(hostIds);
172 }
173
174 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700175 * Returns the set of link identifiers for this region.
176 *
177 * @return link identifiers for this region
178 */
179 public Set<UiLinkId> linkIds() {
180 return ImmutableSet.copyOf(uiLinkIds);
181 }
182
183 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700184 * Returns the links in this region.
185 *
186 * @return the links in this region
187 */
188 public Set<UiLink> links() {
189 return topology.linkSet(uiLinkIds);
190 }
Simon Huntd5b96732016-07-08 13:22:27 -0700191
192 /**
193 * Returns the order in which layers should be rendered. Lower layers
194 * come earlier in the list. For example, to indicate that nodes in the
195 * optical layer should be rendered "below" nodes in the packet layer,
196 * this method should return:
197 * <pre>
198 * [UiNode.LAYER_OPTICAL, UiNode.LAYER_PACKET]
199 * </pre>
200 *
201 * @return layer ordering
202 */
203 public List<String> layerOrder() {
204 return Collections.unmodifiableList(layerOrder);
205 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700206}