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