blob: 5ce91daa015eb887f23781de35e3e89bf8592d31 [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 Hunt338a3b42016-04-14 09:43:52 -070019import org.onosproject.cluster.NodeId;
Simon Huntc0f20c12016-05-09 09:30:20 -070020import org.onosproject.net.DeviceId;
21import org.onosproject.net.HostId;
22import org.onosproject.net.region.RegionId;
Simon Hunt23fb1352016-04-11 12:15:19 -070023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Simon Huntd5b96732016-07-08 13:22:27 -070026import java.util.ArrayList;
27import java.util.Collections;
28import java.util.Comparator;
Simon Huntc0f20c12016-05-09 09:30:20 -070029import java.util.HashMap;
30import java.util.HashSet;
Simon Huntd5b96732016-07-08 13:22:27 -070031import java.util.List;
Simon Huntc0f20c12016-05-09 09:30:20 -070032import java.util.Map;
Simon Hunt23fb1352016-04-11 12:15:19 -070033import java.util.Set;
Simon Huntc0f20c12016-05-09 09:30:20 -070034
35import static com.google.common.base.MoreObjects.toStringHelper;
Simon Hunt23fb1352016-04-11 12:15:19 -070036
Simon Hunt5f6dbf82016-03-30 08:53:33 -070037/**
38 * Represents the overall network topology.
39 */
Simon Huntcda9c032016-04-11 10:32:54 -070040public class UiTopology extends UiElement {
Simon Hunt23fb1352016-04-11 12:15:19 -070041
Simon Hunt58a0dd02016-05-17 11:54:23 -070042 private static final String INDENT_1 = " ";
43 private static final String INDENT_2 = " ";
44 private static final String EOL = String.format("%n");
45
Simon Huntc0f20c12016-05-09 09:30:20 -070046 private static final String E_UNMAPPED =
47 "Attempting to retrieve unmapped {}: {}";
48
Simon Hunt642bc452016-05-04 19:34:45 -070049 private static final String DEFAULT_TOPOLOGY_ID = "TOPOLOGY-0";
50
Simon Hunt23fb1352016-04-11 12:15:19 -070051 private static final Logger log = LoggerFactory.getLogger(UiTopology.class);
52
Simon Huntd5b96732016-07-08 13:22:27 -070053 private static final Comparator<UiClusterMember> CLUSTER_MEMBER_COMPARATOR =
54 (o1, o2) -> o1.idAsString().compareTo(o2.idAsString());
55
Simon Huntc0f20c12016-05-09 09:30:20 -070056
57 // top level mappings of topology elements by ID
58 private final Map<NodeId, UiClusterMember> cnodeLookup = new HashMap<>();
59 private final Map<RegionId, UiRegion> regionLookup = new HashMap<>();
60 private final Map<DeviceId, UiDevice> deviceLookup = new HashMap<>();
61 private final Map<HostId, UiHost> hostLookup = new HashMap<>();
62 private final Map<UiLinkId, UiLink> linkLookup = new HashMap<>();
63
Simon Hunt23fb1352016-04-11 12:15:19 -070064
Simon Hunt338a3b42016-04-14 09:43:52 -070065 @Override
66 public String toString() {
Simon Huntc0f20c12016-05-09 09:30:20 -070067 return toStringHelper(this)
68 .add("#cnodes", clusterMemberCount())
69 .add("#regions", regionCount())
70 .add("#devices", deviceLookup.size())
71 .add("#hosts", hostLookup.size())
72 .add("#links", linkLookup.size())
73 .toString();
74 }
75
76 @Override
77 public String idAsString() {
78 return DEFAULT_TOPOLOGY_ID;
Simon Hunt338a3b42016-04-14 09:43:52 -070079 }
80
Simon Hunt23fb1352016-04-11 12:15:19 -070081 /**
82 * Clears the topology state; that is, drops all regions, devices, hosts,
83 * links, and cluster members.
84 */
85 public void clear() {
86 log.debug("clearing topology model");
Simon Huntc0f20c12016-05-09 09:30:20 -070087 cnodeLookup.clear();
88 regionLookup.clear();
89 deviceLookup.clear();
90 hostLookup.clear();
91 linkLookup.clear();
Simon Hunt23fb1352016-04-11 12:15:19 -070092 }
Simon Hunt338a3b42016-04-14 09:43:52 -070093
Simon Huntd5b96732016-07-08 13:22:27 -070094
95 /**
96 * Returns all the cluster members, sorted by their ID.
97 *
98 * @return all cluster members
99 */
100 public List<UiClusterMember> allClusterMembers() {
101 List<UiClusterMember> members = new ArrayList<>(cnodeLookup.values());
102 Collections.sort(members, CLUSTER_MEMBER_COMPARATOR);
103 return members;
104 }
105
Simon Hunt338a3b42016-04-14 09:43:52 -0700106 /**
107 * Returns the cluster member with the given identifier, or null if no
Simon Huntc0f20c12016-05-09 09:30:20 -0700108 * such member exists.
Simon Hunt338a3b42016-04-14 09:43:52 -0700109 *
110 * @param id cluster node identifier
Simon Huntc0f20c12016-05-09 09:30:20 -0700111 * @return corresponding UI cluster member
Simon Hunt338a3b42016-04-14 09:43:52 -0700112 */
113 public UiClusterMember findClusterMember(NodeId id) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700114 return cnodeLookup.get(id);
Simon Hunt338a3b42016-04-14 09:43:52 -0700115 }
116
117 /**
118 * Adds the given cluster member to the topology model.
119 *
120 * @param member cluster member to add
121 */
122 public void add(UiClusterMember member) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700123 cnodeLookup.put(member.id(), member);
Simon Hunt338a3b42016-04-14 09:43:52 -0700124 }
125
126 /**
Simon Hunt642bc452016-05-04 19:34:45 -0700127 * Removes the given cluster member from the topology model.
128 *
129 * @param member cluster member to remove
130 */
131 public void remove(UiClusterMember member) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700132 UiClusterMember m = cnodeLookup.remove(member.id());
133 if (m != null) {
134 m.destroy();
135 }
Simon Hunt642bc452016-05-04 19:34:45 -0700136 }
137
138 /**
Simon Hunt338a3b42016-04-14 09:43:52 -0700139 * Returns the number of members in the cluster.
140 *
141 * @return number of cluster members
142 */
143 public int clusterMemberCount() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700144 return cnodeLookup.size();
145 }
146
147 /**
148 * Returns the region with the specified identifier, or null if
149 * no such region exists.
150 *
151 * @param id region identifier
152 * @return corresponding UI region
153 */
154 public UiRegion findRegion(RegionId id) {
155 return regionLookup.get(id);
Simon Hunt338a3b42016-04-14 09:43:52 -0700156 }
157
158 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700159 * Adds the given region to the topology model.
160 *
161 * @param uiRegion region to add
162 */
163 public void add(UiRegion uiRegion) {
164 regionLookup.put(uiRegion.id(), uiRegion);
Simon Hunt642bc452016-05-04 19:34:45 -0700165 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700166
167 /**
168 * Removes the given region from the topology model.
169 *
170 * @param uiRegion region to remove
171 */
172 public void remove(UiRegion uiRegion) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700173 UiRegion r = regionLookup.remove(uiRegion.id());
174 if (r != null) {
175 r.destroy();
176 }
177 }
178
179 /**
180 * Returns the number of regions configured in the topology.
181 *
182 * @return number of regions
183 */
184 public int regionCount() {
185 return regionLookup.size();
Simon Huntc0f20c12016-05-09 09:30:20 -0700186 }
187
188 /**
189 * Returns the device with the specified identifier, or null if
190 * no such device exists.
191 *
192 * @param id device identifier
193 * @return corresponding UI device
194 */
195 public UiDevice findDevice(DeviceId id) {
196 return deviceLookup.get(id);
197 }
198
199 /**
200 * Adds the given device to the topology model.
201 *
202 * @param uiDevice device to add
203 */
204 public void add(UiDevice uiDevice) {
205 deviceLookup.put(uiDevice.id(), uiDevice);
206 }
207
208 /**
209 * Removes the given device from the topology model.
210 *
211 * @param uiDevice device to remove
212 */
213 public void remove(UiDevice uiDevice) {
214 UiDevice d = deviceLookup.remove(uiDevice.id());
215 if (d != null) {
216 d.destroy();
217 }
218 }
219
220 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700221 * Returns the number of devices configured in the topology.
222 *
223 * @return number of devices
224 */
225 public int deviceCount() {
226 return deviceLookup.size();
227 }
228
229 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700230 * Returns the link with the specified identifier, or null if no such
231 * link exists.
232 *
233 * @param id the canonicalized link identifier
234 * @return corresponding UI link
235 */
236 public UiLink findLink(UiLinkId id) {
237 return linkLookup.get(id);
238 }
239
240 /**
241 * Adds the given UI link to the topology model.
242 *
243 * @param uiLink link to add
244 */
245 public void add(UiLink uiLink) {
246 linkLookup.put(uiLink.id(), uiLink);
247 }
248
249 /**
250 * Removes the given UI link from the model.
251 *
252 * @param uiLink link to remove
253 */
254 public void remove(UiLink uiLink) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700255 UiLink link = linkLookup.remove(uiLink.id());
Simon Huntc0f20c12016-05-09 09:30:20 -0700256 if (link != null) {
257 link.destroy();
258 }
259 }
260
261 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700262 * Returns the number of links configured in the topology.
263 *
264 * @return number of links
265 */
266 public int linkCount() {
267 return linkLookup.size();
268 }
269
270 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700271 * Returns the host with the specified identifier, or null if no such
272 * host exists.
273 *
274 * @param id host identifier
275 * @return corresponding UI host
276 */
277 public UiHost findHost(HostId id) {
278 return hostLookup.get(id);
279 }
280
281 /**
282 * Adds the given host to the topology model.
283 *
284 * @param uiHost host to add
285 */
286 public void add(UiHost uiHost) {
287 hostLookup.put(uiHost.id(), uiHost);
288 }
289
290 /**
291 * Removes the given host from the topology model.
292 *
293 * @param uiHost host to remove
294 */
295 public void remove(UiHost uiHost) {
296 UiHost h = hostLookup.remove(uiHost.id());
297 if (h != null) {
298 h.destroy();
299 }
300 }
301
Simon Hunt58a0dd02016-05-17 11:54:23 -0700302 /**
303 * Returns the number of hosts configured in the topology.
304 *
305 * @return number of hosts
306 */
307 public int hostCount() {
308 return hostLookup.size();
309 }
310
311
Simon Huntc0f20c12016-05-09 09:30:20 -0700312 // ==
313 // package private methods for supporting linkage amongst topology entities
314 // ==
315
316 /**
317 * Returns the set of UI devices with the given identifiers.
318 *
319 * @param deviceIds device identifiers
320 * @return set of matching UI device instances
321 */
322 Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) {
323 Set<UiDevice> uiDevices = new HashSet<>();
324 for (DeviceId id : deviceIds) {
325 UiDevice d = deviceLookup.get(id);
326 if (d != null) {
327 uiDevices.add(d);
328 } else {
329 log.warn(E_UNMAPPED, "device", id);
330 }
331 }
332 return uiDevices;
333 }
334
335 /**
336 * Returns the set of UI hosts with the given identifiers.
337 *
338 * @param hostIds host identifiers
339 * @return set of matching UI host instances
340 */
341 Set<UiHost> hostSet(Set<HostId> hostIds) {
342 Set<UiHost> uiHosts = new HashSet<>();
343 for (HostId id : hostIds) {
344 UiHost h = hostLookup.get(id);
345 if (h != null) {
346 uiHosts.add(h);
347 } else {
348 log.warn(E_UNMAPPED, "host", id);
349 }
350 }
351 return uiHosts;
352 }
353
354 /**
355 * Returns the set of UI links with the given identifiers.
356 *
357 * @param uiLinkIds link identifiers
358 * @return set of matching UI link instances
359 */
360 Set<UiLink> linkSet(Set<UiLinkId> uiLinkIds) {
361 Set<UiLink> uiLinks = new HashSet<>();
362 for (UiLinkId id : uiLinkIds) {
363 UiLink link = linkLookup.get(id);
364 if (link != null) {
365 uiLinks.add(link);
366 } else {
367 log.warn(E_UNMAPPED, "link", id);
368 }
369 }
370 return uiLinks;
371 }
372
Simon Hunt58a0dd02016-05-17 11:54:23 -0700373 /**
374 * Returns a detailed (multi-line) string showing the contents of the
375 * topology.
376 *
377 * @return detailed string
378 */
379 public String dumpString() {
380 StringBuilder sb = new StringBuilder("Topology:").append(EOL);
381
382 sb.append(INDENT_1).append("Cluster Members").append(EOL);
383 for (UiClusterMember m : cnodeLookup.values()) {
384 sb.append(INDENT_2).append(m).append(EOL);
385 }
386
387 sb.append(INDENT_1).append("Regions").append(EOL);
388 for (UiRegion r : regionLookup.values()) {
389 sb.append(INDENT_2).append(r).append(EOL);
390 }
391
392 sb.append(INDENT_1).append("Devices").append(EOL);
393 for (UiDevice d : deviceLookup.values()) {
394 sb.append(INDENT_2).append(d).append(EOL);
395 }
396
397 sb.append(INDENT_1).append("Hosts").append(EOL);
398 for (UiHost h : hostLookup.values()) {
399 sb.append(INDENT_2).append(h).append(EOL);
400 }
401
402 sb.append(INDENT_1).append("Links").append(EOL);
403 for (UiLink link : linkLookup.values()) {
404 sb.append(INDENT_2).append(link).append(EOL);
405 }
406 sb.append("------").append(EOL);
407
408 return sb.toString();
409 }
410
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700411}