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