blob: 41e609069c11aae87e5945947864b89badf4cf43 [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);
Simon Hunt23fb1352016-04-11 12:15:19 -0700514 }
515
Simon Huntc0f20c12016-05-09 09:30:20 -0700516 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700517 void moveHost(Host host, Host prevHost) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700518 UiHost uiHost = uiTopology.findHost(prevHost.id());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700519 if (uiHost != null) {
520 updateHost(uiHost, host);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800521 postEvent(HOST_MOVED, uiHost, MEMO_MOVED);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700522 } else {
523 log.warn(E_NO_ELEMENT, "host", prevHost.id());
524 }
525 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700526
Simon Hunt58a0dd02016-05-17 11:54:23 -0700527 // package private for unit test access
528 UiHost accessHost(HostId id) {
529 return uiTopology.findHost(id);
Simon Hunt23fb1352016-04-11 12:15:19 -0700530 }
531
Simon Huntc0f20c12016-05-09 09:30:20 -0700532 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700533 void removeHost(Host host) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700534 HostId id = host.id();
535 UiHost uiHost = uiTopology.findHost(id);
536 if (uiHost != null) {
Simon Huntc13082f2016-08-03 21:20:23 -0700537 UiEdgeLink edgeLink = uiTopology.findEdgeLink(uiHost.edgeLinkId());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700538 uiTopology.remove(edgeLink);
Simon Huntc0f20c12016-05-09 09:30:20 -0700539 uiTopology.remove(uiHost);
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800540 postEvent(HOST_REMOVED, uiHost, MEMO_REMOVED);
Simon Huntc0f20c12016-05-09 09:30:20 -0700541 } else {
542 log.warn(E_NO_ELEMENT, "host", id);
543 }
Simon Hunt23fb1352016-04-11 12:15:19 -0700544 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700545
Simon Hunt4854f3d2016-08-02 18:13:15 -0700546 Set<UiHost> getAllHosts() {
547 return uiTopology.allHosts();
548 }
549
Simon Huntc0f20c12016-05-09 09:30:20 -0700550
Simon Huntc13082f2016-08-03 21:20:23 -0700551 // === SYNTHETIC LINKS
552
553 List<UiSynthLink> getSynthLinks(RegionId regionId) {
554 return uiTopology.findSynthLinks(regionId);
555 }
556
Simon Hunt0e161092017-05-08 17:41:38 -0700557 Map<UiLinkId, UiSynthLink> relevantSynthLinks(RegionId regionId) {
558 Map<UiLinkId, UiSynthLink> result = new HashMap<>();
559 for (UiSynthLink sl : getSynthLinks(regionId)) {
560 result.put(sl.original().id(), sl);
561 }
562 return result;
563 }
564
Simon Huntb1ce2602016-07-23 14:04:31 -0700565 /**
566 * Refreshes the internal state.
567 */
568 public void refresh() {
Simon Huntc13082f2016-08-03 21:20:23 -0700569 // fix up internal linkages to ensure they are correct
Simon Huntb1ce2602016-07-23 14:04:31 -0700570
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700571 // make sure regions reflect layout containment hierarchy
572 fixupContainmentHierarchy(uiTopology.nullRegion());
573 uiTopology.allRegions().forEach(this::fixupContainmentHierarchy);
574
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500575 // make sure devices and hosts are in the correct region
Simon Huntb1ce2602016-07-23 14:04:31 -0700576 Set<UiDevice> allDevices = uiTopology.allDevices();
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500577 Set<UiHost> allHosts = uiTopology.allHosts();
Simon Huntb1ce2602016-07-23 14:04:31 -0700578
579 services.region().getRegions().forEach(r -> {
580 RegionId rid = r.id();
581 UiRegion region = uiTopology.findRegion(rid);
582 if (region != null) {
Steven Burrows8f45ce22016-10-27 20:04:14 -0500583 reconcileDevicesAndHostsWithRegion(allDevices, allHosts, rid, region);
Simon Huntb1ce2602016-07-23 14:04:31 -0700584 } else {
585 log.warn("No UiRegion in topology for ID {}", rid);
586 }
587 });
588
589 // what is left over, must belong to the null-region
590 Set<DeviceId> leftOver = new HashSet<>(allDevices.size());
591 allDevices.forEach(d -> leftOver.add(d.id()));
592 uiTopology.nullRegion().reconcileDevices(leftOver);
Simon Huntc13082f2016-08-03 21:20:23 -0700593
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500594 Set<HostId> leftOverHosts = new HashSet<>(allHosts.size());
595 allHosts.forEach(h -> leftOverHosts.add(h.id()));
596 uiTopology.nullRegion().reconcileHosts(leftOverHosts);
597
Simon Huntc13082f2016-08-03 21:20:23 -0700598 // now that we have correct region hierarchy, and devices are in their
599 // respective regions, we can compute synthetic links for each region.
600 uiTopology.computeSynthLinks();
Simon Huntb1ce2602016-07-23 14:04:31 -0700601 }
602
Steven Burrows8f45ce22016-10-27 20:04:14 -0500603 private void reconcileDevicesAndHostsWithRegion(Set<UiDevice> allDevices,
604 Set<UiHost> allHosts,
605 RegionId rid,
606 UiRegion region) {
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500607 Set<DeviceId> deviceIds = services.region().getRegionDevices(rid);
Steven Burrows8f45ce22016-10-27 20:04:14 -0500608 Set<HostId> hostIds = new HashSet<>();
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500609 region.reconcileDevices(deviceIds);
610
611 deviceIds.forEach(devId -> {
612 UiDevice dev = uiTopology.findDevice(devId);
613 if (dev != null) {
Steven Burrows8f45ce22016-10-27 20:04:14 -0500614 dev.setRegionId(rid);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500615 allDevices.remove(dev);
616 } else {
617 log.warn("Region device ID {} but no UiDevice in topology",
Simon Huntc4ca7102017-04-08 22:28:04 -0700618 devId);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500619 }
Steven Burrows8f45ce22016-10-27 20:04:14 -0500620
621 Set<Host> hosts = services.host().getConnectedHosts(devId);
622 for (Host h : hosts) {
623 HostId hid = h.id();
624 hostIds.add(hid);
625 UiHost host = uiTopology.findHost(hid);
626
627 if (host != null) {
628 host.setRegionId(rid);
629 allHosts.remove(host);
630 } else {
631 log.warn("Region host ID {} but no UiHost in topology",
Simon Huntc4ca7102017-04-08 22:28:04 -0700632 hid);
Steven Burrows8f45ce22016-10-27 20:04:14 -0500633 }
634 }
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500635 });
Steven Burrows8f45ce22016-10-27 20:04:14 -0500636
637 region.reconcileHosts(hostIds);
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500638 }
639
Simon Huntc13082f2016-08-03 21:20:23 -0700640
Simon Huntc0f20c12016-05-09 09:30:20 -0700641 // === CACHE STATISTICS
642
Simon Hunt338a3b42016-04-14 09:43:52 -0700643 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700644 * Returns a detailed (multi-line) string showing the contents of the cache.
645 *
646 * @return detailed string
647 */
648 public String dumpString() {
649 return uiTopology.dumpString();
650 }
651
652 /**
Simon Hunt338a3b42016-04-14 09:43:52 -0700653 * Returns the number of members in the cluster.
654 *
655 * @return number of cluster members
656 */
657 public int clusterMemberCount() {
658 return uiTopology.clusterMemberCount();
659 }
660
661 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700662 * Returns the number of regions in the topology.
Simon Hunt338a3b42016-04-14 09:43:52 -0700663 *
664 * @return number of regions
665 */
666 public int regionCount() {
667 return uiTopology.regionCount();
668 }
Simon Hunt58a0dd02016-05-17 11:54:23 -0700669
670 /**
671 * Returns the number of devices in the topology.
672 *
673 * @return number of devices
674 */
675 public int deviceCount() {
676 return uiTopology.deviceCount();
677 }
678
679 /**
Simon Huntc13082f2016-08-03 21:20:23 -0700680 * Returns the number of device links in the topology.
Simon Hunt58a0dd02016-05-17 11:54:23 -0700681 *
Simon Huntc13082f2016-08-03 21:20:23 -0700682 * @return number of device links
Simon Hunt58a0dd02016-05-17 11:54:23 -0700683 */
Simon Huntc13082f2016-08-03 21:20:23 -0700684 public int deviceLinkCount() {
685 return uiTopology.deviceLinkCount();
686 }
687
688 /**
689 * Returns the number of edge links in the topology.
690 *
691 * @return number of edge links
692 */
693 public int edgeLinkCount() {
694 return uiTopology.edgeLinkCount();
Simon Hunt58a0dd02016-05-17 11:54:23 -0700695 }
696
697 /**
698 * Returns the number of hosts in the topology.
699 *
700 * @return number of hosts
701 */
702 public int hostCount() {
703 return uiTopology.hostCount();
704 }
Simon Huntd5b96732016-07-08 13:22:27 -0700705
Simon Huntc13082f2016-08-03 21:20:23 -0700706 /**
707 * Returns the number of synthetic links in the topology.
708 *
709 * @return the number of synthetic links
710 */
711 public int synthLinkCount() {
712 return uiTopology.synthLinkCount();
713 }
Simon Huntcda9c032016-04-11 10:32:54 -0700714}