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