Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onlab.util.AbstractAccumulator; |
| 24 | import org.onlab.util.Accumulator; |
| 25 | import org.onosproject.cluster.ClusterEvent; |
| 26 | import org.onosproject.cluster.ClusterEventListener; |
| 27 | import org.onosproject.cluster.ControllerNode; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.event.Event; |
| 31 | import org.onosproject.mastership.MastershipAdminService; |
| 32 | import org.onosproject.mastership.MastershipEvent; |
| 33 | import org.onosproject.mastership.MastershipListener; |
| 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.Host; |
| 37 | import org.onosproject.net.HostId; |
| 38 | import org.onosproject.net.HostLocation; |
| 39 | import org.onosproject.net.Link; |
| 40 | import org.onosproject.net.device.DeviceEvent; |
| 41 | import org.onosproject.net.device.DeviceListener; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.FlowRuleEvent; |
| 45 | import org.onosproject.net.flow.FlowRuleListener; |
| 46 | import org.onosproject.net.flow.TrafficSelector; |
| 47 | import org.onosproject.net.flow.TrafficTreatment; |
| 48 | import org.onosproject.net.host.HostEvent; |
| 49 | import org.onosproject.net.host.HostListener; |
| 50 | import org.onosproject.net.intent.HostToHostIntent; |
| 51 | import org.onosproject.net.intent.Intent; |
| 52 | import org.onosproject.net.intent.IntentEvent; |
| 53 | import org.onosproject.net.intent.IntentListener; |
| 54 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 55 | import org.onosproject.net.link.LinkEvent; |
| 56 | import org.onosproject.net.link.LinkListener; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 57 | import org.onosproject.ui.JsonUtils; |
| 58 | import org.onosproject.ui.RequestHandler; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 59 | import org.onosproject.ui.UiConnection; |
| 60 | |
| 61 | import java.util.ArrayList; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 62 | import java.util.Collection; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 63 | import java.util.Collections; |
| 64 | import java.util.Comparator; |
| 65 | import java.util.HashSet; |
| 66 | import java.util.List; |
| 67 | import java.util.Set; |
| 68 | import java.util.Timer; |
| 69 | import java.util.TimerTask; |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 70 | import java.util.concurrent.ExecutorService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 71 | |
| 72 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 73 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 74 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 75 | import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED; |
| 76 | import static org.onosproject.net.DeviceId.deviceId; |
| 77 | import static org.onosproject.net.HostId.hostId; |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 78 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 79 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 80 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
| 81 | |
| 82 | /** |
| 83 | * Web socket capable of interacting with the GUI topology view. |
| 84 | */ |
| 85 | public class TopologyViewMessageHandler extends TopologyViewMessageHandlerBase { |
| 86 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 87 | private static final String REQ_DETAILS = "requestDetails"; |
| 88 | private static final String UPDATE_META = "updateMeta"; |
| 89 | private static final String ADD_HOST_INTENT = "addHostIntent"; |
| 90 | private static final String ADD_MULTI_SRC_INTENT = "addMultiSourceIntent"; |
| 91 | private static final String REQ_RELATED_INTENTS = "requestRelatedIntents"; |
| 92 | private static final String REQ_NEXT_INTENT = "requestNextRelatedIntent"; |
| 93 | private static final String REQ_PREV_INTENT = "requestPrevRelatedIntent"; |
| 94 | private static final String REQ_SEL_INTENT_TRAFFIC = "requestSelectedIntentTraffic"; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 95 | private static final String REQ_ALL_FLOW_TRAFFIC = "requestAllFlowTraffic"; |
| 96 | private static final String REQ_ALL_PORT_TRAFFIC = "requestAllPortTraffic"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 97 | private static final String REQ_DEV_LINK_FLOWS = "requestDeviceLinkFlows"; |
| 98 | private static final String CANCEL_TRAFFIC = "cancelTraffic"; |
| 99 | private static final String REQ_SUMMARY = "requestSummary"; |
| 100 | private static final String CANCEL_SUMMARY = "cancelSummary"; |
| 101 | private static final String EQ_MASTERS = "equalizeMasters"; |
| 102 | private static final String SPRITE_LIST_REQ = "spriteListRequest"; |
| 103 | private static final String SPRITE_DATA_REQ = "spriteDataRequest"; |
| 104 | private static final String TOPO_START = "topoStart"; |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 105 | private static final String TOPO_HEARTBEAT = "topoHeartbeat"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 106 | private static final String TOPO_STOP = "topoStop"; |
| 107 | |
| 108 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 109 | private static final String APP_ID = "org.onosproject.gui"; |
| 110 | |
| 111 | private static final long TRAFFIC_FREQUENCY = 5000; |
| 112 | private static final long SUMMARY_FREQUENCY = 30000; |
| 113 | |
| 114 | private static final Comparator<? super ControllerNode> NODE_COMPARATOR = |
Thomas Vachuska | c0fe09a | 2015-05-21 12:56:22 -0700 | [diff] [blame] | 115 | (o1, o2) -> o1.id().toString().compareTo(o2.id().toString()); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 116 | |
| 117 | |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 118 | private final Timer timer = new Timer("onos-topology-view"); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 119 | |
| 120 | private static final int MAX_EVENTS = 1000; |
| 121 | private static final int MAX_BATCH_MS = 5000; |
| 122 | private static final int MAX_IDLE_MS = 1000; |
| 123 | |
| 124 | private ApplicationId appId; |
| 125 | |
| 126 | private final ClusterEventListener clusterListener = new InternalClusterListener(); |
| 127 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
| 128 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 129 | private final LinkListener linkListener = new InternalLinkListener(); |
| 130 | private final HostListener hostListener = new InternalHostListener(); |
| 131 | private final IntentListener intentListener = new InternalIntentListener(); |
| 132 | private final FlowRuleListener flowListener = new InternalFlowListener(); |
| 133 | |
| 134 | private final Accumulator<Event> eventAccummulator = new InternalEventAccummulator(); |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 135 | private final ExecutorService msgSender = |
| 136 | newSingleThreadExecutor(groupedThreads("onos/gui", "msg-sender")); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 137 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 138 | private TimerTask trafficTask = null; |
| 139 | private TrafficEvent trafficEvent = null; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 140 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 141 | private TimerTask summaryTask = null; |
| 142 | private boolean summaryRunning = false; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 143 | |
| 144 | private boolean listenersRemoved = false; |
| 145 | |
| 146 | private TopologyViewIntentFilter intentFilter; |
| 147 | |
| 148 | // Current selection context |
| 149 | private Set<Host> selectedHosts; |
| 150 | private Set<Device> selectedDevices; |
| 151 | private List<Intent> selectedIntents; |
| 152 | private int currentIntentIndex = -1; |
| 153 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 154 | |
| 155 | @Override |
| 156 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 157 | super.init(connection, directory); |
| 158 | intentFilter = new TopologyViewIntentFilter(intentService, deviceService, |
| 159 | hostService, linkService); |
| 160 | appId = directory.get(CoreService.class).registerApplication(APP_ID); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void destroy() { |
| 165 | cancelAllRequests(); |
Thomas Vachuska | 2bb4863 | 2015-04-28 14:40:42 -0700 | [diff] [blame] | 166 | removeListeners(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 167 | super.destroy(); |
| 168 | } |
| 169 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 170 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 171 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 172 | return ImmutableSet.of( |
| 173 | new TopoStart(), |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 174 | new TopoHeartbeat(), |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 175 | new TopoStop(), |
| 176 | new ReqSummary(), |
| 177 | new CancelSummary(), |
| 178 | new SpriteListReq(), |
| 179 | new SpriteDataReq(), |
| 180 | new RequestDetails(), |
| 181 | new UpdateMeta(), |
| 182 | new EqMasters(), |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 183 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 184 | // TODO: migrate traffic related to separate app |
| 185 | new AddHostIntent(), |
| 186 | new AddMultiSourceIntent(), |
| 187 | new ReqRelatedIntents(), |
| 188 | new ReqNextIntent(), |
| 189 | new ReqPrevIntent(), |
| 190 | new ReqSelectedIntentTraffic(), |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 191 | new ReqAllFlowTraffic(), |
| 192 | new ReqAllPortTraffic(), |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 193 | new ReqDevLinkFlows(), |
| 194 | new CancelTraffic() |
| 195 | ); |
| 196 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 197 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 198 | // ================================================================== |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 199 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 200 | @Deprecated |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 201 | private final class TopoStart extends RequestHandler { |
| 202 | private TopoStart() { |
| 203 | super(TOPO_START); |
| 204 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 205 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public void process(long sid, ObjectNode payload) { |
| 208 | addListeners(); |
| 209 | sendAllInstances(null); |
| 210 | sendAllDevices(); |
| 211 | sendAllLinks(); |
| 212 | sendAllHosts(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 216 | @Deprecated |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 217 | private final class TopoHeartbeat extends RequestHandler { |
| 218 | private TopoHeartbeat() { |
| 219 | super(TOPO_HEARTBEAT); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void process(long sid, ObjectNode payload) { |
| 224 | // place holder for now |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | @Deprecated |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 229 | private final class TopoStop extends RequestHandler { |
| 230 | private TopoStop() { |
| 231 | super(TOPO_STOP); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void process(long sid, ObjectNode payload) { |
| 236 | stopSummaryMonitoring(); |
| 237 | stopTrafficMonitoring(); |
| 238 | } |
| 239 | } |
| 240 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 241 | @Deprecated |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 242 | private final class ReqSummary extends RequestHandler { |
| 243 | private ReqSummary() { |
| 244 | super(REQ_SUMMARY); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void process(long sid, ObjectNode payload) { |
| 249 | requestSummary(sid); |
| 250 | startSummaryMonitoring(); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | private final class CancelSummary extends RequestHandler { |
| 255 | private CancelSummary() { |
| 256 | super(CANCEL_SUMMARY); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void process(long sid, ObjectNode payload) { |
| 261 | stopSummaryMonitoring(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private final class SpriteListReq extends RequestHandler { |
| 266 | private SpriteListReq() { |
| 267 | super(SPRITE_LIST_REQ); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 272 | ObjectNode root = objectNode(); |
| 273 | ArrayNode names = arrayNode(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 274 | get(SpriteService.class).getNames().forEach(names::add); |
| 275 | root.set("names", names); |
| 276 | sendMessage("spriteListResponse", sid, root); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | private final class SpriteDataReq extends RequestHandler { |
| 281 | private SpriteDataReq() { |
| 282 | super(SPRITE_DATA_REQ); |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public void process(long sid, ObjectNode payload) { |
| 287 | String name = string(payload, "name"); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 288 | ObjectNode root = objectNode(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 289 | root.set("data", get(SpriteService.class).get(name)); |
| 290 | sendMessage("spriteDataResponse", sid, root); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | private final class RequestDetails extends RequestHandler { |
| 295 | private RequestDetails() { |
| 296 | super(REQ_DETAILS); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void process(long sid, ObjectNode payload) { |
| 301 | String type = string(payload, "class", "unknown"); |
| 302 | String id = JsonUtils.string(payload, "id"); |
| 303 | |
| 304 | if (type.equals("device")) { |
| 305 | sendMessage(deviceDetails(deviceId(id), sid)); |
| 306 | } else if (type.equals("host")) { |
| 307 | sendMessage(hostDetails(hostId(id), sid)); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | private final class UpdateMeta extends RequestHandler { |
| 313 | private UpdateMeta() { |
| 314 | super(UPDATE_META); |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public void process(long sid, ObjectNode payload) { |
| 319 | updateMetaUi(payload); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | private final class EqMasters extends RequestHandler { |
| 324 | private EqMasters() { |
| 325 | super(EQ_MASTERS); |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void process(long sid, ObjectNode payload) { |
| 330 | directory.get(MastershipAdminService.class).balanceRoles(); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // === TODO: move traffic related classes to traffic app |
| 335 | |
| 336 | private final class AddHostIntent extends RequestHandler { |
| 337 | private AddHostIntent() { |
| 338 | super(ADD_HOST_INTENT); |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public void process(long sid, ObjectNode payload) { |
| 343 | // TODO: add protection against device ids and non-existent hosts. |
| 344 | HostId one = hostId(string(payload, "one")); |
| 345 | HostId two = hostId(string(payload, "two")); |
| 346 | |
| 347 | HostToHostIntent intent = HostToHostIntent.builder() |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 348 | .appId(appId) |
| 349 | .one(one) |
| 350 | .two(two) |
| 351 | .build(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 352 | |
| 353 | intentService.submit(intent); |
| 354 | startMonitoringIntent(intent); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | private final class AddMultiSourceIntent extends RequestHandler { |
| 359 | private AddMultiSourceIntent() { |
| 360 | super(ADD_MULTI_SRC_INTENT); |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public void process(long sid, ObjectNode payload) { |
| 365 | // TODO: add protection against device ids and non-existent hosts. |
| 366 | Set<HostId> src = getHostIds((ArrayNode) payload.path("src")); |
| 367 | HostId dst = hostId(string(payload, "dst")); |
| 368 | Host dstHost = hostService.getHost(dst); |
| 369 | |
| 370 | Set<ConnectPoint> ingressPoints = getHostLocations(src); |
| 371 | |
| 372 | // FIXME: clearly, this is not enough |
| 373 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 374 | .matchEthDst(dstHost.mac()).build(); |
| 375 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 376 | |
| 377 | MultiPointToSinglePointIntent intent = |
| 378 | MultiPointToSinglePointIntent.builder() |
| 379 | .appId(appId) |
| 380 | .selector(selector) |
| 381 | .treatment(treatment) |
| 382 | .ingressPoints(ingressPoints) |
| 383 | .egressPoint(dstHost.location()) |
| 384 | .build(); |
| 385 | |
| 386 | intentService.submit(intent); |
| 387 | startMonitoringIntent(intent); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | private final class ReqRelatedIntents extends RequestHandler { |
| 392 | private ReqRelatedIntents() { |
| 393 | super(REQ_RELATED_INTENTS); |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public void process(long sid, ObjectNode payload) { |
| 398 | // Cancel any other traffic monitoring mode. |
| 399 | stopTrafficMonitoring(); |
| 400 | |
| 401 | if (!payload.has("ids")) { |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | // Get the set of selected hosts and their intents. |
| 406 | ArrayNode ids = (ArrayNode) payload.path("ids"); |
| 407 | selectedHosts = getHosts(ids); |
| 408 | selectedDevices = getDevices(ids); |
| 409 | selectedIntents = intentFilter.findPathIntents( |
| 410 | selectedHosts, selectedDevices, intentService.getIntents()); |
| 411 | currentIntentIndex = -1; |
| 412 | |
| 413 | if (haveSelectedIntents()) { |
| 414 | // Send a message to highlight all links of all monitored intents. |
| 415 | sendMessage(trafficMessage(new TrafficClass("primary", selectedIntents))); |
| 416 | } |
| 417 | |
| 418 | // TODO: Re-introduce once the client click vs hover gesture stuff is sorted out. |
| 419 | // String hover = string(payload, "hover"); |
| 420 | // if (!isNullOrEmpty(hover)) { |
| 421 | // // If there is a hover node, include it in the selection and find intents. |
| 422 | // processHoverExtendedSelection(sid, hover); |
| 423 | // } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | private final class ReqNextIntent extends RequestHandler { |
| 428 | private ReqNextIntent() { |
| 429 | super(REQ_NEXT_INTENT); |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public void process(long sid, ObjectNode payload) { |
| 434 | stopTrafficMonitoring(); |
| 435 | requestAnotherRelatedIntent(+1); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | private final class ReqPrevIntent extends RequestHandler { |
| 440 | private ReqPrevIntent() { |
| 441 | super(REQ_PREV_INTENT); |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public void process(long sid, ObjectNode payload) { |
| 446 | stopTrafficMonitoring(); |
| 447 | requestAnotherRelatedIntent(-1); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | private final class ReqSelectedIntentTraffic extends RequestHandler { |
| 452 | private ReqSelectedIntentTraffic() { |
| 453 | super(REQ_SEL_INTENT_TRAFFIC); |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public void process(long sid, ObjectNode payload) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 458 | trafficEvent = new TrafficEvent(TrafficEvent.Type.SEL_INTENT, payload); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 459 | requestSelectedIntentTraffic(); |
| 460 | startTrafficMonitoring(); |
| 461 | } |
| 462 | } |
| 463 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 464 | private final class ReqAllFlowTraffic extends RequestHandler { |
| 465 | private ReqAllFlowTraffic() { |
| 466 | super(REQ_ALL_FLOW_TRAFFIC); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public void process(long sid, ObjectNode payload) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 471 | trafficEvent = new TrafficEvent(TrafficEvent.Type.ALL_FLOW_TRAFFIC, payload); |
| 472 | requestAllFlowTraffic(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | private final class ReqAllPortTraffic extends RequestHandler { |
| 477 | private ReqAllPortTraffic() { |
| 478 | super(REQ_ALL_PORT_TRAFFIC); |
| 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public void process(long sid, ObjectNode payload) { |
| 483 | trafficEvent = new TrafficEvent(TrafficEvent.Type.ALL_PORT_TRAFFIC, payload); |
| 484 | requestAllPortTraffic(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | private final class ReqDevLinkFlows extends RequestHandler { |
| 489 | private ReqDevLinkFlows() { |
| 490 | super(REQ_DEV_LINK_FLOWS); |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public void process(long sid, ObjectNode payload) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 495 | trafficEvent = new TrafficEvent(TrafficEvent.Type.DEV_LINK_FLOWS, payload); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 496 | requestDeviceLinkFlows(payload); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | private final class CancelTraffic extends RequestHandler { |
| 501 | private CancelTraffic() { |
| 502 | super(CANCEL_TRAFFIC); |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public void process(long sid, ObjectNode payload) { |
| 507 | selectedIntents = null; |
| 508 | sendMessage(trafficMessage()); |
| 509 | stopTrafficMonitoring(); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | //======================================================================= |
| 514 | |
| 515 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 516 | // Sends the specified data to the client. |
| 517 | protected synchronized void sendMessage(ObjectNode data) { |
| 518 | UiConnection connection = connection(); |
| 519 | if (connection != null) { |
| 520 | connection.sendMessage(data); |
| 521 | } |
| 522 | } |
| 523 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 524 | // Subscribes for summary messages. |
| 525 | private synchronized void requestSummary(long sid) { |
| 526 | sendMessage(summmaryMessage(sid)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 529 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 530 | private void cancelAllRequests() { |
| 531 | stopSummaryMonitoring(); |
| 532 | stopTrafficMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // Sends all controller nodes to the client as node-added messages. |
| 536 | private void sendAllInstances(String messageType) { |
| 537 | List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes()); |
| 538 | Collections.sort(nodes, NODE_COMPARATOR); |
| 539 | for (ControllerNode node : nodes) { |
| 540 | sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node), |
| 541 | messageType)); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // Sends all devices to the client as device-added messages. |
| 546 | private void sendAllDevices() { |
| 547 | // Send optical first, others later for layered rendering |
| 548 | for (Device device : deviceService.getDevices()) { |
| 549 | if (device.type() == Device.Type.ROADM) { |
| 550 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 551 | } |
| 552 | } |
| 553 | for (Device device : deviceService.getDevices()) { |
| 554 | if (device.type() != Device.Type.ROADM) { |
| 555 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // Sends all links to the client as link-added messages. |
| 561 | private void sendAllLinks() { |
| 562 | // Send optical first, others later for layered rendering |
| 563 | for (Link link : linkService.getLinks()) { |
| 564 | if (link.type() == Link.Type.OPTICAL) { |
| 565 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 566 | } |
| 567 | } |
| 568 | for (Link link : linkService.getLinks()) { |
| 569 | if (link.type() != Link.Type.OPTICAL) { |
| 570 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // Sends all hosts to the client as host-added messages. |
| 576 | private void sendAllHosts() { |
| 577 | for (Host host : hostService.getHosts()) { |
| 578 | sendMessage(hostMessage(new HostEvent(HOST_ADDED, host))); |
| 579 | } |
| 580 | } |
| 581 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 582 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 583 | private synchronized void startMonitoringIntent(Intent intent) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 584 | selectedHosts = new HashSet<>(); |
| 585 | selectedDevices = new HashSet<>(); |
| 586 | selectedIntents = new ArrayList<>(); |
| 587 | selectedIntents.add(intent); |
| 588 | currentIntentIndex = -1; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 589 | requestAnotherRelatedIntent(+1); |
| 590 | requestSelectedIntentTraffic(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
| 594 | private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) { |
| 595 | Set<ConnectPoint> points = new HashSet<>(); |
| 596 | for (HostId hostId : hostIds) { |
| 597 | points.add(getHostLocation(hostId)); |
| 598 | } |
| 599 | return points; |
| 600 | } |
| 601 | |
| 602 | private HostLocation getHostLocation(HostId hostId) { |
| 603 | return hostService.getHost(hostId).location(); |
| 604 | } |
| 605 | |
| 606 | // Produces a list of host ids from the specified JSON array. |
| 607 | private Set<HostId> getHostIds(ArrayNode ids) { |
| 608 | Set<HostId> hostIds = new HashSet<>(); |
| 609 | for (JsonNode id : ids) { |
| 610 | hostIds.add(hostId(id.asText())); |
| 611 | } |
| 612 | return hostIds; |
| 613 | } |
| 614 | |
| 615 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 616 | private synchronized void startTrafficMonitoring() { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 617 | stopTrafficMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 618 | trafficTask = new TrafficMonitor(); |
| 619 | timer.schedule(trafficTask, TRAFFIC_FREQUENCY, TRAFFIC_FREQUENCY); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | private synchronized void stopTrafficMonitoring() { |
| 623 | if (trafficTask != null) { |
| 624 | trafficTask.cancel(); |
| 625 | trafficTask = null; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 629 | // Subscribes for flow traffic messages. |
| 630 | private synchronized void requestAllFlowTraffic() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 631 | startTrafficMonitoring(); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 632 | sendMessage(trafficSummaryMessage(StatsType.FLOW)); |
| 633 | } |
| 634 | |
| 635 | // Subscribes for port traffic messages. |
| 636 | private synchronized void requestAllPortTraffic() { |
| 637 | startTrafficMonitoring(); |
| 638 | sendMessage(trafficSummaryMessage(StatsType.PORT)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 641 | private void requestDeviceLinkFlows(ObjectNode payload) { |
| 642 | startTrafficMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 643 | |
| 644 | // Get the set of selected hosts and their intents. |
| 645 | ArrayNode ids = (ArrayNode) payload.path("ids"); |
| 646 | Set<Host> hosts = new HashSet<>(); |
| 647 | Set<Device> devices = getDevices(ids); |
| 648 | |
| 649 | // If there is a hover node, include it in the hosts and find intents. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 650 | String hover = JsonUtils.string(payload, "hover"); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 651 | if (!isNullOrEmpty(hover)) { |
| 652 | addHover(hosts, devices, hover); |
| 653 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 654 | sendMessage(flowSummaryMessage(devices)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 658 | private boolean haveSelectedIntents() { |
| 659 | return selectedIntents != null && !selectedIntents.isEmpty(); |
| 660 | } |
| 661 | |
| 662 | // Processes the selection extended with hovered item to segregate items |
| 663 | // into primary (those including the hover) vs secondary highlights. |
| 664 | private void processHoverExtendedSelection(long sid, String hover) { |
| 665 | Set<Host> hoverSelHosts = new HashSet<>(selectedHosts); |
| 666 | Set<Device> hoverSelDevices = new HashSet<>(selectedDevices); |
| 667 | addHover(hoverSelHosts, hoverSelDevices, hover); |
| 668 | |
| 669 | List<Intent> primary = selectedIntents == null ? new ArrayList<>() : |
| 670 | intentFilter.findPathIntents(hoverSelHosts, hoverSelDevices, |
| 671 | selectedIntents); |
| 672 | Set<Intent> secondary = new HashSet<>(selectedIntents); |
| 673 | secondary.removeAll(primary); |
| 674 | |
| 675 | // Send a message to highlight all links of all monitored intents. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 676 | sendMessage(trafficMessage(new TrafficClass("primary", primary), |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 677 | new TrafficClass("secondary", secondary))); |
| 678 | } |
| 679 | |
| 680 | // Requests next or previous related intent. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 681 | private void requestAnotherRelatedIntent(int offset) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 682 | if (haveSelectedIntents()) { |
| 683 | currentIntentIndex = currentIntentIndex + offset; |
| 684 | if (currentIntentIndex < 0) { |
| 685 | currentIntentIndex = selectedIntents.size() - 1; |
| 686 | } else if (currentIntentIndex >= selectedIntents.size()) { |
| 687 | currentIntentIndex = 0; |
| 688 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 689 | sendSelectedIntent(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
| 693 | // Sends traffic information on the related intents with the currently |
| 694 | // selected intent highlighted. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 695 | private void sendSelectedIntent() { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 696 | Intent selectedIntent = selectedIntents.get(currentIntentIndex); |
| 697 | log.info("Requested next intent {}", selectedIntent.id()); |
| 698 | |
| 699 | Set<Intent> primary = new HashSet<>(); |
| 700 | primary.add(selectedIntent); |
| 701 | |
| 702 | Set<Intent> secondary = new HashSet<>(selectedIntents); |
| 703 | secondary.remove(selectedIntent); |
| 704 | |
| 705 | // Send a message to highlight all links of the selected intent. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 706 | sendMessage(trafficMessage(new TrafficClass("primary", primary), |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 707 | new TrafficClass("secondary", secondary))); |
| 708 | } |
| 709 | |
| 710 | // Requests monitoring of traffic for the selected intent. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 711 | private void requestSelectedIntentTraffic() { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 712 | if (haveSelectedIntents()) { |
| 713 | if (currentIntentIndex < 0) { |
| 714 | currentIntentIndex = 0; |
| 715 | } |
| 716 | Intent selectedIntent = selectedIntents.get(currentIntentIndex); |
| 717 | log.info("Requested traffic for selected {}", selectedIntent.id()); |
| 718 | |
| 719 | Set<Intent> primary = new HashSet<>(); |
| 720 | primary.add(selectedIntent); |
| 721 | |
| 722 | // Send a message to highlight all links of the selected intent. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 723 | sendMessage(trafficMessage(new TrafficClass("primary", primary, true))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 727 | private synchronized void startSummaryMonitoring() { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 728 | stopSummaryMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 729 | summaryTask = new SummaryMonitor(); |
| 730 | timer.schedule(summaryTask, SUMMARY_FREQUENCY, SUMMARY_FREQUENCY); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 731 | summaryRunning = true; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | private synchronized void stopSummaryMonitoring() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 735 | if (summaryTask != null) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 736 | summaryTask.cancel(); |
| 737 | summaryTask = null; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 738 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 739 | summaryRunning = false; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 742 | |
| 743 | // Adds all internal listeners. |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 744 | private synchronized void addListeners() { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 745 | listenersRemoved = false; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 746 | clusterService.addListener(clusterListener); |
| 747 | mastershipService.addListener(mastershipListener); |
| 748 | deviceService.addListener(deviceListener); |
| 749 | linkService.addListener(linkListener); |
| 750 | hostService.addListener(hostListener); |
| 751 | intentService.addListener(intentListener); |
| 752 | flowService.addListener(flowListener); |
| 753 | } |
| 754 | |
| 755 | // Removes all internal listeners. |
| 756 | private synchronized void removeListeners() { |
| 757 | if (!listenersRemoved) { |
| 758 | listenersRemoved = true; |
| 759 | clusterService.removeListener(clusterListener); |
| 760 | mastershipService.removeListener(mastershipListener); |
| 761 | deviceService.removeListener(deviceListener); |
| 762 | linkService.removeListener(linkListener); |
| 763 | hostService.removeListener(hostListener); |
| 764 | intentService.removeListener(intentListener); |
| 765 | flowService.removeListener(flowListener); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | // Cluster event listener. |
| 770 | private class InternalClusterListener implements ClusterEventListener { |
| 771 | @Override |
| 772 | public void event(ClusterEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 773 | msgSender.execute(() -> sendMessage(instanceMessage(event, null))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
| 777 | // Mastership change listener |
| 778 | private class InternalMastershipListener implements MastershipListener { |
| 779 | @Override |
| 780 | public void event(MastershipEvent event) { |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 781 | msgSender.execute(() -> { |
| 782 | sendAllInstances("updateInstance"); |
| 783 | Device device = deviceService.getDevice(event.subject()); |
| 784 | if (device != null) { |
| 785 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device))); |
| 786 | } |
| 787 | }); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
| 791 | // Device event listener. |
| 792 | private class InternalDeviceListener implements DeviceListener { |
| 793 | @Override |
| 794 | public void event(DeviceEvent event) { |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 795 | if (event.type() != PORT_STATS_UPDATED) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 796 | msgSender.execute(() -> sendMessage(deviceMessage(event))); |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 797 | eventAccummulator.add(event); |
| 798 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
| 802 | // Link event listener. |
| 803 | private class InternalLinkListener implements LinkListener { |
| 804 | @Override |
| 805 | public void event(LinkEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 806 | msgSender.execute(() -> sendMessage(linkMessage(event))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 807 | eventAccummulator.add(event); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // Host event listener. |
| 812 | private class InternalHostListener implements HostListener { |
| 813 | @Override |
| 814 | public void event(HostEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 815 | msgSender.execute(() -> sendMessage(hostMessage(event))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 816 | eventAccummulator.add(event); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | // Intent event listener. |
| 821 | private class InternalIntentListener implements IntentListener { |
| 822 | @Override |
| 823 | public void event(IntentEvent event) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 824 | if (trafficTask != null) { |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 825 | msgSender.execute(TopologyViewMessageHandler.this::requestSelectedIntentTraffic); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 826 | } |
| 827 | eventAccummulator.add(event); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | // Intent event listener. |
| 832 | private class InternalFlowListener implements FlowRuleListener { |
| 833 | @Override |
| 834 | public void event(FlowRuleEvent event) { |
| 835 | eventAccummulator.add(event); |
| 836 | } |
| 837 | } |
| 838 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 839 | // encapsulate |
| 840 | private static class TrafficEvent { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 841 | enum Type { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 842 | ALL_FLOW_TRAFFIC, ALL_PORT_TRAFFIC, DEV_LINK_FLOWS, SEL_INTENT |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 843 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 844 | |
| 845 | private final Type type; |
| 846 | private final ObjectNode payload; |
| 847 | |
| 848 | TrafficEvent(Type type, ObjectNode payload) { |
| 849 | this.type = type; |
| 850 | this.payload = payload; |
| 851 | } |
| 852 | } |
| 853 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 854 | // Periodic update of the traffic information |
| 855 | private class TrafficMonitor extends TimerTask { |
| 856 | @Override |
| 857 | public void run() { |
| 858 | try { |
| 859 | if (trafficEvent != null) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 860 | switch (trafficEvent.type) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame^] | 861 | case ALL_FLOW_TRAFFIC: |
| 862 | requestAllFlowTraffic(); |
| 863 | break; |
| 864 | case ALL_PORT_TRAFFIC: |
| 865 | requestAllPortTraffic(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 866 | break; |
| 867 | case DEV_LINK_FLOWS: |
| 868 | requestDeviceLinkFlows(trafficEvent.payload); |
| 869 | break; |
| 870 | case SEL_INTENT: |
| 871 | requestSelectedIntentTraffic(); |
| 872 | break; |
| 873 | default: |
| 874 | // nothing to do |
| 875 | break; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } catch (Exception e) { |
| 879 | log.warn("Unable to handle traffic request due to {}", e.getMessage()); |
| 880 | log.warn("Boom!", e); |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | // Periodic update of the summary information |
| 886 | private class SummaryMonitor extends TimerTask { |
| 887 | @Override |
| 888 | public void run() { |
| 889 | try { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 890 | if (summaryRunning) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 891 | msgSender.execute(() -> requestSummary(0)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 892 | } |
| 893 | } catch (Exception e) { |
| 894 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
| 895 | log.warn("Boom!", e); |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // Accumulates events to drive methodic update of the summary pane. |
| 901 | private class InternalEventAccummulator extends AbstractAccumulator<Event> { |
| 902 | protected InternalEventAccummulator() { |
| 903 | super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS); |
| 904 | } |
| 905 | |
| 906 | @Override |
| 907 | public void processItems(List<Event> items) { |
| 908 | try { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 909 | if (summaryRunning) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 910 | msgSender.execute(() -> requestSummary(0)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 911 | } |
| 912 | } catch (Exception e) { |
| 913 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
| 914 | log.debug("Boom!", e); |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | } |