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; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 51 | import org.onosproject.net.intent.IntentEvent; |
| 52 | import org.onosproject.net.intent.IntentListener; |
| 53 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 54 | import org.onosproject.net.link.LinkEvent; |
| 55 | import org.onosproject.net.link.LinkListener; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 56 | import org.onosproject.ui.RequestHandler; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 57 | import org.onosproject.ui.UiConnection; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 58 | import org.onosproject.ui.impl.TrafficMonitor.Mode; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 59 | import org.onosproject.ui.topo.Highlights; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 60 | import org.onosproject.ui.topo.NodeSelection; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 61 | import org.onosproject.ui.topo.PropertyPanel; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 62 | |
| 63 | import java.util.ArrayList; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 64 | import java.util.Collection; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 65 | import java.util.Collections; |
| 66 | import java.util.Comparator; |
| 67 | import java.util.HashSet; |
| 68 | import java.util.List; |
| 69 | import java.util.Set; |
| 70 | import java.util.Timer; |
| 71 | import java.util.TimerTask; |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 72 | import java.util.concurrent.ExecutorService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 73 | |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 74 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 75 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 76 | import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED; |
| 77 | import static org.onosproject.net.DeviceId.deviceId; |
| 78 | import static org.onosproject.net.HostId.hostId; |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 79 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 80 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 81 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 82 | import static org.onosproject.ui.JsonUtils.envelope; |
| 83 | import static org.onosproject.ui.impl.topo.TopoJson.json; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Web socket capable of interacting with the GUI topology view. |
| 87 | */ |
| 88 | public class TopologyViewMessageHandler extends TopologyViewMessageHandlerBase { |
| 89 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 90 | // incoming event types |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 91 | private static final String REQ_DETAILS = "requestDetails"; |
| 92 | private static final String UPDATE_META = "updateMeta"; |
| 93 | private static final String ADD_HOST_INTENT = "addHostIntent"; |
| 94 | private static final String ADD_MULTI_SRC_INTENT = "addMultiSourceIntent"; |
| 95 | private static final String REQ_RELATED_INTENTS = "requestRelatedIntents"; |
| 96 | private static final String REQ_NEXT_INTENT = "requestNextRelatedIntent"; |
| 97 | private static final String REQ_PREV_INTENT = "requestPrevRelatedIntent"; |
| 98 | private static final String REQ_SEL_INTENT_TRAFFIC = "requestSelectedIntentTraffic"; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 99 | private static final String REQ_ALL_FLOW_TRAFFIC = "requestAllFlowTraffic"; |
| 100 | private static final String REQ_ALL_PORT_TRAFFIC = "requestAllPortTraffic"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 101 | private static final String REQ_DEV_LINK_FLOWS = "requestDeviceLinkFlows"; |
| 102 | private static final String CANCEL_TRAFFIC = "cancelTraffic"; |
| 103 | private static final String REQ_SUMMARY = "requestSummary"; |
| 104 | private static final String CANCEL_SUMMARY = "cancelSummary"; |
| 105 | private static final String EQ_MASTERS = "equalizeMasters"; |
| 106 | private static final String SPRITE_LIST_REQ = "spriteListRequest"; |
| 107 | private static final String SPRITE_DATA_REQ = "spriteDataRequest"; |
| 108 | private static final String TOPO_START = "topoStart"; |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 109 | private static final String TOPO_HEARTBEAT = "topoHeartbeat"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 110 | private static final String TOPO_SELECT_OVERLAY = "topoSelectOverlay"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 111 | private static final String TOPO_STOP = "topoStop"; |
| 112 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 113 | // outgoing event types |
| 114 | private static final String SHOW_SUMMARY = "showSummary"; |
| 115 | private static final String SHOW_DETAILS = "showDetails"; |
| 116 | private static final String SPRITE_LIST_RESPONSE = "spriteListResponse"; |
| 117 | private static final String SPRITE_DATA_RESPONSE = "spriteDataResponse"; |
| 118 | private static final String UPDATE_INSTANCE = "updateInstance"; |
| 119 | |
| 120 | // fields |
| 121 | private static final String ID = "id"; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 122 | private static final String DEVICE = "device"; |
| 123 | private static final String HOST = "host"; |
| 124 | private static final String CLASS = "class"; |
| 125 | private static final String UNKNOWN = "unknown"; |
| 126 | private static final String ONE = "one"; |
| 127 | private static final String TWO = "two"; |
| 128 | private static final String SRC = "src"; |
| 129 | private static final String DST = "dst"; |
| 130 | private static final String DATA = "data"; |
| 131 | private static final String NAME = "name"; |
| 132 | private static final String NAMES = "names"; |
| 133 | private static final String ACTIVATE = "activate"; |
| 134 | private static final String DEACTIVATE = "deactivate"; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 135 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 136 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 137 | private static final String APP_ID = "org.onosproject.gui"; |
| 138 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 139 | private static final long TRAFFIC_PERIOD = 5000; |
| 140 | private static final long SUMMARY_PERIOD = 30000; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 141 | |
| 142 | private static final Comparator<? super ControllerNode> NODE_COMPARATOR = |
Thomas Vachuska | c0fe09a | 2015-05-21 12:56:22 -0700 | [diff] [blame] | 143 | (o1, o2) -> o1.id().toString().compareTo(o2.id().toString()); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 144 | |
| 145 | |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 146 | private final Timer timer = new Timer("onos-topology-view"); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 147 | |
| 148 | private static final int MAX_EVENTS = 1000; |
| 149 | private static final int MAX_BATCH_MS = 5000; |
| 150 | private static final int MAX_IDLE_MS = 1000; |
| 151 | |
| 152 | private ApplicationId appId; |
| 153 | |
| 154 | private final ClusterEventListener clusterListener = new InternalClusterListener(); |
| 155 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
| 156 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 157 | private final LinkListener linkListener = new InternalLinkListener(); |
| 158 | private final HostListener hostListener = new InternalHostListener(); |
| 159 | private final IntentListener intentListener = new InternalIntentListener(); |
| 160 | private final FlowRuleListener flowListener = new InternalFlowListener(); |
| 161 | |
| 162 | private final Accumulator<Event> eventAccummulator = new InternalEventAccummulator(); |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 163 | private final ExecutorService msgSender = |
| 164 | newSingleThreadExecutor(groupedThreads("onos/gui", "msg-sender")); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 165 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 166 | private TopoOverlayCache overlayCache; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 167 | private TrafficMonitor traffic; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 168 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 169 | private TimerTask summaryTask = null; |
| 170 | private boolean summaryRunning = false; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 171 | |
| 172 | private boolean listenersRemoved = false; |
| 173 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 174 | |
| 175 | @Override |
| 176 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 177 | super.init(connection, directory); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 178 | appId = directory.get(CoreService.class).registerApplication(APP_ID); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 179 | traffic = new TrafficMonitor(TRAFFIC_PERIOD, servicesBundle, this); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void destroy() { |
| 184 | cancelAllRequests(); |
Thomas Vachuska | 2bb4863 | 2015-04-28 14:40:42 -0700 | [diff] [blame] | 185 | removeListeners(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 186 | super.destroy(); |
| 187 | } |
| 188 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 189 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 190 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 191 | return ImmutableSet.of( |
| 192 | new TopoStart(), |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 193 | new TopoHeartbeat(), |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 194 | new TopoSelectOverlay(), |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 195 | new TopoStop(), |
| 196 | new ReqSummary(), |
| 197 | new CancelSummary(), |
| 198 | new SpriteListReq(), |
| 199 | new SpriteDataReq(), |
| 200 | new RequestDetails(), |
| 201 | new UpdateMeta(), |
| 202 | new EqMasters(), |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 203 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 204 | // TODO: migrate traffic related to separate app |
| 205 | new AddHostIntent(), |
| 206 | new AddMultiSourceIntent(), |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 207 | |
| 208 | new ReqAllFlowTraffic(), |
| 209 | new ReqAllPortTraffic(), |
| 210 | new ReqDevLinkFlows(), |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 211 | new ReqRelatedIntents(), |
| 212 | new ReqNextIntent(), |
| 213 | new ReqPrevIntent(), |
| 214 | new ReqSelectedIntentTraffic(), |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 215 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 216 | new CancelTraffic() |
| 217 | ); |
| 218 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 219 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 220 | /** |
| 221 | * Injects the topology overlay cache. |
| 222 | * |
| 223 | * @param overlayCache injected cache |
| 224 | */ |
| 225 | void setOverlayCache(TopoOverlayCache overlayCache) { |
| 226 | this.overlayCache = overlayCache; |
| 227 | } |
| 228 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 229 | // ================================================================== |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 230 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 231 | private final class TopoStart extends RequestHandler { |
| 232 | private TopoStart() { |
| 233 | super(TOPO_START); |
| 234 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 235 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 236 | @Override |
| 237 | public void process(long sid, ObjectNode payload) { |
| 238 | addListeners(); |
| 239 | sendAllInstances(null); |
| 240 | sendAllDevices(); |
| 241 | sendAllLinks(); |
| 242 | sendAllHosts(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 246 | private final class TopoHeartbeat extends RequestHandler { |
| 247 | private TopoHeartbeat() { |
| 248 | super(TOPO_HEARTBEAT); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void process(long sid, ObjectNode payload) { |
| 253 | // place holder for now |
| 254 | } |
| 255 | } |
| 256 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 257 | private final class TopoSelectOverlay extends RequestHandler { |
| 258 | private TopoSelectOverlay() { |
| 259 | super(TOPO_SELECT_OVERLAY); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 264 | String deact = string(payload, DEACTIVATE); |
| 265 | String act = string(payload, ACTIVATE); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 266 | overlayCache.switchOverlay(deact, act); |
| 267 | } |
| 268 | } |
| 269 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 270 | private final class TopoStop extends RequestHandler { |
| 271 | private TopoStop() { |
| 272 | super(TOPO_STOP); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void process(long sid, ObjectNode payload) { |
| 277 | stopSummaryMonitoring(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 278 | traffic.stopMonitoring(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | private final class ReqSummary extends RequestHandler { |
| 283 | private ReqSummary() { |
| 284 | super(REQ_SUMMARY); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void process(long sid, ObjectNode payload) { |
| 289 | requestSummary(sid); |
| 290 | startSummaryMonitoring(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | private final class CancelSummary extends RequestHandler { |
| 295 | private CancelSummary() { |
| 296 | super(CANCEL_SUMMARY); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void process(long sid, ObjectNode payload) { |
| 301 | stopSummaryMonitoring(); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private final class SpriteListReq extends RequestHandler { |
| 306 | private SpriteListReq() { |
| 307 | super(SPRITE_LIST_REQ); |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 312 | ObjectNode root = objectNode(); |
| 313 | ArrayNode names = arrayNode(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 314 | get(SpriteService.class).getNames().forEach(names::add); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 315 | root.set(NAMES, names); |
| 316 | sendMessage(SPRITE_LIST_RESPONSE, sid, root); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | private final class SpriteDataReq extends RequestHandler { |
| 321 | private SpriteDataReq() { |
| 322 | super(SPRITE_DATA_REQ); |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 327 | String name = string(payload, NAME); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 328 | ObjectNode root = objectNode(); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 329 | root.set(DATA, get(SpriteService.class).get(name)); |
| 330 | sendMessage(SPRITE_DATA_RESPONSE, sid, root); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | private final class RequestDetails extends RequestHandler { |
| 335 | private RequestDetails() { |
| 336 | super(REQ_DETAILS); |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 341 | String type = string(payload, CLASS, UNKNOWN); |
| 342 | String id = string(payload, ID); |
| 343 | PropertyPanel pp = null; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 344 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 345 | if (type.equals(DEVICE)) { |
| 346 | pp = deviceDetails(deviceId(id), sid); |
| 347 | overlayCache.currentOverlay().modifyDeviceDetails(pp); |
| 348 | } else if (type.equals(HOST)) { |
| 349 | pp = hostDetails(hostId(id), sid); |
| 350 | overlayCache.currentOverlay().modifyHostDetails(pp); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 351 | } |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 352 | |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 353 | sendMessage(envelope(SHOW_DETAILS, sid, json(pp))); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | private final class UpdateMeta extends RequestHandler { |
| 358 | private UpdateMeta() { |
| 359 | super(UPDATE_META); |
| 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public void process(long sid, ObjectNode payload) { |
| 364 | updateMetaUi(payload); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | private final class EqMasters extends RequestHandler { |
| 369 | private EqMasters() { |
| 370 | super(EQ_MASTERS); |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public void process(long sid, ObjectNode payload) { |
| 375 | directory.get(MastershipAdminService.class).balanceRoles(); |
| 376 | } |
| 377 | } |
| 378 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 379 | |
| 380 | // ========= ----------------------------------------------------------------- |
| 381 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 382 | // === TODO: move traffic related classes to traffic app |
| 383 | |
| 384 | private final class AddHostIntent extends RequestHandler { |
| 385 | private AddHostIntent() { |
| 386 | super(ADD_HOST_INTENT); |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public void process(long sid, ObjectNode payload) { |
| 391 | // TODO: add protection against device ids and non-existent hosts. |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 392 | HostId one = hostId(string(payload, ONE)); |
| 393 | HostId two = hostId(string(payload, TWO)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 394 | |
| 395 | HostToHostIntent intent = HostToHostIntent.builder() |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 396 | .appId(appId) |
| 397 | .one(one) |
| 398 | .two(two) |
| 399 | .build(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 400 | |
| 401 | intentService.submit(intent); |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 402 | if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) { |
| 403 | traffic.monitor(intent); |
| 404 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | private final class AddMultiSourceIntent extends RequestHandler { |
| 409 | private AddMultiSourceIntent() { |
| 410 | super(ADD_MULTI_SRC_INTENT); |
| 411 | } |
| 412 | |
| 413 | @Override |
| 414 | public void process(long sid, ObjectNode payload) { |
| 415 | // TODO: add protection against device ids and non-existent hosts. |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 416 | Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC)); |
| 417 | HostId dst = hostId(string(payload, DST)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 418 | Host dstHost = hostService.getHost(dst); |
| 419 | |
| 420 | Set<ConnectPoint> ingressPoints = getHostLocations(src); |
| 421 | |
| 422 | // FIXME: clearly, this is not enough |
| 423 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 424 | .matchEthDst(dstHost.mac()).build(); |
| 425 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 426 | |
| 427 | MultiPointToSinglePointIntent intent = |
| 428 | MultiPointToSinglePointIntent.builder() |
| 429 | .appId(appId) |
| 430 | .selector(selector) |
| 431 | .treatment(treatment) |
| 432 | .ingressPoints(ingressPoints) |
| 433 | .egressPoint(dstHost.location()) |
| 434 | .build(); |
| 435 | |
| 436 | intentService.submit(intent); |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 437 | if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) { |
| 438 | traffic.monitor(intent); |
| 439 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 443 | // ========= ----------------------------------------------------------------- |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 444 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 445 | private final class ReqAllFlowTraffic extends RequestHandler { |
| 446 | private ReqAllFlowTraffic() { |
| 447 | super(REQ_ALL_FLOW_TRAFFIC); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 452 | traffic.monitor(Mode.ALL_FLOW_TRAFFIC); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | private final class ReqAllPortTraffic extends RequestHandler { |
| 457 | private ReqAllPortTraffic() { |
| 458 | super(REQ_ALL_PORT_TRAFFIC); |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 463 | traffic.monitor(Mode.ALL_PORT_TRAFFIC); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | private final class ReqDevLinkFlows extends RequestHandler { |
| 468 | private ReqDevLinkFlows() { |
| 469 | super(REQ_DEV_LINK_FLOWS); |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 474 | NodeSelection nodeSelection = |
| 475 | new NodeSelection(payload, deviceService, hostService); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 476 | traffic.monitor(Mode.DEV_LINK_FLOWS, nodeSelection); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | private final class ReqRelatedIntents extends RequestHandler { |
| 481 | private ReqRelatedIntents() { |
| 482 | super(REQ_RELATED_INTENTS); |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public void process(long sid, ObjectNode payload) { |
| 487 | NodeSelection nodeSelection = |
| 488 | new NodeSelection(payload, deviceService, hostService); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 489 | traffic.monitor(Mode.RELATED_INTENTS, nodeSelection); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
| 493 | private final class ReqNextIntent extends RequestHandler { |
| 494 | private ReqNextIntent() { |
| 495 | super(REQ_NEXT_INTENT); |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 500 | traffic.selectNextIntent(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | private final class ReqPrevIntent extends RequestHandler { |
| 505 | private ReqPrevIntent() { |
| 506 | super(REQ_PREV_INTENT); |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 511 | traffic.selectPreviousIntent(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
| 515 | private final class ReqSelectedIntentTraffic extends RequestHandler { |
| 516 | private ReqSelectedIntentTraffic() { |
| 517 | super(REQ_SEL_INTENT_TRAFFIC); |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 522 | traffic.monitor(Mode.SELECTED_INTENT); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | private final class CancelTraffic extends RequestHandler { |
| 527 | private CancelTraffic() { |
| 528 | super(CANCEL_TRAFFIC); |
| 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 533 | traffic.stopMonitoring(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
| 537 | //======================================================================= |
| 538 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 539 | // Converts highlights to JSON format and sends the message to the client |
| 540 | protected void sendHighlights(Highlights highlights) { |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 541 | sendMessage(envelope(SHOW_HIGHLIGHTS, json(highlights))); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 542 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 543 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 544 | // Sends the specified data to the client. |
| 545 | protected synchronized void sendMessage(ObjectNode data) { |
| 546 | UiConnection connection = connection(); |
| 547 | if (connection != null) { |
| 548 | connection.sendMessage(data); |
| 549 | } |
| 550 | } |
| 551 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 552 | // Subscribes for summary messages. |
| 553 | private synchronized void requestSummary(long sid) { |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 554 | PropertyPanel pp = summmaryMessage(sid); |
| 555 | overlayCache.currentOverlay().modifySummary(pp); |
Simon Hunt | d3ceffa | 2015-08-25 12:44:35 -0700 | [diff] [blame] | 556 | sendMessage(envelope(SHOW_SUMMARY, sid, json(pp))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 559 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 560 | private void cancelAllRequests() { |
| 561 | stopSummaryMonitoring(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 562 | traffic.stopMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // Sends all controller nodes to the client as node-added messages. |
| 566 | private void sendAllInstances(String messageType) { |
| 567 | List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes()); |
| 568 | Collections.sort(nodes, NODE_COMPARATOR); |
| 569 | for (ControllerNode node : nodes) { |
| 570 | sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node), |
| 571 | messageType)); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // Sends all devices to the client as device-added messages. |
| 576 | private void sendAllDevices() { |
| 577 | // Send optical first, others later for layered rendering |
| 578 | for (Device device : deviceService.getDevices()) { |
| 579 | if (device.type() == Device.Type.ROADM) { |
| 580 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 581 | } |
| 582 | } |
| 583 | for (Device device : deviceService.getDevices()) { |
| 584 | if (device.type() != Device.Type.ROADM) { |
| 585 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // Sends all links to the client as link-added messages. |
| 591 | private void sendAllLinks() { |
| 592 | // Send optical first, others later for layered rendering |
| 593 | for (Link link : linkService.getLinks()) { |
| 594 | if (link.type() == Link.Type.OPTICAL) { |
| 595 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 596 | } |
| 597 | } |
| 598 | for (Link link : linkService.getLinks()) { |
| 599 | if (link.type() != Link.Type.OPTICAL) { |
| 600 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | // Sends all hosts to the client as host-added messages. |
| 606 | private void sendAllHosts() { |
| 607 | for (Host host : hostService.getHosts()) { |
| 608 | sendMessage(hostMessage(new HostEvent(HOST_ADDED, host))); |
| 609 | } |
| 610 | } |
| 611 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 612 | private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) { |
| 613 | Set<ConnectPoint> points = new HashSet<>(); |
| 614 | for (HostId hostId : hostIds) { |
| 615 | points.add(getHostLocation(hostId)); |
| 616 | } |
| 617 | return points; |
| 618 | } |
| 619 | |
| 620 | private HostLocation getHostLocation(HostId hostId) { |
| 621 | return hostService.getHost(hostId).location(); |
| 622 | } |
| 623 | |
| 624 | // Produces a list of host ids from the specified JSON array. |
| 625 | private Set<HostId> getHostIds(ArrayNode ids) { |
| 626 | Set<HostId> hostIds = new HashSet<>(); |
| 627 | for (JsonNode id : ids) { |
| 628 | hostIds.add(hostId(id.asText())); |
| 629 | } |
| 630 | return hostIds; |
| 631 | } |
| 632 | |
| 633 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 634 | private synchronized void startSummaryMonitoring() { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 635 | stopSummaryMonitoring(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 636 | summaryTask = new SummaryMonitor(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 637 | timer.schedule(summaryTask, SUMMARY_PERIOD, SUMMARY_PERIOD); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 638 | summaryRunning = true; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | private synchronized void stopSummaryMonitoring() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 642 | if (summaryTask != null) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 643 | summaryTask.cancel(); |
| 644 | summaryTask = null; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 645 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 646 | summaryRunning = false; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 649 | |
| 650 | // Adds all internal listeners. |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 651 | private synchronized void addListeners() { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 652 | listenersRemoved = false; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 653 | clusterService.addListener(clusterListener); |
| 654 | mastershipService.addListener(mastershipListener); |
| 655 | deviceService.addListener(deviceListener); |
| 656 | linkService.addListener(linkListener); |
| 657 | hostService.addListener(hostListener); |
| 658 | intentService.addListener(intentListener); |
| 659 | flowService.addListener(flowListener); |
| 660 | } |
| 661 | |
| 662 | // Removes all internal listeners. |
| 663 | private synchronized void removeListeners() { |
| 664 | if (!listenersRemoved) { |
| 665 | listenersRemoved = true; |
| 666 | clusterService.removeListener(clusterListener); |
| 667 | mastershipService.removeListener(mastershipListener); |
| 668 | deviceService.removeListener(deviceListener); |
| 669 | linkService.removeListener(linkListener); |
| 670 | hostService.removeListener(hostListener); |
| 671 | intentService.removeListener(intentListener); |
| 672 | flowService.removeListener(flowListener); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | // Cluster event listener. |
| 677 | private class InternalClusterListener implements ClusterEventListener { |
| 678 | @Override |
| 679 | public void event(ClusterEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 680 | msgSender.execute(() -> sendMessage(instanceMessage(event, null))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
| 684 | // Mastership change listener |
| 685 | private class InternalMastershipListener implements MastershipListener { |
| 686 | @Override |
| 687 | public void event(MastershipEvent event) { |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 688 | msgSender.execute(() -> { |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 689 | sendAllInstances(UPDATE_INSTANCE); |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 690 | Device device = deviceService.getDevice(event.subject()); |
| 691 | if (device != null) { |
| 692 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device))); |
| 693 | } |
| 694 | }); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | // Device event listener. |
| 699 | private class InternalDeviceListener implements DeviceListener { |
| 700 | @Override |
| 701 | public void event(DeviceEvent event) { |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 702 | if (event.type() != PORT_STATS_UPDATED) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 703 | msgSender.execute(() -> sendMessage(deviceMessage(event))); |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 704 | eventAccummulator.add(event); |
| 705 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
| 709 | // Link event listener. |
| 710 | private class InternalLinkListener implements LinkListener { |
| 711 | @Override |
| 712 | public void event(LinkEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 713 | msgSender.execute(() -> sendMessage(linkMessage(event))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 714 | eventAccummulator.add(event); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | // Host event listener. |
| 719 | private class InternalHostListener implements HostListener { |
| 720 | @Override |
| 721 | public void event(HostEvent event) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 722 | msgSender.execute(() -> sendMessage(hostMessage(event))); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 723 | eventAccummulator.add(event); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | // Intent event listener. |
| 728 | private class InternalIntentListener implements IntentListener { |
| 729 | @Override |
| 730 | public void event(IntentEvent event) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 731 | msgSender.execute(traffic::pokeIntent); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 732 | eventAccummulator.add(event); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | // Intent event listener. |
| 737 | private class InternalFlowListener implements FlowRuleListener { |
| 738 | @Override |
| 739 | public void event(FlowRuleEvent event) { |
| 740 | eventAccummulator.add(event); |
| 741 | } |
| 742 | } |
| 743 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 744 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 745 | // === SUMMARY MONITORING |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 746 | |
| 747 | // Periodic update of the summary information |
| 748 | private class SummaryMonitor extends TimerTask { |
| 749 | @Override |
| 750 | public void run() { |
| 751 | try { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 752 | if (summaryRunning) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 753 | msgSender.execute(() -> requestSummary(0)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 754 | } |
| 755 | } catch (Exception e) { |
| 756 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
| 757 | log.warn("Boom!", e); |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | // Accumulates events to drive methodic update of the summary pane. |
| 763 | private class InternalEventAccummulator extends AbstractAccumulator<Event> { |
| 764 | protected InternalEventAccummulator() { |
| 765 | super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS); |
| 766 | } |
| 767 | |
| 768 | @Override |
| 769 | public void processItems(List<Event> items) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 770 | // Start-of-Debugging -- Keep in until ONOS-2572 is fixed for reals |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 771 | long now = System.currentTimeMillis(); |
| 772 | String me = this.toString(); |
| 773 | String miniMe = me.replaceAll("^.*@", "me@"); |
| 774 | log.debug("Time: {}; this: {}, processing items ({} events)", |
| 775 | now, miniMe, items.size()); |
| 776 | // End-of-Debugging |
| 777 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 778 | try { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 779 | if (summaryRunning) { |
Thomas Vachuska | c7f7996 | 2015-05-28 09:37:34 -0700 | [diff] [blame] | 780 | msgSender.execute(() -> requestSummary(0)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 781 | } |
| 782 | } catch (Exception e) { |
| 783 | log.warn("Unable to handle summary request due to {}", e.getMessage()); |
| 784 | log.debug("Boom!", e); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |