blob: d48a089ad840a5d6c78790cefbcda9f391b97136 [file] [log] [blame]
Thomas Vachuska329af532015-03-10 02:08:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska329af532015-03-10 02:08:33 -07003 *
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;
Simon Hunt4a6b54b2015-10-27 22:08:25 -070030import org.onosproject.core.DefaultApplicationId;
Thomas Vachuska329af532015-03-10 02:08:33 -070031import org.onosproject.event.Event;
32import org.onosproject.mastership.MastershipAdminService;
33import org.onosproject.mastership.MastershipEvent;
34import org.onosproject.mastership.MastershipListener;
35import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.Device;
Simon Huntde99e0b2015-10-23 18:54:06 -070037import org.onosproject.net.DeviceId;
Thomas Vachuska329af532015-03-10 02:08:33 -070038import org.onosproject.net.Host;
39import org.onosproject.net.HostId;
40import org.onosproject.net.HostLocation;
41import org.onosproject.net.Link;
42import org.onosproject.net.device.DeviceEvent;
43import org.onosproject.net.device.DeviceListener;
44import org.onosproject.net.flow.DefaultTrafficSelector;
45import org.onosproject.net.flow.DefaultTrafficTreatment;
46import org.onosproject.net.flow.FlowRuleEvent;
47import org.onosproject.net.flow.FlowRuleListener;
48import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
50import org.onosproject.net.host.HostEvent;
51import org.onosproject.net.host.HostListener;
52import org.onosproject.net.intent.HostToHostIntent;
Simon Hunt4a6b54b2015-10-27 22:08:25 -070053import org.onosproject.net.intent.Intent;
Thomas Vachuska329af532015-03-10 02:08:33 -070054import org.onosproject.net.intent.IntentEvent;
55import org.onosproject.net.intent.IntentListener;
Simon Hunt4a6b54b2015-10-27 22:08:25 -070056import org.onosproject.net.intent.Key;
Thomas Vachuska329af532015-03-10 02:08:33 -070057import org.onosproject.net.intent.MultiPointToSinglePointIntent;
58import org.onosproject.net.link.LinkEvent;
59import org.onosproject.net.link.LinkListener;
Simon Hunt4a6b54b2015-10-27 22:08:25 -070060import org.onosproject.ui.JsonUtils;
Simon Huntd2747a02015-04-30 22:41:16 -070061import org.onosproject.ui.RequestHandler;
Thomas Vachuska329af532015-03-10 02:08:33 -070062import org.onosproject.ui.UiConnection;
Simon Hunt4fc86852015-08-20 17:57:52 -070063import org.onosproject.ui.impl.TrafficMonitor.Mode;
Simon Hunta17fa672015-08-19 18:42:22 -070064import org.onosproject.ui.topo.Highlights;
Simon Huntd3ceffa2015-08-25 12:44:35 -070065import org.onosproject.ui.topo.NodeSelection;
Simon Hunt0af1ec32015-07-24 12:17:55 -070066import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070067
68import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070069import java.util.Collection;
Thomas Vachuska329af532015-03-10 02:08:33 -070070import java.util.Collections;
71import java.util.Comparator;
72import java.util.HashSet;
73import java.util.List;
Simon Hunt5c1a9382016-06-01 19:35:35 -070074import java.util.Map;
Thomas Vachuska329af532015-03-10 02:08:33 -070075import java.util.Set;
76import java.util.Timer;
77import java.util.TimerTask;
Thomas Vachuska52c98bd2015-05-27 20:54:02 -070078import java.util.concurrent.ExecutorService;
Thomas Vachuska329af532015-03-10 02:08:33 -070079
Thomas Vachuska52c98bd2015-05-27 20:54:02 -070080import static java.util.concurrent.Executors.newSingleThreadExecutor;
81import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska329af532015-03-10 02:08:33 -070082import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED;
83import static org.onosproject.net.DeviceId.deviceId;
84import static org.onosproject.net.HostId.hostId;
Simon Hunt4a6b54b2015-10-27 22:08:25 -070085import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
86import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED;
87import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED;
Thomas Vachuska329af532015-03-10 02:08:33 -070088import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
89import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
Simon Huntd3ceffa2015-08-25 12:44:35 -070090import static org.onosproject.ui.JsonUtils.envelope;
Viswanath KSP0f297702016-08-13 18:02:43 +053091import static org.onosproject.ui.JsonUtils.string;
Simon Hunt52560662015-08-27 22:46:44 -070092import static org.onosproject.ui.topo.TopoJson.highlightsMessage;
93import static org.onosproject.ui.topo.TopoJson.json;
Thomas Vachuska329af532015-03-10 02:08:33 -070094
95/**
96 * Web socket capable of interacting with the GUI topology view.
97 */
98public class TopologyViewMessageHandler extends TopologyViewMessageHandlerBase {
99
Simon Huntb745ca62015-07-28 15:37:11 -0700100 // incoming event types
Simon Huntd2747a02015-04-30 22:41:16 -0700101 private static final String REQ_DETAILS = "requestDetails";
102 private static final String UPDATE_META = "updateMeta";
103 private static final String ADD_HOST_INTENT = "addHostIntent";
Viswanath KSP0f297702016-08-13 18:02:43 +0530104 private static final String REMOVE_INTENT = "removeIntent";
Simon Huntd2747a02015-04-30 22:41:16 -0700105 private static final String ADD_MULTI_SRC_INTENT = "addMultiSourceIntent";
106 private static final String REQ_RELATED_INTENTS = "requestRelatedIntents";
107 private static final String REQ_NEXT_INTENT = "requestNextRelatedIntent";
108 private static final String REQ_PREV_INTENT = "requestPrevRelatedIntent";
109 private static final String REQ_SEL_INTENT_TRAFFIC = "requestSelectedIntentTraffic";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700110 private static final String SEL_INTENT = "selectIntent";
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700111 private static final String REQ_ALL_FLOW_TRAFFIC = "requestAllFlowTraffic";
112 private static final String REQ_ALL_PORT_TRAFFIC = "requestAllPortTraffic";
Simon Huntd2747a02015-04-30 22:41:16 -0700113 private static final String REQ_DEV_LINK_FLOWS = "requestDeviceLinkFlows";
114 private static final String CANCEL_TRAFFIC = "cancelTraffic";
115 private static final String REQ_SUMMARY = "requestSummary";
116 private static final String CANCEL_SUMMARY = "cancelSummary";
117 private static final String EQ_MASTERS = "equalizeMasters";
118 private static final String SPRITE_LIST_REQ = "spriteListRequest";
119 private static final String SPRITE_DATA_REQ = "spriteDataRequest";
120 private static final String TOPO_START = "topoStart";
Simon Hunt732bb2e2015-05-13 18:32:16 -0700121 private static final String TOPO_HEARTBEAT = "topoHeartbeat";
Simon Hunte05cae42015-07-23 17:35:24 -0700122 private static final String TOPO_SELECT_OVERLAY = "topoSelectOverlay";
Simon Huntd2747a02015-04-30 22:41:16 -0700123 private static final String TOPO_STOP = "topoStop";
124
Simon Huntb745ca62015-07-28 15:37:11 -0700125 // outgoing event types
126 private static final String SHOW_SUMMARY = "showSummary";
127 private static final String SHOW_DETAILS = "showDetails";
128 private static final String SPRITE_LIST_RESPONSE = "spriteListResponse";
129 private static final String SPRITE_DATA_RESPONSE = "spriteDataResponse";
130 private static final String UPDATE_INSTANCE = "updateInstance";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700131 private static final String TOPO_START_DONE = "topoStartDone";
Simon Huntb745ca62015-07-28 15:37:11 -0700132
133 // fields
Simon Hunt5c1a9382016-06-01 19:35:35 -0700134 private static final String PAYLOAD = "payload";
135 private static final String EXTRA = "extra";
Simon Huntb745ca62015-07-28 15:37:11 -0700136 private static final String ID = "id";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700137 private static final String KEY = "key";
138 private static final String APP_ID = "appId";
139 private static final String APP_NAME = "appName";
Simon Huntb745ca62015-07-28 15:37:11 -0700140 private static final String DEVICE = "device";
141 private static final String HOST = "host";
142 private static final String CLASS = "class";
143 private static final String UNKNOWN = "unknown";
144 private static final String ONE = "one";
145 private static final String TWO = "two";
146 private static final String SRC = "src";
147 private static final String DST = "dst";
148 private static final String DATA = "data";
149 private static final String NAME = "name";
150 private static final String NAMES = "names";
151 private static final String ACTIVATE = "activate";
152 private static final String DEACTIVATE = "deactivate";
Simon Huntb745ca62015-07-28 15:37:11 -0700153
Simon Huntd2747a02015-04-30 22:41:16 -0700154
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700155 private static final String MY_APP_ID = "org.onosproject.gui";
Thomas Vachuska329af532015-03-10 02:08:33 -0700156
Simon Hunta17fa672015-08-19 18:42:22 -0700157 private static final long TRAFFIC_PERIOD = 5000;
158 private static final long SUMMARY_PERIOD = 30000;
Thomas Vachuska329af532015-03-10 02:08:33 -0700159
160 private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
Thomas Vachuskac0fe09a2015-05-21 12:56:22 -0700161 (o1, o2) -> o1.id().toString().compareTo(o2.id().toString());
Thomas Vachuska329af532015-03-10 02:08:33 -0700162
163
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700164 private final Timer timer = new Timer("onos-topology-view");
Thomas Vachuska329af532015-03-10 02:08:33 -0700165
166 private static final int MAX_EVENTS = 1000;
167 private static final int MAX_BATCH_MS = 5000;
168 private static final int MAX_IDLE_MS = 1000;
169
170 private ApplicationId appId;
171
172 private final ClusterEventListener clusterListener = new InternalClusterListener();
173 private final MastershipListener mastershipListener = new InternalMastershipListener();
174 private final DeviceListener deviceListener = new InternalDeviceListener();
175 private final LinkListener linkListener = new InternalLinkListener();
176 private final HostListener hostListener = new InternalHostListener();
177 private final IntentListener intentListener = new InternalIntentListener();
178 private final FlowRuleListener flowListener = new InternalFlowListener();
179
180 private final Accumulator<Event> eventAccummulator = new InternalEventAccummulator();
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700181 private final ExecutorService msgSender =
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700182 newSingleThreadExecutor(groupedThreads("onos/gui", "msg-sender", log));
Thomas Vachuska329af532015-03-10 02:08:33 -0700183
Simon Hunta17fa672015-08-19 18:42:22 -0700184 private TopoOverlayCache overlayCache;
Simon Hunt4fc86852015-08-20 17:57:52 -0700185 private TrafficMonitor traffic;
Thomas Vachuska329af532015-03-10 02:08:33 -0700186
Simon Huntd2747a02015-04-30 22:41:16 -0700187 private TimerTask summaryTask = null;
188 private boolean summaryRunning = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700189
Simon Huntd5b96732016-07-08 13:22:27 -0700190 private volatile boolean listenersRemoved = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700191
Thomas Vachuska329af532015-03-10 02:08:33 -0700192
193 @Override
194 public void init(UiConnection connection, ServiceDirectory directory) {
195 super.init(connection, directory);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700196 appId = directory.get(CoreService.class).registerApplication(MY_APP_ID);
Simon Hunt4fc86852015-08-20 17:57:52 -0700197 traffic = new TrafficMonitor(TRAFFIC_PERIOD, servicesBundle, this);
Thomas Vachuska329af532015-03-10 02:08:33 -0700198 }
199
200 @Override
201 public void destroy() {
202 cancelAllRequests();
Thomas Vachuska2bb48632015-04-28 14:40:42 -0700203 removeListeners();
Thomas Vachuska329af532015-03-10 02:08:33 -0700204 super.destroy();
205 }
206
Thomas Vachuska329af532015-03-10 02:08:33 -0700207 @Override
Simon Huntda580882015-05-12 20:58:18 -0700208 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700209 return ImmutableSet.of(
210 new TopoStart(),
Simon Hunt732bb2e2015-05-13 18:32:16 -0700211 new TopoHeartbeat(),
Simon Hunte05cae42015-07-23 17:35:24 -0700212 new TopoSelectOverlay(),
Simon Huntd2747a02015-04-30 22:41:16 -0700213 new TopoStop(),
214 new ReqSummary(),
215 new CancelSummary(),
216 new SpriteListReq(),
217 new SpriteDataReq(),
218 new RequestDetails(),
219 new UpdateMeta(),
220 new EqMasters(),
Thomas Vachuska329af532015-03-10 02:08:33 -0700221
Simon Huntd2747a02015-04-30 22:41:16 -0700222 // TODO: migrate traffic related to separate app
223 new AddHostIntent(),
224 new AddMultiSourceIntent(),
Viswanath KSP0f297702016-08-13 18:02:43 +0530225 new RemoveIntent(),
Simon Hunta17fa672015-08-19 18:42:22 -0700226
227 new ReqAllFlowTraffic(),
228 new ReqAllPortTraffic(),
229 new ReqDevLinkFlows(),
Simon Huntd2747a02015-04-30 22:41:16 -0700230 new ReqRelatedIntents(),
231 new ReqNextIntent(),
232 new ReqPrevIntent(),
233 new ReqSelectedIntentTraffic(),
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700234 new SelIntent(),
Simon Hunta17fa672015-08-19 18:42:22 -0700235
Simon Huntd2747a02015-04-30 22:41:16 -0700236 new CancelTraffic()
237 );
238 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700239
Simon Hunte05cae42015-07-23 17:35:24 -0700240 /**
241 * Injects the topology overlay cache.
242 *
243 * @param overlayCache injected cache
244 */
245 void setOverlayCache(TopoOverlayCache overlayCache) {
246 this.overlayCache = overlayCache;
247 }
248
Simon Huntd2747a02015-04-30 22:41:16 -0700249 // ==================================================================
Thomas Vachuska329af532015-03-10 02:08:33 -0700250
Simon Huntd2747a02015-04-30 22:41:16 -0700251 private final class TopoStart extends RequestHandler {
252 private TopoStart() {
253 super(TOPO_START);
254 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700255
Simon Huntd2747a02015-04-30 22:41:16 -0700256 @Override
257 public void process(long sid, ObjectNode payload) {
258 addListeners();
259 sendAllInstances(null);
260 sendAllDevices();
261 sendAllLinks();
262 sendAllHosts();
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700263 sendTopoStartDone();
Thomas Vachuska329af532015-03-10 02:08:33 -0700264 }
265 }
266
Simon Hunt732bb2e2015-05-13 18:32:16 -0700267 private final class TopoHeartbeat extends RequestHandler {
268 private TopoHeartbeat() {
269 super(TOPO_HEARTBEAT);
270 }
271
272 @Override
273 public void process(long sid, ObjectNode payload) {
274 // place holder for now
275 }
276 }
277
Simon Hunte05cae42015-07-23 17:35:24 -0700278 private final class TopoSelectOverlay extends RequestHandler {
279 private TopoSelectOverlay() {
280 super(TOPO_SELECT_OVERLAY);
281 }
282
283 @Override
284 public void process(long sid, ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700285 String deact = string(payload, DEACTIVATE);
286 String act = string(payload, ACTIVATE);
Simon Hunte05cae42015-07-23 17:35:24 -0700287 overlayCache.switchOverlay(deact, act);
288 }
289 }
290
Simon Huntd2747a02015-04-30 22:41:16 -0700291 private final class TopoStop extends RequestHandler {
292 private TopoStop() {
293 super(TOPO_STOP);
294 }
295
296 @Override
297 public void process(long sid, ObjectNode payload) {
Simon Hunt1ad59272015-11-10 15:23:21 -0800298 removeListeners();
Simon Huntd2747a02015-04-30 22:41:16 -0700299 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700300 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700301 }
302 }
303
304 private final class ReqSummary extends RequestHandler {
305 private ReqSummary() {
306 super(REQ_SUMMARY);
307 }
308
309 @Override
310 public void process(long sid, ObjectNode payload) {
311 requestSummary(sid);
312 startSummaryMonitoring();
313 }
314 }
315
316 private final class CancelSummary extends RequestHandler {
317 private CancelSummary() {
318 super(CANCEL_SUMMARY);
319 }
320
321 @Override
322 public void process(long sid, ObjectNode payload) {
323 stopSummaryMonitoring();
324 }
325 }
326
327 private final class SpriteListReq extends RequestHandler {
328 private SpriteListReq() {
329 super(SPRITE_LIST_REQ);
330 }
331
332 @Override
333 public void process(long sid, ObjectNode payload) {
Simon Huntda580882015-05-12 20:58:18 -0700334 ObjectNode root = objectNode();
335 ArrayNode names = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700336 get(SpriteService.class).getNames().forEach(names::add);
Simon Huntb745ca62015-07-28 15:37:11 -0700337 root.set(NAMES, names);
338 sendMessage(SPRITE_LIST_RESPONSE, sid, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700339 }
340 }
341
342 private final class SpriteDataReq extends RequestHandler {
343 private SpriteDataReq() {
344 super(SPRITE_DATA_REQ);
345 }
346
347 @Override
348 public void process(long sid, ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700349 String name = string(payload, NAME);
Simon Huntda580882015-05-12 20:58:18 -0700350 ObjectNode root = objectNode();
Simon Huntb745ca62015-07-28 15:37:11 -0700351 root.set(DATA, get(SpriteService.class).get(name));
352 sendMessage(SPRITE_DATA_RESPONSE, sid, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700353 }
354 }
355
356 private final class RequestDetails extends RequestHandler {
357 private RequestDetails() {
358 super(REQ_DETAILS);
359 }
360
361 @Override
362 public void process(long sid, ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700363 String type = string(payload, CLASS, UNKNOWN);
364 String id = string(payload, ID);
365 PropertyPanel pp = null;
Simon Huntd2747a02015-04-30 22:41:16 -0700366
Simon Huntb745ca62015-07-28 15:37:11 -0700367 if (type.equals(DEVICE)) {
Simon Huntde99e0b2015-10-23 18:54:06 -0700368 DeviceId did = deviceId(id);
369 pp = deviceDetails(did, sid);
370 overlayCache.currentOverlay().modifyDeviceDetails(pp, did);
Simon Huntb745ca62015-07-28 15:37:11 -0700371 } else if (type.equals(HOST)) {
Simon Huntde99e0b2015-10-23 18:54:06 -0700372 HostId hid = hostId(id);
373 pp = hostDetails(hid, sid);
374 overlayCache.currentOverlay().modifyHostDetails(pp, hid);
Simon Huntd2747a02015-04-30 22:41:16 -0700375 }
Simon Huntb745ca62015-07-28 15:37:11 -0700376
Simon Huntd3ceffa2015-08-25 12:44:35 -0700377 sendMessage(envelope(SHOW_DETAILS, sid, json(pp)));
Simon Huntd2747a02015-04-30 22:41:16 -0700378 }
379 }
380
381 private final class UpdateMeta extends RequestHandler {
382 private UpdateMeta() {
383 super(UPDATE_META);
384 }
385
386 @Override
387 public void process(long sid, ObjectNode payload) {
388 updateMetaUi(payload);
389 }
390 }
391
392 private final class EqMasters extends RequestHandler {
393 private EqMasters() {
394 super(EQ_MASTERS);
395 }
396
397 @Override
398 public void process(long sid, ObjectNode payload) {
399 directory.get(MastershipAdminService.class).balanceRoles();
400 }
401 }
402
Simon Hunta17fa672015-08-19 18:42:22 -0700403
404 // ========= -----------------------------------------------------------------
405
Simon Huntd2747a02015-04-30 22:41:16 -0700406 // === TODO: move traffic related classes to traffic app
407
408 private final class AddHostIntent extends RequestHandler {
409 private AddHostIntent() {
410 super(ADD_HOST_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 HostId one = hostId(string(payload, ONE));
417 HostId two = hostId(string(payload, TWO));
Simon Huntd2747a02015-04-30 22:41:16 -0700418
419 HostToHostIntent intent = HostToHostIntent.builder()
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700420 .appId(appId)
421 .one(one)
422 .two(two)
423 .build();
Simon Huntd2747a02015-04-30 22:41:16 -0700424
425 intentService.submit(intent);
Simon Huntd2862c32015-08-24 17:41:51 -0700426 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
427 traffic.monitor(intent);
428 }
Simon Huntd2747a02015-04-30 22:41:16 -0700429 }
430 }
431
Viswanath KSP0f297702016-08-13 18:02:43 +0530432 private Intent findIntentByPayload(ObjectNode payload) {
433 int appId = Integer.parseInt(string(payload, APP_ID));
434 String appName = string(payload, APP_NAME);
435 ApplicationId applicId = new DefaultApplicationId(appId, appName);
436 long intentKey = Long.decode(string(payload, KEY));
437
438 Key key = Key.of(intentKey, applicId);
439 log.debug("Attempting to select intent by key={}", key);
440
441 Intent intent = intentService.getIntent(key);
442
443 return intent;
444 }
445
446 private final class RemoveIntent extends RequestHandler {
447 private RemoveIntent() {
448 super(REMOVE_INTENT);
449 }
450
451 @Override
452 public void process(long sid, ObjectNode payload) {
453 Intent intent = findIntentByPayload(payload);
454 if (intent == null) {
455 log.warn("Unable to find intent from payload {}", payload);
456 } else {
457 log.debug("Removing intent {}", intent.key());
458 intentService.withdraw(intent);
459 }
460 }
461 }
462
Simon Huntd2747a02015-04-30 22:41:16 -0700463 private final class AddMultiSourceIntent extends RequestHandler {
464 private AddMultiSourceIntent() {
465 super(ADD_MULTI_SRC_INTENT);
466 }
467
468 @Override
469 public void process(long sid, ObjectNode payload) {
470 // TODO: add protection against device ids and non-existent hosts.
Simon Huntb745ca62015-07-28 15:37:11 -0700471 Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC));
472 HostId dst = hostId(string(payload, DST));
Simon Huntd2747a02015-04-30 22:41:16 -0700473 Host dstHost = hostService.getHost(dst);
474
475 Set<ConnectPoint> ingressPoints = getHostLocations(src);
476
477 // FIXME: clearly, this is not enough
478 TrafficSelector selector = DefaultTrafficSelector.builder()
479 .matchEthDst(dstHost.mac()).build();
480 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
481
482 MultiPointToSinglePointIntent intent =
483 MultiPointToSinglePointIntent.builder()
484 .appId(appId)
485 .selector(selector)
486 .treatment(treatment)
487 .ingressPoints(ingressPoints)
488 .egressPoint(dstHost.location())
489 .build();
490
491 intentService.submit(intent);
Simon Huntd2862c32015-08-24 17:41:51 -0700492 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
493 traffic.monitor(intent);
494 }
Simon Huntd2747a02015-04-30 22:41:16 -0700495 }
496 }
497
Simon Hunta17fa672015-08-19 18:42:22 -0700498 // ========= -----------------------------------------------------------------
Simon Huntd2747a02015-04-30 22:41:16 -0700499
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700500 private final class ReqAllFlowTraffic extends RequestHandler {
501 private ReqAllFlowTraffic() {
502 super(REQ_ALL_FLOW_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700503 }
504
505 @Override
506 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700507 traffic.monitor(Mode.ALL_FLOW_TRAFFIC);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700508 }
509 }
510
511 private final class ReqAllPortTraffic extends RequestHandler {
512 private ReqAllPortTraffic() {
513 super(REQ_ALL_PORT_TRAFFIC);
514 }
515
516 @Override
517 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700518 traffic.monitor(Mode.ALL_PORT_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700519 }
520 }
521
522 private final class ReqDevLinkFlows extends RequestHandler {
523 private ReqDevLinkFlows() {
524 super(REQ_DEV_LINK_FLOWS);
525 }
526
527 @Override
528 public void process(long sid, ObjectNode payload) {
Simon Hunta17fa672015-08-19 18:42:22 -0700529 NodeSelection nodeSelection =
530 new NodeSelection(payload, deviceService, hostService);
Simon Hunt4fc86852015-08-20 17:57:52 -0700531 traffic.monitor(Mode.DEV_LINK_FLOWS, nodeSelection);
Simon Hunta17fa672015-08-19 18:42:22 -0700532 }
533 }
534
535 private final class ReqRelatedIntents extends RequestHandler {
536 private ReqRelatedIntents() {
537 super(REQ_RELATED_INTENTS);
538 }
539
540 @Override
541 public void process(long sid, ObjectNode payload) {
542 NodeSelection nodeSelection =
543 new NodeSelection(payload, deviceService, hostService);
Simon Hunt4fc86852015-08-20 17:57:52 -0700544 traffic.monitor(Mode.RELATED_INTENTS, nodeSelection);
Simon Hunta17fa672015-08-19 18:42:22 -0700545 }
546 }
547
548 private final class ReqNextIntent extends RequestHandler {
549 private ReqNextIntent() {
550 super(REQ_NEXT_INTENT);
551 }
552
553 @Override
554 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700555 traffic.selectNextIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700556 }
557 }
558
559 private final class ReqPrevIntent extends RequestHandler {
560 private ReqPrevIntent() {
561 super(REQ_PREV_INTENT);
562 }
563
564 @Override
565 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700566 traffic.selectPreviousIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700567 }
568 }
569
570 private final class ReqSelectedIntentTraffic extends RequestHandler {
571 private ReqSelectedIntentTraffic() {
572 super(REQ_SEL_INTENT_TRAFFIC);
573 }
574
575 @Override
576 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700577 traffic.monitor(Mode.SELECTED_INTENT);
Simon Huntd2747a02015-04-30 22:41:16 -0700578 }
579 }
580
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700581 private final class SelIntent extends RequestHandler {
582 private SelIntent() {
583 super(SEL_INTENT);
584 }
585
586 @Override
587 public void process(long sid, ObjectNode payload) {
Viswanath KSP0f297702016-08-13 18:02:43 +0530588 Intent intent = findIntentByPayload(payload);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700589 if (intent == null) {
Viswanath KSP0f297702016-08-13 18:02:43 +0530590 log.warn("Unable to find intent from payload {}", payload);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700591 } else {
Viswanath KSP0f297702016-08-13 18:02:43 +0530592 log.debug("starting to monitor intent {}", intent.key());
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700593 traffic.monitor(intent);
594 }
595 }
596 }
597
Simon Huntd2747a02015-04-30 22:41:16 -0700598 private final class CancelTraffic extends RequestHandler {
599 private CancelTraffic() {
600 super(CANCEL_TRAFFIC);
601 }
602
603 @Override
604 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700605 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700606 }
607 }
608
609 //=======================================================================
610
Simon Hunta17fa672015-08-19 18:42:22 -0700611 // Converts highlights to JSON format and sends the message to the client
612 protected void sendHighlights(Highlights highlights) {
Simon Hunt52560662015-08-27 22:46:44 -0700613 sendMessage(highlightsMessage(highlights));
Thomas Vachuska329af532015-03-10 02:08:33 -0700614 }
615
Simon Huntd2747a02015-04-30 22:41:16 -0700616 // Subscribes for summary messages.
617 private synchronized void requestSummary(long sid) {
Simon Hunt0af1ec32015-07-24 12:17:55 -0700618 PropertyPanel pp = summmaryMessage(sid);
619 overlayCache.currentOverlay().modifySummary(pp);
Simon Huntd3ceffa2015-08-25 12:44:35 -0700620 sendMessage(envelope(SHOW_SUMMARY, sid, json(pp)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700621 }
622
Simon Huntd2747a02015-04-30 22:41:16 -0700623
Thomas Vachuska329af532015-03-10 02:08:33 -0700624 private void cancelAllRequests() {
625 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700626 traffic.stopMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700627 }
628
629 // Sends all controller nodes to the client as node-added messages.
630 private void sendAllInstances(String messageType) {
631 List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
632 Collections.sort(nodes, NODE_COMPARATOR);
633 for (ControllerNode node : nodes) {
634 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node),
Simon Hunt5c1a9382016-06-01 19:35:35 -0700635 messageType));
Thomas Vachuska329af532015-03-10 02:08:33 -0700636 }
637 }
638
639 // Sends all devices to the client as device-added messages.
640 private void sendAllDevices() {
641 // Send optical first, others later for layered rendering
642 for (Device device : deviceService.getDevices()) {
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200643 if ((device.type() == Device.Type.ROADM) ||
Simon Hunt5c1a9382016-06-01 19:35:35 -0700644 (device.type() == Device.Type.OTN)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700645 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
646 }
647 }
648 for (Device device : deviceService.getDevices()) {
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200649 if ((device.type() != Device.Type.ROADM) &&
Simon Hunt5c1a9382016-06-01 19:35:35 -0700650 (device.type() != Device.Type.OTN)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700651 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
652 }
653 }
654 }
655
656 // Sends all links to the client as link-added messages.
657 private void sendAllLinks() {
658 // Send optical first, others later for layered rendering
659 for (Link link : linkService.getLinks()) {
660 if (link.type() == Link.Type.OPTICAL) {
Simon Hunt5c1a9382016-06-01 19:35:35 -0700661 sendMessage(composeLinkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700662 }
663 }
664 for (Link link : linkService.getLinks()) {
665 if (link.type() != Link.Type.OPTICAL) {
Simon Hunt5c1a9382016-06-01 19:35:35 -0700666 sendMessage(composeLinkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700667 }
668 }
669 }
670
Simon Hunt5c1a9382016-06-01 19:35:35 -0700671 // Temporary mechanism to support topology overlays adding their own
672 // properties to the link events.
673 private ObjectNode composeLinkMessage(LinkEvent event) {
674 // start with base message
675 ObjectNode msg = linkMessage(event);
676 Map<String, String> additional =
677 overlayCache.currentOverlay().additionalLinkData(event);
678
679 if (additional != null) {
680 // attach additional key-value pairs as extra data structure
681 ObjectNode payload = (ObjectNode) msg.get(PAYLOAD);
682 payload.set(EXTRA, createExtra(additional));
683 }
684 return msg;
685 }
686
687 private ObjectNode createExtra(Map<String, String> additional) {
688 ObjectNode extra = objectNode();
689 for (Map.Entry<String, String> entry : additional.entrySet()) {
690 extra.put(entry.getKey(), entry.getValue());
691 }
692 return extra;
693 }
694
Thomas Vachuska329af532015-03-10 02:08:33 -0700695 // Sends all hosts to the client as host-added messages.
696 private void sendAllHosts() {
697 for (Host host : hostService.getHosts()) {
698 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
699 }
700 }
701
Thomas Vachuska329af532015-03-10 02:08:33 -0700702 private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) {
703 Set<ConnectPoint> points = new HashSet<>();
704 for (HostId hostId : hostIds) {
705 points.add(getHostLocation(hostId));
706 }
707 return points;
708 }
709
710 private HostLocation getHostLocation(HostId hostId) {
711 return hostService.getHost(hostId).location();
712 }
713
714 // Produces a list of host ids from the specified JSON array.
715 private Set<HostId> getHostIds(ArrayNode ids) {
716 Set<HostId> hostIds = new HashSet<>();
717 for (JsonNode id : ids) {
718 hostIds.add(hostId(id.asText()));
719 }
720 return hostIds;
721 }
722
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700723 private void sendTopoStartDone() {
724 sendMessage(JsonUtils.envelope(TOPO_START_DONE, objectNode()));
725 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700726
Simon Huntd2747a02015-04-30 22:41:16 -0700727 private synchronized void startSummaryMonitoring() {
Thomas Vachuska329af532015-03-10 02:08:33 -0700728 stopSummaryMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700729 summaryTask = new SummaryMonitor();
Simon Hunta17fa672015-08-19 18:42:22 -0700730 timer.schedule(summaryTask, SUMMARY_PERIOD, SUMMARY_PERIOD);
Simon Huntd2747a02015-04-30 22:41:16 -0700731 summaryRunning = true;
Thomas Vachuska329af532015-03-10 02:08:33 -0700732 }
733
734 private synchronized void stopSummaryMonitoring() {
Simon Huntd2747a02015-04-30 22:41:16 -0700735 if (summaryTask != null) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700736 summaryTask.cancel();
737 summaryTask = null;
Thomas Vachuska329af532015-03-10 02:08:33 -0700738 }
Simon Huntd2747a02015-04-30 22:41:16 -0700739 summaryRunning = false;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700740 }
741
Thomas Vachuska329af532015-03-10 02:08:33 -0700742
743 // Adds all internal listeners.
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700744 private synchronized void addListeners() {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700745 listenersRemoved = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700746 clusterService.addListener(clusterListener);
747 mastershipService.addListener(mastershipListener);
748 deviceService.addListener(deviceListener);
749 linkService.addListener(linkListener);
750 hostService.addListener(hostListener);
751 intentService.addListener(intentListener);
752 flowService.addListener(flowListener);
753 }
754
755 // Removes all internal listeners.
756 private synchronized void removeListeners() {
757 if (!listenersRemoved) {
758 listenersRemoved = true;
759 clusterService.removeListener(clusterListener);
760 mastershipService.removeListener(mastershipListener);
761 deviceService.removeListener(deviceListener);
762 linkService.removeListener(linkListener);
763 hostService.removeListener(hostListener);
764 intentService.removeListener(intentListener);
765 flowService.removeListener(flowListener);
766 }
767 }
768
769 // Cluster event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700770 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
771 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700772 private class InternalClusterListener implements ClusterEventListener {
773 @Override
774 public void event(ClusterEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700775 msgSender.execute(() -> sendMessage(instanceMessage(event, null)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700776 }
777 }
778
779 // Mastership change listener
Simon Hunt7092cc42016-04-06 18:40:17 -0700780 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
781 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700782 private class InternalMastershipListener implements MastershipListener {
783 @Override
784 public void event(MastershipEvent event) {
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700785 msgSender.execute(() -> {
Simon Huntb745ca62015-07-28 15:37:11 -0700786 sendAllInstances(UPDATE_INSTANCE);
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700787 Device device = deviceService.getDevice(event.subject());
788 if (device != null) {
789 sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device)));
790 }
791 });
Thomas Vachuska329af532015-03-10 02:08:33 -0700792 }
793 }
794
795 // Device event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700796 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
797 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700798 private class InternalDeviceListener implements DeviceListener {
799 @Override
800 public void event(DeviceEvent event) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700801 if (event.type() != PORT_STATS_UPDATED) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700802 msgSender.execute(() -> sendMessage(deviceMessage(event)));
Simon Hunta1f1c022016-03-03 15:54:57 -0800803 msgSender.execute(traffic::pokeIntent);
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700804 eventAccummulator.add(event);
805 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700806 }
807 }
808
809 // Link event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700810 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
811 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700812 private class InternalLinkListener implements LinkListener {
813 @Override
814 public void event(LinkEvent event) {
Simon Hunt5c1a9382016-06-01 19:35:35 -0700815 msgSender.execute(() -> sendMessage(composeLinkMessage(event)));
Simon Hunta1f1c022016-03-03 15:54:57 -0800816 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700817 eventAccummulator.add(event);
818 }
819 }
820
821 // Host event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700822 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
823 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700824 private class InternalHostListener implements HostListener {
825 @Override
826 public void event(HostEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700827 msgSender.execute(() -> sendMessage(hostMessage(event)));
Simon Hunta1f1c022016-03-03 15:54:57 -0800828 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700829 eventAccummulator.add(event);
830 }
831 }
832
833 // Intent event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700834 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
835 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700836 private class InternalIntentListener implements IntentListener {
837 @Override
838 public void event(IntentEvent event) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700839 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700840 eventAccummulator.add(event);
841 }
842 }
843
844 // Intent event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700845 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
846 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700847 private class InternalFlowListener implements FlowRuleListener {
848 @Override
849 public void event(FlowRuleEvent event) {
850 eventAccummulator.add(event);
851 }
852 }
853
Simon Huntd2747a02015-04-30 22:41:16 -0700854
Simon Hunta17fa672015-08-19 18:42:22 -0700855 // === SUMMARY MONITORING
Thomas Vachuska329af532015-03-10 02:08:33 -0700856
857 // Periodic update of the summary information
858 private class SummaryMonitor extends TimerTask {
859 @Override
860 public void run() {
861 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700862 if (summaryRunning) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700863 msgSender.execute(() -> requestSummary(0));
Thomas Vachuska329af532015-03-10 02:08:33 -0700864 }
865 } catch (Exception e) {
866 log.warn("Unable to handle summary request due to {}", e.getMessage());
867 log.warn("Boom!", e);
868 }
869 }
870 }
871
872 // Accumulates events to drive methodic update of the summary pane.
873 private class InternalEventAccummulator extends AbstractAccumulator<Event> {
874 protected InternalEventAccummulator() {
875 super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS);
876 }
877
878 @Override
879 public void processItems(List<Event> items) {
Simon Hunta17fa672015-08-19 18:42:22 -0700880 // Start-of-Debugging -- Keep in until ONOS-2572 is fixed for reals
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700881 long now = System.currentTimeMillis();
882 String me = this.toString();
883 String miniMe = me.replaceAll("^.*@", "me@");
884 log.debug("Time: {}; this: {}, processing items ({} events)",
Simon Hunt5c1a9382016-06-01 19:35:35 -0700885 now, miniMe, items.size());
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700886 // End-of-Debugging
887
Thomas Vachuska329af532015-03-10 02:08:33 -0700888 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700889 if (summaryRunning) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700890 msgSender.execute(() -> requestSummary(0));
Thomas Vachuska329af532015-03-10 02:08:33 -0700891 }
892 } catch (Exception e) {
893 log.warn("Unable to handle summary request due to {}", e.getMessage());
894 log.debug("Boom!", e);
895 }
896 }
897 }
898}