blob: 3a11fcf8a6c98cac342bd64f92f2a6432a21214a [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 Huntc0f20c12016-05-09 09:30:20 -070038 private static final String E_UNMAPPED =
39 "Attempting to retrieve unmapped {}: {}";
40
Simon Hunt642bc452016-05-04 19:34:45 -070041 private static final String DEFAULT_TOPOLOGY_ID = "TOPOLOGY-0";
42
Simon Hunt23fb1352016-04-11 12:15:19 -070043 private static final Logger log = LoggerFactory.getLogger(UiTopology.class);
44
Simon Huntc0f20c12016-05-09 09:30:20 -070045
46 // top level mappings of topology elements by ID
47 private final Map<NodeId, UiClusterMember> cnodeLookup = new HashMap<>();
48 private final Map<RegionId, UiRegion> regionLookup = new HashMap<>();
49 private final Map<DeviceId, UiDevice> deviceLookup = new HashMap<>();
50 private final Map<HostId, UiHost> hostLookup = new HashMap<>();
51 private final Map<UiLinkId, UiLink> linkLookup = new HashMap<>();
52
Simon Hunt23fb1352016-04-11 12:15:19 -070053
Simon Hunt338a3b42016-04-14 09:43:52 -070054 @Override
55 public String toString() {
Simon Huntc0f20c12016-05-09 09:30:20 -070056 return toStringHelper(this)
57 .add("#cnodes", clusterMemberCount())
58 .add("#regions", regionCount())
59 .add("#devices", deviceLookup.size())
60 .add("#hosts", hostLookup.size())
61 .add("#links", linkLookup.size())
62 .toString();
63 }
64
65 @Override
66 public String idAsString() {
67 return DEFAULT_TOPOLOGY_ID;
Simon Hunt338a3b42016-04-14 09:43:52 -070068 }
69
Simon Hunt23fb1352016-04-11 12:15:19 -070070 /**
71 * Clears the topology state; that is, drops all regions, devices, hosts,
72 * links, and cluster members.
73 */
74 public void clear() {
75 log.debug("clearing topology model");
Simon Huntc0f20c12016-05-09 09:30:20 -070076 cnodeLookup.clear();
77 regionLookup.clear();
78 deviceLookup.clear();
79 hostLookup.clear();
80 linkLookup.clear();
Simon Hunt23fb1352016-04-11 12:15:19 -070081 }
Simon Hunt338a3b42016-04-14 09:43:52 -070082
83 /**
84 * Returns the cluster member with the given identifier, or null if no
Simon Huntc0f20c12016-05-09 09:30:20 -070085 * such member exists.
Simon Hunt338a3b42016-04-14 09:43:52 -070086 *
87 * @param id cluster node identifier
Simon Huntc0f20c12016-05-09 09:30:20 -070088 * @return corresponding UI cluster member
Simon Hunt338a3b42016-04-14 09:43:52 -070089 */
90 public UiClusterMember findClusterMember(NodeId id) {
Simon Huntc0f20c12016-05-09 09:30:20 -070091 return cnodeLookup.get(id);
Simon Hunt338a3b42016-04-14 09:43:52 -070092 }
93
94 /**
95 * Adds the given cluster member to the topology model.
96 *
97 * @param member cluster member to add
98 */
99 public void add(UiClusterMember member) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700100 cnodeLookup.put(member.id(), member);
Simon Hunt338a3b42016-04-14 09:43:52 -0700101 }
102
103 /**
Simon Hunt642bc452016-05-04 19:34:45 -0700104 * Removes the given cluster member from the topology model.
105 *
106 * @param member cluster member to remove
107 */
108 public void remove(UiClusterMember member) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700109 UiClusterMember m = cnodeLookup.remove(member.id());
110 if (m != null) {
111 m.destroy();
112 }
Simon Hunt642bc452016-05-04 19:34:45 -0700113 }
114
115 /**
Simon Hunt338a3b42016-04-14 09:43:52 -0700116 * Returns the number of members in the cluster.
117 *
118 * @return number of cluster members
119 */
120 public int clusterMemberCount() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700121 return cnodeLookup.size();
122 }
123
124 /**
125 * Returns the region with the specified identifier, or null if
126 * no such region exists.
127 *
128 * @param id region identifier
129 * @return corresponding UI region
130 */
131 public UiRegion findRegion(RegionId id) {
132 return regionLookup.get(id);
Simon Hunt338a3b42016-04-14 09:43:52 -0700133 }
134
135 /**
136 * Returns the number of regions configured in the topology.
137 *
138 * @return number of regions
139 */
140 public int regionCount() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700141 return regionLookup.size();
Simon Hunt338a3b42016-04-14 09:43:52 -0700142 }
Simon Hunt642bc452016-05-04 19:34:45 -0700143
Simon Huntc0f20c12016-05-09 09:30:20 -0700144 /**
145 * Adds the given region to the topology model.
146 *
147 * @param uiRegion region to add
148 */
149 public void add(UiRegion uiRegion) {
150 regionLookup.put(uiRegion.id(), uiRegion);
Simon Hunt642bc452016-05-04 19:34:45 -0700151 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700152
153 /**
154 * Removes the given region from the topology model.
155 *
156 * @param uiRegion region to remove
157 */
158 public void remove(UiRegion uiRegion) {
159 regionLookup.remove(uiRegion.id());
160 }
161
162 /**
163 * Returns the device with the specified identifier, or null if
164 * no such device exists.
165 *
166 * @param id device identifier
167 * @return corresponding UI device
168 */
169 public UiDevice findDevice(DeviceId id) {
170 return deviceLookup.get(id);
171 }
172
173 /**
174 * Adds the given device to the topology model.
175 *
176 * @param uiDevice device to add
177 */
178 public void add(UiDevice uiDevice) {
179 deviceLookup.put(uiDevice.id(), uiDevice);
180 }
181
182 /**
183 * Removes the given device from the topology model.
184 *
185 * @param uiDevice device to remove
186 */
187 public void remove(UiDevice uiDevice) {
188 UiDevice d = deviceLookup.remove(uiDevice.id());
189 if (d != null) {
190 d.destroy();
191 }
192 }
193
194 /**
195 * Returns the link with the specified identifier, or null if no such
196 * link exists.
197 *
198 * @param id the canonicalized link identifier
199 * @return corresponding UI link
200 */
201 public UiLink findLink(UiLinkId id) {
202 return linkLookup.get(id);
203 }
204
205 /**
206 * Adds the given UI link to the topology model.
207 *
208 * @param uiLink link to add
209 */
210 public void add(UiLink uiLink) {
211 linkLookup.put(uiLink.id(), uiLink);
212 }
213
214 /**
215 * Removes the given UI link from the model.
216 *
217 * @param uiLink link to remove
218 */
219 public void remove(UiLink uiLink) {
220 UiLink link = linkLookup.get(uiLink.id());
221 if (link != null) {
222 link.destroy();
223 }
224 }
225
226 /**
227 * Returns the host with the specified identifier, or null if no such
228 * host exists.
229 *
230 * @param id host identifier
231 * @return corresponding UI host
232 */
233 public UiHost findHost(HostId id) {
234 return hostLookup.get(id);
235 }
236
237 /**
238 * Adds the given host to the topology model.
239 *
240 * @param uiHost host to add
241 */
242 public void add(UiHost uiHost) {
243 hostLookup.put(uiHost.id(), uiHost);
244 }
245
246 /**
247 * Removes the given host from the topology model.
248 *
249 * @param uiHost host to remove
250 */
251 public void remove(UiHost uiHost) {
252 UiHost h = hostLookup.remove(uiHost.id());
253 if (h != null) {
254 h.destroy();
255 }
256 }
257
258 // ==
259 // package private methods for supporting linkage amongst topology entities
260 // ==
261
262 /**
263 * Returns the set of UI devices with the given identifiers.
264 *
265 * @param deviceIds device identifiers
266 * @return set of matching UI device instances
267 */
268 Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) {
269 Set<UiDevice> uiDevices = new HashSet<>();
270 for (DeviceId id : deviceIds) {
271 UiDevice d = deviceLookup.get(id);
272 if (d != null) {
273 uiDevices.add(d);
274 } else {
275 log.warn(E_UNMAPPED, "device", id);
276 }
277 }
278 return uiDevices;
279 }
280
281 /**
282 * Returns the set of UI hosts with the given identifiers.
283 *
284 * @param hostIds host identifiers
285 * @return set of matching UI host instances
286 */
287 Set<UiHost> hostSet(Set<HostId> hostIds) {
288 Set<UiHost> uiHosts = new HashSet<>();
289 for (HostId id : hostIds) {
290 UiHost h = hostLookup.get(id);
291 if (h != null) {
292 uiHosts.add(h);
293 } else {
294 log.warn(E_UNMAPPED, "host", id);
295 }
296 }
297 return uiHosts;
298 }
299
300 /**
301 * Returns the set of UI links with the given identifiers.
302 *
303 * @param uiLinkIds link identifiers
304 * @return set of matching UI link instances
305 */
306 Set<UiLink> linkSet(Set<UiLinkId> uiLinkIds) {
307 Set<UiLink> uiLinks = new HashSet<>();
308 for (UiLinkId id : uiLinkIds) {
309 UiLink link = linkLookup.get(id);
310 if (link != null) {
311 uiLinks.add(link);
312 } else {
313 log.warn(E_UNMAPPED, "link", id);
314 }
315 }
316 return uiLinks;
317 }
318
Simon Hunt5f6dbf82016-03-30 08:53:33 -0700319}