blob: cfe3c7aeec88d40b10af4b97cb1a6e0b076f6765 [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
Sean Condon058804c2019-04-16 09:41:52 +0100194 UiDevice uiDevice = uiTopology.findDevice(deviceId);
Sean Condonbed2e032019-04-17 22:22:49 +0100195 if (uiDevice != null) {
196 postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice, MEMO_UPDATED);
197 } else {
198 this.log.warn("DeviceID {} not found as a UiDevice", deviceId);
199 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700200 }
201
Simon Huntb1ce2602016-07-23 14:04:31 -0700202 // === THE NULL REGION
203
204 UiRegion nullRegion() {
205 return uiTopology.nullRegion();
206 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700207
208 // === REGIONS
209
210 private UiRegion addNewRegion(Region r) {
211 UiRegion region = new UiRegion(uiTopology, r);
212 uiTopology.add(region);
Simon Huntb1ce2602016-07-23 14:04:31 -0700213 log.debug("Region {} added to topology", region);
Simon Huntc0f20c12016-05-09 09:30:20 -0700214 return region;
215 }
216
217 private void updateRegion(UiRegion region) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700218 RegionId rid = region.id();
219 Set<DeviceId> deviceIds = services.region().getRegionDevices(rid);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500220 Set<HostId> hostIds = services.region().getRegionHosts(rid);
Simon Huntb1ce2602016-07-23 14:04:31 -0700221
222 // Make sure device objects refer to their region
223 deviceIds.forEach(d -> {
224 UiDevice dev = uiTopology.findDevice(d);
225 if (dev != null) {
226 dev.setRegionId(rid);
227 } else {
228 // if we don't have the UiDevice in the topology, what can we do?
229 log.warn("Region device {}, but we don't have UiDevice in topology", d);
230 }
231 });
232
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500233 hostIds.forEach(d -> {
234 UiHost host = uiTopology.findHost(d);
235 if (host != null) {
236 host.setRegionId(rid);
237 } else {
238 // if we don't have the UiDevice in the topology, what can we do?
239 log.warn("Region host {}, but we don't have UiHost in topology", d);
240 }
241 });
242
Simon Huntb1ce2602016-07-23 14:04:31 -0700243 // Make sure the region object refers to the devices
244 region.reconcileDevices(deviceIds);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500245 region.reconcileHosts(hostIds);
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700246
247 fixupContainmentHierarchy(region);
248 }
249
250 private void fixupContainmentHierarchy(UiRegion region) {
251 UiTopoLayoutService ls = services.layout();
252 RegionId regionId = region.id();
253
254 UiTopoLayout layout = ls.getLayout(regionId);
255 if (layout == null) {
256 // no layout backed by this region
257 log.warn("No layout backed by region {}", regionId);
258 return;
259 }
260
261 UiTopoLayoutId layoutId = layout.id();
262
263 if (!layout.isRoot()) {
264 UiTopoLayoutId parentId = layout.parent();
265 UiTopoLayout parentLayout = ls.getLayout(parentId);
266 RegionId parentRegionId = parentLayout.regionId();
267 region.setParent(parentRegionId);
268 }
269
270 Set<UiTopoLayout> kids = ls.getChildren(layoutId);
271 Set<RegionId> kidRegionIds = new HashSet<>(kids.size());
272 kids.forEach(k -> kidRegionIds.add(k.regionId()));
273 region.setChildren(kidRegionIds);
Simon Huntc0f20c12016-05-09 09:30:20 -0700274 }
275
276 private void loadRegions() {
277 for (Region r : services.region().getRegions()) {
278 UiRegion region = addNewRegion(r);
279 updateRegion(region);
280 }
281 }
282
283 // invoked from UiSharedTopologyModel region listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700284 void addOrUpdateRegion(Region region) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700285 RegionId id = region.id();
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800286 String memo = MEMO_UPDATED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700287 UiRegion uiRegion = uiTopology.findRegion(id);
288 if (uiRegion == null) {
289 uiRegion = addNewRegion(region);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800290 memo = MEMO_ADDED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700291 }
292 updateRegion(uiRegion);
293
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800294 postEvent(REGION_ADDED_OR_UPDATED, uiRegion, memo);
Simon Hunt338a3b42016-04-14 09:43:52 -0700295 }
296
Simon Hunt58a0dd02016-05-17 11:54:23 -0700297 // package private for unit test access
298 UiRegion accessRegion(RegionId id) {
Simon Huntd5b96732016-07-08 13:22:27 -0700299 return id == null ? null : uiTopology.findRegion(id);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700300 }
301
Simon Huntc0f20c12016-05-09 09:30:20 -0700302 // invoked from UiSharedTopologyModel region listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700303 void removeRegion(Region region) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700304 RegionId id = region.id();
305 UiRegion uiRegion = uiTopology.findRegion(id);
306 if (uiRegion != null) {
307 uiTopology.remove(uiRegion);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800308 postEvent(REGION_REMOVED, uiRegion, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700309 } else {
310 log.warn(E_NO_ELEMENT, "region", id);
311 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700312 }
313
Simon Hunt10973dd2016-08-01 15:50:35 -0700314 Set<UiRegion> getAllRegions() {
315 return uiTopology.allRegions();
316 }
317
Simon Huntc0f20c12016-05-09 09:30:20 -0700318
319 // === DEVICES
320
321 private UiDevice addNewDevice(Device d) {
322 UiDevice device = new UiDevice(uiTopology, d);
Simon Huntb1ce2602016-07-23 14:04:31 -0700323 updateDevice(device);
Simon Huntc0f20c12016-05-09 09:30:20 -0700324 uiTopology.add(device);
Simon Huntb1ce2602016-07-23 14:04:31 -0700325 log.debug("Device {} added to topology", device);
Simon Huntc0f20c12016-05-09 09:30:20 -0700326 return device;
327 }
328
Simon Huntb1ce2602016-07-23 14:04:31 -0700329 // make sure the UiDevice is tagged with the region it belongs to
Simon Huntc0f20c12016-05-09 09:30:20 -0700330 private void updateDevice(UiDevice device) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700331 Region r = services.region().getRegionForDevice(device.id());
332 RegionId rid = r == null ? UiRegion.NULL_ID : r.id();
333 device.setRegionId(rid);
Simon Huntc0f20c12016-05-09 09:30:20 -0700334 }
335
336 private void loadDevices() {
337 for (Device d : services.device().getDevices()) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700338 addNewDevice(d);
Simon Huntc0f20c12016-05-09 09:30:20 -0700339 }
340 }
341
342 // invoked from UiSharedTopologyModel device listener
Simon Huntcda9c032016-04-11 10:32:54 -0700343 void addOrUpdateDevice(Device device) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700344 DeviceId id = device.id();
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800345 String memo = MEMO_UPDATED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700346 UiDevice uiDevice = uiTopology.findDevice(id);
347 if (uiDevice == null) {
348 uiDevice = addNewDevice(device);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800349 memo = MEMO_ADDED;
Simon Huntb1ce2602016-07-23 14:04:31 -0700350 } else {
351 updateDevice(uiDevice);
Simon Huntc0f20c12016-05-09 09:30:20 -0700352 }
Simon Huntcda9c032016-04-11 10:32:54 -0700353
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800354 postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice, memo);
Simon Huntcda9c032016-04-11 10:32:54 -0700355 }
356
Simon Hunt58a0dd02016-05-17 11:54:23 -0700357 // package private for unit test access
358 UiDevice accessDevice(DeviceId id) {
359 return uiTopology.findDevice(id);
360 }
361
Simon Huntc0f20c12016-05-09 09:30:20 -0700362 // invoked from UiSharedTopologyModel device listener
Simon Huntcda9c032016-04-11 10:32:54 -0700363 void removeDevice(Device device) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700364 DeviceId id = device.id();
365 UiDevice uiDevice = uiTopology.findDevice(id);
366 if (uiDevice != null) {
367 uiTopology.remove(uiDevice);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800368 postEvent(DEVICE_REMOVED, uiDevice, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700369 } else {
370 log.warn(E_NO_ELEMENT, "device", id);
371 }
Simon Huntcda9c032016-04-11 10:32:54 -0700372 }
373
Simon Hunt4854f3d2016-08-02 18:13:15 -0700374 Set<UiDevice> getAllDevices() {
375 return uiTopology.allDevices();
376 }
377
Simon Huntc0f20c12016-05-09 09:30:20 -0700378
Simon Huntc13082f2016-08-03 21:20:23 -0700379 // === LINKS ===
Simon Huntc0f20c12016-05-09 09:30:20 -0700380
Simon Huntc13082f2016-08-03 21:20:23 -0700381 private UiDeviceLink addNewDeviceLink(UiLinkId id) {
382 UiDeviceLink uiDeviceLink = new UiDeviceLink(uiTopology, id);
383 uiTopology.add(uiDeviceLink);
384 return uiDeviceLink;
Simon Huntc0f20c12016-05-09 09:30:20 -0700385 }
386
Simon Huntc13082f2016-08-03 21:20:23 -0700387 private UiEdgeLink addNewEdgeLink(UiLinkId id) {
388 UiEdgeLink uiEdgeLink = new UiEdgeLink(uiTopology, id);
389 uiTopology.add(uiEdgeLink);
390 return uiEdgeLink;
Simon Huntc0f20c12016-05-09 09:30:20 -0700391 }
392
Simon Huntc13082f2016-08-03 21:20:23 -0700393 private void updateDeviceLink(UiDeviceLink uiDeviceLink, Link link) {
394 uiDeviceLink.attachBackingLink(link);
395 }
396
397 private void loadDeviceLinks() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700398 for (Link link : services.link().getLinks()) {
399 UiLinkId id = uiLinkId(link);
400
Simon Huntc13082f2016-08-03 21:20:23 -0700401 UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id);
402 if (uiDeviceLink == null) {
403 uiDeviceLink = addNewDeviceLink(id);
Simon Huntc0f20c12016-05-09 09:30:20 -0700404 }
Simon Huntc13082f2016-08-03 21:20:23 -0700405 updateDeviceLink(uiDeviceLink, link);
Simon Huntc0f20c12016-05-09 09:30:20 -0700406 }
407 }
408
409 // invoked from UiSharedTopologyModel link listener
Simon Huntc13082f2016-08-03 21:20:23 -0700410 void addOrUpdateDeviceLink(Link link) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700411 UiLinkId id = uiLinkId(link);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800412 String memo = MEMO_UPDATED;
Simon Huntc13082f2016-08-03 21:20:23 -0700413 UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id);
414 if (uiDeviceLink == null) {
415 uiDeviceLink = addNewDeviceLink(id);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800416 memo = MEMO_ADDED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700417 }
Simon Huntc13082f2016-08-03 21:20:23 -0700418 updateDeviceLink(uiDeviceLink, link);
Simon Huntc0f20c12016-05-09 09:30:20 -0700419
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800420 postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, memo);
Simon Hunt23fb1352016-04-11 12:15:19 -0700421 }
422
Simon Hunt58a0dd02016-05-17 11:54:23 -0700423 // package private for unit test access
Simon Huntc13082f2016-08-03 21:20:23 -0700424 UiDeviceLink accessDeviceLink(UiLinkId id) {
425 return uiTopology.findDeviceLink(id);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700426 }
427
Simon Huntc0f20c12016-05-09 09:30:20 -0700428 // invoked from UiSharedTopologyModel link listener
Simon Huntc13082f2016-08-03 21:20:23 -0700429 void removeDeviceLink(Link link) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700430 UiLinkId id = uiLinkId(link);
Simon Huntc13082f2016-08-03 21:20:23 -0700431 UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id);
432 if (uiDeviceLink != null) {
433 boolean remaining = uiDeviceLink.detachBackingLink(link);
Simon Huntc0f20c12016-05-09 09:30:20 -0700434 if (remaining) {
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800435 postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, MEMO_UPDATED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700436 } else {
Simon Huntc13082f2016-08-03 21:20:23 -0700437 uiTopology.remove(uiDeviceLink);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800438 postEvent(LINK_REMOVED, uiDeviceLink, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700439 }
440 } else {
Simon Huntc13082f2016-08-03 21:20:23 -0700441 log.warn(E_NO_ELEMENT, "Device link", id);
Simon Huntc0f20c12016-05-09 09:30:20 -0700442 }
Simon Hunt23fb1352016-04-11 12:15:19 -0700443 }
444
Simon Huntc13082f2016-08-03 21:20:23 -0700445 Set<UiDeviceLink> getAllDeviceLinks() {
446 return uiTopology.allDeviceLinks();
Simon Hunt4854f3d2016-08-02 18:13:15 -0700447 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700448
449 // === HOSTS
450
Simon Hunt58a0dd02016-05-17 11:54:23 -0700451 private EdgeLink synthesizeLink(Host h) {
452 return createEdgeLink(h, true);
453 }
454
Simon Huntc0f20c12016-05-09 09:30:20 -0700455 private UiHost addNewHost(Host h) {
456 UiHost host = new UiHost(uiTopology, h);
457 uiTopology.add(host);
458
Simon Hunt58a0dd02016-05-17 11:54:23 -0700459 EdgeLink elink = synthesizeLink(h);
460 UiLinkId elinkId = uiLinkId(elink);
461 host.setEdgeLinkId(elinkId);
462
463 // add synthesized edge link to the topology
Simon Huntb7fd0802016-10-27 12:21:40 -0700464 addNewEdgeLink(elinkId);
Simon Huntc0f20c12016-05-09 09:30:20 -0700465
466 return host;
467 }
468
Simon Huntb7fd0802016-10-27 12:21:40 -0700469 private void insertNewUiEdgeLink(UiLinkId id) {
470 addNewEdgeLink(id);
Simon Huntc0f20c12016-05-09 09:30:20 -0700471 }
472
473 private void updateHost(UiHost uiHost, Host h) {
Simon Huntc13082f2016-08-03 21:20:23 -0700474 UiEdgeLink existing = uiTopology.findEdgeLink(uiHost.edgeLinkId());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700475
Simon Huntb7fd0802016-10-27 12:21:40 -0700476 // TODO: review - do we need EdgeLink now that we are creating from id only?
Simon Hunt58a0dd02016-05-17 11:54:23 -0700477 EdgeLink currentElink = synthesizeLink(h);
478 UiLinkId currentElinkId = uiLinkId(currentElink);
479
480 if (existing != null) {
481 if (!currentElinkId.equals(existing.id())) {
482 // edge link has changed
Simon Huntb7fd0802016-10-27 12:21:40 -0700483 insertNewUiEdgeLink(currentElinkId);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700484 uiHost.setEdgeLinkId(currentElinkId);
485
486 uiTopology.remove(existing);
487 }
488
489 } else {
490 // no previously existing edge link
Simon Huntb7fd0802016-10-27 12:21:40 -0700491 insertNewUiEdgeLink(currentElinkId);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700492 uiHost.setEdgeLinkId(currentElinkId);
493
494 }
495
Simon Huntc0f20c12016-05-09 09:30:20 -0700496 HostLocation hloc = h.location();
497 uiHost.setLocation(hloc.deviceId(), hloc.port());
Simon Huntc0f20c12016-05-09 09:30:20 -0700498 }
499
500 private void loadHosts() {
501 for (Host h : services.host().getHosts()) {
502 UiHost host = addNewHost(h);
503 updateHost(host, h);
504 }
505 }
506
507 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700508 void addOrUpdateHost(Host host) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700509 HostId id = host.id();
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800510 String memo = MEMO_UPDATED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700511 UiHost uiHost = uiTopology.findHost(id);
512 if (uiHost == null) {
513 uiHost = addNewHost(host);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800514 memo = MEMO_ADDED;
Simon Huntc0f20c12016-05-09 09:30:20 -0700515 }
516 updateHost(uiHost, host);
517
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800518 postEvent(HOST_ADDED_OR_UPDATED, uiHost, memo);
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100519 // Link event must be sent after the host event
520 UiEdgeLink uiEdgeLink = uiTopology.findEdgeLink(uiHost.edgeLinkId());
521 postEvent(LINK_ADDED_OR_UPDATED, uiEdgeLink, memo);
Simon Hunt23fb1352016-04-11 12:15:19 -0700522 }
523
Simon Huntc0f20c12016-05-09 09:30:20 -0700524 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700525 void moveHost(Host host, Host prevHost) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700526 UiHost uiHost = uiTopology.findHost(prevHost.id());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700527 if (uiHost != null) {
528 updateHost(uiHost, host);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800529 postEvent(HOST_MOVED, uiHost, MEMO_MOVED);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700530 } else {
531 log.warn(E_NO_ELEMENT, "host", prevHost.id());
532 }
533 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700534
Simon Hunt58a0dd02016-05-17 11:54:23 -0700535 // package private for unit test access
536 UiHost accessHost(HostId id) {
537 return uiTopology.findHost(id);
Simon Hunt23fb1352016-04-11 12:15:19 -0700538 }
539
Simon Huntc0f20c12016-05-09 09:30:20 -0700540 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700541 void removeHost(Host host) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700542 HostId id = host.id();
543 UiHost uiHost = uiTopology.findHost(id);
544 if (uiHost != null) {
Simon Huntc13082f2016-08-03 21:20:23 -0700545 UiEdgeLink edgeLink = uiTopology.findEdgeLink(uiHost.edgeLinkId());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700546 uiTopology.remove(edgeLink);
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100547 postEvent(LINK_REMOVED, edgeLink, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700548 uiTopology.remove(uiHost);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800549 postEvent(HOST_REMOVED, uiHost, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700550 } else {
551 log.warn(E_NO_ELEMENT, "host", id);
552 }
Simon Hunt23fb1352016-04-11 12:15:19 -0700553 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700554
Simon Hunt4854f3d2016-08-02 18:13:15 -0700555 Set<UiHost> getAllHosts() {
556 return uiTopology.allHosts();
557 }
558
Simon Huntc0f20c12016-05-09 09:30:20 -0700559
Simon Huntc13082f2016-08-03 21:20:23 -0700560 // === SYNTHETIC LINKS
561
562 List<UiSynthLink> getSynthLinks(RegionId regionId) {
563 return uiTopology.findSynthLinks(regionId);
564 }
565
Simon Hunt0e161092017-05-08 17:41:38 -0700566 Map<UiLinkId, UiSynthLink> relevantSynthLinks(RegionId regionId) {
567 Map<UiLinkId, UiSynthLink> result = new HashMap<>();
568 for (UiSynthLink sl : getSynthLinks(regionId)) {
569 result.put(sl.original().id(), sl);
570 }
571 return result;
572 }
573
Simon Huntb1ce2602016-07-23 14:04:31 -0700574 /**
575 * Refreshes the internal state.
576 */
577 public void refresh() {
Simon Huntc13082f2016-08-03 21:20:23 -0700578 // fix up internal linkages to ensure they are correct
Simon Huntb1ce2602016-07-23 14:04:31 -0700579
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700580 // make sure regions reflect layout containment hierarchy
581 fixupContainmentHierarchy(uiTopology.nullRegion());
582 uiTopology.allRegions().forEach(this::fixupContainmentHierarchy);
583
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500584 // make sure devices and hosts are in the correct region
Simon Huntb1ce2602016-07-23 14:04:31 -0700585 Set<UiDevice> allDevices = uiTopology.allDevices();
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500586 Set<UiHost> allHosts = uiTopology.allHosts();
Simon Huntb1ce2602016-07-23 14:04:31 -0700587
588 services.region().getRegions().forEach(r -> {
589 RegionId rid = r.id();
590 UiRegion region = uiTopology.findRegion(rid);
591 if (region != null) {
Steven Burrows8f45ce22016-10-27 20:04:14 -0500592 reconcileDevicesAndHostsWithRegion(allDevices, allHosts, rid, region);
Simon Huntb1ce2602016-07-23 14:04:31 -0700593 } else {
594 log.warn("No UiRegion in topology for ID {}", rid);
595 }
596 });
597
598 // what is left over, must belong to the null-region
599 Set<DeviceId> leftOver = new HashSet<>(allDevices.size());
600 allDevices.forEach(d -> leftOver.add(d.id()));
601 uiTopology.nullRegion().reconcileDevices(leftOver);
Simon Huntc13082f2016-08-03 21:20:23 -0700602
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500603 Set<HostId> leftOverHosts = new HashSet<>(allHosts.size());
604 allHosts.forEach(h -> leftOverHosts.add(h.id()));
605 uiTopology.nullRegion().reconcileHosts(leftOverHosts);
606
Simon Huntc13082f2016-08-03 21:20:23 -0700607 // now that we have correct region hierarchy, and devices are in their
608 // respective regions, we can compute synthetic links for each region.
609 uiTopology.computeSynthLinks();
Simon Huntb1ce2602016-07-23 14:04:31 -0700610 }
611
Steven Burrows8f45ce22016-10-27 20:04:14 -0500612 private void reconcileDevicesAndHostsWithRegion(Set<UiDevice> allDevices,
613 Set<UiHost> allHosts,
614 RegionId rid,
615 UiRegion region) {
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500616 Set<DeviceId> deviceIds = services.region().getRegionDevices(rid);
Steven Burrows8f45ce22016-10-27 20:04:14 -0500617 Set<HostId> hostIds = new HashSet<>();
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500618 region.reconcileDevices(deviceIds);
619
620 deviceIds.forEach(devId -> {
621 UiDevice dev = uiTopology.findDevice(devId);
622 if (dev != null) {
Steven Burrows8f45ce22016-10-27 20:04:14 -0500623 dev.setRegionId(rid);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500624 allDevices.remove(dev);
625 } else {
626 log.warn("Region device ID {} but no UiDevice in topology",
Simon Huntc4ca7102017-04-08 22:28:04 -0700627 devId);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500628 }
Steven Burrows8f45ce22016-10-27 20:04:14 -0500629
630 Set<Host> hosts = services.host().getConnectedHosts(devId);
631 for (Host h : hosts) {
632 HostId hid = h.id();
633 hostIds.add(hid);
634 UiHost host = uiTopology.findHost(hid);
635
636 if (host != null) {
637 host.setRegionId(rid);
638 allHosts.remove(host);
639 } else {
640 log.warn("Region host ID {} but no UiHost in topology",
Simon Huntc4ca7102017-04-08 22:28:04 -0700641 hid);
Steven Burrows8f45ce22016-10-27 20:04:14 -0500642 }
643 }
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500644 });
Steven Burrows8f45ce22016-10-27 20:04:14 -0500645
646 region.reconcileHosts(hostIds);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500647 }
648
Simon Huntc13082f2016-08-03 21:20:23 -0700649
Simon Huntc0f20c12016-05-09 09:30:20 -0700650 // === CACHE STATISTICS
651
Simon Hunt338a3b42016-04-14 09:43:52 -0700652 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700653 * Returns a detailed (multi-line) string showing the contents of the cache.
654 *
655 * @return detailed string
656 */
657 public String dumpString() {
658 return uiTopology.dumpString();
659 }
660
661 /**
Simon Hunt338a3b42016-04-14 09:43:52 -0700662 * Returns the number of members in the cluster.
663 *
664 * @return number of cluster members
665 */
666 public int clusterMemberCount() {
667 return uiTopology.clusterMemberCount();
668 }
669
670 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700671 * Returns the number of regions in the topology.
Simon Hunt338a3b42016-04-14 09:43:52 -0700672 *
673 * @return number of regions
674 */
675 public int regionCount() {
676 return uiTopology.regionCount();
677 }
Simon Hunt58a0dd02016-05-17 11:54:23 -0700678
679 /**
680 * Returns the number of devices in the topology.
681 *
682 * @return number of devices
683 */
684 public int deviceCount() {
685 return uiTopology.deviceCount();
686 }
687
688 /**
Simon Huntc13082f2016-08-03 21:20:23 -0700689 * Returns the number of device links in the topology.
Simon Hunt58a0dd02016-05-17 11:54:23 -0700690 *
Simon Huntc13082f2016-08-03 21:20:23 -0700691 * @return number of device links
Simon Hunt58a0dd02016-05-17 11:54:23 -0700692 */
Simon Huntc13082f2016-08-03 21:20:23 -0700693 public int deviceLinkCount() {
694 return uiTopology.deviceLinkCount();
695 }
696
697 /**
698 * Returns the number of edge links in the topology.
699 *
700 * @return number of edge links
701 */
702 public int edgeLinkCount() {
703 return uiTopology.edgeLinkCount();
Simon Hunt58a0dd02016-05-17 11:54:23 -0700704 }
705
706 /**
707 * Returns the number of hosts in the topology.
708 *
709 * @return number of hosts
710 */
711 public int hostCount() {
712 return uiTopology.hostCount();
713 }
Simon Huntd5b96732016-07-08 13:22:27 -0700714
Simon Huntc13082f2016-08-03 21:20:23 -0700715 /**
716 * Returns the number of synthetic links in the topology.
717 *
718 * @return the number of synthetic links
719 */
720 public int synthLinkCount() {
721 return uiTopology.synthLinkCount();
722 }
Simon Huntcda9c032016-04-11 10:32:54 -0700723}