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