blob: 1c629c7bc5b9e760ace713776a915845cb99b284 [file] [log] [blame]
Thomas Vachuska329af532015-03-10 02:08:33 -07001/*
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 */
16package org.onosproject.ui.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.onlab.osgi.ServiceDirectory;
23import org.onlab.util.AbstractAccumulator;
24import org.onlab.util.Accumulator;
25import org.onosproject.cluster.ClusterEvent;
26import org.onosproject.cluster.ClusterEventListener;
27import org.onosproject.cluster.ControllerNode;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
30import org.onosproject.event.Event;
31import org.onosproject.mastership.MastershipAdminService;
32import org.onosproject.mastership.MastershipEvent;
33import org.onosproject.mastership.MastershipListener;
34import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.Device;
36import org.onosproject.net.Host;
37import org.onosproject.net.HostId;
38import org.onosproject.net.HostLocation;
39import org.onosproject.net.Link;
40import org.onosproject.net.device.DeviceEvent;
41import org.onosproject.net.device.DeviceListener;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.FlowRuleEvent;
45import org.onosproject.net.flow.FlowRuleListener;
46import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
48import org.onosproject.net.host.HostEvent;
49import org.onosproject.net.host.HostListener;
50import org.onosproject.net.intent.HostToHostIntent;
Thomas Vachuska329af532015-03-10 02:08:33 -070051import org.onosproject.net.intent.IntentEvent;
52import org.onosproject.net.intent.IntentListener;
53import org.onosproject.net.intent.MultiPointToSinglePointIntent;
54import org.onosproject.net.link.LinkEvent;
55import org.onosproject.net.link.LinkListener;
Simon Huntd2747a02015-04-30 22:41:16 -070056import org.onosproject.ui.JsonUtils;
57import org.onosproject.ui.RequestHandler;
Thomas Vachuska329af532015-03-10 02:08:33 -070058import org.onosproject.ui.UiConnection;
Simon Hunt4fc86852015-08-20 17:57:52 -070059import org.onosproject.ui.impl.TrafficMonitor.Mode;
Simon Hunt191c84a2015-08-21 08:24:48 -070060import org.onosproject.ui.topo.NodeSelection;
Simon Hunta17fa672015-08-19 18:42:22 -070061import org.onosproject.ui.topo.Highlights;
Simon Hunt0af1ec32015-07-24 12:17:55 -070062import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070063
64import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070065import java.util.Collection;
Thomas Vachuska329af532015-03-10 02:08:33 -070066import java.util.Collections;
67import java.util.Comparator;
68import java.util.HashSet;
69import java.util.List;
70import java.util.Set;
71import java.util.Timer;
72import java.util.TimerTask;
Thomas Vachuska52c98bd2015-05-27 20:54:02 -070073import java.util.concurrent.ExecutorService;
Thomas Vachuska329af532015-03-10 02:08:33 -070074
Thomas Vachuska52c98bd2015-05-27 20:54:02 -070075import static java.util.concurrent.Executors.newSingleThreadExecutor;
76import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska329af532015-03-10 02:08:33 -070077import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED;
78import static org.onosproject.net.DeviceId.deviceId;
79import static org.onosproject.net.HostId.hostId;
Thomas Vachuskacb5016f2015-05-18 14:11:43 -070080import static org.onosproject.net.device.DeviceEvent.Type.*;
Thomas Vachuska329af532015-03-10 02:08:33 -070081import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
82import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
83
84/**
85 * Web socket capable of interacting with the GUI topology view.
86 */
87public class TopologyViewMessageHandler extends TopologyViewMessageHandlerBase {
88
Simon Huntb745ca62015-07-28 15:37:11 -070089 // incoming event types
Simon Huntd2747a02015-04-30 22:41:16 -070090 private static final String REQ_DETAILS = "requestDetails";
91 private static final String UPDATE_META = "updateMeta";
92 private static final String ADD_HOST_INTENT = "addHostIntent";
93 private static final String ADD_MULTI_SRC_INTENT = "addMultiSourceIntent";
94 private static final String REQ_RELATED_INTENTS = "requestRelatedIntents";
95 private static final String REQ_NEXT_INTENT = "requestNextRelatedIntent";
96 private static final String REQ_PREV_INTENT = "requestPrevRelatedIntent";
97 private static final String REQ_SEL_INTENT_TRAFFIC = "requestSelectedIntentTraffic";
Thomas Vachuskaf0397b52015-05-29 13:50:17 -070098 private static final String REQ_ALL_FLOW_TRAFFIC = "requestAllFlowTraffic";
99 private static final String REQ_ALL_PORT_TRAFFIC = "requestAllPortTraffic";
Simon Huntd2747a02015-04-30 22:41:16 -0700100 private static final String REQ_DEV_LINK_FLOWS = "requestDeviceLinkFlows";
101 private static final String CANCEL_TRAFFIC = "cancelTraffic";
102 private static final String REQ_SUMMARY = "requestSummary";
103 private static final String CANCEL_SUMMARY = "cancelSummary";
104 private static final String EQ_MASTERS = "equalizeMasters";
105 private static final String SPRITE_LIST_REQ = "spriteListRequest";
106 private static final String SPRITE_DATA_REQ = "spriteDataRequest";
107 private static final String TOPO_START = "topoStart";
Simon Hunt732bb2e2015-05-13 18:32:16 -0700108 private static final String TOPO_HEARTBEAT = "topoHeartbeat";
Simon Hunte05cae42015-07-23 17:35:24 -0700109 private static final String TOPO_SELECT_OVERLAY = "topoSelectOverlay";
Simon Huntd2747a02015-04-30 22:41:16 -0700110 private static final String TOPO_STOP = "topoStop";
111
Simon Huntb745ca62015-07-28 15:37:11 -0700112 // outgoing event types
113 private static final String SHOW_SUMMARY = "showSummary";
114 private static final String SHOW_DETAILS = "showDetails";
115 private static final String SPRITE_LIST_RESPONSE = "spriteListResponse";
116 private static final String SPRITE_DATA_RESPONSE = "spriteDataResponse";
117 private static final String UPDATE_INSTANCE = "updateInstance";
118
119 // fields
120 private static final String ID = "id";
Simon Huntb745ca62015-07-28 15:37:11 -0700121 private static final String DEVICE = "device";
122 private static final String HOST = "host";
123 private static final String CLASS = "class";
124 private static final String UNKNOWN = "unknown";
125 private static final String ONE = "one";
126 private static final String TWO = "two";
127 private static final String SRC = "src";
128 private static final String DST = "dst";
129 private static final String DATA = "data";
130 private static final String NAME = "name";
131 private static final String NAMES = "names";
132 private static final String ACTIVATE = "activate";
133 private static final String DEACTIVATE = "deactivate";
Simon Huntb745ca62015-07-28 15:37:11 -0700134
Simon Huntd2747a02015-04-30 22:41:16 -0700135
Thomas Vachuska329af532015-03-10 02:08:33 -0700136 private static final String APP_ID = "org.onosproject.gui";
137
Simon Hunta17fa672015-08-19 18:42:22 -0700138 private static final long TRAFFIC_PERIOD = 5000;
139 private static final long SUMMARY_PERIOD = 30000;
Thomas Vachuska329af532015-03-10 02:08:33 -0700140
141 private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
Thomas Vachuskac0fe09a2015-05-21 12:56:22 -0700142 (o1, o2) -> o1.id().toString().compareTo(o2.id().toString());
Thomas Vachuska329af532015-03-10 02:08:33 -0700143
144
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700145 private final Timer timer = new Timer("onos-topology-view");
Thomas Vachuska329af532015-03-10 02:08:33 -0700146
147 private static final int MAX_EVENTS = 1000;
148 private static final int MAX_BATCH_MS = 5000;
149 private static final int MAX_IDLE_MS = 1000;
150
151 private ApplicationId appId;
152
153 private final ClusterEventListener clusterListener = new InternalClusterListener();
154 private final MastershipListener mastershipListener = new InternalMastershipListener();
155 private final DeviceListener deviceListener = new InternalDeviceListener();
156 private final LinkListener linkListener = new InternalLinkListener();
157 private final HostListener hostListener = new InternalHostListener();
158 private final IntentListener intentListener = new InternalIntentListener();
159 private final FlowRuleListener flowListener = new InternalFlowListener();
160
161 private final Accumulator<Event> eventAccummulator = new InternalEventAccummulator();
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700162 private final ExecutorService msgSender =
163 newSingleThreadExecutor(groupedThreads("onos/gui", "msg-sender"));
Thomas Vachuska329af532015-03-10 02:08:33 -0700164
Simon Hunta17fa672015-08-19 18:42:22 -0700165 private TopoOverlayCache overlayCache;
Simon Hunt4fc86852015-08-20 17:57:52 -0700166 private TrafficMonitor traffic;
Thomas Vachuska329af532015-03-10 02:08:33 -0700167
Simon Huntd2747a02015-04-30 22:41:16 -0700168 private TimerTask summaryTask = null;
169 private boolean summaryRunning = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700170
171 private boolean listenersRemoved = false;
172
Thomas Vachuska329af532015-03-10 02:08:33 -0700173
174 @Override
175 public void init(UiConnection connection, ServiceDirectory directory) {
176 super.init(connection, directory);
Thomas Vachuska329af532015-03-10 02:08:33 -0700177 appId = directory.get(CoreService.class).registerApplication(APP_ID);
Simon Hunt4fc86852015-08-20 17:57:52 -0700178 traffic = new TrafficMonitor(TRAFFIC_PERIOD, servicesBundle, this);
Thomas Vachuska329af532015-03-10 02:08:33 -0700179 }
180
181 @Override
182 public void destroy() {
183 cancelAllRequests();
Thomas Vachuska2bb48632015-04-28 14:40:42 -0700184 removeListeners();
Thomas Vachuska329af532015-03-10 02:08:33 -0700185 super.destroy();
186 }
187
Thomas Vachuska329af532015-03-10 02:08:33 -0700188 @Override
Simon Huntda580882015-05-12 20:58:18 -0700189 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700190 return ImmutableSet.of(
191 new TopoStart(),
Simon Hunt732bb2e2015-05-13 18:32:16 -0700192 new TopoHeartbeat(),
Simon Hunte05cae42015-07-23 17:35:24 -0700193 new TopoSelectOverlay(),
Simon Huntd2747a02015-04-30 22:41:16 -0700194 new TopoStop(),
195 new ReqSummary(),
196 new CancelSummary(),
197 new SpriteListReq(),
198 new SpriteDataReq(),
199 new RequestDetails(),
200 new UpdateMeta(),
201 new EqMasters(),
Thomas Vachuska329af532015-03-10 02:08:33 -0700202
Simon Huntd2747a02015-04-30 22:41:16 -0700203 // TODO: migrate traffic related to separate app
204 new AddHostIntent(),
205 new AddMultiSourceIntent(),
Simon Hunta17fa672015-08-19 18:42:22 -0700206
207 new ReqAllFlowTraffic(),
208 new ReqAllPortTraffic(),
209 new ReqDevLinkFlows(),
Simon Huntd2747a02015-04-30 22:41:16 -0700210 new ReqRelatedIntents(),
211 new ReqNextIntent(),
212 new ReqPrevIntent(),
213 new ReqSelectedIntentTraffic(),
Simon Hunta17fa672015-08-19 18:42:22 -0700214
Simon Huntd2747a02015-04-30 22:41:16 -0700215 new CancelTraffic()
216 );
217 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700218
Simon Hunte05cae42015-07-23 17:35:24 -0700219 /**
220 * Injects the topology overlay cache.
221 *
222 * @param overlayCache injected cache
223 */
224 void setOverlayCache(TopoOverlayCache overlayCache) {
225 this.overlayCache = overlayCache;
226 }
227
Simon Huntd2747a02015-04-30 22:41:16 -0700228 // ==================================================================
Thomas Vachuska329af532015-03-10 02:08:33 -0700229
Simon Huntd2747a02015-04-30 22:41:16 -0700230 private final class TopoStart extends RequestHandler {
231 private TopoStart() {
232 super(TOPO_START);
233 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700234
Simon Huntd2747a02015-04-30 22:41:16 -0700235 @Override
236 public void process(long sid, ObjectNode payload) {
237 addListeners();
238 sendAllInstances(null);
239 sendAllDevices();
240 sendAllLinks();
241 sendAllHosts();
Thomas Vachuska329af532015-03-10 02:08:33 -0700242 }
243 }
244
Simon Hunt732bb2e2015-05-13 18:32:16 -0700245 private final class TopoHeartbeat extends RequestHandler {
246 private TopoHeartbeat() {
247 super(TOPO_HEARTBEAT);
248 }
249
250 @Override
251 public void process(long sid, ObjectNode payload) {
252 // place holder for now
253 }
254 }
255
Simon Hunte05cae42015-07-23 17:35:24 -0700256 private final class TopoSelectOverlay extends RequestHandler {
257 private TopoSelectOverlay() {
258 super(TOPO_SELECT_OVERLAY);
259 }
260
261 @Override
262 public void process(long sid, ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700263 String deact = string(payload, DEACTIVATE);
264 String act = string(payload, ACTIVATE);
Simon Hunte05cae42015-07-23 17:35:24 -0700265 overlayCache.switchOverlay(deact, act);
266 }
267 }
268
Simon Huntd2747a02015-04-30 22:41:16 -0700269 private final class TopoStop extends RequestHandler {
270 private TopoStop() {
271 super(TOPO_STOP);
272 }
273
274 @Override
275 public void process(long sid, ObjectNode payload) {
276 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700277 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700278 }
279 }
280
281 private final class ReqSummary extends RequestHandler {
282 private ReqSummary() {
283 super(REQ_SUMMARY);
284 }
285
286 @Override
287 public void process(long sid, ObjectNode payload) {
288 requestSummary(sid);
289 startSummaryMonitoring();
290 }
291 }
292
293 private final class CancelSummary extends RequestHandler {
294 private CancelSummary() {
295 super(CANCEL_SUMMARY);
296 }
297
298 @Override
299 public void process(long sid, ObjectNode payload) {
300 stopSummaryMonitoring();
301 }
302 }
303
304 private final class SpriteListReq extends RequestHandler {
305 private SpriteListReq() {
306 super(SPRITE_LIST_REQ);
307 }
308
309 @Override
310 public void process(long sid, ObjectNode payload) {
Simon Huntda580882015-05-12 20:58:18 -0700311 ObjectNode root = objectNode();
312 ArrayNode names = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700313 get(SpriteService.class).getNames().forEach(names::add);
Simon Huntb745ca62015-07-28 15:37:11 -0700314 root.set(NAMES, names);
315 sendMessage(SPRITE_LIST_RESPONSE, sid, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700316 }
317 }
318
319 private final class SpriteDataReq extends RequestHandler {
320 private SpriteDataReq() {
321 super(SPRITE_DATA_REQ);
322 }
323
324 @Override
325 public void process(long sid, ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700326 String name = string(payload, NAME);
Simon Huntda580882015-05-12 20:58:18 -0700327 ObjectNode root = objectNode();
Simon Huntb745ca62015-07-28 15:37:11 -0700328 root.set(DATA, get(SpriteService.class).get(name));
329 sendMessage(SPRITE_DATA_RESPONSE, sid, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700330 }
331 }
332
333 private final class RequestDetails extends RequestHandler {
334 private RequestDetails() {
335 super(REQ_DETAILS);
336 }
337
338 @Override
339 public void process(long sid, ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700340 String type = string(payload, CLASS, UNKNOWN);
341 String id = string(payload, ID);
342 PropertyPanel pp = null;
Simon Huntd2747a02015-04-30 22:41:16 -0700343
Simon Huntb745ca62015-07-28 15:37:11 -0700344 if (type.equals(DEVICE)) {
345 pp = deviceDetails(deviceId(id), sid);
346 overlayCache.currentOverlay().modifyDeviceDetails(pp);
347 } else if (type.equals(HOST)) {
348 pp = hostDetails(hostId(id), sid);
349 overlayCache.currentOverlay().modifyHostDetails(pp);
Simon Huntd2747a02015-04-30 22:41:16 -0700350 }
Simon Huntb745ca62015-07-28 15:37:11 -0700351
352 ObjectNode json = JsonUtils.envelope(SHOW_DETAILS, sid, json(pp));
353 sendMessage(json);
Simon Huntd2747a02015-04-30 22:41:16 -0700354 }
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 Hunta17fa672015-08-19 18:42:22 -0700379
380 // ========= -----------------------------------------------------------------
381
Simon Huntd2747a02015-04-30 22:41:16 -0700382 // === 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 Huntb745ca62015-07-28 15:37:11 -0700392 HostId one = hostId(string(payload, ONE));
393 HostId two = hostId(string(payload, TWO));
Simon Huntd2747a02015-04-30 22:41:16 -0700394
395 HostToHostIntent intent = HostToHostIntent.builder()
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700396 .appId(appId)
397 .one(one)
398 .two(two)
399 .build();
Simon Huntd2747a02015-04-30 22:41:16 -0700400
401 intentService.submit(intent);
Simon Huntd2862c32015-08-24 17:41:51 -0700402 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
403 traffic.monitor(intent);
404 }
Simon Huntd2747a02015-04-30 22:41:16 -0700405 }
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 Huntb745ca62015-07-28 15:37:11 -0700416 Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC));
417 HostId dst = hostId(string(payload, DST));
Simon Huntd2747a02015-04-30 22:41:16 -0700418 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 Huntd2862c32015-08-24 17:41:51 -0700437 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
438 traffic.monitor(intent);
439 }
Simon Huntd2747a02015-04-30 22:41:16 -0700440 }
441 }
442
Simon Hunta17fa672015-08-19 18:42:22 -0700443 // ========= -----------------------------------------------------------------
Simon Huntd2747a02015-04-30 22:41:16 -0700444
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700445 private final class ReqAllFlowTraffic extends RequestHandler {
446 private ReqAllFlowTraffic() {
447 super(REQ_ALL_FLOW_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700448 }
449
450 @Override
451 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700452 traffic.monitor(Mode.ALL_FLOW_TRAFFIC);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700453 }
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 Hunt4fc86852015-08-20 17:57:52 -0700463 traffic.monitor(Mode.ALL_PORT_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700464 }
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 Hunta17fa672015-08-19 18:42:22 -0700474 NodeSelection nodeSelection =
475 new NodeSelection(payload, deviceService, hostService);
Simon Hunt4fc86852015-08-20 17:57:52 -0700476 traffic.monitor(Mode.DEV_LINK_FLOWS, nodeSelection);
Simon Hunta17fa672015-08-19 18:42:22 -0700477 }
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 Hunt4fc86852015-08-20 17:57:52 -0700489 traffic.monitor(Mode.RELATED_INTENTS, nodeSelection);
Simon Hunta17fa672015-08-19 18:42:22 -0700490 }
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 Hunt4fc86852015-08-20 17:57:52 -0700500 traffic.selectNextIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700501 }
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 Hunt4fc86852015-08-20 17:57:52 -0700511 traffic.selectPreviousIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700512 }
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 Hunt4fc86852015-08-20 17:57:52 -0700522 traffic.monitor(Mode.SELECTED_INTENT);
Simon Huntd2747a02015-04-30 22:41:16 -0700523 }
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 Hunt4fc86852015-08-20 17:57:52 -0700533 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700534 }
535 }
536
537 //=======================================================================
538
Simon Hunta17fa672015-08-19 18:42:22 -0700539 // Converts highlights to JSON format and sends the message to the client
540 protected void sendHighlights(Highlights highlights) {
541 sendMessage(JsonUtils.envelope(SHOW_HIGHLIGHTS, json(highlights)));
542 }
Simon Huntd2747a02015-04-30 22:41:16 -0700543
Thomas Vachuska329af532015-03-10 02:08:33 -0700544 // 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 Huntd2747a02015-04-30 22:41:16 -0700552 // Subscribes for summary messages.
553 private synchronized void requestSummary(long sid) {
Simon Hunt0af1ec32015-07-24 12:17:55 -0700554 PropertyPanel pp = summmaryMessage(sid);
555 overlayCache.currentOverlay().modifySummary(pp);
Simon Huntb745ca62015-07-28 15:37:11 -0700556 ObjectNode json = JsonUtils.envelope(SHOW_SUMMARY, sid, json(pp));
Simon Hunt0af1ec32015-07-24 12:17:55 -0700557 sendMessage(json);
Thomas Vachuska329af532015-03-10 02:08:33 -0700558 }
559
Simon Huntd2747a02015-04-30 22:41:16 -0700560
Thomas Vachuska329af532015-03-10 02:08:33 -0700561 private void cancelAllRequests() {
562 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700563 traffic.stopMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700564 }
565
566 // Sends all controller nodes to the client as node-added messages.
567 private void sendAllInstances(String messageType) {
568 List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
569 Collections.sort(nodes, NODE_COMPARATOR);
570 for (ControllerNode node : nodes) {
571 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node),
572 messageType));
573 }
574 }
575
576 // Sends all devices to the client as device-added messages.
577 private void sendAllDevices() {
578 // Send optical first, others later for layered rendering
579 for (Device device : deviceService.getDevices()) {
580 if (device.type() == Device.Type.ROADM) {
581 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
582 }
583 }
584 for (Device device : deviceService.getDevices()) {
585 if (device.type() != Device.Type.ROADM) {
586 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
587 }
588 }
589 }
590
591 // Sends all links to the client as link-added messages.
592 private void sendAllLinks() {
593 // Send optical first, others later for layered rendering
594 for (Link link : linkService.getLinks()) {
595 if (link.type() == Link.Type.OPTICAL) {
596 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
597 }
598 }
599 for (Link link : linkService.getLinks()) {
600 if (link.type() != Link.Type.OPTICAL) {
601 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
602 }
603 }
604 }
605
606 // Sends all hosts to the client as host-added messages.
607 private void sendAllHosts() {
608 for (Host host : hostService.getHosts()) {
609 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
610 }
611 }
612
Thomas Vachuska329af532015-03-10 02:08:33 -0700613 private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) {
614 Set<ConnectPoint> points = new HashSet<>();
615 for (HostId hostId : hostIds) {
616 points.add(getHostLocation(hostId));
617 }
618 return points;
619 }
620
621 private HostLocation getHostLocation(HostId hostId) {
622 return hostService.getHost(hostId).location();
623 }
624
625 // Produces a list of host ids from the specified JSON array.
626 private Set<HostId> getHostIds(ArrayNode ids) {
627 Set<HostId> hostIds = new HashSet<>();
628 for (JsonNode id : ids) {
629 hostIds.add(hostId(id.asText()));
630 }
631 return hostIds;
632 }
633
634
Simon Huntd2747a02015-04-30 22:41:16 -0700635 private synchronized void startSummaryMonitoring() {
Thomas Vachuska329af532015-03-10 02:08:33 -0700636 stopSummaryMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700637 summaryTask = new SummaryMonitor();
Simon Hunta17fa672015-08-19 18:42:22 -0700638 timer.schedule(summaryTask, SUMMARY_PERIOD, SUMMARY_PERIOD);
Simon Huntd2747a02015-04-30 22:41:16 -0700639 summaryRunning = true;
Thomas Vachuska329af532015-03-10 02:08:33 -0700640 }
641
642 private synchronized void stopSummaryMonitoring() {
Simon Huntd2747a02015-04-30 22:41:16 -0700643 if (summaryTask != null) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700644 summaryTask.cancel();
645 summaryTask = null;
Thomas Vachuska329af532015-03-10 02:08:33 -0700646 }
Simon Huntd2747a02015-04-30 22:41:16 -0700647 summaryRunning = false;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700648 }
649
Thomas Vachuska329af532015-03-10 02:08:33 -0700650
651 // Adds all internal listeners.
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700652 private synchronized void addListeners() {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700653 listenersRemoved = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700654 clusterService.addListener(clusterListener);
655 mastershipService.addListener(mastershipListener);
656 deviceService.addListener(deviceListener);
657 linkService.addListener(linkListener);
658 hostService.addListener(hostListener);
659 intentService.addListener(intentListener);
660 flowService.addListener(flowListener);
661 }
662
663 // Removes all internal listeners.
664 private synchronized void removeListeners() {
665 if (!listenersRemoved) {
666 listenersRemoved = true;
667 clusterService.removeListener(clusterListener);
668 mastershipService.removeListener(mastershipListener);
669 deviceService.removeListener(deviceListener);
670 linkService.removeListener(linkListener);
671 hostService.removeListener(hostListener);
672 intentService.removeListener(intentListener);
673 flowService.removeListener(flowListener);
674 }
675 }
676
677 // Cluster event listener.
678 private class InternalClusterListener implements ClusterEventListener {
679 @Override
680 public void event(ClusterEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700681 msgSender.execute(() -> sendMessage(instanceMessage(event, null)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700682 }
683 }
684
685 // Mastership change listener
686 private class InternalMastershipListener implements MastershipListener {
687 @Override
688 public void event(MastershipEvent event) {
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700689 msgSender.execute(() -> {
Simon Huntb745ca62015-07-28 15:37:11 -0700690 sendAllInstances(UPDATE_INSTANCE);
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700691 Device device = deviceService.getDevice(event.subject());
692 if (device != null) {
693 sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device)));
694 }
695 });
Thomas Vachuska329af532015-03-10 02:08:33 -0700696 }
697 }
698
699 // Device event listener.
700 private class InternalDeviceListener implements DeviceListener {
701 @Override
702 public void event(DeviceEvent event) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700703 if (event.type() != PORT_STATS_UPDATED) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700704 msgSender.execute(() -> sendMessage(deviceMessage(event)));
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700705 eventAccummulator.add(event);
706 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700707 }
708 }
709
710 // Link event listener.
711 private class InternalLinkListener implements LinkListener {
712 @Override
713 public void event(LinkEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700714 msgSender.execute(() -> sendMessage(linkMessage(event)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700715 eventAccummulator.add(event);
716 }
717 }
718
719 // Host event listener.
720 private class InternalHostListener implements HostListener {
721 @Override
722 public void event(HostEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700723 msgSender.execute(() -> sendMessage(hostMessage(event)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700724 eventAccummulator.add(event);
725 }
726 }
727
728 // Intent event listener.
729 private class InternalIntentListener implements IntentListener {
730 @Override
731 public void event(IntentEvent event) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700732 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700733 eventAccummulator.add(event);
734 }
735 }
736
737 // Intent event listener.
738 private class InternalFlowListener implements FlowRuleListener {
739 @Override
740 public void event(FlowRuleEvent event) {
741 eventAccummulator.add(event);
742 }
743 }
744
Simon Huntd2747a02015-04-30 22:41:16 -0700745
Simon Hunta17fa672015-08-19 18:42:22 -0700746 // === SUMMARY MONITORING
Thomas Vachuska329af532015-03-10 02:08:33 -0700747
748 // Periodic update of the summary information
749 private class SummaryMonitor extends TimerTask {
750 @Override
751 public void run() {
752 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700753 if (summaryRunning) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700754 msgSender.execute(() -> requestSummary(0));
Thomas Vachuska329af532015-03-10 02:08:33 -0700755 }
756 } catch (Exception e) {
757 log.warn("Unable to handle summary request due to {}", e.getMessage());
758 log.warn("Boom!", e);
759 }
760 }
761 }
762
763 // Accumulates events to drive methodic update of the summary pane.
764 private class InternalEventAccummulator extends AbstractAccumulator<Event> {
765 protected InternalEventAccummulator() {
766 super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS);
767 }
768
769 @Override
770 public void processItems(List<Event> items) {
Simon Hunta17fa672015-08-19 18:42:22 -0700771 // Start-of-Debugging -- Keep in until ONOS-2572 is fixed for reals
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700772 long now = System.currentTimeMillis();
773 String me = this.toString();
774 String miniMe = me.replaceAll("^.*@", "me@");
775 log.debug("Time: {}; this: {}, processing items ({} events)",
776 now, miniMe, items.size());
777 // End-of-Debugging
778
Thomas Vachuska329af532015-03-10 02:08:33 -0700779 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700780 if (summaryRunning) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700781 msgSender.execute(() -> requestSummary(0));
Thomas Vachuska329af532015-03-10 02:08:33 -0700782 }
783 } catch (Exception e) {
784 log.warn("Unable to handle summary request due to {}", e.getMessage());
785 log.debug("Boom!", e);
786 }
787 }
788 }
789}