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