blob: b4eabe91343b4a9a04ccd48983de39e4ddad1a32 [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 Huntb1ce2602016-07-23 14:04:31 -070064 // a container for devices, hosts, etc. belonging to no region
65 private final UiRegion nullRegion = new UiRegion(this, null);
66
Simon Hunt23fb1352016-04-11 12:15:19 -070067
Simon Hunt338a3b42016-04-14 09:43:52 -070068 @Override
69 public String toString() {
Simon Huntc0f20c12016-05-09 09:30:20 -070070 return toStringHelper(this)
71 .add("#cnodes", clusterMemberCount())
72 .add("#regions", regionCount())
73 .add("#devices", deviceLookup.size())
74 .add("#hosts", hostLookup.size())
75 .add("#links", linkLookup.size())
76 .toString();
77 }
78
79 @Override
80 public String idAsString() {
81 return DEFAULT_TOPOLOGY_ID;
Simon Hunt338a3b42016-04-14 09:43:52 -070082 }
83
Simon Hunt23fb1352016-04-11 12:15:19 -070084 /**
85 * Clears the topology state; that is, drops all regions, devices, hosts,
86 * links, and cluster members.
87 */
88 public void clear() {
89 log.debug("clearing topology model");
Simon Huntc0f20c12016-05-09 09:30:20 -070090 cnodeLookup.clear();
91 regionLookup.clear();
92 deviceLookup.clear();
93 hostLookup.clear();
94 linkLookup.clear();
Simon Huntb1ce2602016-07-23 14:04:31 -070095
96 nullRegion.destroy();
Simon Hunt23fb1352016-04-11 12:15:19 -070097 }
Simon Hunt338a3b42016-04-14 09:43:52 -070098
Simon Huntd5b96732016-07-08 13:22:27 -070099
100 /**
101 * Returns all the cluster members, sorted by their ID.
102 *
103 * @return all cluster members
104 */
105 public List<UiClusterMember> allClusterMembers() {
106 List<UiClusterMember> members = new ArrayList<>(cnodeLookup.values());
107 Collections.sort(members, CLUSTER_MEMBER_COMPARATOR);
108 return members;
109 }
110
Simon Hunt338a3b42016-04-14 09:43:52 -0700111 /**
112 * Returns the cluster member with the given identifier, or null if no
Simon Huntc0f20c12016-05-09 09:30:20 -0700113 * such member exists.
Simon Hunt338a3b42016-04-14 09:43:52 -0700114 *
115 * @param id cluster node identifier
Simon Huntc0f20c12016-05-09 09:30:20 -0700116 * @return corresponding UI cluster member
Simon Hunt338a3b42016-04-14 09:43:52 -0700117 */
118 public UiClusterMember findClusterMember(NodeId id) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700119 return cnodeLookup.get(id);
Simon Hunt338a3b42016-04-14 09:43:52 -0700120 }
121
122 /**
123 * Adds the given cluster member to the topology model.
124 *
125 * @param member cluster member to add
126 */
127 public void add(UiClusterMember member) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700128 cnodeLookup.put(member.id(), member);
Simon Hunt338a3b42016-04-14 09:43:52 -0700129 }
130
131 /**
Simon Hunt642bc452016-05-04 19:34:45 -0700132 * Removes the given cluster member from the topology model.
133 *
134 * @param member cluster member to remove
135 */
136 public void remove(UiClusterMember member) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700137 UiClusterMember m = cnodeLookup.remove(member.id());
138 if (m != null) {
139 m.destroy();
140 }
Simon Hunt642bc452016-05-04 19:34:45 -0700141 }
142
143 /**
Simon Hunt338a3b42016-04-14 09:43:52 -0700144 * Returns the number of members in the cluster.
145 *
146 * @return number of cluster members
147 */
148 public int clusterMemberCount() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700149 return cnodeLookup.size();
150 }
151
Simon Hunt10973dd2016-08-01 15:50:35 -0700152
153 /**
154 * Returns all regions in the model.
155 *
156 * @return all regions
157 */
158 public Set<UiRegion> allRegions() {
159 return new HashSet<>(regionLookup.values());
160 }
161
Simon Huntc0f20c12016-05-09 09:30:20 -0700162 /**
Simon Huntb1ce2602016-07-23 14:04:31 -0700163 * Returns a reference to the null-region. That is, the container for
164 * devices, hosts, and links that belong to no region.
165 *
166 * @return the null-region
167 */
168 public UiRegion nullRegion() {
169 return nullRegion;
170 }
171
172 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700173 * Returns the region with the specified identifier, or null if
174 * no such region exists.
175 *
176 * @param id region identifier
177 * @return corresponding UI region
178 */
179 public UiRegion findRegion(RegionId id) {
180 return regionLookup.get(id);
Simon Hunt338a3b42016-04-14 09:43:52 -0700181 }
182
183 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700184 * Adds the given region to the topology model.
185 *
186 * @param uiRegion region to add
187 */
188 public void add(UiRegion uiRegion) {
189 regionLookup.put(uiRegion.id(), uiRegion);
Simon Hunt642bc452016-05-04 19:34:45 -0700190 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700191
192 /**
193 * Removes the given region from the topology model.
194 *
195 * @param uiRegion region to remove
196 */
197 public void remove(UiRegion uiRegion) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700198 UiRegion r = regionLookup.remove(uiRegion.id());
199 if (r != null) {
200 r.destroy();
201 }
202 }
203
204 /**
205 * Returns the number of regions configured in the topology.
206 *
207 * @return number of regions
208 */
209 public int regionCount() {
210 return regionLookup.size();
Simon Huntc0f20c12016-05-09 09:30:20 -0700211 }
212
213 /**
Simon Huntb1ce2602016-07-23 14:04:31 -0700214 * Returns all devices in the model.
215 *
216 * @return all devices
217 */
218 public Set<UiDevice> allDevices() {
219 return new HashSet<>(deviceLookup.values());
220 }
221
222 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700223 * Returns the device with the specified identifier, or null if
224 * no such device exists.
225 *
226 * @param id device identifier
227 * @return corresponding UI device
228 */
229 public UiDevice findDevice(DeviceId id) {
230 return deviceLookup.get(id);
231 }
232
233 /**
234 * Adds the given device to the topology model.
235 *
236 * @param uiDevice device to add
237 */
238 public void add(UiDevice uiDevice) {
239 deviceLookup.put(uiDevice.id(), uiDevice);
240 }
241
242 /**
243 * Removes the given device from the topology model.
244 *
245 * @param uiDevice device to remove
246 */
247 public void remove(UiDevice uiDevice) {
248 UiDevice d = deviceLookup.remove(uiDevice.id());
249 if (d != null) {
250 d.destroy();
251 }
252 }
253
254 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700255 * Returns the number of devices configured in the topology.
256 *
257 * @return number of devices
258 */
259 public int deviceCount() {
260 return deviceLookup.size();
261 }
262
263 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700264 * Returns the link with the specified identifier, or null if no such
265 * link exists.
266 *
267 * @param id the canonicalized link identifier
268 * @return corresponding UI link
269 */
270 public UiLink findLink(UiLinkId id) {
271 return linkLookup.get(id);
272 }
273
274 /**
275 * Adds the given UI link to the topology model.
276 *
277 * @param uiLink link to add
278 */
279 public void add(UiLink uiLink) {
280 linkLookup.put(uiLink.id(), uiLink);
281 }
282
283 /**
284 * Removes the given UI link from the model.
285 *
286 * @param uiLink link to remove
287 */
288 public void remove(UiLink uiLink) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700289 UiLink link = linkLookup.remove(uiLink.id());
Simon Huntc0f20c12016-05-09 09:30:20 -0700290 if (link != null) {
291 link.destroy();
292 }
293 }
294
295 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700296 * Returns the number of links configured in the topology.
297 *
298 * @return number of links
299 */
300 public int linkCount() {
301 return linkLookup.size();
302 }
303
304 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700305 * Returns the host with the specified identifier, or null if no such
306 * host exists.
307 *
308 * @param id host identifier
309 * @return corresponding UI host
310 */
311 public UiHost findHost(HostId id) {
312 return hostLookup.get(id);
313 }
314
315 /**
316 * Adds the given host to the topology model.
317 *
318 * @param uiHost host to add
319 */
320 public void add(UiHost uiHost) {
321 hostLookup.put(uiHost.id(), uiHost);
322 }
323
324 /**
325 * Removes the given host from the topology model.
326 *
327 * @param uiHost host to remove
328 */
329 public void remove(UiHost uiHost) {
330 UiHost h = hostLookup.remove(uiHost.id());
331 if (h != null) {
332 h.destroy();
333 }
334 }
335
Simon Hunt58a0dd02016-05-17 11:54:23 -0700336 /**
337 * Returns the number of hosts configured in the topology.
338 *
339 * @return number of hosts
340 */
341 public int hostCount() {
342 return hostLookup.size();
343 }
344
345
Simon Huntc0f20c12016-05-09 09:30:20 -0700346 // ==
347 // package private methods for supporting linkage amongst topology entities
348 // ==
349
350 /**
351 * Returns the set of UI devices with the given identifiers.
352 *
353 * @param deviceIds device identifiers
354 * @return set of matching UI device instances
355 */
356 Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) {
357 Set<UiDevice> uiDevices = new HashSet<>();
358 for (DeviceId id : deviceIds) {
359 UiDevice d = deviceLookup.get(id);
360 if (d != null) {
361 uiDevices.add(d);
362 } else {
363 log.warn(E_UNMAPPED, "device", id);
364 }
365 }
366 return uiDevices;
367 }
368
369 /**
370 * Returns the set of UI hosts with the given identifiers.
371 *
372 * @param hostIds host identifiers
373 * @return set of matching UI host instances
374 */
375 Set<UiHost> hostSet(Set<HostId> hostIds) {
376 Set<UiHost> uiHosts = new HashSet<>();
377 for (HostId id : hostIds) {
378 UiHost h = hostLookup.get(id);
379 if (h != null) {
380 uiHosts.add(h);
381 } else {
382 log.warn(E_UNMAPPED, "host", id);
383 }
384 }
385 return uiHosts;
386 }
387
388 /**
389 * Returns the set of UI links with the given identifiers.
390 *
391 * @param uiLinkIds link identifiers
392 * @return set of matching UI link instances
393 */
394 Set<UiLink> linkSet(Set<UiLinkId> uiLinkIds) {
395 Set<UiLink> uiLinks = new HashSet<>();
396 for (UiLinkId id : uiLinkIds) {
397 UiLink link = linkLookup.get(id);
398 if (link != null) {
399 uiLinks.add(link);
400 } else {
401 log.warn(E_UNMAPPED, "link", id);
402 }
403 }
404 return uiLinks;
405 }
406
Simon Hunt58a0dd02016-05-17 11:54:23 -0700407 /**
408 * Returns a detailed (multi-line) string showing the contents of the
409 * topology.
410 *
411 * @return detailed string
412 */
413 public String dumpString() {
414 StringBuilder sb = new StringBuilder("Topology:").append(EOL);
415
416 sb.append(INDENT_1).append("Cluster Members").append(EOL);
417 for (UiClusterMember m : cnodeLookup.values()) {
418 sb.append(INDENT_2).append(m).append(EOL);
419 }
420
421 sb.append(INDENT_1).append("Regions").append(EOL);
422 for (UiRegion r : regionLookup.values()) {
423 sb.append(INDENT_2).append(r).append(EOL);
424 }
425
426 sb.append(INDENT_1).append("Devices").append(EOL);
427 for (UiDevice d : deviceLookup.values()) {
428 sb.append(INDENT_2).append(d).append(EOL);
429 }
430
431 sb.append(INDENT_1).append("Hosts").append(EOL);
432 for (UiHost h : hostLookup.values()) {
433 sb.append(INDENT_2).append(h).append(EOL);
434 }
435
436 sb.append(INDENT_1).append("Links").append(EOL);
437 for (UiLink link : linkLookup.values()) {
438 sb.append(INDENT_2).append(link).append(EOL);
439 }
440 sb.append("------").append(EOL);
441
442 return sb.toString();
443 }
444
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700445}