blob: c2f54e4a428e0226309beeb43a125a2690f601b8 [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 Hunta17fa672015-08-19 18:42:22 -070059import org.onosproject.ui.impl.TrafficMonitorObject.Mode;
60import org.onosproject.ui.impl.topo.NodeSelection;
61import 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 TrafficMonitorObject tmo;
Simon Hunte05cae42015-07-23 17:35:24 -0700166
Simon Hunta17fa672015-08-19 18:42:22 -0700167 private TopoOverlayCache overlayCache;
Thomas Vachuska329af532015-03-10 02:08:33 -0700168
Simon Huntd2747a02015-04-30 22:41:16 -0700169 private TimerTask summaryTask = null;
170 private boolean summaryRunning = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700171
172 private boolean listenersRemoved = false;
173
Thomas Vachuska329af532015-03-10 02:08:33 -0700174
175 @Override
176 public void init(UiConnection connection, ServiceDirectory directory) {
177 super.init(connection, directory);
Thomas Vachuska329af532015-03-10 02:08:33 -0700178 appId = directory.get(CoreService.class).registerApplication(APP_ID);
Simon Hunta17fa672015-08-19 18:42:22 -0700179 tmo = new TrafficMonitorObject(TRAFFIC_PERIOD, servicesBundle, this);
Thomas Vachuska329af532015-03-10 02:08:33 -0700180 }
181
182 @Override
183 public void destroy() {
184 cancelAllRequests();
Thomas Vachuska2bb48632015-04-28 14:40:42 -0700185 removeListeners();
Thomas Vachuska329af532015-03-10 02:08:33 -0700186 super.destroy();
187 }
188
Thomas Vachuska329af532015-03-10 02:08:33 -0700189 @Override
Simon Huntda580882015-05-12 20:58:18 -0700190 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700191 return ImmutableSet.of(
192 new TopoStart(),
Simon Hunt732bb2e2015-05-13 18:32:16 -0700193 new TopoHeartbeat(),
Simon Hunte05cae42015-07-23 17:35:24 -0700194 new TopoSelectOverlay(),
Simon Huntd2747a02015-04-30 22:41:16 -0700195 new TopoStop(),
196 new ReqSummary(),
197 new CancelSummary(),
198 new SpriteListReq(),
199 new SpriteDataReq(),
200 new RequestDetails(),
201 new UpdateMeta(),
202 new EqMasters(),
Thomas Vachuska329af532015-03-10 02:08:33 -0700203
Simon Huntd2747a02015-04-30 22:41:16 -0700204 // TODO: migrate traffic related to separate app
205 new AddHostIntent(),
206 new AddMultiSourceIntent(),
Simon Hunta17fa672015-08-19 18:42:22 -0700207
208 new ReqAllFlowTraffic(),
209 new ReqAllPortTraffic(),
210 new ReqDevLinkFlows(),
Simon Huntd2747a02015-04-30 22:41:16 -0700211 new ReqRelatedIntents(),
212 new ReqNextIntent(),
213 new ReqPrevIntent(),
214 new ReqSelectedIntentTraffic(),
Simon Hunta17fa672015-08-19 18:42:22 -0700215
Simon Huntd2747a02015-04-30 22:41:16 -0700216 new CancelTraffic()
217 );
218 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700219
Simon Hunte05cae42015-07-23 17:35:24 -0700220 /**
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 Huntd2747a02015-04-30 22:41:16 -0700229 // ==================================================================
Thomas Vachuska329af532015-03-10 02:08:33 -0700230
Simon Huntd2747a02015-04-30 22:41:16 -0700231 private final class TopoStart extends RequestHandler {
232 private TopoStart() {
233 super(TOPO_START);
234 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700235
Simon Huntd2747a02015-04-30 22:41:16 -0700236 @Override
237 public void process(long sid, ObjectNode payload) {
238 addListeners();
239 sendAllInstances(null);
240 sendAllDevices();
241 sendAllLinks();
242 sendAllHosts();
Thomas Vachuska329af532015-03-10 02:08:33 -0700243 }
244 }
245
Simon Hunt732bb2e2015-05-13 18:32:16 -0700246 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 Hunte05cae42015-07-23 17:35:24 -0700257 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 Huntb745ca62015-07-28 15:37:11 -0700264 String deact = string(payload, DEACTIVATE);
265 String act = string(payload, ACTIVATE);
Simon Hunte05cae42015-07-23 17:35:24 -0700266 overlayCache.switchOverlay(deact, act);
267 }
268 }
269
Simon Huntd2747a02015-04-30 22:41:16 -0700270 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 Hunta17fa672015-08-19 18:42:22 -0700278 tmo.stop();
Simon Huntd2747a02015-04-30 22:41:16 -0700279 }
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 Huntda580882015-05-12 20:58:18 -0700312 ObjectNode root = objectNode();
313 ArrayNode names = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700314 get(SpriteService.class).getNames().forEach(names::add);
Simon Huntb745ca62015-07-28 15:37:11 -0700315 root.set(NAMES, names);
316 sendMessage(SPRITE_LIST_RESPONSE, sid, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700317 }
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 Huntb745ca62015-07-28 15:37:11 -0700327 String name = string(payload, NAME);
Simon Huntda580882015-05-12 20:58:18 -0700328 ObjectNode root = objectNode();
Simon Huntb745ca62015-07-28 15:37:11 -0700329 root.set(DATA, get(SpriteService.class).get(name));
330 sendMessage(SPRITE_DATA_RESPONSE, sid, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700331 }
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 Huntb745ca62015-07-28 15:37:11 -0700341 String type = string(payload, CLASS, UNKNOWN);
342 String id = string(payload, ID);
343 PropertyPanel pp = null;
Simon Huntd2747a02015-04-30 22:41:16 -0700344
Simon Huntb745ca62015-07-28 15:37:11 -0700345 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 Huntd2747a02015-04-30 22:41:16 -0700351 }
Simon Huntb745ca62015-07-28 15:37:11 -0700352
353 ObjectNode json = JsonUtils.envelope(SHOW_DETAILS, sid, json(pp));
354 sendMessage(json);
Simon Huntd2747a02015-04-30 22:41:16 -0700355 }
356 }
357
358 private final class UpdateMeta extends RequestHandler {
359 private UpdateMeta() {
360 super(UPDATE_META);
361 }
362
363 @Override
364 public void process(long sid, ObjectNode payload) {
365 updateMetaUi(payload);
366 }
367 }
368
369 private final class EqMasters extends RequestHandler {
370 private EqMasters() {
371 super(EQ_MASTERS);
372 }
373
374 @Override
375 public void process(long sid, ObjectNode payload) {
376 directory.get(MastershipAdminService.class).balanceRoles();
377 }
378 }
379
Simon Hunta17fa672015-08-19 18:42:22 -0700380
381 // ========= -----------------------------------------------------------------
382
Simon Huntd2747a02015-04-30 22:41:16 -0700383 // === TODO: move traffic related classes to traffic app
384
385 private final class AddHostIntent extends RequestHandler {
386 private AddHostIntent() {
387 super(ADD_HOST_INTENT);
388 }
389
390 @Override
391 public void process(long sid, ObjectNode payload) {
392 // TODO: add protection against device ids and non-existent hosts.
Simon Huntb745ca62015-07-28 15:37:11 -0700393 HostId one = hostId(string(payload, ONE));
394 HostId two = hostId(string(payload, TWO));
Simon Huntd2747a02015-04-30 22:41:16 -0700395
396 HostToHostIntent intent = HostToHostIntent.builder()
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700397 .appId(appId)
398 .one(one)
399 .two(two)
400 .build();
Simon Huntd2747a02015-04-30 22:41:16 -0700401
402 intentService.submit(intent);
Simon Hunta17fa672015-08-19 18:42:22 -0700403 tmo.monitor(intent);
Simon Huntd2747a02015-04-30 22:41:16 -0700404 }
405 }
406
407 private final class AddMultiSourceIntent extends RequestHandler {
408 private AddMultiSourceIntent() {
409 super(ADD_MULTI_SRC_INTENT);
410 }
411
412 @Override
413 public void process(long sid, ObjectNode payload) {
414 // TODO: add protection against device ids and non-existent hosts.
Simon Huntb745ca62015-07-28 15:37:11 -0700415 Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC));
416 HostId dst = hostId(string(payload, DST));
Simon Huntd2747a02015-04-30 22:41:16 -0700417 Host dstHost = hostService.getHost(dst);
418
419 Set<ConnectPoint> ingressPoints = getHostLocations(src);
420
421 // FIXME: clearly, this is not enough
422 TrafficSelector selector = DefaultTrafficSelector.builder()
423 .matchEthDst(dstHost.mac()).build();
424 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
425
426 MultiPointToSinglePointIntent intent =
427 MultiPointToSinglePointIntent.builder()
428 .appId(appId)
429 .selector(selector)
430 .treatment(treatment)
431 .ingressPoints(ingressPoints)
432 .egressPoint(dstHost.location())
433 .build();
434
435 intentService.submit(intent);
Simon Hunta17fa672015-08-19 18:42:22 -0700436 tmo.monitor(intent);
Simon Huntd2747a02015-04-30 22:41:16 -0700437 }
438 }
439
Simon Hunta17fa672015-08-19 18:42:22 -0700440 // ========= -----------------------------------------------------------------
Simon Huntd2747a02015-04-30 22:41:16 -0700441
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700442 private final class ReqAllFlowTraffic extends RequestHandler {
443 private ReqAllFlowTraffic() {
444 super(REQ_ALL_FLOW_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700445 }
446
447 @Override
448 public void process(long sid, ObjectNode payload) {
Simon Hunta17fa672015-08-19 18:42:22 -0700449 tmo.monitor(Mode.ALL_FLOW_TRAFFIC);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700450 }
451 }
452
453 private final class ReqAllPortTraffic extends RequestHandler {
454 private ReqAllPortTraffic() {
455 super(REQ_ALL_PORT_TRAFFIC);
456 }
457
458 @Override
459 public void process(long sid, ObjectNode payload) {
Simon Hunta17fa672015-08-19 18:42:22 -0700460 tmo.monitor(Mode.ALL_PORT_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700461 }
462 }
463
464 private final class ReqDevLinkFlows extends RequestHandler {
465 private ReqDevLinkFlows() {
466 super(REQ_DEV_LINK_FLOWS);
467 }
468
469 @Override
470 public void process(long sid, ObjectNode payload) {
Simon Hunta17fa672015-08-19 18:42:22 -0700471 NodeSelection nodeSelection =
472 new NodeSelection(payload, deviceService, hostService);
473 tmo.monitor(Mode.DEV_LINK_FLOWS, nodeSelection);
474 }
475 }
476
477 private final class ReqRelatedIntents extends RequestHandler {
478 private ReqRelatedIntents() {
479 super(REQ_RELATED_INTENTS);
480 }
481
482 @Override
483 public void process(long sid, ObjectNode payload) {
484 NodeSelection nodeSelection =
485 new NodeSelection(payload, deviceService, hostService);
486 tmo.monitor(Mode.RELATED_INTENTS, nodeSelection);
487 }
488 }
489
490 private final class ReqNextIntent extends RequestHandler {
491 private ReqNextIntent() {
492 super(REQ_NEXT_INTENT);
493 }
494
495 @Override
496 public void process(long sid, ObjectNode payload) {
497 tmo.selectNextIntent();
498 }
499 }
500
501 private final class ReqPrevIntent extends RequestHandler {
502 private ReqPrevIntent() {
503 super(REQ_PREV_INTENT);
504 }
505
506 @Override
507 public void process(long sid, ObjectNode payload) {
508 tmo.selectPreviousIntent();
509 }
510 }
511
512 private final class ReqSelectedIntentTraffic extends RequestHandler {
513 private ReqSelectedIntentTraffic() {
514 super(REQ_SEL_INTENT_TRAFFIC);
515 }
516
517 @Override
518 public void process(long sid, ObjectNode payload) {
519 tmo.monitor(Mode.SEL_INTENT);
Simon Huntd2747a02015-04-30 22:41:16 -0700520 }
521 }
522
523 private final class CancelTraffic extends RequestHandler {
524 private CancelTraffic() {
525 super(CANCEL_TRAFFIC);
526 }
527
528 @Override
529 public void process(long sid, ObjectNode payload) {
Simon Hunta17fa672015-08-19 18:42:22 -0700530 tmo.stop();
Simon Huntd2747a02015-04-30 22:41:16 -0700531 }
532 }
533
534 //=======================================================================
535
Simon Hunta17fa672015-08-19 18:42:22 -0700536 // Converts highlights to JSON format and sends the message to the client
537 protected void sendHighlights(Highlights highlights) {
538 sendMessage(JsonUtils.envelope(SHOW_HIGHLIGHTS, json(highlights)));
539 }
Simon Huntd2747a02015-04-30 22:41:16 -0700540
Thomas Vachuska329af532015-03-10 02:08:33 -0700541 // Sends the specified data to the client.
542 protected synchronized void sendMessage(ObjectNode data) {
543 UiConnection connection = connection();
544 if (connection != null) {
545 connection.sendMessage(data);
546 }
547 }
548
Simon Huntd2747a02015-04-30 22:41:16 -0700549 // Subscribes for summary messages.
550 private synchronized void requestSummary(long sid) {
Simon Hunt0af1ec32015-07-24 12:17:55 -0700551 PropertyPanel pp = summmaryMessage(sid);
552 overlayCache.currentOverlay().modifySummary(pp);
Simon Huntb745ca62015-07-28 15:37:11 -0700553 ObjectNode json = JsonUtils.envelope(SHOW_SUMMARY, sid, json(pp));
Simon Hunt0af1ec32015-07-24 12:17:55 -0700554 sendMessage(json);
Thomas Vachuska329af532015-03-10 02:08:33 -0700555 }
556
Simon Huntd2747a02015-04-30 22:41:16 -0700557
Thomas Vachuska329af532015-03-10 02:08:33 -0700558 private void cancelAllRequests() {
559 stopSummaryMonitoring();
Simon Hunta17fa672015-08-19 18:42:22 -0700560 tmo.stop();
Thomas Vachuska329af532015-03-10 02:08:33 -0700561 }
562
563 // Sends all controller nodes to the client as node-added messages.
564 private void sendAllInstances(String messageType) {
565 List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
566 Collections.sort(nodes, NODE_COMPARATOR);
567 for (ControllerNode node : nodes) {
568 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node),
569 messageType));
570 }
571 }
572
573 // Sends all devices to the client as device-added messages.
574 private void sendAllDevices() {
575 // Send optical first, others later for layered rendering
576 for (Device device : deviceService.getDevices()) {
577 if (device.type() == Device.Type.ROADM) {
578 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
579 }
580 }
581 for (Device device : deviceService.getDevices()) {
582 if (device.type() != Device.Type.ROADM) {
583 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
584 }
585 }
586 }
587
588 // Sends all links to the client as link-added messages.
589 private void sendAllLinks() {
590 // Send optical first, others later for layered rendering
591 for (Link link : linkService.getLinks()) {
592 if (link.type() == Link.Type.OPTICAL) {
593 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
594 }
595 }
596 for (Link link : linkService.getLinks()) {
597 if (link.type() != Link.Type.OPTICAL) {
598 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
599 }
600 }
601 }
602
603 // Sends all hosts to the client as host-added messages.
604 private void sendAllHosts() {
605 for (Host host : hostService.getHosts()) {
606 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
607 }
608 }
609
Thomas Vachuska329af532015-03-10 02:08:33 -0700610 private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) {
611 Set<ConnectPoint> points = new HashSet<>();
612 for (HostId hostId : hostIds) {
613 points.add(getHostLocation(hostId));
614 }
615 return points;
616 }
617
618 private HostLocation getHostLocation(HostId hostId) {
619 return hostService.getHost(hostId).location();
620 }
621
622 // Produces a list of host ids from the specified JSON array.
623 private Set<HostId> getHostIds(ArrayNode ids) {
624 Set<HostId> hostIds = new HashSet<>();
625 for (JsonNode id : ids) {
626 hostIds.add(hostId(id.asText()));
627 }
628 return hostIds;
629 }
630
631
Simon Huntd2747a02015-04-30 22:41:16 -0700632 private synchronized void startSummaryMonitoring() {
Thomas Vachuska329af532015-03-10 02:08:33 -0700633 stopSummaryMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700634 summaryTask = new SummaryMonitor();
Simon Hunta17fa672015-08-19 18:42:22 -0700635 timer.schedule(summaryTask, SUMMARY_PERIOD, SUMMARY_PERIOD);
Simon Huntd2747a02015-04-30 22:41:16 -0700636 summaryRunning = true;
Thomas Vachuska329af532015-03-10 02:08:33 -0700637 }
638
639 private synchronized void stopSummaryMonitoring() {
Simon Huntd2747a02015-04-30 22:41:16 -0700640 if (summaryTask != null) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700641 summaryTask.cancel();
642 summaryTask = null;
Thomas Vachuska329af532015-03-10 02:08:33 -0700643 }
Simon Huntd2747a02015-04-30 22:41:16 -0700644 summaryRunning = false;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700645 }
646
Thomas Vachuska329af532015-03-10 02:08:33 -0700647
648 // Adds all internal listeners.
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700649 private synchronized void addListeners() {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700650 listenersRemoved = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700651 clusterService.addListener(clusterListener);
652 mastershipService.addListener(mastershipListener);
653 deviceService.addListener(deviceListener);
654 linkService.addListener(linkListener);
655 hostService.addListener(hostListener);
656 intentService.addListener(intentListener);
657 flowService.addListener(flowListener);
658 }
659
660 // Removes all internal listeners.
661 private synchronized void removeListeners() {
662 if (!listenersRemoved) {
663 listenersRemoved = true;
664 clusterService.removeListener(clusterListener);
665 mastershipService.removeListener(mastershipListener);
666 deviceService.removeListener(deviceListener);
667 linkService.removeListener(linkListener);
668 hostService.removeListener(hostListener);
669 intentService.removeListener(intentListener);
670 flowService.removeListener(flowListener);
671 }
672 }
673
674 // Cluster event listener.
675 private class InternalClusterListener implements ClusterEventListener {
676 @Override
677 public void event(ClusterEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700678 msgSender.execute(() -> sendMessage(instanceMessage(event, null)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700679 }
680 }
681
682 // Mastership change listener
683 private class InternalMastershipListener implements MastershipListener {
684 @Override
685 public void event(MastershipEvent event) {
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700686 msgSender.execute(() -> {
Simon Huntb745ca62015-07-28 15:37:11 -0700687 sendAllInstances(UPDATE_INSTANCE);
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700688 Device device = deviceService.getDevice(event.subject());
689 if (device != null) {
690 sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device)));
691 }
692 });
Thomas Vachuska329af532015-03-10 02:08:33 -0700693 }
694 }
695
696 // Device event listener.
697 private class InternalDeviceListener implements DeviceListener {
698 @Override
699 public void event(DeviceEvent event) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700700 if (event.type() != PORT_STATS_UPDATED) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700701 msgSender.execute(() -> sendMessage(deviceMessage(event)));
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700702 eventAccummulator.add(event);
703 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700704 }
705 }
706
707 // Link event listener.
708 private class InternalLinkListener implements LinkListener {
709 @Override
710 public void event(LinkEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700711 msgSender.execute(() -> sendMessage(linkMessage(event)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700712 eventAccummulator.add(event);
713 }
714 }
715
716 // Host event listener.
717 private class InternalHostListener implements HostListener {
718 @Override
719 public void event(HostEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700720 msgSender.execute(() -> sendMessage(hostMessage(event)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700721 eventAccummulator.add(event);
722 }
723 }
724
725 // Intent event listener.
726 private class InternalIntentListener implements IntentListener {
727 @Override
728 public void event(IntentEvent event) {
Simon Hunta17fa672015-08-19 18:42:22 -0700729 msgSender.execute(tmo::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700730 eventAccummulator.add(event);
731 }
732 }
733
734 // Intent event listener.
735 private class InternalFlowListener implements FlowRuleListener {
736 @Override
737 public void event(FlowRuleEvent event) {
738 eventAccummulator.add(event);
739 }
740 }
741
Simon Huntd2747a02015-04-30 22:41:16 -0700742
Simon Hunta17fa672015-08-19 18:42:22 -0700743 // === SUMMARY MONITORING
Thomas Vachuska329af532015-03-10 02:08:33 -0700744
745 // Periodic update of the summary information
746 private class SummaryMonitor extends TimerTask {
747 @Override
748 public void run() {
749 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700750 if (summaryRunning) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700751 msgSender.execute(() -> requestSummary(0));
Thomas Vachuska329af532015-03-10 02:08:33 -0700752 }
753 } catch (Exception e) {
754 log.warn("Unable to handle summary request due to {}", e.getMessage());
755 log.warn("Boom!", e);
756 }
757 }
758 }
759
760 // Accumulates events to drive methodic update of the summary pane.
761 private class InternalEventAccummulator extends AbstractAccumulator<Event> {
762 protected InternalEventAccummulator() {
763 super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS);
764 }
765
766 @Override
767 public void processItems(List<Event> items) {
Simon Hunta17fa672015-08-19 18:42:22 -0700768 // Start-of-Debugging -- Keep in until ONOS-2572 is fixed for reals
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700769 long now = System.currentTimeMillis();
770 String me = this.toString();
771 String miniMe = me.replaceAll("^.*@", "me@");
772 log.debug("Time: {}; this: {}, processing items ({} events)",
773 now, miniMe, items.size());
774 // End-of-Debugging
775
Thomas Vachuska329af532015-03-10 02:08:33 -0700776 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700777 if (summaryRunning) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700778 msgSender.execute(() -> requestSummary(0));
Thomas Vachuska329af532015-03-10 02:08:33 -0700779 }
780 } catch (Exception e) {
781 log.warn("Unable to handle summary request due to {}", e.getMessage());
782 log.debug("Boom!", e);
783 }
784 }
785 }
786}