blob: 8647c59317c9d6900a4945dd00a8019ba90a72a9 [file] [log] [blame]
Simon Huntcda9c032016-04-11 10:32:54 -07001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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.impl.topo.model;
18
Simon Hunt23fb1352016-04-11 12:15:19 -070019import org.onosproject.cluster.ControllerNode;
Simon Hunt642bc452016-05-04 19:34:45 -070020import org.onosproject.cluster.NodeId;
Simon Hunt23fb1352016-04-11 12:15:19 -070021import org.onosproject.cluster.RoleInfo;
Simon Huntcda9c032016-04-11 10:32:54 -070022import org.onosproject.event.EventDispatcher;
23import org.onosproject.net.Device;
Simon Hunt23fb1352016-04-11 12:15:19 -070024import org.onosproject.net.DeviceId;
Simon Huntc0f20c12016-05-09 09:30:20 -070025import org.onosproject.net.EdgeLink;
Simon Hunt23fb1352016-04-11 12:15:19 -070026import org.onosproject.net.Host;
Simon Huntc0f20c12016-05-09 09:30:20 -070027import org.onosproject.net.HostId;
28import org.onosproject.net.HostLocation;
Simon Hunt23fb1352016-04-11 12:15:19 -070029import org.onosproject.net.Link;
30import org.onosproject.net.region.Region;
Simon Huntc0f20c12016-05-09 09:30:20 -070031import org.onosproject.net.region.RegionId;
Simon Hunt642bc452016-05-04 19:34:45 -070032import org.onosproject.ui.model.ServiceBundle;
Simon Hunt338a3b42016-04-14 09:43:52 -070033import org.onosproject.ui.model.topo.UiClusterMember;
Simon Huntcda9c032016-04-11 10:32:54 -070034import org.onosproject.ui.model.topo.UiDevice;
Simon Huntc0f20c12016-05-09 09:30:20 -070035import org.onosproject.ui.model.topo.UiElement;
36import org.onosproject.ui.model.topo.UiHost;
37import org.onosproject.ui.model.topo.UiLink;
38import org.onosproject.ui.model.topo.UiLinkId;
39import org.onosproject.ui.model.topo.UiRegion;
Simon Hunt23fb1352016-04-11 12:15:19 -070040import org.onosproject.ui.model.topo.UiTopology;
Simon Hunt642bc452016-05-04 19:34:45 -070041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
Simon Hunt23fb1352016-04-11 12:15:19 -070043
Simon Huntb1ce2602016-07-23 14:04:31 -070044import java.util.HashSet;
Simon Huntd5b96732016-07-08 13:22:27 -070045import java.util.List;
Simon Huntc0f20c12016-05-09 09:30:20 -070046import java.util.Set;
47
48import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Simon Hunt642bc452016-05-04 19:34:45 -070049import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED;
50import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED;
51import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED;
Simon Hunt23fb1352016-04-11 12:15:19 -070052import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_REMOVED;
Simon Huntc0f20c12016-05-09 09:30:20 -070053import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_ADDED_OR_UPDATED;
54import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_MOVED;
55import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_REMOVED;
56import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_ADDED_OR_UPDATED;
57import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_REMOVED;
58import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_ADDED_OR_UPDATED;
59import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_REMOVED;
60import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId;
Simon Huntcda9c032016-04-11 10:32:54 -070061
62/**
63 * UI Topology Model cache.
64 */
65class ModelCache {
66
Simon Huntc0f20c12016-05-09 09:30:20 -070067 private static final String E_NO_ELEMENT = "Tried to remove non-member {}: {}";
68
Simon Hunt642bc452016-05-04 19:34:45 -070069 private static final Logger log = LoggerFactory.getLogger(ModelCache.class);
70
71 private final ServiceBundle services;
Simon Huntcda9c032016-04-11 10:32:54 -070072 private final EventDispatcher dispatcher;
Simon Hunt23fb1352016-04-11 12:15:19 -070073 private final UiTopology uiTopology = new UiTopology();
Simon Huntcda9c032016-04-11 10:32:54 -070074
Simon Hunt642bc452016-05-04 19:34:45 -070075 ModelCache(ServiceBundle services, EventDispatcher eventDispatcher) {
76 this.services = services;
Simon Huntcda9c032016-04-11 10:32:54 -070077 this.dispatcher = eventDispatcher;
78 }
79
Simon Hunt338a3b42016-04-14 09:43:52 -070080 @Override
81 public String toString() {
82 return "ModelCache{" + uiTopology + "}";
83 }
84
Simon Huntc0f20c12016-05-09 09:30:20 -070085 private void postEvent(UiModelEvent.Type type, UiElement subject) {
86 dispatcher.post(new UiModelEvent(type, subject));
87 }
88
Simon Huntcda9c032016-04-11 10:32:54 -070089 void clear() {
Simon Hunt23fb1352016-04-11 12:15:19 -070090 uiTopology.clear();
Simon Huntcda9c032016-04-11 10:32:54 -070091 }
92
93 /**
Simon Huntc0f20c12016-05-09 09:30:20 -070094 * Create our internal model of the global topology. An assumption we are
95 * making is that the topology is empty to start.
Simon Huntcda9c032016-04-11 10:32:54 -070096 */
97 void load() {
Simon Huntc0f20c12016-05-09 09:30:20 -070098 loadClusterMembers();
99 loadRegions();
100 loadDevices();
101 loadLinks();
102 loadHosts();
Simon Huntcda9c032016-04-11 10:32:54 -0700103 }
104
105
Simon Huntc0f20c12016-05-09 09:30:20 -0700106 // === CLUSTER MEMBERS
107
108 private UiClusterMember addNewClusterMember(ControllerNode n) {
109 UiClusterMember member = new UiClusterMember(uiTopology, n);
110 uiTopology.add(member);
111 return member;
112 }
113
114 private void updateClusterMember(UiClusterMember member) {
115 ControllerNode.State state = services.cluster().getState(member.id());
116 member.setState(state);
117 member.setMastership(services.mastership().getDevicesOf(member.id()));
118 // NOTE: 'UI-attached' is session-based data, not global, so will
119 // be set elsewhere
120 }
121
122 private void loadClusterMembers() {
123 for (ControllerNode n : services.cluster().getNodes()) {
124 UiClusterMember member = addNewClusterMember(n);
125 updateClusterMember(member);
126 }
127 }
128
129 // invoked from UiSharedTopologyModel cluster event listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700130 void addOrUpdateClusterMember(ControllerNode cnode) {
Simon Hunt642bc452016-05-04 19:34:45 -0700131 NodeId id = cnode.id();
132 UiClusterMember member = uiTopology.findClusterMember(id);
133 if (member == null) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700134 member = addNewClusterMember(cnode);
Simon Hunt338a3b42016-04-14 09:43:52 -0700135 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700136 updateClusterMember(member);
Simon Hunt338a3b42016-04-14 09:43:52 -0700137
Simon Huntc0f20c12016-05-09 09:30:20 -0700138 postEvent(CLUSTER_MEMBER_ADDED_OR_UPDATED, member);
Simon Hunt338a3b42016-04-14 09:43:52 -0700139 }
140
Simon Huntc0f20c12016-05-09 09:30:20 -0700141 // package private for unit test access
142 UiClusterMember accessClusterMember(NodeId id) {
143 return uiTopology.findClusterMember(id);
144 }
145
146 // invoked from UiSharedTopologyModel cluster event listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700147 void removeClusterMember(ControllerNode cnode) {
Simon Hunt642bc452016-05-04 19:34:45 -0700148 NodeId id = cnode.id();
149 UiClusterMember member = uiTopology.findClusterMember(id);
150 if (member != null) {
151 uiTopology.remove(member);
Simon Huntc0f20c12016-05-09 09:30:20 -0700152 postEvent(CLUSTER_MEMBER_REMOVED, member);
Simon Hunt642bc452016-05-04 19:34:45 -0700153 } else {
Simon Huntc0f20c12016-05-09 09:30:20 -0700154 log.warn(E_NO_ELEMENT, "cluster node", id);
Simon Hunt642bc452016-05-04 19:34:45 -0700155 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700156 }
157
Simon Huntd5b96732016-07-08 13:22:27 -0700158 List<UiClusterMember> getAllClusterMembers() {
159 return uiTopology.allClusterMembers();
160 }
161
Simon Huntc0f20c12016-05-09 09:30:20 -0700162
163 // === MASTERSHIP CHANGES
164
165 // invoked from UiSharedTopologyModel mastership listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700166 void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700167 // To think about:: do we need to store mastership info?
168 // or can we rely on looking it up live?
Simon Hunt338a3b42016-04-14 09:43:52 -0700169 // TODO: store the updated mastership information
170 // TODO: post event
171 }
172
Simon Huntb1ce2602016-07-23 14:04:31 -0700173 // === THE NULL REGION
174
175 UiRegion nullRegion() {
176 return uiTopology.nullRegion();
177 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700178
179 // === REGIONS
180
181 private UiRegion addNewRegion(Region r) {
182 UiRegion region = new UiRegion(uiTopology, r);
183 uiTopology.add(region);
Simon Huntb1ce2602016-07-23 14:04:31 -0700184 log.debug("Region {} added to topology", region);
Simon Huntc0f20c12016-05-09 09:30:20 -0700185 return region;
186 }
187
188 private void updateRegion(UiRegion region) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700189 RegionId rid = region.id();
190 Set<DeviceId> deviceIds = services.region().getRegionDevices(rid);
191
192 // Make sure device objects refer to their region
193 deviceIds.forEach(d -> {
194 UiDevice dev = uiTopology.findDevice(d);
195 if (dev != null) {
196 dev.setRegionId(rid);
197 } else {
198 // if we don't have the UiDevice in the topology, what can we do?
199 log.warn("Region device {}, but we don't have UiDevice in topology", d);
200 }
201 });
202
203 // Make sure the region object refers to the devices
204 region.reconcileDevices(deviceIds);
Simon Huntc0f20c12016-05-09 09:30:20 -0700205 }
206
207 private void loadRegions() {
208 for (Region r : services.region().getRegions()) {
209 UiRegion region = addNewRegion(r);
210 updateRegion(region);
211 }
212 }
213
214 // invoked from UiSharedTopologyModel region listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700215 void addOrUpdateRegion(Region region) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700216 RegionId id = region.id();
217 UiRegion uiRegion = uiTopology.findRegion(id);
218 if (uiRegion == null) {
219 uiRegion = addNewRegion(region);
220 }
221 updateRegion(uiRegion);
222
223 postEvent(REGION_ADDED_OR_UPDATED, uiRegion);
Simon Hunt338a3b42016-04-14 09:43:52 -0700224 }
225
Simon Hunt58a0dd02016-05-17 11:54:23 -0700226 // package private for unit test access
227 UiRegion accessRegion(RegionId id) {
Simon Huntd5b96732016-07-08 13:22:27 -0700228 return id == null ? null : uiTopology.findRegion(id);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700229 }
230
Simon Huntc0f20c12016-05-09 09:30:20 -0700231 // invoked from UiSharedTopologyModel region listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700232 void removeRegion(Region region) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700233 RegionId id = region.id();
234 UiRegion uiRegion = uiTopology.findRegion(id);
235 if (uiRegion != null) {
236 uiTopology.remove(uiRegion);
237 postEvent(REGION_REMOVED, uiRegion);
238 } else {
239 log.warn(E_NO_ELEMENT, "region", id);
240 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700241 }
242
Simon Huntc0f20c12016-05-09 09:30:20 -0700243
244 // === DEVICES
245
246 private UiDevice addNewDevice(Device d) {
247 UiDevice device = new UiDevice(uiTopology, d);
Simon Huntb1ce2602016-07-23 14:04:31 -0700248 updateDevice(device);
Simon Huntc0f20c12016-05-09 09:30:20 -0700249 uiTopology.add(device);
Simon Huntb1ce2602016-07-23 14:04:31 -0700250 log.debug("Device {} added to topology", device);
Simon Huntc0f20c12016-05-09 09:30:20 -0700251 return device;
252 }
253
Simon Huntb1ce2602016-07-23 14:04:31 -0700254 // make sure the UiDevice is tagged with the region it belongs to
Simon Huntc0f20c12016-05-09 09:30:20 -0700255 private void updateDevice(UiDevice device) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700256 Region r = services.region().getRegionForDevice(device.id());
257 RegionId rid = r == null ? UiRegion.NULL_ID : r.id();
258 device.setRegionId(rid);
Simon Huntc0f20c12016-05-09 09:30:20 -0700259 }
260
261 private void loadDevices() {
262 for (Device d : services.device().getDevices()) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700263 addNewDevice(d);
Simon Huntc0f20c12016-05-09 09:30:20 -0700264 }
265 }
266
267 // invoked from UiSharedTopologyModel device listener
Simon Huntcda9c032016-04-11 10:32:54 -0700268 void addOrUpdateDevice(Device device) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700269 DeviceId id = device.id();
270 UiDevice uiDevice = uiTopology.findDevice(id);
271 if (uiDevice == null) {
272 uiDevice = addNewDevice(device);
Simon Huntb1ce2602016-07-23 14:04:31 -0700273 } else {
274 updateDevice(uiDevice);
Simon Huntc0f20c12016-05-09 09:30:20 -0700275 }
Simon Huntcda9c032016-04-11 10:32:54 -0700276
Simon Huntc0f20c12016-05-09 09:30:20 -0700277 postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice);
Simon Huntcda9c032016-04-11 10:32:54 -0700278 }
279
Simon Hunt58a0dd02016-05-17 11:54:23 -0700280 // package private for unit test access
281 UiDevice accessDevice(DeviceId id) {
282 return uiTopology.findDevice(id);
283 }
284
Simon Huntc0f20c12016-05-09 09:30:20 -0700285 // invoked from UiSharedTopologyModel device listener
Simon Huntcda9c032016-04-11 10:32:54 -0700286 void removeDevice(Device device) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700287 DeviceId id = device.id();
288 UiDevice uiDevice = uiTopology.findDevice(id);
289 if (uiDevice != null) {
290 uiTopology.remove(uiDevice);
291 postEvent(DEVICE_REMOVED, uiDevice);
292 } else {
293 log.warn(E_NO_ELEMENT, "device", id);
294 }
Simon Huntcda9c032016-04-11 10:32:54 -0700295 }
296
Simon Huntc0f20c12016-05-09 09:30:20 -0700297
298 // === LINKS
299
300 private UiLink addNewLink(UiLinkId id) {
301 UiLink uiLink = new UiLink(uiTopology, id);
302 uiTopology.add(uiLink);
303 return uiLink;
304 }
305
306 private void updateLink(UiLink uiLink, Link link) {
307 uiLink.attachBackingLink(link);
308 }
309
310 private void loadLinks() {
311 for (Link link : services.link().getLinks()) {
312 UiLinkId id = uiLinkId(link);
313
314 UiLink uiLink = uiTopology.findLink(id);
315 if (uiLink == null) {
316 uiLink = addNewLink(id);
317 }
318 updateLink(uiLink, link);
319 }
320 }
321
322 // invoked from UiSharedTopologyModel link listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700323 void addOrUpdateLink(Link link) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700324 UiLinkId id = uiLinkId(link);
325 UiLink uiLink = uiTopology.findLink(id);
326 if (uiLink == null) {
327 uiLink = addNewLink(id);
328 }
329 updateLink(uiLink, link);
330
331 postEvent(LINK_ADDED_OR_UPDATED, uiLink);
Simon Hunt23fb1352016-04-11 12:15:19 -0700332 }
333
Simon Hunt58a0dd02016-05-17 11:54:23 -0700334 // package private for unit test access
335 UiLink accessLink(UiLinkId id) {
336 return uiTopology.findLink(id);
337 }
338
Simon Huntc0f20c12016-05-09 09:30:20 -0700339 // invoked from UiSharedTopologyModel link listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700340 void removeLink(Link link) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700341 UiLinkId id = uiLinkId(link);
342 UiLink uiLink = uiTopology.findLink(id);
343 if (uiLink != null) {
344 boolean remaining = uiLink.detachBackingLink(link);
345 if (remaining) {
346 postEvent(LINK_ADDED_OR_UPDATED, uiLink);
347 } else {
348 uiTopology.remove(uiLink);
349 postEvent(LINK_REMOVED, uiLink);
350 }
351 } else {
352 log.warn(E_NO_ELEMENT, "link", id);
353 }
Simon Hunt23fb1352016-04-11 12:15:19 -0700354 }
355
Simon Huntc0f20c12016-05-09 09:30:20 -0700356
357 // === HOSTS
358
Simon Hunt58a0dd02016-05-17 11:54:23 -0700359 private EdgeLink synthesizeLink(Host h) {
360 return createEdgeLink(h, true);
361 }
362
Simon Huntc0f20c12016-05-09 09:30:20 -0700363 private UiHost addNewHost(Host h) {
364 UiHost host = new UiHost(uiTopology, h);
365 uiTopology.add(host);
366
Simon Hunt58a0dd02016-05-17 11:54:23 -0700367 EdgeLink elink = synthesizeLink(h);
368 UiLinkId elinkId = uiLinkId(elink);
369 host.setEdgeLinkId(elinkId);
370
371 // add synthesized edge link to the topology
372 UiLink edgeLink = addNewLink(elinkId);
373 edgeLink.attachEdgeLink(elink);
Simon Huntc0f20c12016-05-09 09:30:20 -0700374
375 return host;
376 }
377
Simon Hunt58a0dd02016-05-17 11:54:23 -0700378 private void insertNewUiLink(UiLinkId id, EdgeLink e) {
379 UiLink newEdgeLink = addNewLink(id);
380 newEdgeLink.attachEdgeLink(e);
Simon Huntc0f20c12016-05-09 09:30:20 -0700381
Simon Huntc0f20c12016-05-09 09:30:20 -0700382 }
383
384 private void updateHost(UiHost uiHost, Host h) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700385 UiLink existing = uiTopology.findLink(uiHost.edgeLinkId());
386
387 EdgeLink currentElink = synthesizeLink(h);
388 UiLinkId currentElinkId = uiLinkId(currentElink);
389
390 if (existing != null) {
391 if (!currentElinkId.equals(existing.id())) {
392 // edge link has changed
393 insertNewUiLink(currentElinkId, currentElink);
394 uiHost.setEdgeLinkId(currentElinkId);
395
396 uiTopology.remove(existing);
397 }
398
399 } else {
400 // no previously existing edge link
401 insertNewUiLink(currentElinkId, currentElink);
402 uiHost.setEdgeLinkId(currentElinkId);
403
404 }
405
Simon Huntc0f20c12016-05-09 09:30:20 -0700406 HostLocation hloc = h.location();
407 uiHost.setLocation(hloc.deviceId(), hloc.port());
Simon Huntc0f20c12016-05-09 09:30:20 -0700408 }
409
410 private void loadHosts() {
411 for (Host h : services.host().getHosts()) {
412 UiHost host = addNewHost(h);
413 updateHost(host, h);
414 }
415 }
416
417 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700418 void addOrUpdateHost(Host host) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700419 HostId id = host.id();
420 UiHost uiHost = uiTopology.findHost(id);
421 if (uiHost == null) {
422 uiHost = addNewHost(host);
423 }
424 updateHost(uiHost, host);
425
426 postEvent(HOST_ADDED_OR_UPDATED, uiHost);
Simon Hunt23fb1352016-04-11 12:15:19 -0700427 }
428
Simon Huntc0f20c12016-05-09 09:30:20 -0700429 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700430 void moveHost(Host host, Host prevHost) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700431 UiHost uiHost = uiTopology.findHost(prevHost.id());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700432 if (uiHost != null) {
433 updateHost(uiHost, host);
434 postEvent(HOST_MOVED, uiHost);
435 } else {
436 log.warn(E_NO_ELEMENT, "host", prevHost.id());
437 }
438 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700439
Simon Hunt58a0dd02016-05-17 11:54:23 -0700440 // package private for unit test access
441 UiHost accessHost(HostId id) {
442 return uiTopology.findHost(id);
Simon Hunt23fb1352016-04-11 12:15:19 -0700443 }
444
Simon Huntc0f20c12016-05-09 09:30:20 -0700445 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700446 void removeHost(Host host) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700447 HostId id = host.id();
448 UiHost uiHost = uiTopology.findHost(id);
449 if (uiHost != null) {
Simon Hunt58a0dd02016-05-17 11:54:23 -0700450 UiLink edgeLink = uiTopology.findLink(uiHost.edgeLinkId());
451 uiTopology.remove(edgeLink);
Simon Huntc0f20c12016-05-09 09:30:20 -0700452 uiTopology.remove(uiHost);
Simon Huntc0f20c12016-05-09 09:30:20 -0700453 postEvent(HOST_REMOVED, uiHost);
454 } else {
455 log.warn(E_NO_ELEMENT, "host", id);
456 }
Simon Hunt23fb1352016-04-11 12:15:19 -0700457 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700458
Simon Huntc0f20c12016-05-09 09:30:20 -0700459
Simon Huntb1ce2602016-07-23 14:04:31 -0700460 /**
461 * Refreshes the internal state.
462 */
463 public void refresh() {
464 // fix up internal linkages if they aren't correct
465
466 // at the moment, this is making sure devices are in the correct region
467 Set<UiDevice> allDevices = uiTopology.allDevices();
468
469 services.region().getRegions().forEach(r -> {
470 RegionId rid = r.id();
471 UiRegion region = uiTopology.findRegion(rid);
472 if (region != null) {
473 Set<DeviceId> deviceIds = services.region().getRegionDevices(rid);
474 region.reconcileDevices(deviceIds);
475
476 deviceIds.forEach(devId -> {
477 UiDevice dev = uiTopology.findDevice(devId);
478 if (dev != null) {
479 dev.setRegionId(r.id());
480 allDevices.remove(dev);
481 } else {
482 log.warn("Region device ID {} but no UiDevice in topology",
483 devId);
484 }
485 });
486 } else {
487 log.warn("No UiRegion in topology for ID {}", rid);
488 }
489 });
490
491 // what is left over, must belong to the null-region
492 Set<DeviceId> leftOver = new HashSet<>(allDevices.size());
493 allDevices.forEach(d -> leftOver.add(d.id()));
494 uiTopology.nullRegion().reconcileDevices(leftOver);
495 }
496
Simon Huntc0f20c12016-05-09 09:30:20 -0700497 // === CACHE STATISTICS
498
Simon Hunt338a3b42016-04-14 09:43:52 -0700499 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700500 * Returns a detailed (multi-line) string showing the contents of the cache.
501 *
502 * @return detailed string
503 */
504 public String dumpString() {
505 return uiTopology.dumpString();
506 }
507
508 /**
Simon Hunt338a3b42016-04-14 09:43:52 -0700509 * Returns the number of members in the cluster.
510 *
511 * @return number of cluster members
512 */
513 public int clusterMemberCount() {
514 return uiTopology.clusterMemberCount();
515 }
516
517 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700518 * Returns the number of regions in the topology.
Simon Hunt338a3b42016-04-14 09:43:52 -0700519 *
520 * @return number of regions
521 */
522 public int regionCount() {
523 return uiTopology.regionCount();
524 }
Simon Hunt58a0dd02016-05-17 11:54:23 -0700525
526 /**
527 * Returns the number of devices in the topology.
528 *
529 * @return number of devices
530 */
531 public int deviceCount() {
532 return uiTopology.deviceCount();
533 }
534
535 /**
536 * Returns the number of links in the topology.
537 *
538 * @return number of links
539 */
540 public int linkCount() {
541 return uiTopology.linkCount();
542 }
543
544 /**
545 * Returns the number of hosts in the topology.
546 *
547 * @return number of hosts
548 */
549 public int hostCount() {
550 return uiTopology.hostCount();
551 }
Simon Huntd5b96732016-07-08 13:22:27 -0700552
Simon Huntcda9c032016-04-11 10:32:54 -0700553}