blob: 82b7a78270970ce9f4c645b9d5fc6de974774c06 [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;
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;
74import java.util.Set;
75import java.util.Timer;
76import java.util.TimerTask;
Thomas Vachuska52c98bd2015-05-27 20:54:02 -070077import java.util.concurrent.ExecutorService;
Thomas Vachuska329af532015-03-10 02:08:33 -070078
Thomas Vachuska52c98bd2015-05-27 20:54:02 -070079import static java.util.concurrent.Executors.newSingleThreadExecutor;
80import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska329af532015-03-10 02:08:33 -070081import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED;
82import static org.onosproject.net.DeviceId.deviceId;
83import static org.onosproject.net.HostId.hostId;
Simon Hunt4a6b54b2015-10-27 22:08:25 -070084import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
85import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED;
86import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED;
Thomas Vachuska329af532015-03-10 02:08:33 -070087import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
88import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
Simon Huntd3ceffa2015-08-25 12:44:35 -070089import static org.onosproject.ui.JsonUtils.envelope;
Simon Hunt52560662015-08-27 22:46:44 -070090import static org.onosproject.ui.topo.TopoJson.highlightsMessage;
91import static org.onosproject.ui.topo.TopoJson.json;
Thomas Vachuska329af532015-03-10 02:08:33 -070092
93/**
94 * Web socket capable of interacting with the GUI topology view.
95 */
96public class TopologyViewMessageHandler extends TopologyViewMessageHandlerBase {
97
Simon Huntb745ca62015-07-28 15:37:11 -070098 // incoming event types
Simon Huntd2747a02015-04-30 22:41:16 -070099 private static final String REQ_DETAILS = "requestDetails";
100 private static final String UPDATE_META = "updateMeta";
101 private static final String ADD_HOST_INTENT = "addHostIntent";
102 private static final String ADD_MULTI_SRC_INTENT = "addMultiSourceIntent";
103 private static final String REQ_RELATED_INTENTS = "requestRelatedIntents";
104 private static final String REQ_NEXT_INTENT = "requestNextRelatedIntent";
105 private static final String REQ_PREV_INTENT = "requestPrevRelatedIntent";
106 private static final String REQ_SEL_INTENT_TRAFFIC = "requestSelectedIntentTraffic";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700107 private static final String SEL_INTENT = "selectIntent";
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700108 private static final String REQ_ALL_FLOW_TRAFFIC = "requestAllFlowTraffic";
109 private static final String REQ_ALL_PORT_TRAFFIC = "requestAllPortTraffic";
Simon Huntd2747a02015-04-30 22:41:16 -0700110 private static final String REQ_DEV_LINK_FLOWS = "requestDeviceLinkFlows";
111 private static final String CANCEL_TRAFFIC = "cancelTraffic";
112 private static final String REQ_SUMMARY = "requestSummary";
113 private static final String CANCEL_SUMMARY = "cancelSummary";
114 private static final String EQ_MASTERS = "equalizeMasters";
115 private static final String SPRITE_LIST_REQ = "spriteListRequest";
116 private static final String SPRITE_DATA_REQ = "spriteDataRequest";
117 private static final String TOPO_START = "topoStart";
Simon Hunt732bb2e2015-05-13 18:32:16 -0700118 private static final String TOPO_HEARTBEAT = "topoHeartbeat";
Simon Hunte05cae42015-07-23 17:35:24 -0700119 private static final String TOPO_SELECT_OVERLAY = "topoSelectOverlay";
Simon Huntd2747a02015-04-30 22:41:16 -0700120 private static final String TOPO_STOP = "topoStop";
121
Simon Huntb745ca62015-07-28 15:37:11 -0700122 // outgoing event types
123 private static final String SHOW_SUMMARY = "showSummary";
124 private static final String SHOW_DETAILS = "showDetails";
125 private static final String SPRITE_LIST_RESPONSE = "spriteListResponse";
126 private static final String SPRITE_DATA_RESPONSE = "spriteDataResponse";
127 private static final String UPDATE_INSTANCE = "updateInstance";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700128 private static final String TOPO_START_DONE = "topoStartDone";
Simon Huntb745ca62015-07-28 15:37:11 -0700129
130 // fields
131 private static final String ID = "id";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700132 private static final String KEY = "key";
133 private static final String APP_ID = "appId";
134 private static final String APP_NAME = "appName";
Simon Huntb745ca62015-07-28 15:37:11 -0700135 private static final String DEVICE = "device";
136 private static final String HOST = "host";
137 private static final String CLASS = "class";
138 private static final String UNKNOWN = "unknown";
139 private static final String ONE = "one";
140 private static final String TWO = "two";
141 private static final String SRC = "src";
142 private static final String DST = "dst";
143 private static final String DATA = "data";
144 private static final String NAME = "name";
145 private static final String NAMES = "names";
146 private static final String ACTIVATE = "activate";
147 private static final String DEACTIVATE = "deactivate";
Simon Huntb745ca62015-07-28 15:37:11 -0700148
Simon Huntd2747a02015-04-30 22:41:16 -0700149
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700150 private static final String MY_APP_ID = "org.onosproject.gui";
Thomas Vachuska329af532015-03-10 02:08:33 -0700151
Simon Hunta17fa672015-08-19 18:42:22 -0700152 private static final long TRAFFIC_PERIOD = 5000;
153 private static final long SUMMARY_PERIOD = 30000;
Thomas Vachuska329af532015-03-10 02:08:33 -0700154
155 private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
Thomas Vachuskac0fe09a2015-05-21 12:56:22 -0700156 (o1, o2) -> o1.id().toString().compareTo(o2.id().toString());
Thomas Vachuska329af532015-03-10 02:08:33 -0700157
158
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700159 private final Timer timer = new Timer("onos-topology-view");
Thomas Vachuska329af532015-03-10 02:08:33 -0700160
161 private static final int MAX_EVENTS = 1000;
162 private static final int MAX_BATCH_MS = 5000;
163 private static final int MAX_IDLE_MS = 1000;
164
165 private ApplicationId appId;
166
167 private final ClusterEventListener clusterListener = new InternalClusterListener();
168 private final MastershipListener mastershipListener = new InternalMastershipListener();
169 private final DeviceListener deviceListener = new InternalDeviceListener();
170 private final LinkListener linkListener = new InternalLinkListener();
171 private final HostListener hostListener = new InternalHostListener();
172 private final IntentListener intentListener = new InternalIntentListener();
173 private final FlowRuleListener flowListener = new InternalFlowListener();
174
175 private final Accumulator<Event> eventAccummulator = new InternalEventAccummulator();
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700176 private final ExecutorService msgSender =
177 newSingleThreadExecutor(groupedThreads("onos/gui", "msg-sender"));
Thomas Vachuska329af532015-03-10 02:08:33 -0700178
Simon Hunta17fa672015-08-19 18:42:22 -0700179 private TopoOverlayCache overlayCache;
Simon Hunt4fc86852015-08-20 17:57:52 -0700180 private TrafficMonitor traffic;
Thomas Vachuska329af532015-03-10 02:08:33 -0700181
Simon Huntd2747a02015-04-30 22:41:16 -0700182 private TimerTask summaryTask = null;
183 private boolean summaryRunning = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700184
185 private boolean listenersRemoved = false;
186
Thomas Vachuska329af532015-03-10 02:08:33 -0700187
188 @Override
189 public void init(UiConnection connection, ServiceDirectory directory) {
190 super.init(connection, directory);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700191 appId = directory.get(CoreService.class).registerApplication(MY_APP_ID);
Simon Hunt4fc86852015-08-20 17:57:52 -0700192 traffic = new TrafficMonitor(TRAFFIC_PERIOD, servicesBundle, this);
Thomas Vachuska329af532015-03-10 02:08:33 -0700193 }
194
195 @Override
196 public void destroy() {
197 cancelAllRequests();
Thomas Vachuska2bb48632015-04-28 14:40:42 -0700198 removeListeners();
Thomas Vachuska329af532015-03-10 02:08:33 -0700199 super.destroy();
200 }
201
Thomas Vachuska329af532015-03-10 02:08:33 -0700202 @Override
Simon Huntda580882015-05-12 20:58:18 -0700203 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700204 return ImmutableSet.of(
205 new TopoStart(),
Simon Hunt732bb2e2015-05-13 18:32:16 -0700206 new TopoHeartbeat(),
Simon Hunte05cae42015-07-23 17:35:24 -0700207 new TopoSelectOverlay(),
Simon Huntd2747a02015-04-30 22:41:16 -0700208 new TopoStop(),
209 new ReqSummary(),
210 new CancelSummary(),
211 new SpriteListReq(),
212 new SpriteDataReq(),
213 new RequestDetails(),
214 new UpdateMeta(),
215 new EqMasters(),
Thomas Vachuska329af532015-03-10 02:08:33 -0700216
Simon Huntd2747a02015-04-30 22:41:16 -0700217 // TODO: migrate traffic related to separate app
218 new AddHostIntent(),
219 new AddMultiSourceIntent(),
Simon Hunta17fa672015-08-19 18:42:22 -0700220
221 new ReqAllFlowTraffic(),
222 new ReqAllPortTraffic(),
223 new ReqDevLinkFlows(),
Simon Huntd2747a02015-04-30 22:41:16 -0700224 new ReqRelatedIntents(),
225 new ReqNextIntent(),
226 new ReqPrevIntent(),
227 new ReqSelectedIntentTraffic(),
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700228 new SelIntent(),
Simon Hunta17fa672015-08-19 18:42:22 -0700229
Simon Huntd2747a02015-04-30 22:41:16 -0700230 new CancelTraffic()
231 );
232 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700233
Simon Hunte05cae42015-07-23 17:35:24 -0700234 /**
235 * Injects the topology overlay cache.
236 *
237 * @param overlayCache injected cache
238 */
239 void setOverlayCache(TopoOverlayCache overlayCache) {
240 this.overlayCache = overlayCache;
241 }
242
Simon Huntd2747a02015-04-30 22:41:16 -0700243 // ==================================================================
Thomas Vachuska329af532015-03-10 02:08:33 -0700244
Simon Huntd2747a02015-04-30 22:41:16 -0700245 private final class TopoStart extends RequestHandler {
246 private TopoStart() {
247 super(TOPO_START);
248 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700249
Simon Huntd2747a02015-04-30 22:41:16 -0700250 @Override
251 public void process(long sid, ObjectNode payload) {
252 addListeners();
253 sendAllInstances(null);
254 sendAllDevices();
255 sendAllLinks();
256 sendAllHosts();
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700257 sendTopoStartDone();
Thomas Vachuska329af532015-03-10 02:08:33 -0700258 }
259 }
260
Simon Hunt732bb2e2015-05-13 18:32:16 -0700261 private final class TopoHeartbeat extends RequestHandler {
262 private TopoHeartbeat() {
263 super(TOPO_HEARTBEAT);
264 }
265
266 @Override
267 public void process(long sid, ObjectNode payload) {
268 // place holder for now
269 }
270 }
271
Simon Hunte05cae42015-07-23 17:35:24 -0700272 private final class TopoSelectOverlay extends RequestHandler {
273 private TopoSelectOverlay() {
274 super(TOPO_SELECT_OVERLAY);
275 }
276
277 @Override
278 public void process(long sid, ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700279 String deact = string(payload, DEACTIVATE);
280 String act = string(payload, ACTIVATE);
Simon Hunte05cae42015-07-23 17:35:24 -0700281 overlayCache.switchOverlay(deact, act);
282 }
283 }
284
Simon Huntd2747a02015-04-30 22:41:16 -0700285 private final class TopoStop extends RequestHandler {
286 private TopoStop() {
287 super(TOPO_STOP);
288 }
289
290 @Override
291 public void process(long sid, ObjectNode payload) {
292 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700293 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700294 }
295 }
296
297 private final class ReqSummary extends RequestHandler {
298 private ReqSummary() {
299 super(REQ_SUMMARY);
300 }
301
302 @Override
303 public void process(long sid, ObjectNode payload) {
304 requestSummary(sid);
305 startSummaryMonitoring();
306 }
307 }
308
309 private final class CancelSummary extends RequestHandler {
310 private CancelSummary() {
311 super(CANCEL_SUMMARY);
312 }
313
314 @Override
315 public void process(long sid, ObjectNode payload) {
316 stopSummaryMonitoring();
317 }
318 }
319
320 private final class SpriteListReq extends RequestHandler {
321 private SpriteListReq() {
322 super(SPRITE_LIST_REQ);
323 }
324
325 @Override
326 public void process(long sid, ObjectNode payload) {
Simon Huntda580882015-05-12 20:58:18 -0700327 ObjectNode root = objectNode();
328 ArrayNode names = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700329 get(SpriteService.class).getNames().forEach(names::add);
Simon Huntb745ca62015-07-28 15:37:11 -0700330 root.set(NAMES, names);
331 sendMessage(SPRITE_LIST_RESPONSE, sid, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700332 }
333 }
334
335 private final class SpriteDataReq extends RequestHandler {
336 private SpriteDataReq() {
337 super(SPRITE_DATA_REQ);
338 }
339
340 @Override
341 public void process(long sid, ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700342 String name = string(payload, NAME);
Simon Huntda580882015-05-12 20:58:18 -0700343 ObjectNode root = objectNode();
Simon Huntb745ca62015-07-28 15:37:11 -0700344 root.set(DATA, get(SpriteService.class).get(name));
345 sendMessage(SPRITE_DATA_RESPONSE, sid, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700346 }
347 }
348
349 private final class RequestDetails extends RequestHandler {
350 private RequestDetails() {
351 super(REQ_DETAILS);
352 }
353
354 @Override
355 public void process(long sid, ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700356 String type = string(payload, CLASS, UNKNOWN);
357 String id = string(payload, ID);
358 PropertyPanel pp = null;
Simon Huntd2747a02015-04-30 22:41:16 -0700359
Simon Huntb745ca62015-07-28 15:37:11 -0700360 if (type.equals(DEVICE)) {
Simon Huntde99e0b2015-10-23 18:54:06 -0700361 DeviceId did = deviceId(id);
362 pp = deviceDetails(did, sid);
363 overlayCache.currentOverlay().modifyDeviceDetails(pp, did);
Simon Huntb745ca62015-07-28 15:37:11 -0700364 } else if (type.equals(HOST)) {
Simon Huntde99e0b2015-10-23 18:54:06 -0700365 HostId hid = hostId(id);
366 pp = hostDetails(hid, sid);
367 overlayCache.currentOverlay().modifyHostDetails(pp, hid);
Simon Huntd2747a02015-04-30 22:41:16 -0700368 }
Simon Huntb745ca62015-07-28 15:37:11 -0700369
Simon Huntd3ceffa2015-08-25 12:44:35 -0700370 sendMessage(envelope(SHOW_DETAILS, sid, json(pp)));
Simon Huntd2747a02015-04-30 22:41:16 -0700371 }
372 }
373
374 private final class UpdateMeta extends RequestHandler {
375 private UpdateMeta() {
376 super(UPDATE_META);
377 }
378
379 @Override
380 public void process(long sid, ObjectNode payload) {
381 updateMetaUi(payload);
382 }
383 }
384
385 private final class EqMasters extends RequestHandler {
386 private EqMasters() {
387 super(EQ_MASTERS);
388 }
389
390 @Override
391 public void process(long sid, ObjectNode payload) {
392 directory.get(MastershipAdminService.class).balanceRoles();
393 }
394 }
395
Simon Hunta17fa672015-08-19 18:42:22 -0700396
397 // ========= -----------------------------------------------------------------
398
Simon Huntd2747a02015-04-30 22:41:16 -0700399 // === TODO: move traffic related classes to traffic app
400
401 private final class AddHostIntent extends RequestHandler {
402 private AddHostIntent() {
403 super(ADD_HOST_INTENT);
404 }
405
406 @Override
407 public void process(long sid, ObjectNode payload) {
408 // TODO: add protection against device ids and non-existent hosts.
Simon Huntb745ca62015-07-28 15:37:11 -0700409 HostId one = hostId(string(payload, ONE));
410 HostId two = hostId(string(payload, TWO));
Simon Huntd2747a02015-04-30 22:41:16 -0700411
412 HostToHostIntent intent = HostToHostIntent.builder()
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700413 .appId(appId)
414 .one(one)
415 .two(two)
416 .build();
Simon Huntd2747a02015-04-30 22:41:16 -0700417
418 intentService.submit(intent);
Simon Huntd2862c32015-08-24 17:41:51 -0700419 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
420 traffic.monitor(intent);
421 }
Simon Huntd2747a02015-04-30 22:41:16 -0700422 }
423 }
424
425 private final class AddMultiSourceIntent extends RequestHandler {
426 private AddMultiSourceIntent() {
427 super(ADD_MULTI_SRC_INTENT);
428 }
429
430 @Override
431 public void process(long sid, ObjectNode payload) {
432 // TODO: add protection against device ids and non-existent hosts.
Simon Huntb745ca62015-07-28 15:37:11 -0700433 Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC));
434 HostId dst = hostId(string(payload, DST));
Simon Huntd2747a02015-04-30 22:41:16 -0700435 Host dstHost = hostService.getHost(dst);
436
437 Set<ConnectPoint> ingressPoints = getHostLocations(src);
438
439 // FIXME: clearly, this is not enough
440 TrafficSelector selector = DefaultTrafficSelector.builder()
441 .matchEthDst(dstHost.mac()).build();
442 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
443
444 MultiPointToSinglePointIntent intent =
445 MultiPointToSinglePointIntent.builder()
446 .appId(appId)
447 .selector(selector)
448 .treatment(treatment)
449 .ingressPoints(ingressPoints)
450 .egressPoint(dstHost.location())
451 .build();
452
453 intentService.submit(intent);
Simon Huntd2862c32015-08-24 17:41:51 -0700454 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
455 traffic.monitor(intent);
456 }
Simon Huntd2747a02015-04-30 22:41:16 -0700457 }
458 }
459
Simon Hunta17fa672015-08-19 18:42:22 -0700460 // ========= -----------------------------------------------------------------
Simon Huntd2747a02015-04-30 22:41:16 -0700461
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700462 private final class ReqAllFlowTraffic extends RequestHandler {
463 private ReqAllFlowTraffic() {
464 super(REQ_ALL_FLOW_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700465 }
466
467 @Override
468 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700469 traffic.monitor(Mode.ALL_FLOW_TRAFFIC);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700470 }
471 }
472
473 private final class ReqAllPortTraffic extends RequestHandler {
474 private ReqAllPortTraffic() {
475 super(REQ_ALL_PORT_TRAFFIC);
476 }
477
478 @Override
479 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700480 traffic.monitor(Mode.ALL_PORT_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700481 }
482 }
483
484 private final class ReqDevLinkFlows extends RequestHandler {
485 private ReqDevLinkFlows() {
486 super(REQ_DEV_LINK_FLOWS);
487 }
488
489 @Override
490 public void process(long sid, ObjectNode payload) {
Simon Hunta17fa672015-08-19 18:42:22 -0700491 NodeSelection nodeSelection =
492 new NodeSelection(payload, deviceService, hostService);
Simon Hunt4fc86852015-08-20 17:57:52 -0700493 traffic.monitor(Mode.DEV_LINK_FLOWS, nodeSelection);
Simon Hunta17fa672015-08-19 18:42:22 -0700494 }
495 }
496
497 private final class ReqRelatedIntents extends RequestHandler {
498 private ReqRelatedIntents() {
499 super(REQ_RELATED_INTENTS);
500 }
501
502 @Override
503 public void process(long sid, ObjectNode payload) {
504 NodeSelection nodeSelection =
505 new NodeSelection(payload, deviceService, hostService);
Simon Hunt4fc86852015-08-20 17:57:52 -0700506 traffic.monitor(Mode.RELATED_INTENTS, nodeSelection);
Simon Hunta17fa672015-08-19 18:42:22 -0700507 }
508 }
509
510 private final class ReqNextIntent extends RequestHandler {
511 private ReqNextIntent() {
512 super(REQ_NEXT_INTENT);
513 }
514
515 @Override
516 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700517 traffic.selectNextIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700518 }
519 }
520
521 private final class ReqPrevIntent extends RequestHandler {
522 private ReqPrevIntent() {
523 super(REQ_PREV_INTENT);
524 }
525
526 @Override
527 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700528 traffic.selectPreviousIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700529 }
530 }
531
532 private final class ReqSelectedIntentTraffic extends RequestHandler {
533 private ReqSelectedIntentTraffic() {
534 super(REQ_SEL_INTENT_TRAFFIC);
535 }
536
537 @Override
538 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700539 traffic.monitor(Mode.SELECTED_INTENT);
Simon Huntd2747a02015-04-30 22:41:16 -0700540 }
541 }
542
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700543 private final class SelIntent extends RequestHandler {
544 private SelIntent() {
545 super(SEL_INTENT);
546 }
547
548 @Override
549 public void process(long sid, ObjectNode payload) {
550 int appId = Integer.parseInt(string(payload, APP_ID));
551 String appName = string(payload, APP_NAME);
552 ApplicationId applicId = new DefaultApplicationId(appId, appName);
553 long intentKey = Long.decode(string(payload, KEY));
554
555 Key key = Key.of(intentKey, applicId);
556 log.debug("Attempting to select intent key={}", key);
557
558 Intent intent = intentService.getIntent(key);
559 if (intent == null) {
560 log.debug("no such intent found!");
561 } else {
562 log.debug("starting to monitor intent {}", key);
563 traffic.monitor(intent);
564 }
565 }
566 }
567
Simon Huntd2747a02015-04-30 22:41:16 -0700568 private final class CancelTraffic extends RequestHandler {
569 private CancelTraffic() {
570 super(CANCEL_TRAFFIC);
571 }
572
573 @Override
574 public void process(long sid, ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700575 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700576 }
577 }
578
579 //=======================================================================
580
Simon Hunta17fa672015-08-19 18:42:22 -0700581 // Converts highlights to JSON format and sends the message to the client
582 protected void sendHighlights(Highlights highlights) {
Simon Hunt52560662015-08-27 22:46:44 -0700583 sendMessage(highlightsMessage(highlights));
Thomas Vachuska329af532015-03-10 02:08:33 -0700584 }
585
Simon Huntd2747a02015-04-30 22:41:16 -0700586 // Subscribes for summary messages.
587 private synchronized void requestSummary(long sid) {
Simon Hunt0af1ec32015-07-24 12:17:55 -0700588 PropertyPanel pp = summmaryMessage(sid);
589 overlayCache.currentOverlay().modifySummary(pp);
Simon Huntd3ceffa2015-08-25 12:44:35 -0700590 sendMessage(envelope(SHOW_SUMMARY, sid, json(pp)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700591 }
592
Simon Huntd2747a02015-04-30 22:41:16 -0700593
Thomas Vachuska329af532015-03-10 02:08:33 -0700594 private void cancelAllRequests() {
595 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700596 traffic.stopMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700597 }
598
599 // Sends all controller nodes to the client as node-added messages.
600 private void sendAllInstances(String messageType) {
601 List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
602 Collections.sort(nodes, NODE_COMPARATOR);
603 for (ControllerNode node : nodes) {
604 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node),
605 messageType));
606 }
607 }
608
609 // Sends all devices to the client as device-added messages.
610 private void sendAllDevices() {
611 // Send optical first, others later for layered rendering
612 for (Device device : deviceService.getDevices()) {
613 if (device.type() == Device.Type.ROADM) {
614 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
615 }
616 }
617 for (Device device : deviceService.getDevices()) {
618 if (device.type() != Device.Type.ROADM) {
619 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
620 }
621 }
622 }
623
624 // Sends all links to the client as link-added messages.
625 private void sendAllLinks() {
626 // Send optical first, others later for layered rendering
627 for (Link link : linkService.getLinks()) {
628 if (link.type() == Link.Type.OPTICAL) {
629 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
630 }
631 }
632 for (Link link : linkService.getLinks()) {
633 if (link.type() != Link.Type.OPTICAL) {
634 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
635 }
636 }
637 }
638
639 // Sends all hosts to the client as host-added messages.
640 private void sendAllHosts() {
641 for (Host host : hostService.getHosts()) {
642 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
643 }
644 }
645
Thomas Vachuska329af532015-03-10 02:08:33 -0700646 private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) {
647 Set<ConnectPoint> points = new HashSet<>();
648 for (HostId hostId : hostIds) {
649 points.add(getHostLocation(hostId));
650 }
651 return points;
652 }
653
654 private HostLocation getHostLocation(HostId hostId) {
655 return hostService.getHost(hostId).location();
656 }
657
658 // Produces a list of host ids from the specified JSON array.
659 private Set<HostId> getHostIds(ArrayNode ids) {
660 Set<HostId> hostIds = new HashSet<>();
661 for (JsonNode id : ids) {
662 hostIds.add(hostId(id.asText()));
663 }
664 return hostIds;
665 }
666
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700667 private void sendTopoStartDone() {
668 sendMessage(JsonUtils.envelope(TOPO_START_DONE, objectNode()));
669 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700670
Simon Huntd2747a02015-04-30 22:41:16 -0700671 private synchronized void startSummaryMonitoring() {
Thomas Vachuska329af532015-03-10 02:08:33 -0700672 stopSummaryMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700673 summaryTask = new SummaryMonitor();
Simon Hunta17fa672015-08-19 18:42:22 -0700674 timer.schedule(summaryTask, SUMMARY_PERIOD, SUMMARY_PERIOD);
Simon Huntd2747a02015-04-30 22:41:16 -0700675 summaryRunning = true;
Thomas Vachuska329af532015-03-10 02:08:33 -0700676 }
677
678 private synchronized void stopSummaryMonitoring() {
Simon Huntd2747a02015-04-30 22:41:16 -0700679 if (summaryTask != null) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700680 summaryTask.cancel();
681 summaryTask = null;
Thomas Vachuska329af532015-03-10 02:08:33 -0700682 }
Simon Huntd2747a02015-04-30 22:41:16 -0700683 summaryRunning = false;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700684 }
685
Thomas Vachuska329af532015-03-10 02:08:33 -0700686
687 // Adds all internal listeners.
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700688 private synchronized void addListeners() {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700689 listenersRemoved = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700690 clusterService.addListener(clusterListener);
691 mastershipService.addListener(mastershipListener);
692 deviceService.addListener(deviceListener);
693 linkService.addListener(linkListener);
694 hostService.addListener(hostListener);
695 intentService.addListener(intentListener);
696 flowService.addListener(flowListener);
697 }
698
699 // Removes all internal listeners.
700 private synchronized void removeListeners() {
701 if (!listenersRemoved) {
702 listenersRemoved = true;
703 clusterService.removeListener(clusterListener);
704 mastershipService.removeListener(mastershipListener);
705 deviceService.removeListener(deviceListener);
706 linkService.removeListener(linkListener);
707 hostService.removeListener(hostListener);
708 intentService.removeListener(intentListener);
709 flowService.removeListener(flowListener);
710 }
711 }
712
713 // Cluster event listener.
714 private class InternalClusterListener implements ClusterEventListener {
715 @Override
716 public void event(ClusterEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700717 msgSender.execute(() -> sendMessage(instanceMessage(event, null)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700718 }
719 }
720
721 // Mastership change listener
722 private class InternalMastershipListener implements MastershipListener {
723 @Override
724 public void event(MastershipEvent event) {
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700725 msgSender.execute(() -> {
Simon Huntb745ca62015-07-28 15:37:11 -0700726 sendAllInstances(UPDATE_INSTANCE);
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700727 Device device = deviceService.getDevice(event.subject());
728 if (device != null) {
729 sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device)));
730 }
731 });
Thomas Vachuska329af532015-03-10 02:08:33 -0700732 }
733 }
734
735 // Device event listener.
736 private class InternalDeviceListener implements DeviceListener {
737 @Override
738 public void event(DeviceEvent event) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700739 if (event.type() != PORT_STATS_UPDATED) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700740 msgSender.execute(() -> sendMessage(deviceMessage(event)));
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700741 eventAccummulator.add(event);
742 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700743 }
744 }
745
746 // Link event listener.
747 private class InternalLinkListener implements LinkListener {
748 @Override
749 public void event(LinkEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700750 msgSender.execute(() -> sendMessage(linkMessage(event)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700751 eventAccummulator.add(event);
752 }
753 }
754
755 // Host event listener.
756 private class InternalHostListener implements HostListener {
757 @Override
758 public void event(HostEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700759 msgSender.execute(() -> sendMessage(hostMessage(event)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700760 eventAccummulator.add(event);
761 }
762 }
763
764 // Intent event listener.
765 private class InternalIntentListener implements IntentListener {
766 @Override
767 public void event(IntentEvent event) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700768 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700769 eventAccummulator.add(event);
770 }
771 }
772
773 // Intent event listener.
774 private class InternalFlowListener implements FlowRuleListener {
775 @Override
776 public void event(FlowRuleEvent event) {
777 eventAccummulator.add(event);
778 }
779 }
780
Simon Huntd2747a02015-04-30 22:41:16 -0700781
Simon Hunta17fa672015-08-19 18:42:22 -0700782 // === SUMMARY MONITORING
Thomas Vachuska329af532015-03-10 02:08:33 -0700783
784 // Periodic update of the summary information
785 private class SummaryMonitor extends TimerTask {
786 @Override
787 public void run() {
788 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700789 if (summaryRunning) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700790 msgSender.execute(() -> requestSummary(0));
Thomas Vachuska329af532015-03-10 02:08:33 -0700791 }
792 } catch (Exception e) {
793 log.warn("Unable to handle summary request due to {}", e.getMessage());
794 log.warn("Boom!", e);
795 }
796 }
797 }
798
799 // Accumulates events to drive methodic update of the summary pane.
800 private class InternalEventAccummulator extends AbstractAccumulator<Event> {
801 protected InternalEventAccummulator() {
802 super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS);
803 }
804
805 @Override
806 public void processItems(List<Event> items) {
Simon Hunta17fa672015-08-19 18:42:22 -0700807 // Start-of-Debugging -- Keep in until ONOS-2572 is fixed for reals
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700808 long now = System.currentTimeMillis();
809 String me = this.toString();
810 String miniMe = me.replaceAll("^.*@", "me@");
811 log.debug("Time: {}; this: {}, processing items ({} events)",
812 now, miniMe, items.size());
813 // End-of-Debugging
814
Thomas Vachuska329af532015-03-10 02:08:33 -0700815 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700816 if (summaryRunning) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700817 msgSender.execute(() -> requestSummary(0));
Thomas Vachuska329af532015-03-10 02:08:33 -0700818 }
819 } catch (Exception e) {
820 log.warn("Unable to handle summary request due to {}", e.getMessage());
821 log.debug("Boom!", e);
822 }
823 }
824 }
825}