blob: 85d5eec20c555694739c6006344682e090131488 [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;
Deepa Vaddireddy63340922017-01-19 08:15:31 +053058import org.onosproject.net.intent.IntentService;
Andrea Campanella732ea832017-02-06 09:25:59 -080059import org.onosproject.net.intent.IntentState;
Thomas Vachuska329af532015-03-10 02:08:33 -070060import org.onosproject.net.link.LinkEvent;
61import org.onosproject.net.link.LinkListener;
Simon Hunt4a6b54b2015-10-27 22:08:25 -070062import org.onosproject.ui.JsonUtils;
Simon Huntd2747a02015-04-30 22:41:16 -070063import org.onosproject.ui.RequestHandler;
Thomas Vachuska329af532015-03-10 02:08:33 -070064import org.onosproject.ui.UiConnection;
Simon Hunt4fc86852015-08-20 17:57:52 -070065import org.onosproject.ui.impl.TrafficMonitor.Mode;
Simon Hunta17fa672015-08-19 18:42:22 -070066import org.onosproject.ui.topo.Highlights;
Simon Huntd3ceffa2015-08-25 12:44:35 -070067import org.onosproject.ui.topo.NodeSelection;
Simon Hunt0af1ec32015-07-24 12:17:55 -070068import org.onosproject.ui.topo.PropertyPanel;
Thomas Vachuska329af532015-03-10 02:08:33 -070069
70import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070071import java.util.Collection;
Thomas Vachuska329af532015-03-10 02:08:33 -070072import java.util.Comparator;
73import java.util.HashSet;
74import java.util.List;
Simon Hunt5c1a9382016-06-01 19:35:35 -070075import java.util.Map;
Thomas Vachuska329af532015-03-10 02:08:33 -070076import java.util.Set;
77import java.util.Timer;
78import java.util.TimerTask;
Thomas Vachuska52c98bd2015-05-27 20:54:02 -070079import java.util.concurrent.ExecutorService;
Thomas Vachuska329af532015-03-10 02:08:33 -070080
Thomas Vachuska52c98bd2015-05-27 20:54:02 -070081import static java.util.concurrent.Executors.newSingleThreadExecutor;
82import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska329af532015-03-10 02:08:33 -070083import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED;
84import static org.onosproject.net.DeviceId.deviceId;
85import static org.onosproject.net.HostId.hostId;
Simon Hunt4a6b54b2015-10-27 22:08:25 -070086import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
87import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED;
88import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED;
Thomas Vachuska329af532015-03-10 02:08:33 -070089import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
90import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
Simon Huntd3ceffa2015-08-25 12:44:35 -070091import static org.onosproject.ui.JsonUtils.envelope;
Viswanath KSP0f297702016-08-13 18:02:43 +053092import static org.onosproject.ui.JsonUtils.string;
Simon Hunt52560662015-08-27 22:46:44 -070093import static org.onosproject.ui.topo.TopoJson.highlightsMessage;
94import static org.onosproject.ui.topo.TopoJson.json;
Thomas Vachuska329af532015-03-10 02:08:33 -070095
96/**
97 * Web socket capable of interacting with the GUI topology view.
98 */
99public class TopologyViewMessageHandler extends TopologyViewMessageHandlerBase {
100
Simon Huntb745ca62015-07-28 15:37:11 -0700101 // incoming event types
Simon Huntd2747a02015-04-30 22:41:16 -0700102 private static final String REQ_DETAILS = "requestDetails";
103 private static final String UPDATE_META = "updateMeta";
104 private static final String ADD_HOST_INTENT = "addHostIntent";
Viswanath KSP0f297702016-08-13 18:02:43 +0530105 private static final String REMOVE_INTENT = "removeIntent";
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530106 private static final String REMOVE_INTENTS = "removeIntents";
Viswanath KSP14aee092016-10-02 01:47:40 +0530107 private static final String RESUBMIT_INTENT = "resubmitIntent";
Simon Huntd2747a02015-04-30 22:41:16 -0700108 private static final String ADD_MULTI_SRC_INTENT = "addMultiSourceIntent";
109 private static final String REQ_RELATED_INTENTS = "requestRelatedIntents";
110 private static final String REQ_NEXT_INTENT = "requestNextRelatedIntent";
111 private static final String REQ_PREV_INTENT = "requestPrevRelatedIntent";
112 private static final String REQ_SEL_INTENT_TRAFFIC = "requestSelectedIntentTraffic";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700113 private static final String SEL_INTENT = "selectIntent";
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700114 private static final String REQ_ALL_FLOW_TRAFFIC = "requestAllFlowTraffic";
115 private static final String REQ_ALL_PORT_TRAFFIC = "requestAllPortTraffic";
Simon Huntd2747a02015-04-30 22:41:16 -0700116 private static final String REQ_DEV_LINK_FLOWS = "requestDeviceLinkFlows";
117 private static final String CANCEL_TRAFFIC = "cancelTraffic";
118 private static final String REQ_SUMMARY = "requestSummary";
119 private static final String CANCEL_SUMMARY = "cancelSummary";
120 private static final String EQ_MASTERS = "equalizeMasters";
121 private static final String SPRITE_LIST_REQ = "spriteListRequest";
122 private static final String SPRITE_DATA_REQ = "spriteDataRequest";
123 private static final String TOPO_START = "topoStart";
Simon Hunte05cae42015-07-23 17:35:24 -0700124 private static final String TOPO_SELECT_OVERLAY = "topoSelectOverlay";
Simon Huntd2747a02015-04-30 22:41:16 -0700125 private static final String TOPO_STOP = "topoStop";
126
Andrea Campanella732ea832017-02-06 09:25:59 -0800127 //Protected Intents events
128 private static final String SEL_PROTECTED_INTENT = "selectProtectedIntent";
129 private static final String CANCEL_PROTECTED_INTENT_HIGHLIGHT = "cancelProtectedIntentHighlight";
130
Simon Huntb745ca62015-07-28 15:37:11 -0700131 // outgoing event types
132 private static final String SHOW_SUMMARY = "showSummary";
133 private static final String SHOW_DETAILS = "showDetails";
134 private static final String SPRITE_LIST_RESPONSE = "spriteListResponse";
135 private static final String SPRITE_DATA_RESPONSE = "spriteDataResponse";
136 private static final String UPDATE_INSTANCE = "updateInstance";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700137 private static final String TOPO_START_DONE = "topoStartDone";
Simon Huntb745ca62015-07-28 15:37:11 -0700138
139 // fields
Simon Hunt5c1a9382016-06-01 19:35:35 -0700140 private static final String PAYLOAD = "payload";
141 private static final String EXTRA = "extra";
Simon Huntb745ca62015-07-28 15:37:11 -0700142 private static final String ID = "id";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700143 private static final String KEY = "key";
144 private static final String APP_ID = "appId";
145 private static final String APP_NAME = "appName";
Simon Huntb745ca62015-07-28 15:37:11 -0700146 private static final String DEVICE = "device";
147 private static final String HOST = "host";
148 private static final String CLASS = "class";
149 private static final String UNKNOWN = "unknown";
150 private static final String ONE = "one";
151 private static final String TWO = "two";
152 private static final String SRC = "src";
153 private static final String DST = "dst";
154 private static final String DATA = "data";
155 private static final String NAME = "name";
156 private static final String NAMES = "names";
157 private static final String ACTIVATE = "activate";
158 private static final String DEACTIVATE = "deactivate";
Viswanath KSP813a20d2016-09-13 04:25:41 +0530159 private static final String PURGE = "purge";
Simon Huntb745ca62015-07-28 15:37:11 -0700160
Simon Huntd2747a02015-04-30 22:41:16 -0700161
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700162 private static final String MY_APP_ID = "org.onosproject.gui";
Thomas Vachuska329af532015-03-10 02:08:33 -0700163
Simon Hunta17fa672015-08-19 18:42:22 -0700164 private static final long TRAFFIC_PERIOD = 5000;
165 private static final long SUMMARY_PERIOD = 30000;
Thomas Vachuska329af532015-03-10 02:08:33 -0700166
167 private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
Simon Hunt8a0429a2017-01-06 16:52:47 -0800168 Comparator.comparing(o -> o.id().toString());
Thomas Vachuska329af532015-03-10 02:08:33 -0700169
170
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700171 private final Timer timer = new Timer("onos-topology-view");
Thomas Vachuska329af532015-03-10 02:08:33 -0700172
173 private static final int MAX_EVENTS = 1000;
174 private static final int MAX_BATCH_MS = 5000;
175 private static final int MAX_IDLE_MS = 1000;
176
177 private ApplicationId appId;
178
179 private final ClusterEventListener clusterListener = new InternalClusterListener();
180 private final MastershipListener mastershipListener = new InternalMastershipListener();
181 private final DeviceListener deviceListener = new InternalDeviceListener();
182 private final LinkListener linkListener = new InternalLinkListener();
183 private final HostListener hostListener = new InternalHostListener();
184 private final IntentListener intentListener = new InternalIntentListener();
185 private final FlowRuleListener flowListener = new InternalFlowListener();
186
187 private final Accumulator<Event> eventAccummulator = new InternalEventAccummulator();
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700188 private final ExecutorService msgSender =
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700189 newSingleThreadExecutor(groupedThreads("onos/gui", "msg-sender", log));
Thomas Vachuska329af532015-03-10 02:08:33 -0700190
Simon Hunta17fa672015-08-19 18:42:22 -0700191 private TopoOverlayCache overlayCache;
Simon Hunt4fc86852015-08-20 17:57:52 -0700192 private TrafficMonitor traffic;
Andrea Campanella732ea832017-02-06 09:25:59 -0800193 private ProtectedIntentMonitor protectedIntentMonitor;
Thomas Vachuska329af532015-03-10 02:08:33 -0700194
Simon Huntd2747a02015-04-30 22:41:16 -0700195 private TimerTask summaryTask = null;
196 private boolean summaryRunning = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700197
Simon Huntd5b96732016-07-08 13:22:27 -0700198 private volatile boolean listenersRemoved = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700199
Thomas Vachuska329af532015-03-10 02:08:33 -0700200
201 @Override
202 public void init(UiConnection connection, ServiceDirectory directory) {
203 super.init(connection, directory);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700204 appId = directory.get(CoreService.class).registerApplication(MY_APP_ID);
Simon Hunt4fc86852015-08-20 17:57:52 -0700205 traffic = new TrafficMonitor(TRAFFIC_PERIOD, servicesBundle, this);
Andrea Campanella732ea832017-02-06 09:25:59 -0800206 protectedIntentMonitor = new ProtectedIntentMonitor(TRAFFIC_PERIOD, servicesBundle, this);
Thomas Vachuska329af532015-03-10 02:08:33 -0700207 }
208
209 @Override
210 public void destroy() {
211 cancelAllRequests();
Thomas Vachuska2bb48632015-04-28 14:40:42 -0700212 removeListeners();
Thomas Vachuska329af532015-03-10 02:08:33 -0700213 super.destroy();
214 }
215
Thomas Vachuska329af532015-03-10 02:08:33 -0700216 @Override
Simon Huntda580882015-05-12 20:58:18 -0700217 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700218 return ImmutableSet.of(
219 new TopoStart(),
Simon Hunte05cae42015-07-23 17:35:24 -0700220 new TopoSelectOverlay(),
Simon Huntd2747a02015-04-30 22:41:16 -0700221 new TopoStop(),
222 new ReqSummary(),
223 new CancelSummary(),
224 new SpriteListReq(),
225 new SpriteDataReq(),
226 new RequestDetails(),
227 new UpdateMeta(),
228 new EqMasters(),
Thomas Vachuska329af532015-03-10 02:08:33 -0700229
Simon Huntd2747a02015-04-30 22:41:16 -0700230 // TODO: migrate traffic related to separate app
231 new AddHostIntent(),
232 new AddMultiSourceIntent(),
Viswanath KSP0f297702016-08-13 18:02:43 +0530233 new RemoveIntent(),
Viswanath KSP14aee092016-10-02 01:47:40 +0530234 new ResubmitIntent(),
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530235 new RemoveIntents(),
Simon Hunta17fa672015-08-19 18:42:22 -0700236
237 new ReqAllFlowTraffic(),
238 new ReqAllPortTraffic(),
239 new ReqDevLinkFlows(),
Simon Huntd2747a02015-04-30 22:41:16 -0700240 new ReqRelatedIntents(),
241 new ReqNextIntent(),
242 new ReqPrevIntent(),
243 new ReqSelectedIntentTraffic(),
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700244 new SelIntent(),
Andrea Campanella732ea832017-02-06 09:25:59 -0800245 new SelProtectedIntent(),
Simon Hunta17fa672015-08-19 18:42:22 -0700246
Andrea Campanella732ea832017-02-06 09:25:59 -0800247 new CancelTraffic(),
248 new CancelProtectedIntentHighlight()
Simon Huntd2747a02015-04-30 22:41:16 -0700249 );
250 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700251
Simon Hunte05cae42015-07-23 17:35:24 -0700252 /**
253 * Injects the topology overlay cache.
254 *
255 * @param overlayCache injected cache
256 */
257 void setOverlayCache(TopoOverlayCache overlayCache) {
258 this.overlayCache = overlayCache;
259 }
260
Simon Huntd2747a02015-04-30 22:41:16 -0700261 // ==================================================================
Thomas Vachuska329af532015-03-10 02:08:33 -0700262
Simon Huntd2747a02015-04-30 22:41:16 -0700263 private final class TopoStart extends RequestHandler {
264 private TopoStart() {
265 super(TOPO_START);
266 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700267
Simon Huntd2747a02015-04-30 22:41:16 -0700268 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800269 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700270 addListeners();
271 sendAllInstances(null);
272 sendAllDevices();
273 sendAllLinks();
274 sendAllHosts();
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700275 sendTopoStartDone();
Thomas Vachuska329af532015-03-10 02:08:33 -0700276 }
277 }
278
Simon Hunte05cae42015-07-23 17:35:24 -0700279 private final class TopoSelectOverlay extends RequestHandler {
280 private TopoSelectOverlay() {
281 super(TOPO_SELECT_OVERLAY);
282 }
283
284 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800285 public void process(ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700286 String deact = string(payload, DEACTIVATE);
287 String act = string(payload, ACTIVATE);
Simon Hunte05cae42015-07-23 17:35:24 -0700288 overlayCache.switchOverlay(deact, act);
289 }
290 }
291
Simon Huntd2747a02015-04-30 22:41:16 -0700292 private final class TopoStop extends RequestHandler {
293 private TopoStop() {
294 super(TOPO_STOP);
295 }
296
297 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800298 public void process(ObjectNode payload) {
Simon Hunt1ad59272015-11-10 15:23:21 -0800299 removeListeners();
Simon Huntd2747a02015-04-30 22:41:16 -0700300 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700301 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700302 }
303 }
304
305 private final class ReqSummary extends RequestHandler {
306 private ReqSummary() {
307 super(REQ_SUMMARY);
308 }
309
310 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800311 public void process(ObjectNode payload) {
312 requestSummary();
Simon Huntd2747a02015-04-30 22:41:16 -0700313 startSummaryMonitoring();
314 }
315 }
316
317 private final class CancelSummary extends RequestHandler {
318 private CancelSummary() {
319 super(CANCEL_SUMMARY);
320 }
321
322 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800323 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700324 stopSummaryMonitoring();
325 }
326 }
327
328 private final class SpriteListReq extends RequestHandler {
329 private SpriteListReq() {
330 super(SPRITE_LIST_REQ);
331 }
332
333 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800334 public void process(ObjectNode payload) {
Simon Huntda580882015-05-12 20:58:18 -0700335 ObjectNode root = objectNode();
336 ArrayNode names = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700337 get(SpriteService.class).getNames().forEach(names::add);
Simon Huntb745ca62015-07-28 15:37:11 -0700338 root.set(NAMES, names);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800339 sendMessage(SPRITE_LIST_RESPONSE, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700340 }
341 }
342
343 private final class SpriteDataReq extends RequestHandler {
344 private SpriteDataReq() {
345 super(SPRITE_DATA_REQ);
346 }
347
348 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800349 public void process(ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700350 String name = string(payload, NAME);
Simon Huntda580882015-05-12 20:58:18 -0700351 ObjectNode root = objectNode();
Simon Huntb745ca62015-07-28 15:37:11 -0700352 root.set(DATA, get(SpriteService.class).get(name));
Simon Hunt8a0429a2017-01-06 16:52:47 -0800353 sendMessage(SPRITE_DATA_RESPONSE, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700354 }
355 }
356
357 private final class RequestDetails extends RequestHandler {
358 private RequestDetails() {
359 super(REQ_DETAILS);
360 }
361
362 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800363 public void process(ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700364 String type = string(payload, CLASS, UNKNOWN);
365 String id = string(payload, ID);
366 PropertyPanel pp = null;
Simon Huntd2747a02015-04-30 22:41:16 -0700367
Simon Huntb745ca62015-07-28 15:37:11 -0700368 if (type.equals(DEVICE)) {
Simon Huntde99e0b2015-10-23 18:54:06 -0700369 DeviceId did = deviceId(id);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800370 pp = deviceDetails(did);
Simon Huntde99e0b2015-10-23 18:54:06 -0700371 overlayCache.currentOverlay().modifyDeviceDetails(pp, did);
Simon Huntb745ca62015-07-28 15:37:11 -0700372 } else if (type.equals(HOST)) {
Simon Huntde99e0b2015-10-23 18:54:06 -0700373 HostId hid = hostId(id);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800374 pp = hostDetails(hid);
Simon Huntde99e0b2015-10-23 18:54:06 -0700375 overlayCache.currentOverlay().modifyHostDetails(pp, hid);
Simon Huntd2747a02015-04-30 22:41:16 -0700376 }
Simon Huntb745ca62015-07-28 15:37:11 -0700377
Simon Hunt8a0429a2017-01-06 16:52:47 -0800378 sendMessage(envelope(SHOW_DETAILS, json(pp)));
Simon Huntd2747a02015-04-30 22:41:16 -0700379 }
380 }
381
382 private final class UpdateMeta extends RequestHandler {
383 private UpdateMeta() {
384 super(UPDATE_META);
385 }
386
387 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800388 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700389 updateMetaUi(payload);
390 }
391 }
392
393 private final class EqMasters extends RequestHandler {
394 private EqMasters() {
395 super(EQ_MASTERS);
396 }
397
398 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800399 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700400 directory.get(MastershipAdminService.class).balanceRoles();
401 }
402 }
403
Simon Hunta17fa672015-08-19 18:42:22 -0700404
405 // ========= -----------------------------------------------------------------
406
Simon Huntd2747a02015-04-30 22:41:16 -0700407 // === TODO: move traffic related classes to traffic app
408
409 private final class AddHostIntent extends RequestHandler {
410 private AddHostIntent() {
411 super(ADD_HOST_INTENT);
412 }
413
414 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800415 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700416 // TODO: add protection against device ids and non-existent hosts.
Simon Huntb745ca62015-07-28 15:37:11 -0700417 HostId one = hostId(string(payload, ONE));
418 HostId two = hostId(string(payload, TWO));
Simon Huntd2747a02015-04-30 22:41:16 -0700419
420 HostToHostIntent intent = HostToHostIntent.builder()
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700421 .appId(appId)
422 .one(one)
423 .two(two)
424 .build();
Simon Huntd2747a02015-04-30 22:41:16 -0700425
426 intentService.submit(intent);
Simon Huntd2862c32015-08-24 17:41:51 -0700427 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
428 traffic.monitor(intent);
429 }
Simon Huntd2747a02015-04-30 22:41:16 -0700430 }
431 }
432
Viswanath KSP0f297702016-08-13 18:02:43 +0530433 private Intent findIntentByPayload(ObjectNode payload) {
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800434 Intent intent;
435 Key key;
Viswanath KSP0f297702016-08-13 18:02:43 +0530436 int appId = Integer.parseInt(string(payload, APP_ID));
437 String appName = string(payload, APP_NAME);
438 ApplicationId applicId = new DefaultApplicationId(appId, appName);
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800439 String stringKey = string(payload, KEY);
440 try {
441 // FIXME: If apps use different string key, but they contains
442 // same numeric value (e.g. "020", "0x10", "16", "#10")
443 // and one intent using long key (e.g. 16L)
444 // this function might return wrong intent.
Viswanath KSP0f297702016-08-13 18:02:43 +0530445
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800446 long longKey = Long.decode(stringKey);
447 key = Key.of(longKey, applicId);
448 intent = intentService.getIntent(key);
449
450 if (intent == null) {
451 // Intent might using string key, not long key
452 key = Key.of(stringKey, applicId);
453 intent = intentService.getIntent(key);
454 }
455 } catch (NumberFormatException ex) {
456 // string key
457 key = Key.of(stringKey, applicId);
458 intent = intentService.getIntent(key);
459 }
460
Viswanath KSP0f297702016-08-13 18:02:43 +0530461 log.debug("Attempting to select intent by key={}", key);
462
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800463 return intent;
Viswanath KSP0f297702016-08-13 18:02:43 +0530464 }
465
466 private final class RemoveIntent extends RequestHandler {
467 private RemoveIntent() {
468 super(REMOVE_INTENT);
469 }
470
Viswanath KSP813a20d2016-09-13 04:25:41 +0530471 private boolean isIntentToBePurged(ObjectNode payload) {
472 return bool(payload, PURGE);
473 }
474
Viswanath KSP0f297702016-08-13 18:02:43 +0530475 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800476 public void process(ObjectNode payload) {
Viswanath KSP0f297702016-08-13 18:02:43 +0530477 Intent intent = findIntentByPayload(payload);
478 if (intent == null) {
479 log.warn("Unable to find intent from payload {}", payload);
480 } else {
Viswanath KSP813a20d2016-09-13 04:25:41 +0530481 log.debug("Withdrawing / Purging intent {}", intent.key());
482 if (isIntentToBePurged(payload)) {
483 intentService.purge(intent);
484 } else {
485 intentService.withdraw(intent);
486 }
Viswanath KSP0f297702016-08-13 18:02:43 +0530487 }
488 }
489 }
490
Viswanath KSP14aee092016-10-02 01:47:40 +0530491 private final class ResubmitIntent extends RequestHandler {
492 private ResubmitIntent() {
493 super(RESUBMIT_INTENT);
494 }
495
496 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800497 public void process(ObjectNode payload) {
Viswanath KSP14aee092016-10-02 01:47:40 +0530498 Intent intent = findIntentByPayload(payload);
499 if (intent == null) {
500 log.warn("Unable to find intent from payload {}", payload);
501 } else {
502 log.debug("Resubmitting intent {}", intent.key());
503 intentService.submit(intent);
504 }
505 }
506 }
507
Simon Huntd2747a02015-04-30 22:41:16 -0700508 private final class AddMultiSourceIntent extends RequestHandler {
509 private AddMultiSourceIntent() {
510 super(ADD_MULTI_SRC_INTENT);
511 }
512
513 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800514 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700515 // TODO: add protection against device ids and non-existent hosts.
Simon Huntb745ca62015-07-28 15:37:11 -0700516 Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC));
517 HostId dst = hostId(string(payload, DST));
Simon Huntd2747a02015-04-30 22:41:16 -0700518 Host dstHost = hostService.getHost(dst);
519
520 Set<ConnectPoint> ingressPoints = getHostLocations(src);
521
522 // FIXME: clearly, this is not enough
523 TrafficSelector selector = DefaultTrafficSelector.builder()
524 .matchEthDst(dstHost.mac()).build();
525 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
526
527 MultiPointToSinglePointIntent intent =
528 MultiPointToSinglePointIntent.builder()
529 .appId(appId)
530 .selector(selector)
531 .treatment(treatment)
532 .ingressPoints(ingressPoints)
533 .egressPoint(dstHost.location())
534 .build();
535
536 intentService.submit(intent);
Simon Huntd2862c32015-08-24 17:41:51 -0700537 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
538 traffic.monitor(intent);
539 }
Simon Huntd2747a02015-04-30 22:41:16 -0700540 }
541 }
542
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530543 private final class RemoveIntents extends RequestHandler {
544 private RemoveIntents() {
545 super(REMOVE_INTENTS);
546 }
547
548
549 @Override
550 public void process(ObjectNode payload) {
551 IntentService intentService = get(IntentService.class);
552 for (Intent intent : intentService.getIntents()) {
553 if (intentService.getIntentState(intent.key()) == IntentState.WITHDRAWN) {
554 intentService.purge(intent);
555 }
556 }
557
558 }
559 }
560
Simon Hunta17fa672015-08-19 18:42:22 -0700561 // ========= -----------------------------------------------------------------
Simon Huntd2747a02015-04-30 22:41:16 -0700562
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700563 private final class ReqAllFlowTraffic extends RequestHandler {
564 private ReqAllFlowTraffic() {
565 super(REQ_ALL_FLOW_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700566 }
567
568 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800569 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700570 traffic.monitor(Mode.ALL_FLOW_TRAFFIC);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700571 }
572 }
573
574 private final class ReqAllPortTraffic extends RequestHandler {
575 private ReqAllPortTraffic() {
576 super(REQ_ALL_PORT_TRAFFIC);
577 }
578
579 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800580 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700581 traffic.monitor(Mode.ALL_PORT_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700582 }
583 }
584
585 private final class ReqDevLinkFlows extends RequestHandler {
586 private ReqDevLinkFlows() {
587 super(REQ_DEV_LINK_FLOWS);
588 }
589
590 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800591 public void process(ObjectNode payload) {
Simon Hunta17fa672015-08-19 18:42:22 -0700592 NodeSelection nodeSelection =
Prince Pereira46c82d42016-09-19 13:30:50 +0530593 new NodeSelection(payload, deviceService, hostService, linkService);
Simon Hunt4fc86852015-08-20 17:57:52 -0700594 traffic.monitor(Mode.DEV_LINK_FLOWS, nodeSelection);
Simon Hunta17fa672015-08-19 18:42:22 -0700595 }
596 }
597
598 private final class ReqRelatedIntents extends RequestHandler {
599 private ReqRelatedIntents() {
600 super(REQ_RELATED_INTENTS);
601 }
602
603 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800604 public void process(ObjectNode payload) {
Simon Hunta17fa672015-08-19 18:42:22 -0700605 NodeSelection nodeSelection =
Prince Pereira46c82d42016-09-19 13:30:50 +0530606 new NodeSelection(payload, deviceService, hostService, linkService);
Simon Hunt4fc86852015-08-20 17:57:52 -0700607 traffic.monitor(Mode.RELATED_INTENTS, nodeSelection);
Simon Hunta17fa672015-08-19 18:42:22 -0700608 }
609 }
610
611 private final class ReqNextIntent extends RequestHandler {
612 private ReqNextIntent() {
613 super(REQ_NEXT_INTENT);
614 }
615
616 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800617 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700618 traffic.selectNextIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700619 }
620 }
621
622 private final class ReqPrevIntent extends RequestHandler {
623 private ReqPrevIntent() {
624 super(REQ_PREV_INTENT);
625 }
626
627 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800628 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700629 traffic.selectPreviousIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700630 }
631 }
632
633 private final class ReqSelectedIntentTraffic extends RequestHandler {
634 private ReqSelectedIntentTraffic() {
635 super(REQ_SEL_INTENT_TRAFFIC);
636 }
637
638 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800639 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700640 traffic.monitor(Mode.SELECTED_INTENT);
Simon Huntd2747a02015-04-30 22:41:16 -0700641 }
642 }
643
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700644 private final class SelIntent extends RequestHandler {
645 private SelIntent() {
646 super(SEL_INTENT);
647 }
648
649 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800650 public void process(ObjectNode payload) {
Viswanath KSP0f297702016-08-13 18:02:43 +0530651 Intent intent = findIntentByPayload(payload);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700652 if (intent == null) {
Viswanath KSP0f297702016-08-13 18:02:43 +0530653 log.warn("Unable to find intent from payload {}", payload);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700654 } else {
Viswanath KSP0f297702016-08-13 18:02:43 +0530655 log.debug("starting to monitor intent {}", intent.key());
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700656 traffic.monitor(intent);
657 }
658 }
659 }
660
Andrea Campanella732ea832017-02-06 09:25:59 -0800661 private final class SelProtectedIntent extends RequestHandler {
662 private SelProtectedIntent() {
663 super(SEL_PROTECTED_INTENT);
664 }
665
666 @Override
667 public void process(ObjectNode payload) {
668 Intent intent = findIntentByPayload(payload);
669 if (intent == null) {
670 log.warn("Unable to find protected intent from payload {}", payload);
671 } else {
672 log.debug("starting to monitor protected intent {}", intent.key());
673 protectedIntentMonitor.monitor(intent);
674 }
675 }
676 }
677
Simon Huntd2747a02015-04-30 22:41:16 -0700678 private final class CancelTraffic extends RequestHandler {
679 private CancelTraffic() {
680 super(CANCEL_TRAFFIC);
681 }
682
683 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800684 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700685 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700686 }
687 }
688
Andrea Campanella732ea832017-02-06 09:25:59 -0800689 private final class CancelProtectedIntentHighlight extends RequestHandler {
690 private CancelProtectedIntentHighlight() {
691 super(CANCEL_PROTECTED_INTENT_HIGHLIGHT);
692 }
693
694 @Override
695 public void process(ObjectNode payload) {
696 protectedIntentMonitor.stopMonitoring();
697 }
698 }
699
Simon Huntd2747a02015-04-30 22:41:16 -0700700 //=======================================================================
701
Simon Hunta17fa672015-08-19 18:42:22 -0700702 // Converts highlights to JSON format and sends the message to the client
Simon Hunt8a0429a2017-01-06 16:52:47 -0800703 void sendHighlights(Highlights highlights) {
Simon Hunt52560662015-08-27 22:46:44 -0700704 sendMessage(highlightsMessage(highlights));
Thomas Vachuska329af532015-03-10 02:08:33 -0700705 }
706
Simon Huntd2747a02015-04-30 22:41:16 -0700707 // Subscribes for summary messages.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800708 private synchronized void requestSummary() {
709 PropertyPanel pp = summmaryMessage();
Simon Hunt0af1ec32015-07-24 12:17:55 -0700710 overlayCache.currentOverlay().modifySummary(pp);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800711 sendMessage(envelope(SHOW_SUMMARY, json(pp)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700712 }
713
Simon Huntd2747a02015-04-30 22:41:16 -0700714
Thomas Vachuska329af532015-03-10 02:08:33 -0700715 private void cancelAllRequests() {
716 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700717 traffic.stopMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700718 }
719
720 // Sends all controller nodes to the client as node-added messages.
721 private void sendAllInstances(String messageType) {
722 List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
Simon Hunt8a0429a2017-01-06 16:52:47 -0800723 nodes.sort(NODE_COMPARATOR);
Thomas Vachuska329af532015-03-10 02:08:33 -0700724 for (ControllerNode node : nodes) {
725 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node),
Andrea Campanella732ea832017-02-06 09:25:59 -0800726 messageType));
Thomas Vachuska329af532015-03-10 02:08:33 -0700727 }
728 }
729
730 // Sends all devices to the client as device-added messages.
731 private void sendAllDevices() {
732 // Send optical first, others later for layered rendering
733 for (Device device : deviceService.getDevices()) {
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200734 if ((device.type() == Device.Type.ROADM) ||
Simon Hunt5c1a9382016-06-01 19:35:35 -0700735 (device.type() == Device.Type.OTN)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700736 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
737 }
738 }
739 for (Device device : deviceService.getDevices()) {
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200740 if ((device.type() != Device.Type.ROADM) &&
Simon Hunt5c1a9382016-06-01 19:35:35 -0700741 (device.type() != Device.Type.OTN)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700742 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
743 }
744 }
745 }
746
747 // Sends all links to the client as link-added messages.
748 private void sendAllLinks() {
749 // Send optical first, others later for layered rendering
750 for (Link link : linkService.getLinks()) {
751 if (link.type() == Link.Type.OPTICAL) {
Simon Hunt5c1a9382016-06-01 19:35:35 -0700752 sendMessage(composeLinkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700753 }
754 }
755 for (Link link : linkService.getLinks()) {
756 if (link.type() != Link.Type.OPTICAL) {
Simon Hunt5c1a9382016-06-01 19:35:35 -0700757 sendMessage(composeLinkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700758 }
759 }
760 }
761
Simon Hunt5c1a9382016-06-01 19:35:35 -0700762 // Temporary mechanism to support topology overlays adding their own
763 // properties to the link events.
764 private ObjectNode composeLinkMessage(LinkEvent event) {
765 // start with base message
766 ObjectNode msg = linkMessage(event);
767 Map<String, String> additional =
768 overlayCache.currentOverlay().additionalLinkData(event);
769
770 if (additional != null) {
771 // attach additional key-value pairs as extra data structure
772 ObjectNode payload = (ObjectNode) msg.get(PAYLOAD);
773 payload.set(EXTRA, createExtra(additional));
774 }
775 return msg;
776 }
777
778 private ObjectNode createExtra(Map<String, String> additional) {
779 ObjectNode extra = objectNode();
780 for (Map.Entry<String, String> entry : additional.entrySet()) {
781 extra.put(entry.getKey(), entry.getValue());
782 }
783 return extra;
784 }
785
Thomas Vachuska329af532015-03-10 02:08:33 -0700786 // Sends all hosts to the client as host-added messages.
787 private void sendAllHosts() {
788 for (Host host : hostService.getHosts()) {
789 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
790 }
791 }
792
Thomas Vachuska329af532015-03-10 02:08:33 -0700793 private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) {
794 Set<ConnectPoint> points = new HashSet<>();
795 for (HostId hostId : hostIds) {
796 points.add(getHostLocation(hostId));
797 }
798 return points;
799 }
800
801 private HostLocation getHostLocation(HostId hostId) {
802 return hostService.getHost(hostId).location();
803 }
804
805 // Produces a list of host ids from the specified JSON array.
806 private Set<HostId> getHostIds(ArrayNode ids) {
807 Set<HostId> hostIds = new HashSet<>();
808 for (JsonNode id : ids) {
809 hostIds.add(hostId(id.asText()));
810 }
811 return hostIds;
812 }
813
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700814 private void sendTopoStartDone() {
815 sendMessage(JsonUtils.envelope(TOPO_START_DONE, objectNode()));
816 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700817
Simon Huntd2747a02015-04-30 22:41:16 -0700818 private synchronized void startSummaryMonitoring() {
Thomas Vachuska329af532015-03-10 02:08:33 -0700819 stopSummaryMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700820 summaryTask = new SummaryMonitor();
Simon Hunta17fa672015-08-19 18:42:22 -0700821 timer.schedule(summaryTask, SUMMARY_PERIOD, SUMMARY_PERIOD);
Simon Huntd2747a02015-04-30 22:41:16 -0700822 summaryRunning = true;
Thomas Vachuska329af532015-03-10 02:08:33 -0700823 }
824
825 private synchronized void stopSummaryMonitoring() {
Simon Huntd2747a02015-04-30 22:41:16 -0700826 if (summaryTask != null) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700827 summaryTask.cancel();
828 summaryTask = null;
Thomas Vachuska329af532015-03-10 02:08:33 -0700829 }
Simon Huntd2747a02015-04-30 22:41:16 -0700830 summaryRunning = false;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700831 }
832
Thomas Vachuska329af532015-03-10 02:08:33 -0700833
834 // Adds all internal listeners.
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700835 private synchronized void addListeners() {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700836 listenersRemoved = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700837 clusterService.addListener(clusterListener);
838 mastershipService.addListener(mastershipListener);
839 deviceService.addListener(deviceListener);
840 linkService.addListener(linkListener);
841 hostService.addListener(hostListener);
842 intentService.addListener(intentListener);
843 flowService.addListener(flowListener);
844 }
845
846 // Removes all internal listeners.
847 private synchronized void removeListeners() {
848 if (!listenersRemoved) {
849 listenersRemoved = true;
850 clusterService.removeListener(clusterListener);
851 mastershipService.removeListener(mastershipListener);
852 deviceService.removeListener(deviceListener);
853 linkService.removeListener(linkListener);
854 hostService.removeListener(hostListener);
855 intentService.removeListener(intentListener);
856 flowService.removeListener(flowListener);
857 }
858 }
859
860 // Cluster event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700861 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
862 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700863 private class InternalClusterListener implements ClusterEventListener {
864 @Override
865 public void event(ClusterEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700866 msgSender.execute(() -> sendMessage(instanceMessage(event, null)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700867 }
868 }
869
870 // Mastership change listener
Simon Hunt7092cc42016-04-06 18:40:17 -0700871 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
872 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700873 private class InternalMastershipListener implements MastershipListener {
874 @Override
875 public void event(MastershipEvent event) {
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700876 msgSender.execute(() -> {
Simon Huntb745ca62015-07-28 15:37:11 -0700877 sendAllInstances(UPDATE_INSTANCE);
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700878 Device device = deviceService.getDevice(event.subject());
879 if (device != null) {
880 sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device)));
881 }
882 });
Thomas Vachuska329af532015-03-10 02:08:33 -0700883 }
884 }
885
886 // Device event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700887 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
888 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700889 private class InternalDeviceListener implements DeviceListener {
890 @Override
891 public void event(DeviceEvent event) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700892 if (event.type() != PORT_STATS_UPDATED) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700893 msgSender.execute(() -> sendMessage(deviceMessage(event)));
Simon Hunta1f1c022016-03-03 15:54:57 -0800894 msgSender.execute(traffic::pokeIntent);
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700895 eventAccummulator.add(event);
896 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700897 }
898 }
899
900 // Link event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700901 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
902 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700903 private class InternalLinkListener implements LinkListener {
904 @Override
905 public void event(LinkEvent event) {
Simon Hunt5c1a9382016-06-01 19:35:35 -0700906 msgSender.execute(() -> sendMessage(composeLinkMessage(event)));
Simon Hunta1f1c022016-03-03 15:54:57 -0800907 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700908 eventAccummulator.add(event);
909 }
910 }
911
912 // Host event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700913 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
914 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700915 private class InternalHostListener implements HostListener {
916 @Override
917 public void event(HostEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700918 msgSender.execute(() -> sendMessage(hostMessage(event)));
Simon Hunta1f1c022016-03-03 15:54:57 -0800919 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700920 eventAccummulator.add(event);
921 }
922 }
923
924 // Intent event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700925 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
926 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700927 private class InternalIntentListener implements IntentListener {
928 @Override
929 public void event(IntentEvent event) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700930 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700931 eventAccummulator.add(event);
932 }
933 }
934
935 // Intent event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700936 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
937 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700938 private class InternalFlowListener implements FlowRuleListener {
939 @Override
940 public void event(FlowRuleEvent event) {
941 eventAccummulator.add(event);
942 }
943 }
944
Simon Huntd2747a02015-04-30 22:41:16 -0700945
Simon Hunta17fa672015-08-19 18:42:22 -0700946 // === SUMMARY MONITORING
Thomas Vachuska329af532015-03-10 02:08:33 -0700947
948 // Periodic update of the summary information
949 private class SummaryMonitor extends TimerTask {
950 @Override
951 public void run() {
952 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700953 if (summaryRunning) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800954 msgSender.execute(() -> requestSummary());
Thomas Vachuska329af532015-03-10 02:08:33 -0700955 }
956 } catch (Exception e) {
957 log.warn("Unable to handle summary request due to {}", e.getMessage());
958 log.warn("Boom!", e);
959 }
960 }
961 }
962
963 // Accumulates events to drive methodic update of the summary pane.
964 private class InternalEventAccummulator extends AbstractAccumulator<Event> {
965 protected InternalEventAccummulator() {
966 super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS);
967 }
968
969 @Override
970 public void processItems(List<Event> items) {
Simon Hunta17fa672015-08-19 18:42:22 -0700971 // Start-of-Debugging -- Keep in until ONOS-2572 is fixed for reals
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700972 long now = System.currentTimeMillis();
973 String me = this.toString();
974 String miniMe = me.replaceAll("^.*@", "me@");
975 log.debug("Time: {}; this: {}, processing items ({} events)",
Andrea Campanella732ea832017-02-06 09:25:59 -0800976 now, miniMe, items.size());
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700977 // End-of-Debugging
978
Thomas Vachuska329af532015-03-10 02:08:33 -0700979 try {
Simon Huntd2747a02015-04-30 22:41:16 -0700980 if (summaryRunning) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800981 msgSender.execute(() -> requestSummary());
Thomas Vachuska329af532015-03-10 02:08:33 -0700982 }
983 } catch (Exception e) {
984 log.warn("Unable to handle summary request due to {}", e.getMessage());
985 log.debug("Boom!", e);
986 }
987 }
988 }
989}