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