Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.ui.impl.topo.model; |
| 18 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 20 | import org.onosproject.cluster.ControllerNode; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 21 | import org.onosproject.cluster.NodeId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 22 | import org.onosproject.cluster.RoleInfo; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 23 | import org.onosproject.event.EventDispatcher; |
| 24 | import org.onosproject.net.Device; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 26 | import org.onosproject.net.EdgeLink; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Host; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 28 | import org.onosproject.net.HostId; |
| 29 | import org.onosproject.net.HostLocation; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.region.Region; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 32 | import org.onosproject.net.region.RegionId; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 34 | import org.onosproject.ui.impl.topo.Topo2Jsonifier; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.model.ServiceBundle; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.model.topo.UiClusterMember; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.model.topo.UiDevice; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 38 | import org.onosproject.ui.model.topo.UiDeviceLink; |
| 39 | import org.onosproject.ui.model.topo.UiEdgeLink; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 40 | import org.onosproject.ui.model.topo.UiElement; |
| 41 | import org.onosproject.ui.model.topo.UiHost; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 42 | import org.onosproject.ui.model.topo.UiLinkId; |
Thomas Vachuska | 8c0b18a | 2017-04-14 16:27:33 -0700 | [diff] [blame] | 43 | import org.onosproject.ui.model.topo.UiModelEvent; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 44 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 45 | import org.onosproject.ui.model.topo.UiSynthLink; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 46 | import org.onosproject.ui.model.topo.UiTopoLayout; |
| 47 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 48 | import org.onosproject.ui.model.topo.UiTopology; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | import org.slf4j.LoggerFactory; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 51 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 52 | import java.util.HashMap; |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 53 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 54 | import java.util.List; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 55 | import java.util.Map; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 56 | import java.util.Set; |
| 57 | |
| 58 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Thomas Vachuska | 8c0b18a | 2017-04-14 16:27:33 -0700 | [diff] [blame] | 59 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED; |
| 60 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED; |
| 61 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED; |
| 62 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.DEVICE_REMOVED; |
| 63 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.HOST_ADDED_OR_UPDATED; |
| 64 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.HOST_MOVED; |
| 65 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.HOST_REMOVED; |
| 66 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.LINK_ADDED_OR_UPDATED; |
| 67 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.LINK_REMOVED; |
| 68 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.REGION_ADDED_OR_UPDATED; |
| 69 | import static org.onosproject.ui.model.topo.UiModelEvent.Type.REGION_REMOVED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 70 | import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * UI Topology Model cache. |
| 74 | */ |
| 75 | class ModelCache { |
| 76 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 77 | private static final String E_NO_ELEMENT = "Tried to remove non-member {}: {}"; |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 78 | 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 Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 82 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 83 | private static final Logger log = LoggerFactory.getLogger(ModelCache.class); |
| 84 | |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 85 | // TODO: add NetworkConfigService to service bundle |
| 86 | // private final NetworkConfigService cfgService = |
| 87 | // DefaultServiceDirectory.getService(NetworkConfigService.class); |
| 88 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 89 | private final ServiceBundle services; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 90 | private final EventDispatcher dispatcher; |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 91 | private final UiTopology uiTopology; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 92 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 93 | private Topo2Jsonifier t2json; |
| 94 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 95 | ModelCache(ServiceBundle services, EventDispatcher eventDispatcher) { |
| 96 | this.services = services; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 97 | this.dispatcher = eventDispatcher; |
Thomas Vachuska | b877a6f | 2017-04-14 11:43:30 -0700 | [diff] [blame] | 98 | uiTopology = new UiTopology(services); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public String toString() { |
| 103 | return "ModelCache{" + uiTopology + "}"; |
| 104 | } |
| 105 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 106 | 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 Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 115 | void clear() { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 116 | uiTopology.clear(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 120 | * Create our internal model of the global topology. An assumption we are |
| 121 | * making is that the topology is empty to start. |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 122 | */ |
| 123 | void load() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 124 | loadClusterMembers(); |
| 125 | loadRegions(); |
| 126 | loadDevices(); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 127 | loadDeviceLinks(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 128 | loadHosts(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 132 | // === 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 Hunt | 708a5b3 | 2017-08-01 15:06:04 -0700 | [diff] [blame] | 141 | // maybe something to update in the future? |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 142 | } |
| 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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 152 | void addOrUpdateClusterMember(ControllerNode cnode) { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 153 | NodeId id = cnode.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 154 | String memo = MEMO_UPDATED; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 155 | UiClusterMember member = uiTopology.findClusterMember(id); |
| 156 | if (member == null) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 157 | member = addNewClusterMember(cnode); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 158 | memo = MEMO_ADDED; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 159 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 160 | updateClusterMember(member); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 161 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 162 | postEvent(CLUSTER_MEMBER_ADDED_OR_UPDATED, member, memo); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 165 | // 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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 171 | void removeClusterMember(ControllerNode cnode) { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 172 | NodeId id = cnode.id(); |
| 173 | UiClusterMember member = uiTopology.findClusterMember(id); |
| 174 | if (member != null) { |
| 175 | uiTopology.remove(member); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 176 | postEvent(CLUSTER_MEMBER_REMOVED, member, MEMO_REMOVED); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 177 | } else { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 178 | log.warn(E_NO_ELEMENT, "cluster node", id); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 179 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 182 | List<UiClusterMember> getAllClusterMembers() { |
| 183 | return uiTopology.allClusterMembers(); |
| 184 | } |
| 185 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 186 | |
| 187 | // === MASTERSHIP CHANGES |
| 188 | |
| 189 | // invoked from UiSharedTopologyModel mastership listener |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 190 | void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 191 | // To think about:: do we need to store mastership info? |
| 192 | // or can we rely on looking it up live? |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 193 | // TODO: store the updated mastership information |
Sean Condon | 058804c | 2019-04-16 09:41:52 +0100 | [diff] [blame] | 194 | UiDevice uiDevice = uiTopology.findDevice(deviceId); |
Sean Condon | bed2e03 | 2019-04-17 22:22:49 +0100 | [diff] [blame] | 195 | 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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 202 | // === THE NULL REGION |
| 203 | |
| 204 | UiRegion nullRegion() { |
| 205 | return uiTopology.nullRegion(); |
| 206 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 207 | |
| 208 | // === REGIONS |
| 209 | |
| 210 | private UiRegion addNewRegion(Region r) { |
| 211 | UiRegion region = new UiRegion(uiTopology, r); |
| 212 | uiTopology.add(region); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 213 | log.debug("Region {} added to topology", region); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 214 | return region; |
| 215 | } |
| 216 | |
| 217 | private void updateRegion(UiRegion region) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 218 | RegionId rid = region.id(); |
| 219 | Set<DeviceId> deviceIds = services.region().getRegionDevices(rid); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 220 | Set<HostId> hostIds = services.region().getRegionHosts(rid); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 221 | |
| 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 Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 233 | 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 Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 243 | // Make sure the region object refers to the devices |
| 244 | region.reconcileDevices(deviceIds); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 245 | region.reconcileHosts(hostIds); |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 246 | |
| 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 Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 274 | } |
| 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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 284 | void addOrUpdateRegion(Region region) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 285 | RegionId id = region.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 286 | String memo = MEMO_UPDATED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 287 | UiRegion uiRegion = uiTopology.findRegion(id); |
| 288 | if (uiRegion == null) { |
| 289 | uiRegion = addNewRegion(region); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 290 | memo = MEMO_ADDED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 291 | } |
| 292 | updateRegion(uiRegion); |
| 293 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 294 | postEvent(REGION_ADDED_OR_UPDATED, uiRegion, memo); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 297 | // package private for unit test access |
| 298 | UiRegion accessRegion(RegionId id) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 299 | return id == null ? null : uiTopology.findRegion(id); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 302 | // invoked from UiSharedTopologyModel region listener |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 303 | void removeRegion(Region region) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 304 | RegionId id = region.id(); |
| 305 | UiRegion uiRegion = uiTopology.findRegion(id); |
| 306 | if (uiRegion != null) { |
| 307 | uiTopology.remove(uiRegion); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 308 | postEvent(REGION_REMOVED, uiRegion, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 309 | } else { |
| 310 | log.warn(E_NO_ELEMENT, "region", id); |
| 311 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Simon Hunt | 10973dd | 2016-08-01 15:50:35 -0700 | [diff] [blame] | 314 | Set<UiRegion> getAllRegions() { |
| 315 | return uiTopology.allRegions(); |
| 316 | } |
| 317 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 318 | |
| 319 | // === DEVICES |
| 320 | |
| 321 | private UiDevice addNewDevice(Device d) { |
| 322 | UiDevice device = new UiDevice(uiTopology, d); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 323 | updateDevice(device); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 324 | uiTopology.add(device); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 325 | log.debug("Device {} added to topology", device); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 326 | return device; |
| 327 | } |
| 328 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 329 | // make sure the UiDevice is tagged with the region it belongs to |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 330 | private void updateDevice(UiDevice device) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 331 | Region r = services.region().getRegionForDevice(device.id()); |
| 332 | RegionId rid = r == null ? UiRegion.NULL_ID : r.id(); |
| 333 | device.setRegionId(rid); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | private void loadDevices() { |
| 337 | for (Device d : services.device().getDevices()) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 338 | addNewDevice(d); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | // invoked from UiSharedTopologyModel device listener |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 343 | void addOrUpdateDevice(Device device) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 344 | DeviceId id = device.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 345 | String memo = MEMO_UPDATED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 346 | UiDevice uiDevice = uiTopology.findDevice(id); |
| 347 | if (uiDevice == null) { |
| 348 | uiDevice = addNewDevice(device); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 349 | memo = MEMO_ADDED; |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 350 | } else { |
| 351 | updateDevice(uiDevice); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 352 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 353 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 354 | postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice, memo); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 357 | // package private for unit test access |
| 358 | UiDevice accessDevice(DeviceId id) { |
| 359 | return uiTopology.findDevice(id); |
| 360 | } |
| 361 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 362 | // invoked from UiSharedTopologyModel device listener |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 363 | void removeDevice(Device device) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 364 | DeviceId id = device.id(); |
| 365 | UiDevice uiDevice = uiTopology.findDevice(id); |
| 366 | if (uiDevice != null) { |
| 367 | uiTopology.remove(uiDevice); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 368 | postEvent(DEVICE_REMOVED, uiDevice, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 369 | } else { |
| 370 | log.warn(E_NO_ELEMENT, "device", id); |
| 371 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 374 | Set<UiDevice> getAllDevices() { |
| 375 | return uiTopology.allDevices(); |
| 376 | } |
| 377 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 378 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 379 | // === LINKS === |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 380 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 381 | private UiDeviceLink addNewDeviceLink(UiLinkId id) { |
| 382 | UiDeviceLink uiDeviceLink = new UiDeviceLink(uiTopology, id); |
| 383 | uiTopology.add(uiDeviceLink); |
| 384 | return uiDeviceLink; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 387 | private UiEdgeLink addNewEdgeLink(UiLinkId id) { |
| 388 | UiEdgeLink uiEdgeLink = new UiEdgeLink(uiTopology, id); |
| 389 | uiTopology.add(uiEdgeLink); |
| 390 | return uiEdgeLink; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 393 | private void updateDeviceLink(UiDeviceLink uiDeviceLink, Link link) { |
| 394 | uiDeviceLink.attachBackingLink(link); |
| 395 | } |
| 396 | |
| 397 | private void loadDeviceLinks() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 398 | for (Link link : services.link().getLinks()) { |
| 399 | UiLinkId id = uiLinkId(link); |
| 400 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 401 | UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id); |
| 402 | if (uiDeviceLink == null) { |
| 403 | uiDeviceLink = addNewDeviceLink(id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 404 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 405 | updateDeviceLink(uiDeviceLink, link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | // invoked from UiSharedTopologyModel link listener |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 410 | void addOrUpdateDeviceLink(Link link) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 411 | UiLinkId id = uiLinkId(link); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 412 | String memo = MEMO_UPDATED; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 413 | UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id); |
| 414 | if (uiDeviceLink == null) { |
| 415 | uiDeviceLink = addNewDeviceLink(id); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 416 | memo = MEMO_ADDED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 417 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 418 | updateDeviceLink(uiDeviceLink, link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 419 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 420 | postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, memo); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 423 | // package private for unit test access |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 424 | UiDeviceLink accessDeviceLink(UiLinkId id) { |
| 425 | return uiTopology.findDeviceLink(id); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 428 | // invoked from UiSharedTopologyModel link listener |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 429 | void removeDeviceLink(Link link) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 430 | UiLinkId id = uiLinkId(link); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 431 | UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id); |
| 432 | if (uiDeviceLink != null) { |
| 433 | boolean remaining = uiDeviceLink.detachBackingLink(link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 434 | if (remaining) { |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 435 | postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, MEMO_UPDATED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 436 | } else { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 437 | uiTopology.remove(uiDeviceLink); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 438 | postEvent(LINK_REMOVED, uiDeviceLink, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 439 | } |
| 440 | } else { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 441 | log.warn(E_NO_ELEMENT, "Device link", id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 442 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 445 | Set<UiDeviceLink> getAllDeviceLinks() { |
| 446 | return uiTopology.allDeviceLinks(); |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 447 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 448 | |
| 449 | // === HOSTS |
| 450 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 451 | private EdgeLink synthesizeLink(Host h) { |
| 452 | return createEdgeLink(h, true); |
| 453 | } |
| 454 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 455 | private UiHost addNewHost(Host h) { |
| 456 | UiHost host = new UiHost(uiTopology, h); |
| 457 | uiTopology.add(host); |
| 458 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 459 | EdgeLink elink = synthesizeLink(h); |
| 460 | UiLinkId elinkId = uiLinkId(elink); |
| 461 | host.setEdgeLinkId(elinkId); |
| 462 | |
| 463 | // add synthesized edge link to the topology |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 464 | addNewEdgeLink(elinkId); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 465 | |
| 466 | return host; |
| 467 | } |
| 468 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 469 | private void insertNewUiEdgeLink(UiLinkId id) { |
| 470 | addNewEdgeLink(id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | private void updateHost(UiHost uiHost, Host h) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 474 | UiEdgeLink existing = uiTopology.findEdgeLink(uiHost.edgeLinkId()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 475 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 476 | // TODO: review - do we need EdgeLink now that we are creating from id only? |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 477 | 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 Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 483 | insertNewUiEdgeLink(currentElinkId); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 484 | uiHost.setEdgeLinkId(currentElinkId); |
| 485 | |
| 486 | uiTopology.remove(existing); |
| 487 | } |
| 488 | |
| 489 | } else { |
| 490 | // no previously existing edge link |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 491 | insertNewUiEdgeLink(currentElinkId); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 492 | uiHost.setEdgeLinkId(currentElinkId); |
| 493 | |
| 494 | } |
| 495 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 496 | HostLocation hloc = h.location(); |
| 497 | uiHost.setLocation(hloc.deviceId(), hloc.port()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 498 | } |
| 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 Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 508 | void addOrUpdateHost(Host host) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 509 | HostId id = host.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 510 | String memo = MEMO_UPDATED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 511 | UiHost uiHost = uiTopology.findHost(id); |
| 512 | if (uiHost == null) { |
| 513 | uiHost = addNewHost(host); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 514 | memo = MEMO_ADDED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 515 | } |
| 516 | updateHost(uiHost, host); |
| 517 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 518 | postEvent(HOST_ADDED_OR_UPDATED, uiHost, memo); |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 519 | // 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 Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 524 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 525 | void moveHost(Host host, Host prevHost) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 526 | UiHost uiHost = uiTopology.findHost(prevHost.id()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 527 | if (uiHost != null) { |
| 528 | updateHost(uiHost, host); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 529 | postEvent(HOST_MOVED, uiHost, MEMO_MOVED); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 530 | } else { |
| 531 | log.warn(E_NO_ELEMENT, "host", prevHost.id()); |
| 532 | } |
| 533 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 534 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 535 | // package private for unit test access |
| 536 | UiHost accessHost(HostId id) { |
| 537 | return uiTopology.findHost(id); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 540 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 541 | void removeHost(Host host) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 542 | HostId id = host.id(); |
| 543 | UiHost uiHost = uiTopology.findHost(id); |
| 544 | if (uiHost != null) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 545 | UiEdgeLink edgeLink = uiTopology.findEdgeLink(uiHost.edgeLinkId()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 546 | uiTopology.remove(edgeLink); |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 547 | postEvent(LINK_REMOVED, edgeLink, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 548 | uiTopology.remove(uiHost); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 549 | postEvent(HOST_REMOVED, uiHost, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 550 | } else { |
| 551 | log.warn(E_NO_ELEMENT, "host", id); |
| 552 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 553 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 554 | |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 555 | Set<UiHost> getAllHosts() { |
| 556 | return uiTopology.allHosts(); |
| 557 | } |
| 558 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 559 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 560 | // === SYNTHETIC LINKS |
| 561 | |
| 562 | List<UiSynthLink> getSynthLinks(RegionId regionId) { |
| 563 | return uiTopology.findSynthLinks(regionId); |
| 564 | } |
| 565 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 566 | 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 Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 574 | /** |
| 575 | * Refreshes the internal state. |
| 576 | */ |
| 577 | public void refresh() { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 578 | // fix up internal linkages to ensure they are correct |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 579 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 580 | // make sure regions reflect layout containment hierarchy |
| 581 | fixupContainmentHierarchy(uiTopology.nullRegion()); |
| 582 | uiTopology.allRegions().forEach(this::fixupContainmentHierarchy); |
| 583 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 584 | // make sure devices and hosts are in the correct region |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 585 | Set<UiDevice> allDevices = uiTopology.allDevices(); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 586 | Set<UiHost> allHosts = uiTopology.allHosts(); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 587 | |
| 588 | services.region().getRegions().forEach(r -> { |
| 589 | RegionId rid = r.id(); |
| 590 | UiRegion region = uiTopology.findRegion(rid); |
| 591 | if (region != null) { |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 592 | reconcileDevicesAndHostsWithRegion(allDevices, allHosts, rid, region); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 593 | } 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 Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 602 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 603 | Set<HostId> leftOverHosts = new HashSet<>(allHosts.size()); |
| 604 | allHosts.forEach(h -> leftOverHosts.add(h.id())); |
| 605 | uiTopology.nullRegion().reconcileHosts(leftOverHosts); |
| 606 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 607 | // 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 Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 612 | private void reconcileDevicesAndHostsWithRegion(Set<UiDevice> allDevices, |
| 613 | Set<UiHost> allHosts, |
| 614 | RegionId rid, |
| 615 | UiRegion region) { |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 616 | Set<DeviceId> deviceIds = services.region().getRegionDevices(rid); |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 617 | Set<HostId> hostIds = new HashSet<>(); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 618 | region.reconcileDevices(deviceIds); |
| 619 | |
| 620 | deviceIds.forEach(devId -> { |
| 621 | UiDevice dev = uiTopology.findDevice(devId); |
| 622 | if (dev != null) { |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 623 | dev.setRegionId(rid); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 624 | allDevices.remove(dev); |
| 625 | } else { |
| 626 | log.warn("Region device ID {} but no UiDevice in topology", |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 627 | devId); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 628 | } |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 629 | |
| 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 Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 641 | hid); |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 642 | } |
| 643 | } |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 644 | }); |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 645 | |
| 646 | region.reconcileHosts(hostIds); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 647 | } |
| 648 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 649 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 650 | // === CACHE STATISTICS |
| 651 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 652 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 653 | * 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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 662 | * 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 Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 671 | * Returns the number of regions in the topology. |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 672 | * |
| 673 | * @return number of regions |
| 674 | */ |
| 675 | public int regionCount() { |
| 676 | return uiTopology.regionCount(); |
| 677 | } |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 678 | |
| 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 Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 689 | * Returns the number of device links in the topology. |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 690 | * |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 691 | * @return number of device links |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 692 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 693 | 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 Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 704 | } |
| 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 Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 714 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 715 | /** |
| 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 Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 723 | } |