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