Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
| 43 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 44 | import org.onosproject.ui.model.topo.UiSynthLink; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 45 | import org.onosproject.ui.model.topo.UiTopoLayout; |
| 46 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 47 | import org.onosproject.ui.model.topo.UiTopology; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 50 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 51 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 52 | import java.util.List; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 53 | import java.util.Set; |
| 54 | |
| 55 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 56 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED; |
| 57 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED; |
| 58 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 59 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_REMOVED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 60 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_ADDED_OR_UPDATED; |
| 61 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_MOVED; |
| 62 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_REMOVED; |
| 63 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_ADDED_OR_UPDATED; |
| 64 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_REMOVED; |
| 65 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_ADDED_OR_UPDATED; |
| 66 | import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_REMOVED; |
| 67 | import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * UI Topology Model cache. |
| 71 | */ |
| 72 | class ModelCache { |
| 73 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 74 | private static final String E_NO_ELEMENT = "Tried to remove non-member {}: {}"; |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 75 | private static final String MEMO_ADDED = "added"; |
| 76 | private static final String MEMO_UPDATED = "updated"; |
| 77 | private static final String MEMO_REMOVED = "removed"; |
| 78 | private static final String MEMO_MOVED = "moved"; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 79 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 80 | private static final Logger log = LoggerFactory.getLogger(ModelCache.class); |
| 81 | |
| 82 | private final ServiceBundle services; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 83 | private final EventDispatcher dispatcher; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 84 | private final UiTopology uiTopology = new UiTopology(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 85 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 86 | private Topo2Jsonifier t2json; |
| 87 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 88 | ModelCache(ServiceBundle services, EventDispatcher eventDispatcher) { |
| 89 | this.services = services; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 90 | this.dispatcher = eventDispatcher; |
| 91 | } |
| 92 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public String toString() { |
| 95 | return "ModelCache{" + uiTopology + "}"; |
| 96 | } |
| 97 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 98 | private void postEvent(UiModelEvent.Type type, UiElement subject, String memo) { |
| 99 | ObjectNode data = t2json != null ? t2json.jsonUiElement(subject) : null; |
| 100 | dispatcher.post(new UiModelEvent(type, subject, data, memo)); |
| 101 | } |
| 102 | |
| 103 | void injectJsonifier(Topo2Jsonifier t2json) { |
| 104 | this.t2json = t2json; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 107 | void clear() { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 108 | uiTopology.clear(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 112 | * Create our internal model of the global topology. An assumption we are |
| 113 | * making is that the topology is empty to start. |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 114 | */ |
| 115 | void load() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 116 | loadClusterMembers(); |
| 117 | loadRegions(); |
| 118 | loadDevices(); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 119 | loadDeviceLinks(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 120 | loadHosts(); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 124 | // === CLUSTER MEMBERS |
| 125 | |
| 126 | private UiClusterMember addNewClusterMember(ControllerNode n) { |
| 127 | UiClusterMember member = new UiClusterMember(uiTopology, n); |
| 128 | uiTopology.add(member); |
| 129 | return member; |
| 130 | } |
| 131 | |
| 132 | private void updateClusterMember(UiClusterMember member) { |
| 133 | ControllerNode.State state = services.cluster().getState(member.id()); |
| 134 | member.setState(state); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | private void loadClusterMembers() { |
| 138 | for (ControllerNode n : services.cluster().getNodes()) { |
| 139 | UiClusterMember member = addNewClusterMember(n); |
| 140 | updateClusterMember(member); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // invoked from UiSharedTopologyModel cluster event listener |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 145 | void addOrUpdateClusterMember(ControllerNode cnode) { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 146 | NodeId id = cnode.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 147 | String memo = MEMO_UPDATED; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 148 | UiClusterMember member = uiTopology.findClusterMember(id); |
| 149 | if (member == null) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 150 | member = addNewClusterMember(cnode); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 151 | memo = MEMO_ADDED; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 152 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 153 | updateClusterMember(member); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 154 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 155 | postEvent(CLUSTER_MEMBER_ADDED_OR_UPDATED, member, memo); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 158 | // package private for unit test access |
| 159 | UiClusterMember accessClusterMember(NodeId id) { |
| 160 | return uiTopology.findClusterMember(id); |
| 161 | } |
| 162 | |
| 163 | // invoked from UiSharedTopologyModel cluster event listener |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 164 | void removeClusterMember(ControllerNode cnode) { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 165 | NodeId id = cnode.id(); |
| 166 | UiClusterMember member = uiTopology.findClusterMember(id); |
| 167 | if (member != null) { |
| 168 | uiTopology.remove(member); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 169 | postEvent(CLUSTER_MEMBER_REMOVED, member, MEMO_REMOVED); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 170 | } else { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 171 | log.warn(E_NO_ELEMENT, "cluster node", id); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 172 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 175 | List<UiClusterMember> getAllClusterMembers() { |
| 176 | return uiTopology.allClusterMembers(); |
| 177 | } |
| 178 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 179 | |
| 180 | // === MASTERSHIP CHANGES |
| 181 | |
| 182 | // invoked from UiSharedTopologyModel mastership listener |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 183 | void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 184 | // To think about:: do we need to store mastership info? |
| 185 | // or can we rely on looking it up live? |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 186 | // TODO: store the updated mastership information |
| 187 | // TODO: post event |
| 188 | } |
| 189 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 190 | // === THE NULL REGION |
| 191 | |
| 192 | UiRegion nullRegion() { |
| 193 | return uiTopology.nullRegion(); |
| 194 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 195 | |
| 196 | // === REGIONS |
| 197 | |
| 198 | private UiRegion addNewRegion(Region r) { |
| 199 | UiRegion region = new UiRegion(uiTopology, r); |
| 200 | uiTopology.add(region); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 201 | log.debug("Region {} added to topology", region); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 202 | return region; |
| 203 | } |
| 204 | |
| 205 | private void updateRegion(UiRegion region) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 206 | RegionId rid = region.id(); |
| 207 | Set<DeviceId> deviceIds = services.region().getRegionDevices(rid); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 208 | Set<HostId> hostIds = services.region().getRegionHosts(rid); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 209 | |
| 210 | // Make sure device objects refer to their region |
| 211 | deviceIds.forEach(d -> { |
| 212 | UiDevice dev = uiTopology.findDevice(d); |
| 213 | if (dev != null) { |
| 214 | dev.setRegionId(rid); |
| 215 | } else { |
| 216 | // if we don't have the UiDevice in the topology, what can we do? |
| 217 | log.warn("Region device {}, but we don't have UiDevice in topology", d); |
| 218 | } |
| 219 | }); |
| 220 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 221 | hostIds.forEach(d -> { |
| 222 | UiHost host = uiTopology.findHost(d); |
| 223 | if (host != null) { |
| 224 | host.setRegionId(rid); |
| 225 | } else { |
| 226 | // if we don't have the UiDevice in the topology, what can we do? |
| 227 | log.warn("Region host {}, but we don't have UiHost in topology", d); |
| 228 | } |
| 229 | }); |
| 230 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 231 | // Make sure the region object refers to the devices |
| 232 | region.reconcileDevices(deviceIds); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 233 | region.reconcileHosts(hostIds); |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 234 | |
| 235 | fixupContainmentHierarchy(region); |
| 236 | } |
| 237 | |
| 238 | private void fixupContainmentHierarchy(UiRegion region) { |
| 239 | UiTopoLayoutService ls = services.layout(); |
| 240 | RegionId regionId = region.id(); |
| 241 | |
| 242 | UiTopoLayout layout = ls.getLayout(regionId); |
| 243 | if (layout == null) { |
| 244 | // no layout backed by this region |
| 245 | log.warn("No layout backed by region {}", regionId); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | UiTopoLayoutId layoutId = layout.id(); |
| 250 | |
| 251 | if (!layout.isRoot()) { |
| 252 | UiTopoLayoutId parentId = layout.parent(); |
| 253 | UiTopoLayout parentLayout = ls.getLayout(parentId); |
| 254 | RegionId parentRegionId = parentLayout.regionId(); |
| 255 | region.setParent(parentRegionId); |
| 256 | } |
| 257 | |
| 258 | Set<UiTopoLayout> kids = ls.getChildren(layoutId); |
| 259 | Set<RegionId> kidRegionIds = new HashSet<>(kids.size()); |
| 260 | kids.forEach(k -> kidRegionIds.add(k.regionId())); |
| 261 | region.setChildren(kidRegionIds); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | private void loadRegions() { |
| 265 | for (Region r : services.region().getRegions()) { |
| 266 | UiRegion region = addNewRegion(r); |
| 267 | updateRegion(region); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // invoked from UiSharedTopologyModel region listener |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 272 | void addOrUpdateRegion(Region region) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 273 | RegionId id = region.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 274 | String memo = MEMO_UPDATED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 275 | UiRegion uiRegion = uiTopology.findRegion(id); |
| 276 | if (uiRegion == null) { |
| 277 | uiRegion = addNewRegion(region); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 278 | memo = MEMO_ADDED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 279 | } |
| 280 | updateRegion(uiRegion); |
| 281 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 282 | postEvent(REGION_ADDED_OR_UPDATED, uiRegion, memo); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 285 | // package private for unit test access |
| 286 | UiRegion accessRegion(RegionId id) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 287 | return id == null ? null : uiTopology.findRegion(id); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 290 | // invoked from UiSharedTopologyModel region listener |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 291 | void removeRegion(Region region) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 292 | RegionId id = region.id(); |
| 293 | UiRegion uiRegion = uiTopology.findRegion(id); |
| 294 | if (uiRegion != null) { |
| 295 | uiTopology.remove(uiRegion); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 296 | postEvent(REGION_REMOVED, uiRegion, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 297 | } else { |
| 298 | log.warn(E_NO_ELEMENT, "region", id); |
| 299 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Simon Hunt | 10973dd | 2016-08-01 15:50:35 -0700 | [diff] [blame] | 302 | Set<UiRegion> getAllRegions() { |
| 303 | return uiTopology.allRegions(); |
| 304 | } |
| 305 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 306 | |
| 307 | // === DEVICES |
| 308 | |
| 309 | private UiDevice addNewDevice(Device d) { |
| 310 | UiDevice device = new UiDevice(uiTopology, d); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 311 | updateDevice(device); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 312 | uiTopology.add(device); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 313 | log.debug("Device {} added to topology", device); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 314 | return device; |
| 315 | } |
| 316 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 317 | // make sure the UiDevice is tagged with the region it belongs to |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 318 | private void updateDevice(UiDevice device) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 319 | Region r = services.region().getRegionForDevice(device.id()); |
| 320 | RegionId rid = r == null ? UiRegion.NULL_ID : r.id(); |
| 321 | device.setRegionId(rid); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | private void loadDevices() { |
| 325 | for (Device d : services.device().getDevices()) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 326 | addNewDevice(d); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | // invoked from UiSharedTopologyModel device listener |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 331 | void addOrUpdateDevice(Device device) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 332 | DeviceId id = device.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 333 | String memo = MEMO_UPDATED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 334 | UiDevice uiDevice = uiTopology.findDevice(id); |
| 335 | if (uiDevice == null) { |
| 336 | uiDevice = addNewDevice(device); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 337 | memo = MEMO_ADDED; |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 338 | } else { |
| 339 | updateDevice(uiDevice); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 340 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 341 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 342 | postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice, memo); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 345 | // package private for unit test access |
| 346 | UiDevice accessDevice(DeviceId id) { |
| 347 | return uiTopology.findDevice(id); |
| 348 | } |
| 349 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 350 | // invoked from UiSharedTopologyModel device listener |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 351 | void removeDevice(Device device) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 352 | DeviceId id = device.id(); |
| 353 | UiDevice uiDevice = uiTopology.findDevice(id); |
| 354 | if (uiDevice != null) { |
| 355 | uiTopology.remove(uiDevice); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 356 | postEvent(DEVICE_REMOVED, uiDevice, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 357 | } else { |
| 358 | log.warn(E_NO_ELEMENT, "device", id); |
| 359 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 362 | Set<UiDevice> getAllDevices() { |
| 363 | return uiTopology.allDevices(); |
| 364 | } |
| 365 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 366 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 367 | // === LINKS === |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 368 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 369 | private UiDeviceLink addNewDeviceLink(UiLinkId id) { |
| 370 | UiDeviceLink uiDeviceLink = new UiDeviceLink(uiTopology, id); |
| 371 | uiTopology.add(uiDeviceLink); |
| 372 | return uiDeviceLink; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 375 | private UiEdgeLink addNewEdgeLink(UiLinkId id) { |
| 376 | UiEdgeLink uiEdgeLink = new UiEdgeLink(uiTopology, id); |
| 377 | uiTopology.add(uiEdgeLink); |
| 378 | return uiEdgeLink; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 381 | private void updateDeviceLink(UiDeviceLink uiDeviceLink, Link link) { |
| 382 | uiDeviceLink.attachBackingLink(link); |
| 383 | } |
| 384 | |
| 385 | private void loadDeviceLinks() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 386 | for (Link link : services.link().getLinks()) { |
| 387 | UiLinkId id = uiLinkId(link); |
| 388 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 389 | UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id); |
| 390 | if (uiDeviceLink == null) { |
| 391 | uiDeviceLink = addNewDeviceLink(id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 392 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 393 | updateDeviceLink(uiDeviceLink, link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | // invoked from UiSharedTopologyModel link listener |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 398 | void addOrUpdateDeviceLink(Link link) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 399 | UiLinkId id = uiLinkId(link); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 400 | String memo = MEMO_UPDATED; |
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 | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 404 | memo = MEMO_ADDED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 405 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 406 | updateDeviceLink(uiDeviceLink, link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 407 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 408 | postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, memo); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 411 | // package private for unit test access |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 412 | UiDeviceLink accessDeviceLink(UiLinkId id) { |
| 413 | return uiTopology.findDeviceLink(id); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 416 | // invoked from UiSharedTopologyModel link listener |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 417 | void removeDeviceLink(Link link) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 418 | UiLinkId id = uiLinkId(link); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 419 | UiDeviceLink uiDeviceLink = uiTopology.findDeviceLink(id); |
| 420 | if (uiDeviceLink != null) { |
| 421 | boolean remaining = uiDeviceLink.detachBackingLink(link); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 422 | if (remaining) { |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 423 | postEvent(LINK_ADDED_OR_UPDATED, uiDeviceLink, MEMO_UPDATED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 424 | } else { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 425 | uiTopology.remove(uiDeviceLink); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 426 | postEvent(LINK_REMOVED, uiDeviceLink, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 427 | } |
| 428 | } else { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 429 | log.warn(E_NO_ELEMENT, "Device link", id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 430 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 433 | Set<UiDeviceLink> getAllDeviceLinks() { |
| 434 | return uiTopology.allDeviceLinks(); |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 435 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 436 | |
| 437 | // === HOSTS |
| 438 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 439 | private EdgeLink synthesizeLink(Host h) { |
| 440 | return createEdgeLink(h, true); |
| 441 | } |
| 442 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 443 | private UiHost addNewHost(Host h) { |
| 444 | UiHost host = new UiHost(uiTopology, h); |
| 445 | uiTopology.add(host); |
| 446 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 447 | EdgeLink elink = synthesizeLink(h); |
| 448 | UiLinkId elinkId = uiLinkId(elink); |
| 449 | host.setEdgeLinkId(elinkId); |
| 450 | |
| 451 | // add synthesized edge link to the topology |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 452 | addNewEdgeLink(elinkId); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 453 | |
| 454 | return host; |
| 455 | } |
| 456 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 457 | private void insertNewUiEdgeLink(UiLinkId id) { |
| 458 | addNewEdgeLink(id); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | private void updateHost(UiHost uiHost, Host h) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 462 | UiEdgeLink existing = uiTopology.findEdgeLink(uiHost.edgeLinkId()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 463 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 464 | // 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] | 465 | EdgeLink currentElink = synthesizeLink(h); |
| 466 | UiLinkId currentElinkId = uiLinkId(currentElink); |
| 467 | |
| 468 | if (existing != null) { |
| 469 | if (!currentElinkId.equals(existing.id())) { |
| 470 | // edge link has changed |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 471 | insertNewUiEdgeLink(currentElinkId); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 472 | uiHost.setEdgeLinkId(currentElinkId); |
| 473 | |
| 474 | uiTopology.remove(existing); |
| 475 | } |
| 476 | |
| 477 | } else { |
| 478 | // no previously existing edge link |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 479 | insertNewUiEdgeLink(currentElinkId); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 480 | uiHost.setEdgeLinkId(currentElinkId); |
| 481 | |
| 482 | } |
| 483 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 484 | HostLocation hloc = h.location(); |
| 485 | uiHost.setLocation(hloc.deviceId(), hloc.port()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | private void loadHosts() { |
| 489 | for (Host h : services.host().getHosts()) { |
| 490 | UiHost host = addNewHost(h); |
| 491 | updateHost(host, h); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 496 | void addOrUpdateHost(Host host) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 497 | HostId id = host.id(); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 498 | String memo = MEMO_UPDATED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 499 | UiHost uiHost = uiTopology.findHost(id); |
| 500 | if (uiHost == null) { |
| 501 | uiHost = addNewHost(host); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 502 | memo = MEMO_ADDED; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 503 | } |
| 504 | updateHost(uiHost, host); |
| 505 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 506 | postEvent(HOST_ADDED_OR_UPDATED, uiHost, memo); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 509 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 510 | void moveHost(Host host, Host prevHost) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 511 | UiHost uiHost = uiTopology.findHost(prevHost.id()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 512 | if (uiHost != null) { |
| 513 | updateHost(uiHost, host); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 514 | postEvent(HOST_MOVED, uiHost, MEMO_MOVED); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 515 | } else { |
| 516 | log.warn(E_NO_ELEMENT, "host", prevHost.id()); |
| 517 | } |
| 518 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 519 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 520 | // package private for unit test access |
| 521 | UiHost accessHost(HostId id) { |
| 522 | return uiTopology.findHost(id); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 525 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 526 | void removeHost(Host host) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 527 | HostId id = host.id(); |
| 528 | UiHost uiHost = uiTopology.findHost(id); |
| 529 | if (uiHost != null) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 530 | UiEdgeLink edgeLink = uiTopology.findEdgeLink(uiHost.edgeLinkId()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 531 | uiTopology.remove(edgeLink); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 532 | uiTopology.remove(uiHost); |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame^] | 533 | postEvent(HOST_REMOVED, uiHost, MEMO_REMOVED); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 534 | } else { |
| 535 | log.warn(E_NO_ELEMENT, "host", id); |
| 536 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 537 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 538 | |
Simon Hunt | 4854f3d | 2016-08-02 18:13:15 -0700 | [diff] [blame] | 539 | Set<UiHost> getAllHosts() { |
| 540 | return uiTopology.allHosts(); |
| 541 | } |
| 542 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 543 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 544 | // === SYNTHETIC LINKS |
| 545 | |
| 546 | List<UiSynthLink> getSynthLinks(RegionId regionId) { |
| 547 | return uiTopology.findSynthLinks(regionId); |
| 548 | } |
| 549 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 550 | /** |
| 551 | * Refreshes the internal state. |
| 552 | */ |
| 553 | public void refresh() { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 554 | // fix up internal linkages to ensure they are correct |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 555 | |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 556 | // make sure regions reflect layout containment hierarchy |
| 557 | fixupContainmentHierarchy(uiTopology.nullRegion()); |
| 558 | uiTopology.allRegions().forEach(this::fixupContainmentHierarchy); |
| 559 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 560 | // make sure devices and hosts are in the correct region |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 561 | Set<UiDevice> allDevices = uiTopology.allDevices(); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 562 | Set<UiHost> allHosts = uiTopology.allHosts(); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 563 | |
| 564 | services.region().getRegions().forEach(r -> { |
| 565 | RegionId rid = r.id(); |
| 566 | UiRegion region = uiTopology.findRegion(rid); |
| 567 | if (region != null) { |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 568 | reconcileDevicesAndHostsWithRegion(allDevices, allHosts, rid, region); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 569 | } else { |
| 570 | log.warn("No UiRegion in topology for ID {}", rid); |
| 571 | } |
| 572 | }); |
| 573 | |
| 574 | // what is left over, must belong to the null-region |
| 575 | Set<DeviceId> leftOver = new HashSet<>(allDevices.size()); |
| 576 | allDevices.forEach(d -> leftOver.add(d.id())); |
| 577 | uiTopology.nullRegion().reconcileDevices(leftOver); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 578 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 579 | Set<HostId> leftOverHosts = new HashSet<>(allHosts.size()); |
| 580 | allHosts.forEach(h -> leftOverHosts.add(h.id())); |
| 581 | uiTopology.nullRegion().reconcileHosts(leftOverHosts); |
| 582 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 583 | // now that we have correct region hierarchy, and devices are in their |
| 584 | // respective regions, we can compute synthetic links for each region. |
| 585 | uiTopology.computeSynthLinks(); |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 588 | private void reconcileDevicesAndHostsWithRegion(Set<UiDevice> allDevices, |
| 589 | Set<UiHost> allHosts, |
| 590 | RegionId rid, |
| 591 | UiRegion region) { |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 592 | Set<DeviceId> deviceIds = services.region().getRegionDevices(rid); |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 593 | Set<HostId> hostIds = new HashSet<>(); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 594 | region.reconcileDevices(deviceIds); |
| 595 | |
| 596 | deviceIds.forEach(devId -> { |
| 597 | UiDevice dev = uiTopology.findDevice(devId); |
| 598 | if (dev != null) { |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 599 | dev.setRegionId(rid); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 600 | allDevices.remove(dev); |
| 601 | } else { |
| 602 | log.warn("Region device ID {} but no UiDevice in topology", |
| 603 | devId); |
| 604 | } |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 605 | |
| 606 | Set<Host> hosts = services.host().getConnectedHosts(devId); |
| 607 | for (Host h : hosts) { |
| 608 | HostId hid = h.id(); |
| 609 | hostIds.add(hid); |
| 610 | UiHost host = uiTopology.findHost(hid); |
| 611 | |
| 612 | if (host != null) { |
| 613 | host.setRegionId(rid); |
| 614 | allHosts.remove(host); |
| 615 | } else { |
| 616 | log.warn("Region host ID {} but no UiHost in topology", |
| 617 | hid); |
| 618 | } |
| 619 | } |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 620 | }); |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 621 | |
| 622 | region.reconcileHosts(hostIds); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 623 | } |
| 624 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 625 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 626 | // === CACHE STATISTICS |
| 627 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 628 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 629 | * Returns a detailed (multi-line) string showing the contents of the cache. |
| 630 | * |
| 631 | * @return detailed string |
| 632 | */ |
| 633 | public String dumpString() { |
| 634 | return uiTopology.dumpString(); |
| 635 | } |
| 636 | |
| 637 | /** |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 638 | * Returns the number of members in the cluster. |
| 639 | * |
| 640 | * @return number of cluster members |
| 641 | */ |
| 642 | public int clusterMemberCount() { |
| 643 | return uiTopology.clusterMemberCount(); |
| 644 | } |
| 645 | |
| 646 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 647 | * Returns the number of regions in the topology. |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 648 | * |
| 649 | * @return number of regions |
| 650 | */ |
| 651 | public int regionCount() { |
| 652 | return uiTopology.regionCount(); |
| 653 | } |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 654 | |
| 655 | /** |
| 656 | * Returns the number of devices in the topology. |
| 657 | * |
| 658 | * @return number of devices |
| 659 | */ |
| 660 | public int deviceCount() { |
| 661 | return uiTopology.deviceCount(); |
| 662 | } |
| 663 | |
| 664 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 665 | * Returns the number of device links in the topology. |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 666 | * |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 667 | * @return number of device links |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 668 | */ |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 669 | public int deviceLinkCount() { |
| 670 | return uiTopology.deviceLinkCount(); |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Returns the number of edge links in the topology. |
| 675 | * |
| 676 | * @return number of edge links |
| 677 | */ |
| 678 | public int edgeLinkCount() { |
| 679 | return uiTopology.edgeLinkCount(); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Returns the number of hosts in the topology. |
| 684 | * |
| 685 | * @return number of hosts |
| 686 | */ |
| 687 | public int hostCount() { |
| 688 | return uiTopology.hostCount(); |
| 689 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 690 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 691 | /** |
| 692 | * Returns the number of synthetic links in the topology. |
| 693 | * |
| 694 | * @return the number of synthetic links |
| 695 | */ |
| 696 | public int synthLinkCount() { |
| 697 | return uiTopology.synthLinkCount(); |
| 698 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 699 | } |