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