blob: 139984711e4f937c82a8803da8562be425757326 [file] [log] [blame]
Simon Huntcda9c032016-04-11 10:32:54 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Simon Huntcda9c032016-04-11 10:32:54 -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.impl.topo.model;
18
Simon Hunt8eac4ae2017-01-20 12:56:45 -080019import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt23fb1352016-04-11 12:15:19 -070020import org.onosproject.cluster.ControllerNode;
Simon Hunt642bc452016-05-04 19:34:45 -070021import org.onosproject.cluster.NodeId;
Simon Hunt23fb1352016-04-11 12:15:19 -070022import org.onosproject.cluster.RoleInfo;
Simon Huntcda9c032016-04-11 10:32:54 -070023import org.onosproject.event.EventDispatcher;
24import org.onosproject.net.Device;
Simon Hunt23fb1352016-04-11 12:15:19 -070025import org.onosproject.net.DeviceId;
Simon Huntc0f20c12016-05-09 09:30:20 -070026import org.onosproject.net.EdgeLink;
Simon Hunt23fb1352016-04-11 12:15:19 -070027import org.onosproject.net.Host;
Simon Huntc0f20c12016-05-09 09:30:20 -070028import org.onosproject.net.HostId;
29import org.onosproject.net.HostLocation;
Simon Hunt23fb1352016-04-11 12:15:19 -070030import org.onosproject.net.Link;
31import org.onosproject.net.region.Region;
Simon Huntc0f20c12016-05-09 09:30:20 -070032import org.onosproject.net.region.RegionId;
Simon Hunt4f4ffc32016-08-03 18:30:47 -070033import org.onosproject.ui.UiTopoLayoutService;
Simon Hunt8eac4ae2017-01-20 12:56:45 -080034import org.onosproject.ui.impl.topo.Topo2Jsonifier;
Simon Hunt642bc452016-05-04 19:34:45 -070035import org.onosproject.ui.model.ServiceBundle;
Simon Hunt338a3b42016-04-14 09:43:52 -070036import org.onosproject.ui.model.topo.UiClusterMember;
Simon Huntcda9c032016-04-11 10:32:54 -070037import org.onosproject.ui.model.topo.UiDevice;
Simon Huntc13082f2016-08-03 21:20:23 -070038import org.onosproject.ui.model.topo.UiDeviceLink;
39import org.onosproject.ui.model.topo.UiEdgeLink;
Simon Huntc0f20c12016-05-09 09:30:20 -070040import org.onosproject.ui.model.topo.UiElement;
41import org.onosproject.ui.model.topo.UiHost;
Simon Huntc0f20c12016-05-09 09:30:20 -070042import org.onosproject.ui.model.topo.UiLinkId;
Thomas Vachuska8c0b18a2017-04-14 16:27:33 -070043import org.onosproject.ui.model.topo.UiModelEvent;
Simon Huntc0f20c12016-05-09 09:30:20 -070044import org.onosproject.ui.model.topo.UiRegion;
Simon Huntc13082f2016-08-03 21:20:23 -070045import org.onosproject.ui.model.topo.UiSynthLink;
Simon Hunt4f4ffc32016-08-03 18:30:47 -070046import org.onosproject.ui.model.topo.UiTopoLayout;
47import org.onosproject.ui.model.topo.UiTopoLayoutId;
Simon Hunt23fb1352016-04-11 12:15:19 -070048import org.onosproject.ui.model.topo.UiTopology;
Simon Hunt642bc452016-05-04 19:34:45 -070049import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
Simon Hunt23fb1352016-04-11 12:15:19 -070051
Simon Hunt0e161092017-05-08 17:41:38 -070052import java.util.HashMap;
Simon Huntb1ce2602016-07-23 14:04:31 -070053import java.util.HashSet;
Simon Huntd5b96732016-07-08 13:22:27 -070054import java.util.List;
Simon Hunt0e161092017-05-08 17:41:38 -070055import java.util.Map;
Simon Huntc0f20c12016-05-09 09:30:20 -070056import java.util.Set;
57
58import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Thomas Vachuska8c0b18a2017-04-14 16:27:33 -070059import static org.onosproject.ui.model.topo.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED;
60import static org.onosproject.ui.model.topo.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED;
61import static org.onosproject.ui.model.topo.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED;
62import static org.onosproject.ui.model.topo.UiModelEvent.Type.DEVICE_REMOVED;
63import static org.onosproject.ui.model.topo.UiModelEvent.Type.HOST_ADDED_OR_UPDATED;
64import static org.onosproject.ui.model.topo.UiModelEvent.Type.HOST_MOVED;
65import static org.onosproject.ui.model.topo.UiModelEvent.Type.HOST_REMOVED;
66import static org.onosproject.ui.model.topo.UiModelEvent.Type.LINK_ADDED_OR_UPDATED;
67import static org.onosproject.ui.model.topo.UiModelEvent.Type.LINK_REMOVED;
68import static org.onosproject.ui.model.topo.UiModelEvent.Type.REGION_ADDED_OR_UPDATED;
69import static org.onosproject.ui.model.topo.UiModelEvent.Type.REGION_REMOVED;
Simon Huntc0f20c12016-05-09 09:30:20 -070070import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId;
Simon Huntcda9c032016-04-11 10:32:54 -070071
72/**
73 * UI Topology Model cache.
74 */
75class ModelCache {
76
Simon Huntc0f20c12016-05-09 09:30:20 -070077 private static final String E_NO_ELEMENT = "Tried to remove non-member {}: {}";
Simon Hunt8eac4ae2017-01-20 12:56:45 -080078 private static final String MEMO_ADDED = "added";
79 private static final String MEMO_UPDATED = "updated";
80 private static final String MEMO_REMOVED = "removed";
81 private static final String MEMO_MOVED = "moved";
Simon Huntc0f20c12016-05-09 09:30:20 -070082
Simon Hunt642bc452016-05-04 19:34:45 -070083 private static final Logger log = LoggerFactory.getLogger(ModelCache.class);
84
Simon Huntc4ca7102017-04-08 22:28:04 -070085 // TODO: add NetworkConfigService to service bundle
86// private final NetworkConfigService cfgService =
87// DefaultServiceDirectory.getService(NetworkConfigService.class);
88
Simon Hunt642bc452016-05-04 19:34:45 -070089 private final ServiceBundle services;
Simon Huntcda9c032016-04-11 10:32:54 -070090 private final EventDispatcher dispatcher;
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070091 private final UiTopology uiTopology;
Simon Huntcda9c032016-04-11 10:32:54 -070092
Simon Hunt8eac4ae2017-01-20 12:56:45 -080093 private Topo2Jsonifier t2json;
94
Simon Hunt642bc452016-05-04 19:34:45 -070095 ModelCache(ServiceBundle services, EventDispatcher eventDispatcher) {
96 this.services = services;
Simon Huntcda9c032016-04-11 10:32:54 -070097 this.dispatcher = eventDispatcher;
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070098 uiTopology = new UiTopology(services);
Simon Huntcda9c032016-04-11 10:32:54 -070099 }
100
Simon Hunt338a3b42016-04-14 09:43:52 -0700101 @Override
102 public String toString() {
103 return "ModelCache{" + uiTopology + "}";
104 }
105
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800106 private void postEvent(UiModelEvent.Type type, UiElement subject, String memo) {
107 ObjectNode data = t2json != null ? t2json.jsonUiElement(subject) : null;
108 dispatcher.post(new UiModelEvent(type, subject, data, memo));
109 }
110
111 void injectJsonifier(Topo2Jsonifier t2json) {
112 this.t2json = t2json;
Simon Huntc0f20c12016-05-09 09:30:20 -0700113 }
114
Simon Huntcda9c032016-04-11 10:32:54 -0700115 void clear() {
Simon Hunt23fb1352016-04-11 12:15:19 -0700116 uiTopology.clear();
Simon Huntcda9c032016-04-11 10:32:54 -0700117 }
118
119 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700120 * Create our internal model of the global topology. An assumption we are
121 * making is that the topology is empty to start.
Simon Huntcda9c032016-04-11 10:32:54 -0700122 */
123 void load() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700124 loadClusterMembers();
125 loadRegions();
126 loadDevices();
Simon Huntc13082f2016-08-03 21:20:23 -0700127 loadDeviceLinks();
Simon Huntc0f20c12016-05-09 09:30:20 -0700128 loadHosts();
Simon Huntcda9c032016-04-11 10:32:54 -0700129 }
130
131
Simon Huntc0f20c12016-05-09 09:30:20 -0700132 // === CLUSTER MEMBERS
133
134 private UiClusterMember addNewClusterMember(ControllerNode n) {
135 UiClusterMember member = new UiClusterMember(uiTopology, n);
136 uiTopology.add(member);
137 return member;
138 }
139
140 private void updateClusterMember(UiClusterMember member) {
Simon Hunt708a5b32017-08-01 15:06:04 -0700141 // maybe something to update in the future?
Simon Huntc0f20c12016-05-09 09:30:20 -0700142 }
143
144 private void loadClusterMembers() {
145 for (ControllerNode n : services.cluster().getNodes()) {
146 UiClusterMember member = addNewClusterMember(n);
147 updateClusterMember(member);
148 }
149 }
150
151 // invoked from UiSharedTopologyModel cluster event listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700152 void addOrUpdateClusterMember(ControllerNode cnode) {
Simon Hunt642bc452016-05-04 19:34:45 -0700153 NodeId id = cnode.id();
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800154 String memo = MEMO_UPDATED;
Simon Hunt642bc452016-05-04 19:34:45 -0700155 UiClusterMember member = uiTopology.findClusterMember(id);
156 if (member == null) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700157 member = addNewClusterMember(cnode);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800158 memo = MEMO_ADDED;
Simon Hunt338a3b42016-04-14 09:43:52 -0700159 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700160 updateClusterMember(member);
Simon Hunt338a3b42016-04-14 09:43:52 -0700161
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800162 postEvent(CLUSTER_MEMBER_ADDED_OR_UPDATED, member, memo);
Simon Hunt338a3b42016-04-14 09:43:52 -0700163 }
164
Simon Huntc0f20c12016-05-09 09:30:20 -0700165 // package private for unit test access
166 UiClusterMember accessClusterMember(NodeId id) {
167 return uiTopology.findClusterMember(id);
168 }
169
170 // invoked from UiSharedTopologyModel cluster event listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700171 void removeClusterMember(ControllerNode cnode) {
Simon Hunt642bc452016-05-04 19:34:45 -0700172 NodeId id = cnode.id();
173 UiClusterMember member = uiTopology.findClusterMember(id);
174 if (member != null) {
175 uiTopology.remove(member);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800176 postEvent(CLUSTER_MEMBER_REMOVED, member, MEMO_REMOVED);
Simon Hunt642bc452016-05-04 19:34:45 -0700177 } else {
Simon Huntc0f20c12016-05-09 09:30:20 -0700178 log.warn(E_NO_ELEMENT, "cluster node", id);
Simon Hunt642bc452016-05-04 19:34:45 -0700179 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700180 }
181
Simon Huntd5b96732016-07-08 13:22:27 -0700182 List<UiClusterMember> getAllClusterMembers() {
183 return uiTopology.allClusterMembers();
184 }
185
Simon Huntc0f20c12016-05-09 09:30:20 -0700186
187 // === MASTERSHIP CHANGES
188
189 // invoked from UiSharedTopologyModel mastership listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700190 void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700191 // To think about:: do we need to store mastership info?
192 // or can we rely on looking it up live?
Simon Hunt338a3b42016-04-14 09:43:52 -0700193 // TODO: store the updated mastership information
194 // TODO: post event
195 }
196
Simon Huntb1ce2602016-07-23 14:04:31 -0700197 // === THE NULL REGION
198
199 UiRegion nullRegion() {
200 return uiTopology.nullRegion();
201 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700202
203 // === REGIONS
204
205 private UiRegion addNewRegion(Region r) {
206 UiRegion region = new UiRegion(uiTopology, r);
207 uiTopology.add(region);
Simon Huntb1ce2602016-07-23 14:04:31 -0700208 log.debug("Region {} added to topology", region);
Simon Huntc0f20c12016-05-09 09:30:20 -0700209 return region;
210 }
211
212 private void updateRegion(UiRegion region) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700213 RegionId rid = region.id();
214 Set<DeviceId> deviceIds = services.region().getRegionDevices(rid);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500215 Set<HostId> hostIds = services.region().getRegionHosts(rid);
Simon Huntb1ce2602016-07-23 14:04:31 -0700216
217 // Make sure device objects refer to their region
218 deviceIds.forEach(d -> {
219 UiDevice dev = uiTopology.findDevice(d);
220 if (dev != null) {
221 dev.setRegionId(rid);
222 } else {
223 // if we don't have the UiDevice in the topology, what can we do?
224 log.warn("Region device {}, but we don't have UiDevice in topology", d);
225 }
226 });
227
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500228 hostIds.forEach(d -> {
229 UiHost host = uiTopology.findHost(d);
230 if (host != null) {
231 host.setRegionId(rid);
232 } else {
233 // if we don't have the UiDevice in the topology, what can we do?
234 log.warn("Region host {}, but we don't have UiHost in topology", d);
235 }
236 });
237
Simon Huntb1ce2602016-07-23 14:04:31 -0700238 // Make sure the region object refers to the devices
239 region.reconcileDevices(deviceIds);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500240 region.reconcileHosts(hostIds);
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700241
242 fixupContainmentHierarchy(region);
243 }
244
245 private void fixupContainmentHierarchy(UiRegion region) {
246 UiTopoLayoutService ls = services.layout();
247 RegionId regionId = region.id();
248
249 UiTopoLayout layout = ls.getLayout(regionId);
250 if (layout == null) {
251 // no layout backed by this region
252 log.warn("No layout backed by region {}", regionId);
253 return;
254 }
255
256 UiTopoLayoutId layoutId = layout.id();
257
258 if (!layout.isRoot()) {
259 UiTopoLayoutId parentId = layout.parent();
260 UiTopoLayout parentLayout = ls.getLayout(parentId);
261 RegionId parentRegionId = parentLayout.regionId();
262 region.setParent(parentRegionId);
263 }
264
265 Set<UiTopoLayout> kids = ls.getChildren(layoutId);
266 Set<RegionId> kidRegionIds = new HashSet<>(kids.size());
267 kids.forEach(k -> kidRegionIds.add(k.regionId()));
268 region.setChildren(kidRegionIds);
Simon Huntc0f20c12016-05-09 09:30:20 -0700269 }
270
271 private void loadRegions() {
272 for (Region r : services.region().getRegions()) {
273 UiRegion region = addNewRegion(r);
274 updateRegion(region);
275 }
276 }
277
278 // invoked from UiSharedTopologyModel region listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700279 void addOrUpdateRegion(Region region) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700280 RegionId id = region.id();
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800281 String memo = MEMO_UPDATED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700282 UiRegion uiRegion = uiTopology.findRegion(id);
283 if (uiRegion == null) {
284 uiRegion = addNewRegion(region);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800285 memo = MEMO_ADDED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700286 }
287 updateRegion(uiRegion);
288
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800289 postEvent(REGION_ADDED_OR_UPDATED, uiRegion, memo);
Simon Hunt338a3b42016-04-14 09:43:52 -0700290 }
291
Simon Hunt58a0dd02016-05-17 11:54:23 -0700292 // package private for unit test access
293 UiRegion accessRegion(RegionId id) {
Simon Huntd5b96732016-07-08 13:22:27 -0700294 return id == null ? null : uiTopology.findRegion(id);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700295 }
296
Simon Huntc0f20c12016-05-09 09:30:20 -0700297 // invoked from UiSharedTopologyModel region listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700298 void removeRegion(Region region) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700299 RegionId id = region.id();
300 UiRegion uiRegion = uiTopology.findRegion(id);
301 if (uiRegion != null) {
302 uiTopology.remove(uiRegion);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800303 postEvent(REGION_REMOVED, uiRegion, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700304 } else {
305 log.warn(E_NO_ELEMENT, "region", id);
306 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700307 }
308
Simon Hunt10973dd2016-08-01 15:50:35 -0700309 Set<UiRegion> getAllRegions() {
310 return uiTopology.allRegions();
311 }
312
Simon Huntc0f20c12016-05-09 09:30:20 -0700313
314 // === DEVICES
315
316 private UiDevice addNewDevice(Device d) {
317 UiDevice device = new UiDevice(uiTopology, d);
Simon Huntb1ce2602016-07-23 14:04:31 -0700318 updateDevice(device);
Simon Huntc0f20c12016-05-09 09:30:20 -0700319 uiTopology.add(device);
Simon Huntb1ce2602016-07-23 14:04:31 -0700320 log.debug("Device {} added to topology", device);
Simon Huntc0f20c12016-05-09 09:30:20 -0700321 return device;
322 }
323
Simon Huntb1ce2602016-07-23 14:04:31 -0700324 // make sure the UiDevice is tagged with the region it belongs to
Simon Huntc0f20c12016-05-09 09:30:20 -0700325 private void updateDevice(UiDevice device) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700326 Region r = services.region().getRegionForDevice(device.id());
327 RegionId rid = r == null ? UiRegion.NULL_ID : r.id();
328 device.setRegionId(rid);
Simon Huntc0f20c12016-05-09 09:30:20 -0700329 }
330
331 private void loadDevices() {
332 for (Device d : services.device().getDevices()) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700333 addNewDevice(d);
Simon Huntc0f20c12016-05-09 09:30:20 -0700334 }
335 }
336
337 // invoked from UiSharedTopologyModel device listener
Simon Huntcda9c032016-04-11 10:32:54 -0700338 void addOrUpdateDevice(Device device) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700339 DeviceId id = device.id();
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800340 String memo = MEMO_UPDATED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700341 UiDevice uiDevice = uiTopology.findDevice(id);
342 if (uiDevice == null) {
343 uiDevice = addNewDevice(device);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800344 memo = MEMO_ADDED;
Simon Huntb1ce2602016-07-23 14:04:31 -0700345 } else {
346 updateDevice(uiDevice);
Simon Huntc0f20c12016-05-09 09:30:20 -0700347 }
Simon Huntcda9c032016-04-11 10:32:54 -0700348
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800349 postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice, memo);
Simon Huntcda9c032016-04-11 10:32:54 -0700350 }
351
Simon Hunt58a0dd02016-05-17 11:54:23 -0700352 // package private for unit test access
353 UiDevice accessDevice(DeviceId id) {
354 return uiTopology.findDevice(id);
355 }
356
Simon Huntc0f20c12016-05-09 09:30:20 -0700357 // invoked from UiSharedTopologyModel device listener
Simon Huntcda9c032016-04-11 10:32:54 -0700358 void removeDevice(Device device) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700359 DeviceId id = device.id();
360 UiDevice uiDevice = uiTopology.findDevice(id);
361 if (uiDevice != null) {
362 uiTopology.remove(uiDevice);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800363 postEvent(DEVICE_REMOVED, uiDevice, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700364 } else {
365 log.warn(E_NO_ELEMENT, "device", id);
366 }
Simon Huntcda9c032016-04-11 10:32:54 -0700367 }
368
Simon Hunt4854f3d2016-08-02 18:13:15 -0700369 Set<UiDevice> getAllDevices() {
370 return uiTopology.allDevices();
371 }
372
Simon Huntc0f20c12016-05-09 09:30:20 -0700373
Simon Huntc13082f2016-08-03 21:20:23 -0700374 // === LINKS ===
Simon Huntc0f20c12016-05-09 09:30:20 -0700375
Simon Huntc13082f2016-08-03 21:20:23 -0700376 private UiDeviceLink addNewDeviceLink(UiLinkId id) {
377 UiDeviceLink uiDeviceLink = new UiDeviceLink(uiTopology, id);
378 uiTopology.add(uiDeviceLink);
379 return uiDeviceLink;
Simon Huntc0f20c12016-05-09 09:30:20 -0700380 }
381
Simon Huntc13082f2016-08-03 21:20:23 -0700382 private UiEdgeLink addNewEdgeLink(UiLinkId id) {
383 UiEdgeLink uiEdgeLink = new UiEdgeLink(uiTopology, id);
384 uiTopology.add(uiEdgeLink);
385 return uiEdgeLink;
Simon Huntc0f20c12016-05-09 09:30:20 -0700386 }
387
Simon Huntc13082f2016-08-03 21:20:23 -0700388 private void updateDeviceLink(UiDeviceLink uiDeviceLink, Link link) {
389 uiDeviceLink.attachBackingLink(link);
390 }
391
392 private void loadDeviceLinks() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700393 for (Link link : services.link().getLinks()) {
394 UiLinkId id = uiLinkId(link);
395
Simon Huntc13082f2016-08-03 21:20:23 -0700396 UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id);
397 if (uiDeviceLink == null) {
398 uiDeviceLink = addNewDeviceLink(id);
Simon Huntc0f20c12016-05-09 09:30:20 -0700399 }
Simon Huntc13082f2016-08-03 21:20:23 -0700400 updateDeviceLink(uiDeviceLink, link);
Simon Huntc0f20c12016-05-09 09:30:20 -0700401 }
402 }
403
404 // invoked from UiSharedTopologyModel link listener
Simon Huntc13082f2016-08-03 21:20:23 -0700405 void addOrUpdateDeviceLink(Link link) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700406 UiLinkId id = uiLinkId(link);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800407 String memo = MEMO_UPDATED;
Simon Huntc13082f2016-08-03 21:20:23 -0700408 UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id);
409 if (uiDeviceLink == null) {
410 uiDeviceLink = addNewDeviceLink(id);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800411 memo = MEMO_ADDED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700412 }
Simon Huntc13082f2016-08-03 21:20:23 -0700413 updateDeviceLink(uiDeviceLink, link);
Simon Huntc0f20c12016-05-09 09:30:20 -0700414
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800415 postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, memo);
Simon Hunt23fb1352016-04-11 12:15:19 -0700416 }
417
Simon Hunt58a0dd02016-05-17 11:54:23 -0700418 // package private for unit test access
Simon Huntc13082f2016-08-03 21:20:23 -0700419 UiDeviceLink accessDeviceLink(UiLinkId id) {
420 return uiTopology.findDeviceLink(id);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700421 }
422
Simon Huntc0f20c12016-05-09 09:30:20 -0700423 // invoked from UiSharedTopologyModel link listener
Simon Huntc13082f2016-08-03 21:20:23 -0700424 void removeDeviceLink(Link link) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700425 UiLinkId id = uiLinkId(link);
Simon Huntc13082f2016-08-03 21:20:23 -0700426 UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id);
427 if (uiDeviceLink != null) {
428 boolean remaining = uiDeviceLink.detachBackingLink(link);
Simon Huntc0f20c12016-05-09 09:30:20 -0700429 if (remaining) {
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800430 postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, MEMO_UPDATED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700431 } else {
Simon Huntc13082f2016-08-03 21:20:23 -0700432 uiTopology.remove(uiDeviceLink);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800433 postEvent(LINK_REMOVED, uiDeviceLink, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700434 }
435 } else {
Simon Huntc13082f2016-08-03 21:20:23 -0700436 log.warn(E_NO_ELEMENT, "Device link", id);
Simon Huntc0f20c12016-05-09 09:30:20 -0700437 }
Simon Hunt23fb1352016-04-11 12:15:19 -0700438 }
439
Simon Huntc13082f2016-08-03 21:20:23 -0700440 Set<UiDeviceLink> getAllDeviceLinks() {
441 return uiTopology.allDeviceLinks();
Simon Hunt4854f3d2016-08-02 18:13:15 -0700442 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700443
444 // === HOSTS
445
Simon Hunt58a0dd02016-05-17 11:54:23 -0700446 private EdgeLink synthesizeLink(Host h) {
447 return createEdgeLink(h, true);
448 }
449
Simon Huntc0f20c12016-05-09 09:30:20 -0700450 private UiHost addNewHost(Host h) {
451 UiHost host = new UiHost(uiTopology, h);
452 uiTopology.add(host);
453
Simon Hunt58a0dd02016-05-17 11:54:23 -0700454 EdgeLink elink = synthesizeLink(h);
455 UiLinkId elinkId = uiLinkId(elink);
456 host.setEdgeLinkId(elinkId);
457
458 // add synthesized edge link to the topology
Simon Huntb7fd0802016-10-27 12:21:40 -0700459 addNewEdgeLink(elinkId);
Simon Huntc0f20c12016-05-09 09:30:20 -0700460
461 return host;
462 }
463
Simon Huntb7fd0802016-10-27 12:21:40 -0700464 private void insertNewUiEdgeLink(UiLinkId id) {
465 addNewEdgeLink(id);
Simon Huntc0f20c12016-05-09 09:30:20 -0700466 }
467
468 private void updateHost(UiHost uiHost, Host h) {
Simon Huntc13082f2016-08-03 21:20:23 -0700469 UiEdgeLink existing = uiTopology.findEdgeLink(uiHost.edgeLinkId());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700470
Simon Huntb7fd0802016-10-27 12:21:40 -0700471 // TODO: review - do we need EdgeLink now that we are creating from id only?
Simon Hunt58a0dd02016-05-17 11:54:23 -0700472 EdgeLink currentElink = synthesizeLink(h);
473 UiLinkId currentElinkId = uiLinkId(currentElink);
474
475 if (existing != null) {
476 if (!currentElinkId.equals(existing.id())) {
477 // edge link has changed
Simon Huntb7fd0802016-10-27 12:21:40 -0700478 insertNewUiEdgeLink(currentElinkId);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700479 uiHost.setEdgeLinkId(currentElinkId);
480
481 uiTopology.remove(existing);
482 }
483
484 } else {
485 // no previously existing edge link
Simon Huntb7fd0802016-10-27 12:21:40 -0700486 insertNewUiEdgeLink(currentElinkId);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700487 uiHost.setEdgeLinkId(currentElinkId);
488
489 }
490
Simon Huntc0f20c12016-05-09 09:30:20 -0700491 HostLocation hloc = h.location();
492 uiHost.setLocation(hloc.deviceId(), hloc.port());
Simon Huntc0f20c12016-05-09 09:30:20 -0700493 }
494
495 private void loadHosts() {
496 for (Host h : services.host().getHosts()) {
497 UiHost host = addNewHost(h);
498 updateHost(host, h);
499 }
500 }
501
502 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700503 void addOrUpdateHost(Host host) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700504 HostId id = host.id();
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800505 String memo = MEMO_UPDATED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700506 UiHost uiHost = uiTopology.findHost(id);
507 if (uiHost == null) {
508 uiHost = addNewHost(host);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800509 memo = MEMO_ADDED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700510 }
511 updateHost(uiHost, host);
512
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800513 postEvent(HOST_ADDED_OR_UPDATED, uiHost, memo);
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100514 // Link event must be sent after the host event
515 UiEdgeLink uiEdgeLink = uiTopology.findEdgeLink(uiHost.edgeLinkId());
516 postEvent(LINK_ADDED_OR_UPDATED, uiEdgeLink, memo);
Simon Hunt23fb1352016-04-11 12:15:19 -0700517 }
518
Simon Huntc0f20c12016-05-09 09:30:20 -0700519 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700520 void moveHost(Host host, Host prevHost) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700521 UiHost uiHost = uiTopology.findHost(prevHost.id());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700522 if (uiHost != null) {
523 updateHost(uiHost, host);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800524 postEvent(HOST_MOVED, uiHost, MEMO_MOVED);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700525 } else {
526 log.warn(E_NO_ELEMENT, "host", prevHost.id());
527 }
528 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700529
Simon Hunt58a0dd02016-05-17 11:54:23 -0700530 // package private for unit test access
531 UiHost accessHost(HostId id) {
532 return uiTopology.findHost(id);
Simon Hunt23fb1352016-04-11 12:15:19 -0700533 }
534
Simon Huntc0f20c12016-05-09 09:30:20 -0700535 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700536 void removeHost(Host host) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700537 HostId id = host.id();
538 UiHost uiHost = uiTopology.findHost(id);
539 if (uiHost != null) {
Simon Huntc13082f2016-08-03 21:20:23 -0700540 UiEdgeLink edgeLink = uiTopology.findEdgeLink(uiHost.edgeLinkId());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700541 uiTopology.remove(edgeLink);
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100542 postEvent(LINK_REMOVED, edgeLink, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700543 uiTopology.remove(uiHost);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800544 postEvent(HOST_REMOVED, uiHost, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700545 } else {
546 log.warn(E_NO_ELEMENT, "host", id);
547 }
Simon Hunt23fb1352016-04-11 12:15:19 -0700548 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700549
Simon Hunt4854f3d2016-08-02 18:13:15 -0700550 Set<UiHost> getAllHosts() {
551 return uiTopology.allHosts();
552 }
553
Simon Huntc0f20c12016-05-09 09:30:20 -0700554
Simon Huntc13082f2016-08-03 21:20:23 -0700555 // === SYNTHETIC LINKS
556
557 List<UiSynthLink> getSynthLinks(RegionId regionId) {
558 return uiTopology.findSynthLinks(regionId);
559 }
560
Simon Hunt0e161092017-05-08 17:41:38 -0700561 Map<UiLinkId, UiSynthLink> relevantSynthLinks(RegionId regionId) {
562 Map<UiLinkId, UiSynthLink> result = new HashMap<>();
563 for (UiSynthLink sl : getSynthLinks(regionId)) {
564 result.put(sl.original().id(), sl);
565 }
566 return result;
567 }
568
Simon Huntb1ce2602016-07-23 14:04:31 -0700569 /**
570 * Refreshes the internal state.
571 */
572 public void refresh() {
Simon Huntc13082f2016-08-03 21:20:23 -0700573 // fix up internal linkages to ensure they are correct
Simon Huntb1ce2602016-07-23 14:04:31 -0700574
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700575 // make sure regions reflect layout containment hierarchy
576 fixupContainmentHierarchy(uiTopology.nullRegion());
577 uiTopology.allRegions().forEach(this::fixupContainmentHierarchy);
578
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500579 // make sure devices and hosts are in the correct region
Simon Huntb1ce2602016-07-23 14:04:31 -0700580 Set<UiDevice> allDevices = uiTopology.allDevices();
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500581 Set<UiHost> allHosts = uiTopology.allHosts();
Simon Huntb1ce2602016-07-23 14:04:31 -0700582
583 services.region().getRegions().forEach(r -> {
584 RegionId rid = r.id();
585 UiRegion region = uiTopology.findRegion(rid);
586 if (region != null) {
Steven Burrows8f45ce22016-10-27 20:04:14 -0500587 reconcileDevicesAndHostsWithRegion(allDevices, allHosts, rid, region);
Simon Huntb1ce2602016-07-23 14:04:31 -0700588 } else {
589 log.warn("No UiRegion in topology for ID {}", rid);
590 }
591 });
592
593 // what is left over, must belong to the null-region
594 Set<DeviceId> leftOver = new HashSet<>(allDevices.size());
595 allDevices.forEach(d -> leftOver.add(d.id()));
596 uiTopology.nullRegion().reconcileDevices(leftOver);
Simon Huntc13082f2016-08-03 21:20:23 -0700597
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500598 Set<HostId> leftOverHosts = new HashSet<>(allHosts.size());
599 allHosts.forEach(h -> leftOverHosts.add(h.id()));
600 uiTopology.nullRegion().reconcileHosts(leftOverHosts);
601
Simon Huntc13082f2016-08-03 21:20:23 -0700602 // now that we have correct region hierarchy, and devices are in their
603 // respective regions, we can compute synthetic links for each region.
604 uiTopology.computeSynthLinks();
Simon Huntb1ce2602016-07-23 14:04:31 -0700605 }
606
Steven Burrows8f45ce22016-10-27 20:04:14 -0500607 private void reconcileDevicesAndHostsWithRegion(Set<UiDevice> allDevices,
608 Set<UiHost> allHosts,
609 RegionId rid,
610 UiRegion region) {
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500611 Set<DeviceId> deviceIds = services.region().getRegionDevices(rid);
Steven Burrows8f45ce22016-10-27 20:04:14 -0500612 Set<HostId> hostIds = new HashSet<>();
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500613 region.reconcileDevices(deviceIds);
614
615 deviceIds.forEach(devId -> {
616 UiDevice dev = uiTopology.findDevice(devId);
617 if (dev != null) {
Steven Burrows8f45ce22016-10-27 20:04:14 -0500618 dev.setRegionId(rid);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500619 allDevices.remove(dev);
620 } else {
621 log.warn("Region device ID {} but no UiDevice in topology",
Simon Huntc4ca7102017-04-08 22:28:04 -0700622 devId);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500623 }
Steven Burrows8f45ce22016-10-27 20:04:14 -0500624
625 Set<Host> hosts = services.host().getConnectedHosts(devId);
626 for (Host h : hosts) {
627 HostId hid = h.id();
628 hostIds.add(hid);
629 UiHost host = uiTopology.findHost(hid);
630
631 if (host != null) {
632 host.setRegionId(rid);
633 allHosts.remove(host);
634 } else {
635 log.warn("Region host ID {} but no UiHost in topology",
Simon Huntc4ca7102017-04-08 22:28:04 -0700636 hid);
Steven Burrows8f45ce22016-10-27 20:04:14 -0500637 }
638 }
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500639 });
Steven Burrows8f45ce22016-10-27 20:04:14 -0500640
641 region.reconcileHosts(hostIds);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500642 }
643
Simon Huntc13082f2016-08-03 21:20:23 -0700644
Simon Huntc0f20c12016-05-09 09:30:20 -0700645 // === CACHE STATISTICS
646
Simon Hunt338a3b42016-04-14 09:43:52 -0700647 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700648 * Returns a detailed (multi-line) string showing the contents of the cache.
649 *
650 * @return detailed string
651 */
652 public String dumpString() {
653 return uiTopology.dumpString();
654 }
655
656 /**
Simon Hunt338a3b42016-04-14 09:43:52 -0700657 * Returns the number of members in the cluster.
658 *
659 * @return number of cluster members
660 */
661 public int clusterMemberCount() {
662 return uiTopology.clusterMemberCount();
663 }
664
665 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700666 * Returns the number of regions in the topology.
Simon Hunt338a3b42016-04-14 09:43:52 -0700667 *
668 * @return number of regions
669 */
670 public int regionCount() {
671 return uiTopology.regionCount();
672 }
Simon Hunt58a0dd02016-05-17 11:54:23 -0700673
674 /**
675 * Returns the number of devices in the topology.
676 *
677 * @return number of devices
678 */
679 public int deviceCount() {
680 return uiTopology.deviceCount();
681 }
682
683 /**
Simon Huntc13082f2016-08-03 21:20:23 -0700684 * Returns the number of device links in the topology.
Simon Hunt58a0dd02016-05-17 11:54:23 -0700685 *
Simon Huntc13082f2016-08-03 21:20:23 -0700686 * @return number of device links
Simon Hunt58a0dd02016-05-17 11:54:23 -0700687 */
Simon Huntc13082f2016-08-03 21:20:23 -0700688 public int deviceLinkCount() {
689 return uiTopology.deviceLinkCount();
690 }
691
692 /**
693 * Returns the number of edge links in the topology.
694 *
695 * @return number of edge links
696 */
697 public int edgeLinkCount() {
698 return uiTopology.edgeLinkCount();
Simon Hunt58a0dd02016-05-17 11:54:23 -0700699 }
700
701 /**
702 * Returns the number of hosts in the topology.
703 *
704 * @return number of hosts
705 */
706 public int hostCount() {
707 return uiTopology.hostCount();
708 }
Simon Huntd5b96732016-07-08 13:22:27 -0700709
Simon Huntc13082f2016-08-03 21:20:23 -0700710 /**
711 * Returns the number of synthetic links in the topology.
712 *
713 * @return the number of synthetic links
714 */
715 public int synthLinkCount() {
716 return uiTopology.synthLinkCount();
717 }
Simon Huntcda9c032016-04-11 10:32:54 -0700718}