blob: 585e6589b4fb0df4018490f1621d8f6ba8d876a1 [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 Hunt4854f3d2016-08-02 18:13:15 -0700264 * Returns all links in the model.
265 *
266 * @return all links
267 */
268 public Set<UiLink> allLinks() {
269 return new HashSet<>(linkLookup.values());
270 }
271
272 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700273 * Returns the link with the specified identifier, or null if no such
274 * link exists.
275 *
276 * @param id the canonicalized link identifier
277 * @return corresponding UI link
278 */
279 public UiLink findLink(UiLinkId id) {
280 return linkLookup.get(id);
281 }
282
283 /**
284 * Adds the given UI link to the topology model.
285 *
286 * @param uiLink link to add
287 */
288 public void add(UiLink uiLink) {
289 linkLookup.put(uiLink.id(), uiLink);
290 }
291
292 /**
293 * Removes the given UI link from the model.
294 *
295 * @param uiLink link to remove
296 */
297 public void remove(UiLink uiLink) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700298 UiLink link = linkLookup.remove(uiLink.id());
Simon Huntc0f20c12016-05-09 09:30:20 -0700299 if (link != null) {
300 link.destroy();
301 }
302 }
303
304 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700305 * Returns the number of links configured in the topology.
306 *
307 * @return number of links
308 */
309 public int linkCount() {
310 return linkLookup.size();
311 }
312
313 /**
Simon Hunt4854f3d2016-08-02 18:13:15 -0700314 * Returns all hosts in the model.
315 *
316 * @return all hosts
317 */
318 public Set<UiHost> allHosts() {
319 return new HashSet<>(hostLookup.values());
320 }
321
322 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700323 * Returns the host with the specified identifier, or null if no such
324 * host exists.
325 *
326 * @param id host identifier
327 * @return corresponding UI host
328 */
329 public UiHost findHost(HostId id) {
330 return hostLookup.get(id);
331 }
332
333 /**
334 * Adds the given host to the topology model.
335 *
336 * @param uiHost host to add
337 */
338 public void add(UiHost uiHost) {
339 hostLookup.put(uiHost.id(), uiHost);
340 }
341
342 /**
343 * Removes the given host from the topology model.
344 *
345 * @param uiHost host to remove
346 */
347 public void remove(UiHost uiHost) {
348 UiHost h = hostLookup.remove(uiHost.id());
349 if (h != null) {
350 h.destroy();
351 }
352 }
353
Simon Hunt58a0dd02016-05-17 11:54:23 -0700354 /**
355 * Returns the number of hosts configured in the topology.
356 *
357 * @return number of hosts
358 */
359 public int hostCount() {
360 return hostLookup.size();
361 }
362
363
Simon Huntc0f20c12016-05-09 09:30:20 -0700364 // ==
365 // package private methods for supporting linkage amongst topology entities
366 // ==
367
368 /**
369 * Returns the set of UI devices with the given identifiers.
370 *
371 * @param deviceIds device identifiers
372 * @return set of matching UI device instances
373 */
374 Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) {
375 Set<UiDevice> uiDevices = new HashSet<>();
376 for (DeviceId id : deviceIds) {
377 UiDevice d = deviceLookup.get(id);
378 if (d != null) {
379 uiDevices.add(d);
380 } else {
381 log.warn(E_UNMAPPED, "device", id);
382 }
383 }
384 return uiDevices;
385 }
386
387 /**
388 * Returns the set of UI hosts with the given identifiers.
389 *
390 * @param hostIds host identifiers
391 * @return set of matching UI host instances
392 */
393 Set<UiHost> hostSet(Set<HostId> hostIds) {
394 Set<UiHost> uiHosts = new HashSet<>();
395 for (HostId id : hostIds) {
396 UiHost h = hostLookup.get(id);
397 if (h != null) {
398 uiHosts.add(h);
399 } else {
400 log.warn(E_UNMAPPED, "host", id);
401 }
402 }
403 return uiHosts;
404 }
405
406 /**
407 * Returns the set of UI links with the given identifiers.
408 *
409 * @param uiLinkIds link identifiers
410 * @return set of matching UI link instances
411 */
412 Set<UiLink> linkSet(Set<UiLinkId> uiLinkIds) {
413 Set<UiLink> uiLinks = new HashSet<>();
414 for (UiLinkId id : uiLinkIds) {
415 UiLink link = linkLookup.get(id);
416 if (link != null) {
417 uiLinks.add(link);
418 } else {
419 log.warn(E_UNMAPPED, "link", id);
420 }
421 }
422 return uiLinks;
423 }
424
Simon Hunt58a0dd02016-05-17 11:54:23 -0700425 /**
426 * Returns a detailed (multi-line) string showing the contents of the
427 * topology.
428 *
429 * @return detailed string
430 */
431 public String dumpString() {
432 StringBuilder sb = new StringBuilder("Topology:").append(EOL);
433
434 sb.append(INDENT_1).append("Cluster Members").append(EOL);
435 for (UiClusterMember m : cnodeLookup.values()) {
436 sb.append(INDENT_2).append(m).append(EOL);
437 }
438
439 sb.append(INDENT_1).append("Regions").append(EOL);
440 for (UiRegion r : regionLookup.values()) {
441 sb.append(INDENT_2).append(r).append(EOL);
442 }
443
444 sb.append(INDENT_1).append("Devices").append(EOL);
445 for (UiDevice d : deviceLookup.values()) {
446 sb.append(INDENT_2).append(d).append(EOL);
447 }
448
449 sb.append(INDENT_1).append("Hosts").append(EOL);
450 for (UiHost h : hostLookup.values()) {
451 sb.append(INDENT_2).append(h).append(EOL);
452 }
453
454 sb.append(INDENT_1).append("Links").append(EOL);
455 for (UiLink link : linkLookup.values()) {
456 sb.append(INDENT_2).append(link).append(EOL);
457 }
458 sb.append("------").append(EOL);
459
460 return sb.toString();
461 }
462
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700463}