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 |
| 194 | // TODO: post event |
| 195 | } |
| 196 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 197 | // === THE NULL REGION |
| 198 | |
| 199 | UiRegion nullRegion() { |
| 200 | return uiTopology.nullRegion(); |
| 201 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 202 | |
| 203 | // === REGIONS |
| 204 | |
| 205 | private UiRegion addNewRegion(Region r) { |
| 206 | UiRegion region = new UiRegion(uiTopology, r); |
| 207 | uiTopology.add(region); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 208 | log.debug("Region {} added to topology", region); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 209 | return region; |
| 210 | } |
| 211 | |
| 212 | private void updateRegion(UiRegion region) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 213 | RegionId rid = region.id(); |
| 214 | Set<DeviceId> deviceIds = services.region().getRegionDevices(rid); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 215 | Set<HostId> hostIds = services.region().getRegionHosts(rid); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 216 | |
| 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 Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 228 | 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 Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 238 | // Make sure the region object refers to the devices |
| 239 | region.reconcileDevices(deviceIds); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 240 | region.reconcileHosts(hostIds); |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 241 | |
| 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 Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 269 | } |
| 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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 279 | void addOrUpdateRegion(Region region) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 280 | RegionId id = region.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 281 | String memo = MEMO_UPDATED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 282 | UiRegion uiRegion = uiTopology.findRegion(id); |
| 283 | if (uiRegion == null) { |
| 284 | uiRegion = addNewRegion(region); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 285 | memo = MEMO_ADDED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 286 | } |
| 287 | updateRegion(uiRegion); |
| 288 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 289 | postEvent(REGION_ADDED_OR_UPDATED, uiRegion, memo); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 292 | // package private for unit test access |
| 293 | UiRegion accessRegion(RegionId id) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 294 | return id == null ? null : uiTopology.findRegion(id); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 297 | // invoked from UiSharedTopologyModel region listener |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 298 | void removeRegion(Region region) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 299 | RegionId id = region.id(); |
| 300 | UiRegion uiRegion = uiTopology.findRegion(id); |
| 301 | if (uiRegion != null) { |
| 302 | uiTopology.remove(uiRegion); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 303 | postEvent(REGION_REMOVED, uiRegion, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 304 | } else { |
| 305 | log.warn(E_NO_ELEMENT, "region", id); |
| 306 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Simon Hunt | 10973dd | 2016-08-01 15:50:35 -0700 | [diff] [blame] | 309 | Set<UiRegion> getAllRegions() { |
| 310 | return uiTopology.allRegions(); |
| 311 | } |
| 312 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 313 | |
| 314 | // === DEVICES |
| 315 | |
| 316 | private UiDevice addNewDevice(Device d) { |
| 317 | UiDevice device = new UiDevice(uiTopology, d); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 318 | updateDevice(device); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 319 | uiTopology.add(device); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 320 | log.debug("Device {} added to topology", device); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 321 | return device; |
| 322 | } |
| 323 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 324 | // make sure the UiDevice is tagged with the region it belongs to |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 325 | private void updateDevice(UiDevice device) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 326 | Region r = services.region().getRegionForDevice(device.id()); |
| 327 | RegionId rid = r == null ? UiRegion.NULL_ID : r.id(); |
| 328 | device.setRegionId(rid); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | private void loadDevices() { |
| 332 | for (Device d : services.device().getDevices()) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 333 | addNewDevice(d); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | // invoked from UiSharedTopologyModel device listener |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 338 | void addOrUpdateDevice(Device device) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 339 | DeviceId id = device.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 340 | String memo = MEMO_UPDATED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 341 | UiDevice uiDevice = uiTopology.findDevice(id); |
| 342 | if (uiDevice == null) { |
| 343 | uiDevice = addNewDevice(device); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 344 | memo = MEMO_ADDED; |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 345 | } else { |
| 346 | updateDevice(uiDevice); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 347 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 348 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 349 | postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice, memo); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 352 | // package private for unit test access |
| 353 | UiDevice accessDevice(DeviceId id) { |
| 354 | return uiTopology.findDevice(id); |
| 355 | } |
| 356 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 357 | // invoked from UiSharedTopologyModel device listener |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 358 | void removeDevice(Device device) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 359 | DeviceId id = device.id(); |
| 360 | UiDevice uiDevice = uiTopology.findDevice(id); |
| 361 | if (uiDevice != null) { |
| 362 | uiTopology.remove(uiDevice); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 363 | postEvent(DEVICE_REMOVED, uiDevice, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 364 | } else { |
| 365 | log.warn(E_NO_ELEMENT, "device", id); |
| 366 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 369 | Set<UiDevice> getAllDevices() { |
| 370 | return uiTopology.allDevices(); |
| 371 | } |
| 372 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 373 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 374 | // === LINKS === |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 375 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 376 | private UiDeviceLink addNewDeviceLink(UiLinkId id) { |
| 377 | UiDeviceLink uiDeviceLink = new UiDeviceLink(uiTopology, id); |
| 378 | uiTopology.add(uiDeviceLink); |
| 379 | return uiDeviceLink; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 382 | private UiEdgeLink addNewEdgeLink(UiLinkId id) { |
| 383 | UiEdgeLink uiEdgeLink = new UiEdgeLink(uiTopology, id); |
| 384 | uiTopology.add(uiEdgeLink); |
| 385 | return uiEdgeLink; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 388 | private void updateDeviceLink(UiDeviceLink uiDeviceLink, Link link) { |
| 389 | uiDeviceLink.attachBackingLink(link); |
| 390 | } |
| 391 | |
| 392 | private void loadDeviceLinks() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 393 | for (Link link : services.link().getLinks()) { |
| 394 | UiLinkId id = uiLinkId(link); |
| 395 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 396 | UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id); |
| 397 | if (uiDeviceLink == null) { |
| 398 | uiDeviceLink = addNewDeviceLink(id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 399 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 400 | updateDeviceLink(uiDeviceLink, link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | // invoked from UiSharedTopologyModel link listener |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 405 | void addOrUpdateDeviceLink(Link link) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 406 | UiLinkId id = uiLinkId(link); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 407 | String memo = MEMO_UPDATED; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 408 | UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id); |
| 409 | if (uiDeviceLink == null) { |
| 410 | uiDeviceLink = addNewDeviceLink(id); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 411 | memo = MEMO_ADDED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 412 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 413 | updateDeviceLink(uiDeviceLink, link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 414 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 415 | postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, memo); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 418 | // package private for unit test access |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 419 | UiDeviceLink accessDeviceLink(UiLinkId id) { |
| 420 | return uiTopology.findDeviceLink(id); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 423 | // invoked from UiSharedTopologyModel link listener |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 424 | void removeDeviceLink(Link link) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 425 | UiLinkId id = uiLinkId(link); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 426 | UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id); |
| 427 | if (uiDeviceLink != null) { |
| 428 | boolean remaining = uiDeviceLink.detachBackingLink(link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 429 | if (remaining) { |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 430 | postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, MEMO_UPDATED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 431 | } else { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 432 | uiTopology.remove(uiDeviceLink); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 433 | postEvent(LINK_REMOVED, uiDeviceLink, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 434 | } |
| 435 | } else { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 436 | log.warn(E_NO_ELEMENT, "Device link", id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 437 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 440 | Set<UiDeviceLink> getAllDeviceLinks() { |
| 441 | return uiTopology.allDeviceLinks(); |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 442 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 443 | |
| 444 | // === HOSTS |
| 445 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 446 | private EdgeLink synthesizeLink(Host h) { |
| 447 | return createEdgeLink(h, true); |
| 448 | } |
| 449 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 450 | private UiHost addNewHost(Host h) { |
| 451 | UiHost host = new UiHost(uiTopology, h); |
| 452 | uiTopology.add(host); |
| 453 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 454 | EdgeLink elink = synthesizeLink(h); |
| 455 | UiLinkId elinkId = uiLinkId(elink); |
| 456 | host.setEdgeLinkId(elinkId); |
| 457 | |
| 458 | // add synthesized edge link to the topology |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 459 | addNewEdgeLink(elinkId); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 460 | |
| 461 | return host; |
| 462 | } |
| 463 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 464 | private void insertNewUiEdgeLink(UiLinkId id) { |
| 465 | addNewEdgeLink(id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | private void updateHost(UiHost uiHost, Host h) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 469 | UiEdgeLink existing = uiTopology.findEdgeLink(uiHost.edgeLinkId()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 470 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 471 | // 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] | 472 | 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 Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 478 | insertNewUiEdgeLink(currentElinkId); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 479 | uiHost.setEdgeLinkId(currentElinkId); |
| 480 | |
| 481 | uiTopology.remove(existing); |
| 482 | } |
| 483 | |
| 484 | } else { |
| 485 | // no previously existing edge link |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 486 | insertNewUiEdgeLink(currentElinkId); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 487 | uiHost.setEdgeLinkId(currentElinkId); |
| 488 | |
| 489 | } |
| 490 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 491 | HostLocation hloc = h.location(); |
| 492 | uiHost.setLocation(hloc.deviceId(), hloc.port()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 493 | } |
| 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 Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 503 | void addOrUpdateHost(Host host) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 504 | HostId id = host.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 505 | String memo = MEMO_UPDATED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 506 | UiHost uiHost = uiTopology.findHost(id); |
| 507 | if (uiHost == null) { |
| 508 | uiHost = addNewHost(host); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 509 | memo = MEMO_ADDED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 510 | } |
| 511 | updateHost(uiHost, host); |
| 512 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 513 | postEvent(HOST_ADDED_OR_UPDATED, uiHost, memo); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 516 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 517 | void moveHost(Host host, Host prevHost) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 518 | UiHost uiHost = uiTopology.findHost(prevHost.id()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 519 | if (uiHost != null) { |
| 520 | updateHost(uiHost, host); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 521 | postEvent(HOST_MOVED, uiHost, MEMO_MOVED); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 522 | } else { |
| 523 | log.warn(E_NO_ELEMENT, "host", prevHost.id()); |
| 524 | } |
| 525 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 526 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 527 | // package private for unit test access |
| 528 | UiHost accessHost(HostId id) { |
| 529 | return uiTopology.findHost(id); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 532 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 533 | void removeHost(Host host) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 534 | HostId id = host.id(); |
| 535 | UiHost uiHost = uiTopology.findHost(id); |
| 536 | if (uiHost != null) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 537 | UiEdgeLink edgeLink = uiTopology.findEdgeLink(uiHost.edgeLinkId()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 538 | uiTopology.remove(edgeLink); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 539 | uiTopology.remove(uiHost); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 540 | postEvent(HOST_REMOVED, uiHost, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 541 | } else { |
| 542 | log.warn(E_NO_ELEMENT, "host", id); |
| 543 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 544 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 545 | |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 546 | Set<UiHost> getAllHosts() { |
| 547 | return uiTopology.allHosts(); |
| 548 | } |
| 549 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 550 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 551 | // === SYNTHETIC LINKS |
| 552 | |
| 553 | List<UiSynthLink> getSynthLinks(RegionId regionId) { |
| 554 | return uiTopology.findSynthLinks(regionId); |
| 555 | } |
| 556 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 557 | 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 Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 565 | /** |
| 566 | * Refreshes the internal state. |
| 567 | */ |
| 568 | public void refresh() { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 569 | // fix up internal linkages to ensure they are correct |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 570 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 571 | // make sure regions reflect layout containment hierarchy |
| 572 | fixupContainmentHierarchy(uiTopology.nullRegion()); |
| 573 | uiTopology.allRegions().forEach(this::fixupContainmentHierarchy); |
| 574 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 575 | // make sure devices and hosts are in the correct region |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 576 | Set<UiDevice> allDevices = uiTopology.allDevices(); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 577 | Set<UiHost> allHosts = uiTopology.allHosts(); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 578 | |
| 579 | services.region().getRegions().forEach(r -> { |
| 580 | RegionId rid = r.id(); |
| 581 | UiRegion region = uiTopology.findRegion(rid); |
| 582 | if (region != null) { |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 583 | reconcileDevicesAndHostsWithRegion(allDevices, allHosts, rid, region); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 584 | } 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 Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 593 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 594 | Set<HostId> leftOverHosts = new HashSet<>(allHosts.size()); |
| 595 | allHosts.forEach(h -> leftOverHosts.add(h.id())); |
| 596 | uiTopology.nullRegion().reconcileHosts(leftOverHosts); |
| 597 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 598 | // 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 Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 603 | private void reconcileDevicesAndHostsWithRegion(Set<UiDevice> allDevices, |
| 604 | Set<UiHost> allHosts, |
| 605 | RegionId rid, |
| 606 | UiRegion region) { |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 607 | Set<DeviceId> deviceIds = services.region().getRegionDevices(rid); |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 608 | Set<HostId> hostIds = new HashSet<>(); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 609 | region.reconcileDevices(deviceIds); |
| 610 | |
| 611 | deviceIds.forEach(devId -> { |
| 612 | UiDevice dev = uiTopology.findDevice(devId); |
| 613 | if (dev != null) { |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 614 | dev.setRegionId(rid); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 615 | allDevices.remove(dev); |
| 616 | } else { |
| 617 | log.warn("Region device ID {} but no UiDevice in topology", |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 618 | devId); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 619 | } |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 620 | |
| 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 Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 632 | hid); |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 633 | } |
| 634 | } |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 635 | }); |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 636 | |
| 637 | region.reconcileHosts(hostIds); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 638 | } |
| 639 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 640 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 641 | // === CACHE STATISTICS |
| 642 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 643 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 644 | * 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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 653 | * 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 Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 662 | * Returns the number of regions in the topology. |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 663 | * |
| 664 | * @return number of regions |
| 665 | */ |
| 666 | public int regionCount() { |
| 667 | return uiTopology.regionCount(); |
| 668 | } |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 669 | |
| 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 Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 680 | * Returns the number of device links in the topology. |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 681 | * |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 682 | * @return number of device links |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 683 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 684 | 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 Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 695 | } |
| 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 Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 705 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 706 | /** |
| 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 Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 714 | } |