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) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 227 | Region regionForDevice = services.region().getRegionForDevice(device.id()); |
| 228 | if (regionForDevice != null) { |
| 229 | device.setRegionId(regionForDevice.id()); |
| 230 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | private void loadDevices() { |
| 234 | for (Device d : services.device().getDevices()) { |
| 235 | UiDevice device = addNewDevice(d); |
| 236 | updateDevice(device); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // invoked from UiSharedTopologyModel device listener |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 241 | void addOrUpdateDevice(Device device) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 242 | DeviceId id = device.id(); |
| 243 | UiDevice uiDevice = uiTopology.findDevice(id); |
| 244 | if (uiDevice == null) { |
| 245 | uiDevice = addNewDevice(device); |
| 246 | } |
| 247 | updateDevice(uiDevice); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 248 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 249 | postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice); |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 252 | // package private for unit test access |
| 253 | UiDevice accessDevice(DeviceId id) { |
| 254 | return uiTopology.findDevice(id); |
| 255 | } |
| 256 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 257 | // invoked from UiSharedTopologyModel device listener |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 258 | void removeDevice(Device device) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 259 | DeviceId id = device.id(); |
| 260 | UiDevice uiDevice = uiTopology.findDevice(id); |
| 261 | if (uiDevice != null) { |
| 262 | uiTopology.remove(uiDevice); |
| 263 | postEvent(DEVICE_REMOVED, uiDevice); |
| 264 | } else { |
| 265 | log.warn(E_NO_ELEMENT, "device", id); |
| 266 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 269 | |
| 270 | // === LINKS |
| 271 | |
| 272 | private UiLink addNewLink(UiLinkId id) { |
| 273 | UiLink uiLink = new UiLink(uiTopology, id); |
| 274 | uiTopology.add(uiLink); |
| 275 | return uiLink; |
| 276 | } |
| 277 | |
| 278 | private void updateLink(UiLink uiLink, Link link) { |
| 279 | uiLink.attachBackingLink(link); |
| 280 | } |
| 281 | |
| 282 | private void loadLinks() { |
| 283 | for (Link link : services.link().getLinks()) { |
| 284 | UiLinkId id = uiLinkId(link); |
| 285 | |
| 286 | UiLink uiLink = uiTopology.findLink(id); |
| 287 | if (uiLink == null) { |
| 288 | uiLink = addNewLink(id); |
| 289 | } |
| 290 | updateLink(uiLink, link); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // invoked from UiSharedTopologyModel link listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 295 | void addOrUpdateLink(Link link) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 296 | UiLinkId id = uiLinkId(link); |
| 297 | UiLink uiLink = uiTopology.findLink(id); |
| 298 | if (uiLink == null) { |
| 299 | uiLink = addNewLink(id); |
| 300 | } |
| 301 | updateLink(uiLink, link); |
| 302 | |
| 303 | postEvent(LINK_ADDED_OR_UPDATED, uiLink); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 306 | // package private for unit test access |
| 307 | UiLink accessLink(UiLinkId id) { |
| 308 | return uiTopology.findLink(id); |
| 309 | } |
| 310 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 311 | // invoked from UiSharedTopologyModel link listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 312 | void removeLink(Link link) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 313 | UiLinkId id = uiLinkId(link); |
| 314 | UiLink uiLink = uiTopology.findLink(id); |
| 315 | if (uiLink != null) { |
| 316 | boolean remaining = uiLink.detachBackingLink(link); |
| 317 | if (remaining) { |
| 318 | postEvent(LINK_ADDED_OR_UPDATED, uiLink); |
| 319 | } else { |
| 320 | uiTopology.remove(uiLink); |
| 321 | postEvent(LINK_REMOVED, uiLink); |
| 322 | } |
| 323 | } else { |
| 324 | log.warn(E_NO_ELEMENT, "link", id); |
| 325 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 328 | |
| 329 | // === HOSTS |
| 330 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 331 | private EdgeLink synthesizeLink(Host h) { |
| 332 | return createEdgeLink(h, true); |
| 333 | } |
| 334 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 335 | private UiHost addNewHost(Host h) { |
| 336 | UiHost host = new UiHost(uiTopology, h); |
| 337 | uiTopology.add(host); |
| 338 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 339 | EdgeLink elink = synthesizeLink(h); |
| 340 | UiLinkId elinkId = uiLinkId(elink); |
| 341 | host.setEdgeLinkId(elinkId); |
| 342 | |
| 343 | // add synthesized edge link to the topology |
| 344 | UiLink edgeLink = addNewLink(elinkId); |
| 345 | edgeLink.attachEdgeLink(elink); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 346 | |
| 347 | return host; |
| 348 | } |
| 349 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 350 | private void insertNewUiLink(UiLinkId id, EdgeLink e) { |
| 351 | UiLink newEdgeLink = addNewLink(id); |
| 352 | newEdgeLink.attachEdgeLink(e); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 353 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | private void updateHost(UiHost uiHost, Host h) { |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 357 | UiLink existing = uiTopology.findLink(uiHost.edgeLinkId()); |
| 358 | |
| 359 | EdgeLink currentElink = synthesizeLink(h); |
| 360 | UiLinkId currentElinkId = uiLinkId(currentElink); |
| 361 | |
| 362 | if (existing != null) { |
| 363 | if (!currentElinkId.equals(existing.id())) { |
| 364 | // edge link has changed |
| 365 | insertNewUiLink(currentElinkId, currentElink); |
| 366 | uiHost.setEdgeLinkId(currentElinkId); |
| 367 | |
| 368 | uiTopology.remove(existing); |
| 369 | } |
| 370 | |
| 371 | } else { |
| 372 | // no previously existing edge link |
| 373 | insertNewUiLink(currentElinkId, currentElink); |
| 374 | uiHost.setEdgeLinkId(currentElinkId); |
| 375 | |
| 376 | } |
| 377 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 378 | HostLocation hloc = h.location(); |
| 379 | uiHost.setLocation(hloc.deviceId(), hloc.port()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | private void loadHosts() { |
| 383 | for (Host h : services.host().getHosts()) { |
| 384 | UiHost host = addNewHost(h); |
| 385 | updateHost(host, h); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 390 | void addOrUpdateHost(Host host) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 391 | HostId id = host.id(); |
| 392 | UiHost uiHost = uiTopology.findHost(id); |
| 393 | if (uiHost == null) { |
| 394 | uiHost = addNewHost(host); |
| 395 | } |
| 396 | updateHost(uiHost, host); |
| 397 | |
| 398 | postEvent(HOST_ADDED_OR_UPDATED, uiHost); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 401 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 402 | void moveHost(Host host, Host prevHost) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 403 | UiHost uiHost = uiTopology.findHost(prevHost.id()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 404 | if (uiHost != null) { |
| 405 | updateHost(uiHost, host); |
| 406 | postEvent(HOST_MOVED, uiHost); |
| 407 | } else { |
| 408 | log.warn(E_NO_ELEMENT, "host", prevHost.id()); |
| 409 | } |
| 410 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 411 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 412 | // package private for unit test access |
| 413 | UiHost accessHost(HostId id) { |
| 414 | return uiTopology.findHost(id); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 417 | // invoked from UiSharedTopologyModel host listener |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 418 | void removeHost(Host host) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 419 | HostId id = host.id(); |
| 420 | UiHost uiHost = uiTopology.findHost(id); |
| 421 | if (uiHost != null) { |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 422 | UiLink edgeLink = uiTopology.findLink(uiHost.edgeLinkId()); |
| 423 | uiTopology.remove(edgeLink); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 424 | uiTopology.remove(uiHost); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 425 | postEvent(HOST_REMOVED, uiHost); |
| 426 | } else { |
| 427 | log.warn(E_NO_ELEMENT, "host", id); |
| 428 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 429 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 430 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 431 | |
| 432 | // === CACHE STATISTICS |
| 433 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 434 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 435 | * Returns a detailed (multi-line) string showing the contents of the cache. |
| 436 | * |
| 437 | * @return detailed string |
| 438 | */ |
| 439 | public String dumpString() { |
| 440 | return uiTopology.dumpString(); |
| 441 | } |
| 442 | |
| 443 | /** |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 444 | * Returns the number of members in the cluster. |
| 445 | * |
| 446 | * @return number of cluster members |
| 447 | */ |
| 448 | public int clusterMemberCount() { |
| 449 | return uiTopology.clusterMemberCount(); |
| 450 | } |
| 451 | |
| 452 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 453 | * Returns the number of regions in the topology. |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 454 | * |
| 455 | * @return number of regions |
| 456 | */ |
| 457 | public int regionCount() { |
| 458 | return uiTopology.regionCount(); |
| 459 | } |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 460 | |
| 461 | /** |
| 462 | * Returns the number of devices in the topology. |
| 463 | * |
| 464 | * @return number of devices |
| 465 | */ |
| 466 | public int deviceCount() { |
| 467 | return uiTopology.deviceCount(); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Returns the number of links in the topology. |
| 472 | * |
| 473 | * @return number of links |
| 474 | */ |
| 475 | public int linkCount() { |
| 476 | return uiTopology.linkCount(); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Returns the number of hosts in the topology. |
| 481 | * |
| 482 | * @return number of hosts |
| 483 | */ |
| 484 | public int hostCount() { |
| 485 | return uiTopology.hostCount(); |
| 486 | } |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 487 | } |