blob: 02c21dce846a49d86cf0bb54804b3914358bed3d [file] [log] [blame]
Simon Huntcda9c032016-04-11 10:32:54 -07001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.ui.impl.topo.model;
18
Simon Hunt23fb1352016-04-11 12:15:19 -070019import org.onosproject.cluster.ControllerNode;
Simon Hunt642bc452016-05-04 19:34:45 -070020import org.onosproject.cluster.NodeId;
Simon Hunt23fb1352016-04-11 12:15:19 -070021import org.onosproject.cluster.RoleInfo;
Simon Huntcda9c032016-04-11 10:32:54 -070022import org.onosproject.event.EventDispatcher;
23import org.onosproject.net.Device;
Simon Hunt23fb1352016-04-11 12:15:19 -070024import org.onosproject.net.DeviceId;
Simon Huntc0f20c12016-05-09 09:30:20 -070025import org.onosproject.net.EdgeLink;
Simon Hunt23fb1352016-04-11 12:15:19 -070026import org.onosproject.net.Host;
Simon Huntc0f20c12016-05-09 09:30:20 -070027import org.onosproject.net.HostId;
28import org.onosproject.net.HostLocation;
Simon Hunt23fb1352016-04-11 12:15:19 -070029import org.onosproject.net.Link;
30import org.onosproject.net.region.Region;
Simon Huntc0f20c12016-05-09 09:30:20 -070031import org.onosproject.net.region.RegionId;
Simon Hunt4f4ffc32016-08-03 18:30:47 -070032import org.onosproject.ui.UiTopoLayoutService;
Simon Hunt642bc452016-05-04 19:34:45 -070033import org.onosproject.ui.model.ServiceBundle;
Simon Hunt338a3b42016-04-14 09:43:52 -070034import org.onosproject.ui.model.topo.UiClusterMember;
Simon Huntcda9c032016-04-11 10:32:54 -070035import org.onosproject.ui.model.topo.UiDevice;
Simon Huntc13082f2016-08-03 21:20:23 -070036import org.onosproject.ui.model.topo.UiDeviceLink;
37import org.onosproject.ui.model.topo.UiEdgeLink;
Simon Huntc0f20c12016-05-09 09:30:20 -070038import org.onosproject.ui.model.topo.UiElement;
39import org.onosproject.ui.model.topo.UiHost;
Simon Huntc0f20c12016-05-09 09:30:20 -070040import org.onosproject.ui.model.topo.UiLinkId;
41import org.onosproject.ui.model.topo.UiRegion;
Simon Huntc13082f2016-08-03 21:20:23 -070042import org.onosproject.ui.model.topo.UiSynthLink;
Simon Hunt4f4ffc32016-08-03 18:30:47 -070043import org.onosproject.ui.model.topo.UiTopoLayout;
44import org.onosproject.ui.model.topo.UiTopoLayoutId;
Simon Hunt23fb1352016-04-11 12:15:19 -070045import org.onosproject.ui.model.topo.UiTopology;
Simon Hunt642bc452016-05-04 19:34:45 -070046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
Simon Hunt23fb1352016-04-11 12:15:19 -070048
Simon Huntb1ce2602016-07-23 14:04:31 -070049import java.util.HashSet;
Simon Huntd5b96732016-07-08 13:22:27 -070050import java.util.List;
Simon Huntc0f20c12016-05-09 09:30:20 -070051import java.util.Set;
52
53import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Simon Hunt642bc452016-05-04 19:34:45 -070054import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED;
55import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED;
56import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED;
Simon Hunt23fb1352016-04-11 12:15:19 -070057import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_REMOVED;
Simon Huntc0f20c12016-05-09 09:30:20 -070058import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_ADDED_OR_UPDATED;
59import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_MOVED;
60import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_REMOVED;
61import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_ADDED_OR_UPDATED;
62import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_REMOVED;
63import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_ADDED_OR_UPDATED;
64import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_REMOVED;
65import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId;
Simon Huntcda9c032016-04-11 10:32:54 -070066
67/**
68 * UI Topology Model cache.
69 */
70class ModelCache {
71
Simon Huntc0f20c12016-05-09 09:30:20 -070072 private static final String E_NO_ELEMENT = "Tried to remove non-member {}: {}";
73
Simon Hunt642bc452016-05-04 19:34:45 -070074 private static final Logger log = LoggerFactory.getLogger(ModelCache.class);
75
76 private final ServiceBundle services;
Simon Huntcda9c032016-04-11 10:32:54 -070077 private final EventDispatcher dispatcher;
Simon Hunt23fb1352016-04-11 12:15:19 -070078 private final UiTopology uiTopology = new UiTopology();
Simon Huntcda9c032016-04-11 10:32:54 -070079
Simon Hunt642bc452016-05-04 19:34:45 -070080 ModelCache(ServiceBundle services, EventDispatcher eventDispatcher) {
81 this.services = services;
Simon Huntcda9c032016-04-11 10:32:54 -070082 this.dispatcher = eventDispatcher;
83 }
84
Simon Hunt338a3b42016-04-14 09:43:52 -070085 @Override
86 public String toString() {
87 return "ModelCache{" + uiTopology + "}";
88 }
89
Simon Huntc0f20c12016-05-09 09:30:20 -070090 private void postEvent(UiModelEvent.Type type, UiElement subject) {
91 dispatcher.post(new UiModelEvent(type, subject));
92 }
93
Simon Huntcda9c032016-04-11 10:32:54 -070094 void clear() {
Simon Hunt23fb1352016-04-11 12:15:19 -070095 uiTopology.clear();
Simon Huntcda9c032016-04-11 10:32:54 -070096 }
97
98 /**
Simon Huntc0f20c12016-05-09 09:30:20 -070099 * Create our internal model of the global topology. An assumption we are
100 * making is that the topology is empty to start.
Simon Huntcda9c032016-04-11 10:32:54 -0700101 */
102 void load() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700103 loadClusterMembers();
104 loadRegions();
105 loadDevices();
Simon Huntc13082f2016-08-03 21:20:23 -0700106 loadDeviceLinks();
Simon Huntc0f20c12016-05-09 09:30:20 -0700107 loadHosts();
Simon Huntcda9c032016-04-11 10:32:54 -0700108 }
109
110
Simon Huntc0f20c12016-05-09 09:30:20 -0700111 // === CLUSTER MEMBERS
112
113 private UiClusterMember addNewClusterMember(ControllerNode n) {
114 UiClusterMember member = new UiClusterMember(uiTopology, n);
115 uiTopology.add(member);
116 return member;
117 }
118
119 private void updateClusterMember(UiClusterMember member) {
120 ControllerNode.State state = services.cluster().getState(member.id());
121 member.setState(state);
122 member.setMastership(services.mastership().getDevicesOf(member.id()));
123 // NOTE: 'UI-attached' is session-based data, not global, so will
124 // be set elsewhere
125 }
126
127 private void loadClusterMembers() {
128 for (ControllerNode n : services.cluster().getNodes()) {
129 UiClusterMember member = addNewClusterMember(n);
130 updateClusterMember(member);
131 }
132 }
133
134 // invoked from UiSharedTopologyModel cluster event listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700135 void addOrUpdateClusterMember(ControllerNode cnode) {
Simon Hunt642bc452016-05-04 19:34:45 -0700136 NodeId id = cnode.id();
137 UiClusterMember member = uiTopology.findClusterMember(id);
138 if (member == null) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700139 member = addNewClusterMember(cnode);
Simon Hunt338a3b42016-04-14 09:43:52 -0700140 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700141 updateClusterMember(member);
Simon Hunt338a3b42016-04-14 09:43:52 -0700142
Simon Huntc0f20c12016-05-09 09:30:20 -0700143 postEvent(CLUSTER_MEMBER_ADDED_OR_UPDATED, member);
Simon Hunt338a3b42016-04-14 09:43:52 -0700144 }
145
Simon Huntc0f20c12016-05-09 09:30:20 -0700146 // package private for unit test access
147 UiClusterMember accessClusterMember(NodeId id) {
148 return uiTopology.findClusterMember(id);
149 }
150
151 // invoked from UiSharedTopologyModel cluster event listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700152 void removeClusterMember(ControllerNode cnode) {
Simon Hunt642bc452016-05-04 19:34:45 -0700153 NodeId id = cnode.id();
154 UiClusterMember member = uiTopology.findClusterMember(id);
155 if (member != null) {
156 uiTopology.remove(member);
Simon Huntc0f20c12016-05-09 09:30:20 -0700157 postEvent(CLUSTER_MEMBER_REMOVED, member);
Simon Hunt642bc452016-05-04 19:34:45 -0700158 } else {
Simon Huntc0f20c12016-05-09 09:30:20 -0700159 log.warn(E_NO_ELEMENT, "cluster node", id);
Simon Hunt642bc452016-05-04 19:34:45 -0700160 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700161 }
162
Simon Huntd5b96732016-07-08 13:22:27 -0700163 List<UiClusterMember> getAllClusterMembers() {
164 return uiTopology.allClusterMembers();
165 }
166
Simon Huntc0f20c12016-05-09 09:30:20 -0700167
168 // === MASTERSHIP CHANGES
169
170 // invoked from UiSharedTopologyModel mastership listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700171 void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700172 // To think about:: do we need to store mastership info?
173 // or can we rely on looking it up live?
Simon Hunt338a3b42016-04-14 09:43:52 -0700174 // TODO: store the updated mastership information
175 // TODO: post event
176 }
177
Simon Huntb1ce2602016-07-23 14:04:31 -0700178 // === THE NULL REGION
179
180 UiRegion nullRegion() {
181 return uiTopology.nullRegion();
182 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700183
184 // === REGIONS
185
186 private UiRegion addNewRegion(Region r) {
187 UiRegion region = new UiRegion(uiTopology, r);
188 uiTopology.add(region);
Simon Huntb1ce2602016-07-23 14:04:31 -0700189 log.debug("Region {} added to topology", region);
Simon Huntc0f20c12016-05-09 09:30:20 -0700190 return region;
191 }
192
193 private void updateRegion(UiRegion region) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700194 RegionId rid = region.id();
195 Set<DeviceId> deviceIds = services.region().getRegionDevices(rid);
196
197 // Make sure device objects refer to their region
198 deviceIds.forEach(d -> {
199 UiDevice dev = uiTopology.findDevice(d);
200 if (dev != null) {
201 dev.setRegionId(rid);
202 } else {
203 // if we don't have the UiDevice in the topology, what can we do?
204 log.warn("Region device {}, but we don't have UiDevice in topology", d);
205 }
206 });
207
208 // Make sure the region object refers to the devices
209 region.reconcileDevices(deviceIds);
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700210
211 fixupContainmentHierarchy(region);
212 }
213
214 private void fixupContainmentHierarchy(UiRegion region) {
215 UiTopoLayoutService ls = services.layout();
216 RegionId regionId = region.id();
217
218 UiTopoLayout layout = ls.getLayout(regionId);
219 if (layout == null) {
220 // no layout backed by this region
221 log.warn("No layout backed by region {}", regionId);
222 return;
223 }
224
225 UiTopoLayoutId layoutId = layout.id();
226
227 if (!layout.isRoot()) {
228 UiTopoLayoutId parentId = layout.parent();
229 UiTopoLayout parentLayout = ls.getLayout(parentId);
230 RegionId parentRegionId = parentLayout.regionId();
231 region.setParent(parentRegionId);
232 }
233
234 Set<UiTopoLayout> kids = ls.getChildren(layoutId);
235 Set<RegionId> kidRegionIds = new HashSet<>(kids.size());
236 kids.forEach(k -> kidRegionIds.add(k.regionId()));
237 region.setChildren(kidRegionIds);
Simon Huntc0f20c12016-05-09 09:30:20 -0700238 }
239
240 private void loadRegions() {
241 for (Region r : services.region().getRegions()) {
242 UiRegion region = addNewRegion(r);
243 updateRegion(region);
244 }
245 }
246
247 // invoked from UiSharedTopologyModel region listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700248 void addOrUpdateRegion(Region region) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700249 RegionId id = region.id();
250 UiRegion uiRegion = uiTopology.findRegion(id);
251 if (uiRegion == null) {
252 uiRegion = addNewRegion(region);
253 }
254 updateRegion(uiRegion);
255
256 postEvent(REGION_ADDED_OR_UPDATED, uiRegion);
Simon Hunt338a3b42016-04-14 09:43:52 -0700257 }
258
Simon Hunt58a0dd02016-05-17 11:54:23 -0700259 // package private for unit test access
260 UiRegion accessRegion(RegionId id) {
Simon Huntd5b96732016-07-08 13:22:27 -0700261 return id == null ? null : uiTopology.findRegion(id);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700262 }
263
Simon Huntc0f20c12016-05-09 09:30:20 -0700264 // invoked from UiSharedTopologyModel region listener
Simon Hunt338a3b42016-04-14 09:43:52 -0700265 void removeRegion(Region region) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700266 RegionId id = region.id();
267 UiRegion uiRegion = uiTopology.findRegion(id);
268 if (uiRegion != null) {
269 uiTopology.remove(uiRegion);
270 postEvent(REGION_REMOVED, uiRegion);
271 } else {
272 log.warn(E_NO_ELEMENT, "region", id);
273 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700274 }
275
Simon Hunt10973dd2016-08-01 15:50:35 -0700276 Set<UiRegion> getAllRegions() {
277 return uiTopology.allRegions();
278 }
279
Simon Huntc0f20c12016-05-09 09:30:20 -0700280
281 // === DEVICES
282
283 private UiDevice addNewDevice(Device d) {
284 UiDevice device = new UiDevice(uiTopology, d);
Simon Huntb1ce2602016-07-23 14:04:31 -0700285 updateDevice(device);
Simon Huntc0f20c12016-05-09 09:30:20 -0700286 uiTopology.add(device);
Simon Huntb1ce2602016-07-23 14:04:31 -0700287 log.debug("Device {} added to topology", device);
Simon Huntc0f20c12016-05-09 09:30:20 -0700288 return device;
289 }
290
Simon Huntb1ce2602016-07-23 14:04:31 -0700291 // make sure the UiDevice is tagged with the region it belongs to
Simon Huntc0f20c12016-05-09 09:30:20 -0700292 private void updateDevice(UiDevice device) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700293 Region r = services.region().getRegionForDevice(device.id());
294 RegionId rid = r == null ? UiRegion.NULL_ID : r.id();
295 device.setRegionId(rid);
Simon Huntc0f20c12016-05-09 09:30:20 -0700296 }
297
298 private void loadDevices() {
299 for (Device d : services.device().getDevices()) {
Simon Huntb1ce2602016-07-23 14:04:31 -0700300 addNewDevice(d);
Simon Huntc0f20c12016-05-09 09:30:20 -0700301 }
302 }
303
304 // invoked from UiSharedTopologyModel device listener
Simon Huntcda9c032016-04-11 10:32:54 -0700305 void addOrUpdateDevice(Device device) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700306 DeviceId id = device.id();
307 UiDevice uiDevice = uiTopology.findDevice(id);
308 if (uiDevice == null) {
309 uiDevice = addNewDevice(device);
Simon Huntb1ce2602016-07-23 14:04:31 -0700310 } else {
311 updateDevice(uiDevice);
Simon Huntc0f20c12016-05-09 09:30:20 -0700312 }
Simon Huntcda9c032016-04-11 10:32:54 -0700313
Simon Huntc0f20c12016-05-09 09:30:20 -0700314 postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice);
Simon Huntcda9c032016-04-11 10:32:54 -0700315 }
316
Simon Hunt58a0dd02016-05-17 11:54:23 -0700317 // package private for unit test access
318 UiDevice accessDevice(DeviceId id) {
319 return uiTopology.findDevice(id);
320 }
321
Simon Huntc0f20c12016-05-09 09:30:20 -0700322 // invoked from UiSharedTopologyModel device listener
Simon Huntcda9c032016-04-11 10:32:54 -0700323 void removeDevice(Device device) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700324 DeviceId id = device.id();
325 UiDevice uiDevice = uiTopology.findDevice(id);
326 if (uiDevice != null) {
327 uiTopology.remove(uiDevice);
328 postEvent(DEVICE_REMOVED, uiDevice);
329 } else {
330 log.warn(E_NO_ELEMENT, "device", id);
331 }
Simon Huntcda9c032016-04-11 10:32:54 -0700332 }
333
Simon Hunt4854f3d2016-08-02 18:13:15 -0700334 Set<UiDevice> getAllDevices() {
335 return uiTopology.allDevices();
336 }
337
Simon Huntc0f20c12016-05-09 09:30:20 -0700338
Simon Huntc13082f2016-08-03 21:20:23 -0700339 // === LINKS ===
Simon Huntc0f20c12016-05-09 09:30:20 -0700340
Simon Huntc13082f2016-08-03 21:20:23 -0700341 private UiDeviceLink addNewDeviceLink(UiLinkId id) {
342 UiDeviceLink uiDeviceLink = new UiDeviceLink(uiTopology, id);
343 uiTopology.add(uiDeviceLink);
344 return uiDeviceLink;
Simon Huntc0f20c12016-05-09 09:30:20 -0700345 }
346
Simon Huntc13082f2016-08-03 21:20:23 -0700347 private UiEdgeLink addNewEdgeLink(UiLinkId id) {
348 UiEdgeLink uiEdgeLink = new UiEdgeLink(uiTopology, id);
349 uiTopology.add(uiEdgeLink);
350 return uiEdgeLink;
Simon Huntc0f20c12016-05-09 09:30:20 -0700351 }
352
Simon Huntc13082f2016-08-03 21:20:23 -0700353 private void updateDeviceLink(UiDeviceLink uiDeviceLink, Link link) {
354 uiDeviceLink.attachBackingLink(link);
355 }
356
357 private void loadDeviceLinks() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700358 for (Link link : services.link().getLinks()) {
359 UiLinkId id = uiLinkId(link);
360
Simon Huntc13082f2016-08-03 21:20:23 -0700361 UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id);
362 if (uiDeviceLink == null) {
363 uiDeviceLink = addNewDeviceLink(id);
Simon Huntc0f20c12016-05-09 09:30:20 -0700364 }
Simon Huntc13082f2016-08-03 21:20:23 -0700365 updateDeviceLink(uiDeviceLink, link);
Simon Huntc0f20c12016-05-09 09:30:20 -0700366 }
367 }
368
369 // invoked from UiSharedTopologyModel link listener
Simon Huntc13082f2016-08-03 21:20:23 -0700370 void addOrUpdateDeviceLink(Link link) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700371 UiLinkId id = uiLinkId(link);
Simon Huntc13082f2016-08-03 21:20:23 -0700372 UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id);
373 if (uiDeviceLink == null) {
374 uiDeviceLink = addNewDeviceLink(id);
Simon Huntc0f20c12016-05-09 09:30:20 -0700375 }
Simon Huntc13082f2016-08-03 21:20:23 -0700376 updateDeviceLink(uiDeviceLink, link);
Simon Huntc0f20c12016-05-09 09:30:20 -0700377
Simon Huntc13082f2016-08-03 21:20:23 -0700378 postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink);
Simon Hunt23fb1352016-04-11 12:15:19 -0700379 }
380
Simon Hunt58a0dd02016-05-17 11:54:23 -0700381 // package private for unit test access
Simon Huntc13082f2016-08-03 21:20:23 -0700382 UiDeviceLink accessDeviceLink(UiLinkId id) {
383 return uiTopology.findDeviceLink(id);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700384 }
385
Simon Huntc0f20c12016-05-09 09:30:20 -0700386 // invoked from UiSharedTopologyModel link listener
Simon Huntc13082f2016-08-03 21:20:23 -0700387 void removeDeviceLink(Link link) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700388 UiLinkId id = uiLinkId(link);
Simon Huntc13082f2016-08-03 21:20:23 -0700389 UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id);
390 if (uiDeviceLink != null) {
391 boolean remaining = uiDeviceLink.detachBackingLink(link);
Simon Huntc0f20c12016-05-09 09:30:20 -0700392 if (remaining) {
Simon Huntc13082f2016-08-03 21:20:23 -0700393 postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink);
Simon Huntc0f20c12016-05-09 09:30:20 -0700394 } else {
Simon Huntc13082f2016-08-03 21:20:23 -0700395 uiTopology.remove(uiDeviceLink);
396 postEvent(LINK_REMOVED, uiDeviceLink);
Simon Huntc0f20c12016-05-09 09:30:20 -0700397 }
398 } else {
Simon Huntc13082f2016-08-03 21:20:23 -0700399 log.warn(E_NO_ELEMENT, "Device link", id);
Simon Huntc0f20c12016-05-09 09:30:20 -0700400 }
Simon Hunt23fb1352016-04-11 12:15:19 -0700401 }
402
Simon Huntc13082f2016-08-03 21:20:23 -0700403 Set<UiDeviceLink> getAllDeviceLinks() {
404 return uiTopology.allDeviceLinks();
Simon Hunt4854f3d2016-08-02 18:13:15 -0700405 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700406
407 // === HOSTS
408
Simon Hunt58a0dd02016-05-17 11:54:23 -0700409 private EdgeLink synthesizeLink(Host h) {
410 return createEdgeLink(h, true);
411 }
412
Simon Huntc0f20c12016-05-09 09:30:20 -0700413 private UiHost addNewHost(Host h) {
414 UiHost host = new UiHost(uiTopology, h);
415 uiTopology.add(host);
416
Simon Hunt58a0dd02016-05-17 11:54:23 -0700417 EdgeLink elink = synthesizeLink(h);
418 UiLinkId elinkId = uiLinkId(elink);
419 host.setEdgeLinkId(elinkId);
420
421 // add synthesized edge link to the topology
Simon Huntc13082f2016-08-03 21:20:23 -0700422 UiEdgeLink edgeLink = addNewEdgeLink(elinkId);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700423 edgeLink.attachEdgeLink(elink);
Simon Huntc0f20c12016-05-09 09:30:20 -0700424
425 return host;
426 }
427
Simon Huntc13082f2016-08-03 21:20:23 -0700428 private void insertNewUiEdgeLink(UiLinkId id, EdgeLink e) {
429 UiEdgeLink newEdgeLink = addNewEdgeLink(id);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700430 newEdgeLink.attachEdgeLink(e);
Simon Huntc0f20c12016-05-09 09:30:20 -0700431 }
432
433 private void updateHost(UiHost uiHost, Host h) {
Simon Huntc13082f2016-08-03 21:20:23 -0700434 UiEdgeLink existing = uiTopology.findEdgeLink(uiHost.edgeLinkId());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700435
436 EdgeLink currentElink = synthesizeLink(h);
437 UiLinkId currentElinkId = uiLinkId(currentElink);
438
439 if (existing != null) {
440 if (!currentElinkId.equals(existing.id())) {
441 // edge link has changed
Simon Huntc13082f2016-08-03 21:20:23 -0700442 insertNewUiEdgeLink(currentElinkId, currentElink);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700443 uiHost.setEdgeLinkId(currentElinkId);
444
445 uiTopology.remove(existing);
446 }
447
448 } else {
449 // no previously existing edge link
Simon Huntc13082f2016-08-03 21:20:23 -0700450 insertNewUiEdgeLink(currentElinkId, currentElink);
Simon Hunt58a0dd02016-05-17 11:54:23 -0700451 uiHost.setEdgeLinkId(currentElinkId);
452
453 }
454
Simon Huntc0f20c12016-05-09 09:30:20 -0700455 HostLocation hloc = h.location();
456 uiHost.setLocation(hloc.deviceId(), hloc.port());
Simon Huntc0f20c12016-05-09 09:30:20 -0700457 }
458
459 private void loadHosts() {
460 for (Host h : services.host().getHosts()) {
461 UiHost host = addNewHost(h);
462 updateHost(host, h);
463 }
464 }
465
466 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700467 void addOrUpdateHost(Host host) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700468 HostId id = host.id();
469 UiHost uiHost = uiTopology.findHost(id);
470 if (uiHost == null) {
471 uiHost = addNewHost(host);
472 }
473 updateHost(uiHost, host);
474
475 postEvent(HOST_ADDED_OR_UPDATED, uiHost);
Simon Hunt23fb1352016-04-11 12:15:19 -0700476 }
477
Simon Huntc0f20c12016-05-09 09:30:20 -0700478 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700479 void moveHost(Host host, Host prevHost) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700480 UiHost uiHost = uiTopology.findHost(prevHost.id());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700481 if (uiHost != null) {
482 updateHost(uiHost, host);
483 postEvent(HOST_MOVED, uiHost);
484 } else {
485 log.warn(E_NO_ELEMENT, "host", prevHost.id());
486 }
487 }
Simon Huntc0f20c12016-05-09 09:30:20 -0700488
Simon Hunt58a0dd02016-05-17 11:54:23 -0700489 // package private for unit test access
490 UiHost accessHost(HostId id) {
491 return uiTopology.findHost(id);
Simon Hunt23fb1352016-04-11 12:15:19 -0700492 }
493
Simon Huntc0f20c12016-05-09 09:30:20 -0700494 // invoked from UiSharedTopologyModel host listener
Simon Hunt23fb1352016-04-11 12:15:19 -0700495 void removeHost(Host host) {
Simon Huntc0f20c12016-05-09 09:30:20 -0700496 HostId id = host.id();
497 UiHost uiHost = uiTopology.findHost(id);
498 if (uiHost != null) {
Simon Huntc13082f2016-08-03 21:20:23 -0700499 UiEdgeLink edgeLink = uiTopology.findEdgeLink(uiHost.edgeLinkId());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700500 uiTopology.remove(edgeLink);
Simon Huntc0f20c12016-05-09 09:30:20 -0700501 uiTopology.remove(uiHost);
Simon Huntc0f20c12016-05-09 09:30:20 -0700502 postEvent(HOST_REMOVED, uiHost);
503 } else {
504 log.warn(E_NO_ELEMENT, "host", id);
505 }
Simon Hunt23fb1352016-04-11 12:15:19 -0700506 }
Simon Hunt338a3b42016-04-14 09:43:52 -0700507
Simon Hunt4854f3d2016-08-02 18:13:15 -0700508 Set<UiHost> getAllHosts() {
509 return uiTopology.allHosts();
510 }
511
Simon Huntc0f20c12016-05-09 09:30:20 -0700512
Simon Huntc13082f2016-08-03 21:20:23 -0700513 // === SYNTHETIC LINKS
514
515 List<UiSynthLink> getSynthLinks(RegionId regionId) {
516 return uiTopology.findSynthLinks(regionId);
517 }
518
Simon Huntb1ce2602016-07-23 14:04:31 -0700519 /**
520 * Refreshes the internal state.
521 */
522 public void refresh() {
Simon Huntc13082f2016-08-03 21:20:23 -0700523 // fix up internal linkages to ensure they are correct
Simon Huntb1ce2602016-07-23 14:04:31 -0700524
Simon Hunt4f4ffc32016-08-03 18:30:47 -0700525 // make sure regions reflect layout containment hierarchy
526 fixupContainmentHierarchy(uiTopology.nullRegion());
527 uiTopology.allRegions().forEach(this::fixupContainmentHierarchy);
528
529 // make sure devices are in the correct region
Simon Huntb1ce2602016-07-23 14:04:31 -0700530 Set<UiDevice> allDevices = uiTopology.allDevices();
531
532 services.region().getRegions().forEach(r -> {
533 RegionId rid = r.id();
534 UiRegion region = uiTopology.findRegion(rid);
535 if (region != null) {
536 Set<DeviceId> deviceIds = services.region().getRegionDevices(rid);
537 region.reconcileDevices(deviceIds);
538
539 deviceIds.forEach(devId -> {
540 UiDevice dev = uiTopology.findDevice(devId);
541 if (dev != null) {
542 dev.setRegionId(r.id());
543 allDevices.remove(dev);
544 } else {
545 log.warn("Region device ID {} but no UiDevice in topology",
546 devId);
547 }
548 });
549 } else {
550 log.warn("No UiRegion in topology for ID {}", rid);
551 }
552 });
553
554 // what is left over, must belong to the null-region
555 Set<DeviceId> leftOver = new HashSet<>(allDevices.size());
556 allDevices.forEach(d -> leftOver.add(d.id()));
557 uiTopology.nullRegion().reconcileDevices(leftOver);
Simon Huntc13082f2016-08-03 21:20:23 -0700558
559 // now that we have correct region hierarchy, and devices are in their
560 // respective regions, we can compute synthetic links for each region.
561 uiTopology.computeSynthLinks();
Simon Huntb1ce2602016-07-23 14:04:31 -0700562 }
563
Simon Huntc13082f2016-08-03 21:20:23 -0700564
Simon Huntc0f20c12016-05-09 09:30:20 -0700565 // === CACHE STATISTICS
566
Simon Hunt338a3b42016-04-14 09:43:52 -0700567 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -0700568 * Returns a detailed (multi-line) string showing the contents of the cache.
569 *
570 * @return detailed string
571 */
572 public String dumpString() {
573 return uiTopology.dumpString();
574 }
575
576 /**
Simon Hunt338a3b42016-04-14 09:43:52 -0700577 * Returns the number of members in the cluster.
578 *
579 * @return number of cluster members
580 */
581 public int clusterMemberCount() {
582 return uiTopology.clusterMemberCount();
583 }
584
585 /**
Simon Huntc0f20c12016-05-09 09:30:20 -0700586 * Returns the number of regions in the topology.
Simon Hunt338a3b42016-04-14 09:43:52 -0700587 *
588 * @return number of regions
589 */
590 public int regionCount() {
591 return uiTopology.regionCount();
592 }
Simon Hunt58a0dd02016-05-17 11:54:23 -0700593
594 /**
595 * Returns the number of devices in the topology.
596 *
597 * @return number of devices
598 */
599 public int deviceCount() {
600 return uiTopology.deviceCount();
601 }
602
603 /**
Simon Huntc13082f2016-08-03 21:20:23 -0700604 * Returns the number of device links in the topology.
Simon Hunt58a0dd02016-05-17 11:54:23 -0700605 *
Simon Huntc13082f2016-08-03 21:20:23 -0700606 * @return number of device links
Simon Hunt58a0dd02016-05-17 11:54:23 -0700607 */
Simon Huntc13082f2016-08-03 21:20:23 -0700608 public int deviceLinkCount() {
609 return uiTopology.deviceLinkCount();
610 }
611
612 /**
613 * Returns the number of edge links in the topology.
614 *
615 * @return number of edge links
616 */
617 public int edgeLinkCount() {
618 return uiTopology.edgeLinkCount();
Simon Hunt58a0dd02016-05-17 11:54:23 -0700619 }
620
621 /**
622 * Returns the number of hosts in the topology.
623 *
624 * @return number of hosts
625 */
626 public int hostCount() {
627 return uiTopology.hostCount();
628 }
Simon Huntd5b96732016-07-08 13:22:27 -0700629
Simon Huntc13082f2016-08-03 21:20:23 -0700630 /**
631 * Returns the number of synthetic links in the topology.
632 *
633 * @return the number of synthetic links
634 */
635 public int synthLinkCount() {
636 return uiTopology.synthLinkCount();
637 }
Simon Huntcda9c032016-04-11 10:32:54 -0700638}