blob: 9555e3a41aba17672d6deb144b12eb4eed581a24 [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 Huntc0f20c12016-05-09 09:30:20 -070026import java.util.HashMap;
27import java.util.HashSet;
28import java.util.Map;
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 the overall network topology.
35 */
Simon Huntcda9c032016-04-11 10:32:54 -070036public class UiTopology extends UiElement {
Simon Hunt23fb1352016-04-11 12:15:19 -070037
Simon Hunt58a0dd02016-05-17 11:54:23 -070038 private static final String INDENT_1 = " ";
39 private static final String INDENT_2 = " ";
40 private static final String EOL = String.format("%n");
41
Simon Huntc0f20c12016-05-09 09:30:20 -070042 private static final String E_UNMAPPED =
43 "Attempting to retrieve unmapped {}: {}";
44
Simon Hunt642bc452016-05-04 19:34:45 -070045 private static final String DEFAULT_TOPOLOGY_ID = "TOPOLOGY-0";
46
Simon Hunt23fb1352016-04-11 12:15:19 -070047 private static final Logger log = LoggerFactory.getLogger(UiTopology.class);
48
Simon Huntc0f20c12016-05-09 09:30:20 -070049
50 // top level mappings of topology elements by ID
51 private final Map<NodeId, UiClusterMember> cnodeLookup = new HashMap<>();
52 private final Map<RegionId, UiRegion> regionLookup = new HashMap<>();
53 private final Map<DeviceId, UiDevice> deviceLookup = new HashMap<>();
54 private final Map<HostId, UiHost> hostLookup = new HashMap<>();
55 private final Map<UiLinkId, UiLink> linkLookup = new HashMap<>();
56
Simon Hunt23fb1352016-04-11 12:15:19 -070057
Simon Hunt338a3b42016-04-14 09:43:52 -070058 @Override
59 public String toString() {
Simon Huntc0f20c12016-05-09 09:30:20 -070060 return toStringHelper(this)
61 .add("#cnodes", clusterMemberCount())
62 .add("#regions", regionCount())
63 .add("#devices", deviceLookup.size())
64 .add("#hosts", hostLookup.size())
65 .add("#links", linkLookup.size())
66 .toString();
67 }
68
69 @Override
70 public String idAsString() {
71 return DEFAULT_TOPOLOGY_ID;
Simon Hunt338a3b42016-04-14 09:43:52 -070072 }
73
Simon Hunt23fb1352016-04-11 12:15:19 -070074 /**
75 * Clears the topology state; that is, drops all regions, devices, hosts,
76 * links, and cluster members.
77 */
78 public void clear() {
79 log.debug("clearing topology model");
Simon Huntc0f20c12016-05-09 09:30:20 -070080 cnodeLookup.clear();
81 regionLookup.clear();
82 deviceLookup.clear();
83 hostLookup.clear();
84 linkLookup.clear();
Simon Hunt23fb1352016-04-11 12:15:19 -070085 }
Simon Hunt338a3b42016-04-14 09:43:52 -070086
87 /**
88 * Returns the cluster member with the given identifier, or null if no
Simon Huntc0f20c12016-05-09 09:30:20 -070089 * such member exists.
Simon Hunt338a3b42016-04-14 09:43:52 -070090 *
91 * @param id cluster node identifier
Simon Huntc0f20c12016-05-09 09:30:20 -070092 * @return corresponding UI cluster member
Simon Hunt338a3b42016-04-14 09:43:52 -070093 */
94 public UiClusterMember findClusterMember(NodeId id) {
Simon Huntc0f20c12016-05-09 09:30:20 -070095 return cnodeLookup.get(id);
Simon Hunt338a3b42016-04-14 09:43:52 -070096 }
97
98 /**
99 * Adds the given cluster member to the topology model.
100 *
101 * @param member cluster member to add
102 */
103 public void add(UiClusterMember member) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700104 cnodeLookup.put(member.id(), member);
Simon Hunt338a3b42016-04-14 09:43:52 -0700105 }
106
107 /**
Simon Hunt642bc452016-05-04 19:34:45 -0700108 * Removes the given cluster member from the topology model.
109 *
110 * @param member cluster member to remove
111 */
112 public void remove(UiClusterMember member) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700113 UiClusterMember m = cnodeLookup.remove(member.id());
114 if (m != null) {
115 m.destroy();
116 }
Simon Hunt642bc452016-05-04 19:34:45 -0700117 }
118
119 /**
Simon Hunt338a3b42016-04-14 09:43:52 -0700120 * Returns the number of members in the cluster.
121 *
122 * @return number of cluster members
123 */
124 public int clusterMemberCount() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700125 return cnodeLookup.size();
126 }
127
128 /**
129 * Returns the region with the specified identifier, or null if
130 * no such region exists.
131 *
132 * @param id region identifier
133 * @return corresponding UI region
134 */
135 public UiRegion findRegion(RegionId id) {
136 return regionLookup.get(id);
Simon Hunt338a3b42016-04-14 09:43:52 -0700137 }
138
139 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700140 * Adds the given region to the topology model.
141 *
142 * @param uiRegion region to add
143 */
144 public void add(UiRegion uiRegion) {
145 regionLookup.put(uiRegion.id(), uiRegion);
Simon Hunt642bc452016-05-04 19:34:45 -0700146 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700147
148 /**
149 * Removes the given region from the topology model.
150 *
151 * @param uiRegion region to remove
152 */
153 public void remove(UiRegion uiRegion) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700154 UiRegion r = regionLookup.remove(uiRegion.id());
155 if (r != null) {
156 r.destroy();
157 }
158 }
159
160 /**
161 * Returns the number of regions configured in the topology.
162 *
163 * @return number of regions
164 */
165 public int regionCount() {
166 return regionLookup.size();
Simon Huntc0f20c12016-05-09 09:30:20 -0700167 }
168
169 /**
170 * Returns the device with the specified identifier, or null if
171 * no such device exists.
172 *
173 * @param id device identifier
174 * @return corresponding UI device
175 */
176 public UiDevice findDevice(DeviceId id) {
177 return deviceLookup.get(id);
178 }
179
180 /**
181 * Adds the given device to the topology model.
182 *
183 * @param uiDevice device to add
184 */
185 public void add(UiDevice uiDevice) {
186 deviceLookup.put(uiDevice.id(), uiDevice);
187 }
188
189 /**
190 * Removes the given device from the topology model.
191 *
192 * @param uiDevice device to remove
193 */
194 public void remove(UiDevice uiDevice) {
195 UiDevice d = deviceLookup.remove(uiDevice.id());
196 if (d != null) {
197 d.destroy();
198 }
199 }
200
201 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700202 * Returns the number of devices configured in the topology.
203 *
204 * @return number of devices
205 */
206 public int deviceCount() {
207 return deviceLookup.size();
208 }
209
210 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700211 * Returns the link with the specified identifier, or null if no such
212 * link exists.
213 *
214 * @param id the canonicalized link identifier
215 * @return corresponding UI link
216 */
217 public UiLink findLink(UiLinkId id) {
218 return linkLookup.get(id);
219 }
220
221 /**
222 * Adds the given UI link to the topology model.
223 *
224 * @param uiLink link to add
225 */
226 public void add(UiLink uiLink) {
227 linkLookup.put(uiLink.id(), uiLink);
228 }
229
230 /**
231 * Removes the given UI link from the model.
232 *
233 * @param uiLink link to remove
234 */
235 public void remove(UiLink uiLink) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700236 UiLink link = linkLookup.remove(uiLink.id());
Simon Huntc0f20c12016-05-09 09:30:20 -0700237 if (link != null) {
238 link.destroy();
239 }
240 }
241
242 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700243 * Returns the number of links configured in the topology.
244 *
245 * @return number of links
246 */
247 public int linkCount() {
248 return linkLookup.size();
249 }
250
251 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700252 * Returns the host with the specified identifier, or null if no such
253 * host exists.
254 *
255 * @param id host identifier
256 * @return corresponding UI host
257 */
258 public UiHost findHost(HostId id) {
259 return hostLookup.get(id);
260 }
261
262 /**
263 * Adds the given host to the topology model.
264 *
265 * @param uiHost host to add
266 */
267 public void add(UiHost uiHost) {
268 hostLookup.put(uiHost.id(), uiHost);
269 }
270
271 /**
272 * Removes the given host from the topology model.
273 *
274 * @param uiHost host to remove
275 */
276 public void remove(UiHost uiHost) {
277 UiHost h = hostLookup.remove(uiHost.id());
278 if (h != null) {
279 h.destroy();
280 }
281 }
282
Simon Hunt58a0dd02016-05-17 11:54:23 -0700283 /**
284 * Returns the number of hosts configured in the topology.
285 *
286 * @return number of hosts
287 */
288 public int hostCount() {
289 return hostLookup.size();
290 }
291
292
Simon Huntc0f20c12016-05-09 09:30:20 -0700293 // ==
294 // package private methods for supporting linkage amongst topology entities
295 // ==
296
297 /**
298 * Returns the set of UI devices with the given identifiers.
299 *
300 * @param deviceIds device identifiers
301 * @return set of matching UI device instances
302 */
303 Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) {
304 Set<UiDevice> uiDevices = new HashSet<>();
305 for (DeviceId id : deviceIds) {
306 UiDevice d = deviceLookup.get(id);
307 if (d != null) {
308 uiDevices.add(d);
309 } else {
310 log.warn(E_UNMAPPED, "device", id);
311 }
312 }
313 return uiDevices;
314 }
315
316 /**
317 * Returns the set of UI hosts with the given identifiers.
318 *
319 * @param hostIds host identifiers
320 * @return set of matching UI host instances
321 */
322 Set<UiHost> hostSet(Set<HostId> hostIds) {
323 Set<UiHost> uiHosts = new HashSet<>();
324 for (HostId id : hostIds) {
325 UiHost h = hostLookup.get(id);
326 if (h != null) {
327 uiHosts.add(h);
328 } else {
329 log.warn(E_UNMAPPED, "host", id);
330 }
331 }
332 return uiHosts;
333 }
334
335 /**
336 * Returns the set of UI links with the given identifiers.
337 *
338 * @param uiLinkIds link identifiers
339 * @return set of matching UI link instances
340 */
341 Set<UiLink> linkSet(Set<UiLinkId> uiLinkIds) {
342 Set<UiLink> uiLinks = new HashSet<>();
343 for (UiLinkId id : uiLinkIds) {
344 UiLink link = linkLookup.get(id);
345 if (link != null) {
346 uiLinks.add(link);
347 } else {
348 log.warn(E_UNMAPPED, "link", id);
349 }
350 }
351 return uiLinks;
352 }
353
Simon Hunt58a0dd02016-05-17 11:54:23 -0700354 /**
355 * Returns a detailed (multi-line) string showing the contents of the
356 * topology.
357 *
358 * @return detailed string
359 */
360 public String dumpString() {
361 StringBuilder sb = new StringBuilder("Topology:").append(EOL);
362
363 sb.append(INDENT_1).append("Cluster Members").append(EOL);
364 for (UiClusterMember m : cnodeLookup.values()) {
365 sb.append(INDENT_2).append(m).append(EOL);
366 }
367
368 sb.append(INDENT_1).append("Regions").append(EOL);
369 for (UiRegion r : regionLookup.values()) {
370 sb.append(INDENT_2).append(r).append(EOL);
371 }
372
373 sb.append(INDENT_1).append("Devices").append(EOL);
374 for (UiDevice d : deviceLookup.values()) {
375 sb.append(INDENT_2).append(d).append(EOL);
376 }
377
378 sb.append(INDENT_1).append("Hosts").append(EOL);
379 for (UiHost h : hostLookup.values()) {
380 sb.append(INDENT_2).append(h).append(EOL);
381 }
382
383 sb.append(INDENT_1).append("Links").append(EOL);
384 for (UiLink link : linkLookup.values()) {
385 sb.append(INDENT_2).append(link).append(EOL);
386 }
387 sb.append("------").append(EOL);
388
389 return sb.toString();
390 }
391
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700392}