blob: 455b0f334d1323a2a0a16e3e2a50e06ec09ba2d1 [file] [log] [blame]
Thomas Vachuska329af532015-03-10 02:08:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Thomas Vachuska329af532015-03-10 02:08:33 -070032import org.onosproject.mastership.MastershipEvent;
33import org.onosproject.mastership.MastershipListener;
34import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.Device;
Simon Huntde99e0b2015-10-23 18:54:06 -070036import org.onosproject.net.DeviceId;
Ray Milkey27cff8c2018-03-05 14:58:26 -080037import org.onosproject.net.FilteredConnectPoint;
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;
Deepa Vaddireddy63340922017-01-19 08:15:31 +053056import org.onosproject.net.intent.IntentService;
Andrea Campanella732ea832017-02-06 09:25:59 -080057import org.onosproject.net.intent.IntentState;
Simon Hunt21281fd2017-03-30 22:28:28 -070058import org.onosproject.net.intent.Key;
59import org.onosproject.net.intent.MultiPointToSinglePointIntent;
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 Hunt1911fe42017-05-02 18:25:58 -070065import org.onosproject.ui.impl.TrafficMonitorBase.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;
Simon Hunta58d8942017-08-11 12:51:14 -070084import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
Thomas Vachuska329af532015-03-10 02:08:33 -070085import static org.onosproject.net.DeviceId.deviceId;
86import static org.onosproject.net.HostId.hostId;
Simon Hunt879ce452017-08-10 23:32:00 -070087import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
88import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED;
89import static org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED;
Thomas Vachuska329af532015-03-10 02:08:33 -070090import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
91import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
Simon Huntd3ceffa2015-08-25 12:44:35 -070092import static org.onosproject.ui.JsonUtils.envelope;
Viswanath KSP0f297702016-08-13 18:02:43 +053093import static org.onosproject.ui.JsonUtils.string;
Simon Hunt52560662015-08-27 22:46:44 -070094import static org.onosproject.ui.topo.TopoJson.highlightsMessage;
95import static org.onosproject.ui.topo.TopoJson.json;
Thomas Vachuska329af532015-03-10 02:08:33 -070096
97/**
98 * Web socket capable of interacting with the GUI topology view.
99 */
100public class TopologyViewMessageHandler extends TopologyViewMessageHandlerBase {
101
Simon Huntb745ca62015-07-28 15:37:11 -0700102 // incoming event types
Simon Huntd2747a02015-04-30 22:41:16 -0700103 private static final String REQ_DETAILS = "requestDetails";
104 private static final String UPDATE_META = "updateMeta";
105 private static final String ADD_HOST_INTENT = "addHostIntent";
Viswanath KSP0f297702016-08-13 18:02:43 +0530106 private static final String REMOVE_INTENT = "removeIntent";
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530107 private static final String REMOVE_INTENTS = "removeIntents";
Viswanath KSP14aee092016-10-02 01:47:40 +0530108 private static final String RESUBMIT_INTENT = "resubmitIntent";
Simon Huntd2747a02015-04-30 22:41:16 -0700109 private static final String ADD_MULTI_SRC_INTENT = "addMultiSourceIntent";
110 private static final String REQ_RELATED_INTENTS = "requestRelatedIntents";
111 private static final String REQ_NEXT_INTENT = "requestNextRelatedIntent";
112 private static final String REQ_PREV_INTENT = "requestPrevRelatedIntent";
113 private static final String REQ_SEL_INTENT_TRAFFIC = "requestSelectedIntentTraffic";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700114 private static final String SEL_INTENT = "selectIntent";
Simon Hunt21281fd2017-03-30 22:28:28 -0700115 private static final String REQ_ALL_TRAFFIC = "requestAllTraffic";
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";
Andrea Campanella732ea832017-02-06 09:25:59 -0800126 private static final String SEL_PROTECTED_INTENT = "selectProtectedIntent";
127 private static final String CANCEL_PROTECTED_INTENT_HIGHLIGHT = "cancelProtectedIntentHighlight";
128
Simon Huntb745ca62015-07-28 15:37:11 -0700129 // outgoing event types
130 private static final String SHOW_SUMMARY = "showSummary";
131 private static final String SHOW_DETAILS = "showDetails";
132 private static final String SPRITE_LIST_RESPONSE = "spriteListResponse";
133 private static final String SPRITE_DATA_RESPONSE = "spriteDataResponse";
134 private static final String UPDATE_INSTANCE = "updateInstance";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700135 private static final String TOPO_START_DONE = "topoStartDone";
Simon Huntb745ca62015-07-28 15:37:11 -0700136
137 // fields
Simon Hunt5c1a9382016-06-01 19:35:35 -0700138 private static final String PAYLOAD = "payload";
139 private static final String EXTRA = "extra";
Simon Huntb745ca62015-07-28 15:37:11 -0700140 private static final String ID = "id";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700141 private static final String KEY = "key";
Simon Hunta58d8942017-08-11 12:51:14 -0700142 private static final String IS_EDGE_LINK = "isEdgeLink";
143 private static final String SOURCE_ID = "sourceId";
144 private static final String SOURCE_PORT = "sourcePort";
145 private static final String TARGET_ID = "targetId";
146 private static final String TARGET_PORT = "targetPort";
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700147 private static final String APP_ID = "appId";
148 private static final String APP_NAME = "appName";
Simon Huntb745ca62015-07-28 15:37:11 -0700149 private static final String DEVICE = "device";
150 private static final String HOST = "host";
Simon Hunta58d8942017-08-11 12:51:14 -0700151 private static final String LINK = "link";
Simon Huntb745ca62015-07-28 15:37:11 -0700152 private static final String CLASS = "class";
153 private static final String UNKNOWN = "unknown";
154 private static final String ONE = "one";
155 private static final String TWO = "two";
156 private static final String SRC = "src";
157 private static final String DST = "dst";
158 private static final String DATA = "data";
159 private static final String NAME = "name";
160 private static final String NAMES = "names";
161 private static final String ACTIVATE = "activate";
162 private static final String DEACTIVATE = "deactivate";
Viswanath KSP813a20d2016-09-13 04:25:41 +0530163 private static final String PURGE = "purge";
Simon Hunt21281fd2017-03-30 22:28:28 -0700164 private static final String TRAFFIC_TYPE = "trafficType";
Simon Huntb745ca62015-07-28 15:37:11 -0700165
Simon Hunt21281fd2017-03-30 22:28:28 -0700166 // field values
167 private static final String FLOW_STATS_BYTES = "flowStatsBytes";
168 private static final String PORT_STATS_BIT_SEC = "portStatsBitSec";
169 private static final String PORT_STATS_PKT_SEC = "portStatsPktSec";
Simon Huntd2747a02015-04-30 22:41:16 -0700170
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700171 private static final String MY_APP_ID = "org.onosproject.gui";
Thomas Vachuska329af532015-03-10 02:08:33 -0700172
Simon Hunta58d8942017-08-11 12:51:14 -0700173 private static final String SLASH = "/";
174
Simon Hunta17fa672015-08-19 18:42:22 -0700175 private static final long TRAFFIC_PERIOD = 5000;
176 private static final long SUMMARY_PERIOD = 30000;
Thomas Vachuska329af532015-03-10 02:08:33 -0700177
178 private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
Simon Hunt8a0429a2017-01-06 16:52:47 -0800179 Comparator.comparing(o -> o.id().toString());
Thomas Vachuska329af532015-03-10 02:08:33 -0700180
181
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700182 private final Timer timer = new Timer("onos-topology-view");
Thomas Vachuska329af532015-03-10 02:08:33 -0700183
184 private static final int MAX_EVENTS = 1000;
185 private static final int MAX_BATCH_MS = 5000;
186 private static final int MAX_IDLE_MS = 1000;
187
188 private ApplicationId appId;
189
190 private final ClusterEventListener clusterListener = new InternalClusterListener();
191 private final MastershipListener mastershipListener = new InternalMastershipListener();
192 private final DeviceListener deviceListener = new InternalDeviceListener();
193 private final LinkListener linkListener = new InternalLinkListener();
194 private final HostListener hostListener = new InternalHostListener();
195 private final IntentListener intentListener = new InternalIntentListener();
196 private final FlowRuleListener flowListener = new InternalFlowListener();
197
198 private final Accumulator<Event> eventAccummulator = new InternalEventAccummulator();
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700199 private final ExecutorService msgSender =
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700200 newSingleThreadExecutor(groupedThreads("onos/gui", "msg-sender", log));
Thomas Vachuska329af532015-03-10 02:08:33 -0700201
Simon Hunta17fa672015-08-19 18:42:22 -0700202 private TopoOverlayCache overlayCache;
Simon Hunt4fc86852015-08-20 17:57:52 -0700203 private TrafficMonitor traffic;
Andrea Campanella732ea832017-02-06 09:25:59 -0800204 private ProtectedIntentMonitor protectedIntentMonitor;
Thomas Vachuska329af532015-03-10 02:08:33 -0700205
Simon Huntd2747a02015-04-30 22:41:16 -0700206 private TimerTask summaryTask = null;
207 private boolean summaryRunning = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700208
Simon Huntd5b96732016-07-08 13:22:27 -0700209 private volatile boolean listenersRemoved = false;
Thomas Vachuska329af532015-03-10 02:08:33 -0700210
Thomas Vachuska329af532015-03-10 02:08:33 -0700211
212 @Override
213 public void init(UiConnection connection, ServiceDirectory directory) {
214 super.init(connection, directory);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700215 appId = directory.get(CoreService.class).registerApplication(MY_APP_ID);
Simon Hunt1911fe42017-05-02 18:25:58 -0700216 traffic = new TrafficMonitor(TRAFFIC_PERIOD, services, this);
217 protectedIntentMonitor = new ProtectedIntentMonitor(TRAFFIC_PERIOD, services, this);
Thomas Vachuska329af532015-03-10 02:08:33 -0700218 }
219
220 @Override
221 public void destroy() {
222 cancelAllRequests();
Thomas Vachuska2bb48632015-04-28 14:40:42 -0700223 removeListeners();
Thomas Vachuska329af532015-03-10 02:08:33 -0700224 super.destroy();
225 }
226
Thomas Vachuska329af532015-03-10 02:08:33 -0700227 @Override
Simon Huntda580882015-05-12 20:58:18 -0700228 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700229 return ImmutableSet.of(
230 new TopoStart(),
Simon Hunte05cae42015-07-23 17:35:24 -0700231 new TopoSelectOverlay(),
Simon Huntd2747a02015-04-30 22:41:16 -0700232 new TopoStop(),
233 new ReqSummary(),
234 new CancelSummary(),
235 new SpriteListReq(),
236 new SpriteDataReq(),
237 new RequestDetails(),
238 new UpdateMeta(),
239 new EqMasters(),
Thomas Vachuska329af532015-03-10 02:08:33 -0700240
Simon Huntd2747a02015-04-30 22:41:16 -0700241 // TODO: migrate traffic related to separate app
242 new AddHostIntent(),
243 new AddMultiSourceIntent(),
Viswanath KSP0f297702016-08-13 18:02:43 +0530244 new RemoveIntent(),
Viswanath KSP14aee092016-10-02 01:47:40 +0530245 new ResubmitIntent(),
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530246 new RemoveIntents(),
Simon Hunta17fa672015-08-19 18:42:22 -0700247
Simon Hunt21281fd2017-03-30 22:28:28 -0700248 new ReqAllTraffic(),
Simon Hunta17fa672015-08-19 18:42:22 -0700249 new ReqDevLinkFlows(),
Simon Huntd2747a02015-04-30 22:41:16 -0700250 new ReqRelatedIntents(),
251 new ReqNextIntent(),
252 new ReqPrevIntent(),
253 new ReqSelectedIntentTraffic(),
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700254 new SelIntent(),
Andrea Campanella732ea832017-02-06 09:25:59 -0800255 new SelProtectedIntent(),
Simon Hunta17fa672015-08-19 18:42:22 -0700256
Andrea Campanella732ea832017-02-06 09:25:59 -0800257 new CancelTraffic(),
258 new CancelProtectedIntentHighlight()
Simon Huntd2747a02015-04-30 22:41:16 -0700259 );
260 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700261
Simon Hunte05cae42015-07-23 17:35:24 -0700262 /**
263 * Injects the topology overlay cache.
264 *
265 * @param overlayCache injected cache
266 */
267 void setOverlayCache(TopoOverlayCache overlayCache) {
268 this.overlayCache = overlayCache;
269 }
270
Simon Huntd2747a02015-04-30 22:41:16 -0700271 // ==================================================================
Thomas Vachuska329af532015-03-10 02:08:33 -0700272
Simon Huntd2747a02015-04-30 22:41:16 -0700273 private final class TopoStart extends RequestHandler {
274 private TopoStart() {
275 super(TOPO_START);
276 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700277
Simon Huntd2747a02015-04-30 22:41:16 -0700278 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800279 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700280 addListeners();
281 sendAllInstances(null);
282 sendAllDevices();
283 sendAllLinks();
284 sendAllHosts();
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700285 sendTopoStartDone();
Thomas Vachuska329af532015-03-10 02:08:33 -0700286 }
287 }
288
Simon Hunte05cae42015-07-23 17:35:24 -0700289 private final class TopoSelectOverlay extends RequestHandler {
290 private TopoSelectOverlay() {
291 super(TOPO_SELECT_OVERLAY);
292 }
293
294 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800295 public void process(ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700296 String deact = string(payload, DEACTIVATE);
297 String act = string(payload, ACTIVATE);
Simon Hunte05cae42015-07-23 17:35:24 -0700298 overlayCache.switchOverlay(deact, act);
299 }
300 }
301
Simon Huntd2747a02015-04-30 22:41:16 -0700302 private final class TopoStop extends RequestHandler {
303 private TopoStop() {
304 super(TOPO_STOP);
305 }
306
307 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800308 public void process(ObjectNode payload) {
Simon Hunt1ad59272015-11-10 15:23:21 -0800309 removeListeners();
Simon Huntd2747a02015-04-30 22:41:16 -0700310 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700311 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700312 }
313 }
314
315 private final class ReqSummary extends RequestHandler {
316 private ReqSummary() {
317 super(REQ_SUMMARY);
318 }
319
320 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800321 public void process(ObjectNode payload) {
322 requestSummary();
Simon Huntd2747a02015-04-30 22:41:16 -0700323 startSummaryMonitoring();
324 }
325 }
326
327 private final class CancelSummary extends RequestHandler {
328 private CancelSummary() {
329 super(CANCEL_SUMMARY);
330 }
331
332 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800333 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700334 stopSummaryMonitoring();
335 }
336 }
337
338 private final class SpriteListReq extends RequestHandler {
339 private SpriteListReq() {
340 super(SPRITE_LIST_REQ);
341 }
342
343 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800344 public void process(ObjectNode payload) {
Simon Huntda580882015-05-12 20:58:18 -0700345 ObjectNode root = objectNode();
346 ArrayNode names = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700347 get(SpriteService.class).getNames().forEach(names::add);
Simon Huntb745ca62015-07-28 15:37:11 -0700348 root.set(NAMES, names);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800349 sendMessage(SPRITE_LIST_RESPONSE, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700350 }
351 }
352
353 private final class SpriteDataReq extends RequestHandler {
354 private SpriteDataReq() {
355 super(SPRITE_DATA_REQ);
356 }
357
358 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800359 public void process(ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700360 String name = string(payload, NAME);
Simon Huntda580882015-05-12 20:58:18 -0700361 ObjectNode root = objectNode();
Simon Huntb745ca62015-07-28 15:37:11 -0700362 root.set(DATA, get(SpriteService.class).get(name));
Simon Hunt8a0429a2017-01-06 16:52:47 -0800363 sendMessage(SPRITE_DATA_RESPONSE, root);
Simon Huntd2747a02015-04-30 22:41:16 -0700364 }
365 }
366
367 private final class RequestDetails extends RequestHandler {
368 private RequestDetails() {
369 super(REQ_DETAILS);
370 }
371
372 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800373 public void process(ObjectNode payload) {
Simon Huntb745ca62015-07-28 15:37:11 -0700374 String type = string(payload, CLASS, UNKNOWN);
Simon Hunta58d8942017-08-11 12:51:14 -0700375 String id = string(payload, ID, "");
Simon Huntb745ca62015-07-28 15:37:11 -0700376 PropertyPanel pp = null;
Simon Huntd2747a02015-04-30 22:41:16 -0700377
Simon Huntb745ca62015-07-28 15:37:11 -0700378 if (type.equals(DEVICE)) {
Simon Huntde99e0b2015-10-23 18:54:06 -0700379 DeviceId did = deviceId(id);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800380 pp = deviceDetails(did);
Simon Huntde99e0b2015-10-23 18:54:06 -0700381 overlayCache.currentOverlay().modifyDeviceDetails(pp, did);
Simon Hunta58d8942017-08-11 12:51:14 -0700382
Simon Huntb745ca62015-07-28 15:37:11 -0700383 } else if (type.equals(HOST)) {
Simon Huntde99e0b2015-10-23 18:54:06 -0700384 HostId hid = hostId(id);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800385 pp = hostDetails(hid);
Simon Huntde99e0b2015-10-23 18:54:06 -0700386 overlayCache.currentOverlay().modifyHostDetails(pp, hid);
Simon Hunta58d8942017-08-11 12:51:14 -0700387
388 } else if (type.equals(LINK)) {
389 String srcId = string(payload, SOURCE_ID);
390 String tgtId = string(payload, TARGET_ID);
391 boolean isEdgeLink = bool(payload, IS_EDGE_LINK);
392
393 if (isEdgeLink) {
394 HostId hid = hostId(srcId);
395 String cpstr = tgtId + SLASH + string(payload, TARGET_PORT);
396 ConnectPoint cp = deviceConnectPoint(cpstr);
397
398 pp = edgeLinkDetails(hid, cp);
399 overlayCache.currentOverlay().modifyEdgeLinkDetails(pp, hid, cp);
400
401 } else {
402 String cpAstr = srcId + SLASH + string(payload, SOURCE_PORT);
403 String cpBstr = tgtId + SLASH + string(payload, TARGET_PORT);
404 ConnectPoint cpA = deviceConnectPoint(cpAstr);
405 ConnectPoint cpB = deviceConnectPoint(cpBstr);
406
407 pp = infraLinkDetails(cpA, cpB);
408 overlayCache.currentOverlay().modifyInfraLinkDetails(pp, cpA, cpB);
409 }
Simon Huntd2747a02015-04-30 22:41:16 -0700410 }
Simon Huntb745ca62015-07-28 15:37:11 -0700411
Simon Hunta58d8942017-08-11 12:51:14 -0700412 if (pp != null) {
413 sendMessage(envelope(SHOW_DETAILS, json(pp)));
414 } else {
415 log.warn("Unable to process details request: {}", payload);
416 }
Simon Huntd2747a02015-04-30 22:41:16 -0700417 }
418 }
419
420 private final class UpdateMeta extends RequestHandler {
421 private UpdateMeta() {
422 super(UPDATE_META);
423 }
424
425 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800426 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700427 updateMetaUi(payload);
428 }
429 }
430
431 private final class EqMasters extends RequestHandler {
432 private EqMasters() {
433 super(EQ_MASTERS);
434 }
435
436 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800437 public void process(ObjectNode payload) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700438 services.mastershipAdmin().balanceRoles();
Simon Huntd2747a02015-04-30 22:41:16 -0700439 }
440 }
441
Simon Hunta17fa672015-08-19 18:42:22 -0700442
443 // ========= -----------------------------------------------------------------
444
Simon Huntd2747a02015-04-30 22:41:16 -0700445 // === TODO: move traffic related classes to traffic app
446
447 private final class AddHostIntent extends RequestHandler {
448 private AddHostIntent() {
449 super(ADD_HOST_INTENT);
450 }
451
452 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800453 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700454 // TODO: add protection against device ids and non-existent hosts.
Simon Huntb745ca62015-07-28 15:37:11 -0700455 HostId one = hostId(string(payload, ONE));
456 HostId two = hostId(string(payload, TWO));
Simon Huntd2747a02015-04-30 22:41:16 -0700457
458 HostToHostIntent intent = HostToHostIntent.builder()
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700459 .appId(appId)
460 .one(one)
461 .two(two)
462 .build();
Simon Huntd2747a02015-04-30 22:41:16 -0700463
Simon Hunt1911fe42017-05-02 18:25:58 -0700464 services.intent().submit(intent);
Simon Huntd2862c32015-08-24 17:41:51 -0700465 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
466 traffic.monitor(intent);
467 }
Simon Huntd2747a02015-04-30 22:41:16 -0700468 }
469 }
470
Viswanath KSP0f297702016-08-13 18:02:43 +0530471 private Intent findIntentByPayload(ObjectNode payload) {
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800472 Intent intent;
473 Key key;
Viswanath KSP0f297702016-08-13 18:02:43 +0530474 int appId = Integer.parseInt(string(payload, APP_ID));
475 String appName = string(payload, APP_NAME);
476 ApplicationId applicId = new DefaultApplicationId(appId, appName);
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800477 String stringKey = string(payload, KEY);
478 try {
479 // FIXME: If apps use different string key, but they contains
480 // same numeric value (e.g. "020", "0x10", "16", "#10")
481 // and one intent using long key (e.g. 16L)
482 // this function might return wrong intent.
Viswanath KSP0f297702016-08-13 18:02:43 +0530483
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800484 long longKey = Long.decode(stringKey);
485 key = Key.of(longKey, applicId);
Simon Hunt1911fe42017-05-02 18:25:58 -0700486 intent = services.intent().getIntent(key);
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800487
488 if (intent == null) {
489 // Intent might using string key, not long key
490 key = Key.of(stringKey, applicId);
Simon Hunt1911fe42017-05-02 18:25:58 -0700491 intent = services.intent().getIntent(key);
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800492 }
493 } catch (NumberFormatException ex) {
494 // string key
495 key = Key.of(stringKey, applicId);
Simon Hunt1911fe42017-05-02 18:25:58 -0700496 intent = services.intent().getIntent(key);
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800497 }
498
Viswanath KSP0f297702016-08-13 18:02:43 +0530499 log.debug("Attempting to select intent by key={}", key);
500
Yi Tseng3a9b01c2017-02-16 14:24:28 -0800501 return intent;
Viswanath KSP0f297702016-08-13 18:02:43 +0530502 }
503
504 private final class RemoveIntent extends RequestHandler {
505 private RemoveIntent() {
506 super(REMOVE_INTENT);
507 }
508
Viswanath KSP813a20d2016-09-13 04:25:41 +0530509 private boolean isIntentToBePurged(ObjectNode payload) {
510 return bool(payload, PURGE);
511 }
512
Viswanath KSP0f297702016-08-13 18:02:43 +0530513 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800514 public void process(ObjectNode payload) {
Viswanath KSP0f297702016-08-13 18:02:43 +0530515 Intent intent = findIntentByPayload(payload);
516 if (intent == null) {
517 log.warn("Unable to find intent from payload {}", payload);
518 } else {
Viswanath KSP813a20d2016-09-13 04:25:41 +0530519 log.debug("Withdrawing / Purging intent {}", intent.key());
520 if (isIntentToBePurged(payload)) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700521 services.intent().purge(intent);
Viswanath KSP813a20d2016-09-13 04:25:41 +0530522 } else {
Simon Hunt1911fe42017-05-02 18:25:58 -0700523 services.intent().withdraw(intent);
Viswanath KSP813a20d2016-09-13 04:25:41 +0530524 }
Viswanath KSP0f297702016-08-13 18:02:43 +0530525 }
526 }
527 }
528
Viswanath KSP14aee092016-10-02 01:47:40 +0530529 private final class ResubmitIntent extends RequestHandler {
530 private ResubmitIntent() {
531 super(RESUBMIT_INTENT);
532 }
533
534 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800535 public void process(ObjectNode payload) {
Viswanath KSP14aee092016-10-02 01:47:40 +0530536 Intent intent = findIntentByPayload(payload);
537 if (intent == null) {
538 log.warn("Unable to find intent from payload {}", payload);
539 } else {
540 log.debug("Resubmitting intent {}", intent.key());
Simon Hunt1911fe42017-05-02 18:25:58 -0700541 services.intent().submit(intent);
Viswanath KSP14aee092016-10-02 01:47:40 +0530542 }
543 }
544 }
545
Simon Huntd2747a02015-04-30 22:41:16 -0700546 private final class AddMultiSourceIntent extends RequestHandler {
547 private AddMultiSourceIntent() {
548 super(ADD_MULTI_SRC_INTENT);
549 }
550
551 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800552 public void process(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -0700553 // TODO: add protection against device ids and non-existent hosts.
Simon Huntb745ca62015-07-28 15:37:11 -0700554 Set<HostId> src = getHostIds((ArrayNode) payload.path(SRC));
555 HostId dst = hostId(string(payload, DST));
Simon Hunt1911fe42017-05-02 18:25:58 -0700556 Host dstHost = services.host().getHost(dst);
Simon Huntd2747a02015-04-30 22:41:16 -0700557
Ray Milkey27cff8c2018-03-05 14:58:26 -0800558 Set<FilteredConnectPoint> ingressPoints = getHostLocations(src);
Simon Huntd2747a02015-04-30 22:41:16 -0700559
560 // FIXME: clearly, this is not enough
561 TrafficSelector selector = DefaultTrafficSelector.builder()
562 .matchEthDst(dstHost.mac()).build();
563 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
564
565 MultiPointToSinglePointIntent intent =
566 MultiPointToSinglePointIntent.builder()
567 .appId(appId)
568 .selector(selector)
569 .treatment(treatment)
Ray Milkey27cff8c2018-03-05 14:58:26 -0800570 .filteredIngressPoints(ingressPoints)
571 .filteredEgressPoint(new FilteredConnectPoint(dstHost.location()))
Simon Huntd2747a02015-04-30 22:41:16 -0700572 .build();
573
Simon Hunt1911fe42017-05-02 18:25:58 -0700574 services.intent().submit(intent);
Simon Huntd2862c32015-08-24 17:41:51 -0700575 if (overlayCache.isActive(TrafficOverlay.TRAFFIC_ID)) {
576 traffic.monitor(intent);
577 }
Simon Huntd2747a02015-04-30 22:41:16 -0700578 }
579 }
580
Deepa Vaddireddy63340922017-01-19 08:15:31 +0530581 private final class RemoveIntents extends RequestHandler {
582 private RemoveIntents() {
583 super(REMOVE_INTENTS);
584 }
585
586
587 @Override
588 public void process(ObjectNode payload) {
589 IntentService intentService = get(IntentService.class);
590 for (Intent intent : intentService.getIntents()) {
591 if (intentService.getIntentState(intent.key()) == IntentState.WITHDRAWN) {
592 intentService.purge(intent);
593 }
594 }
595
596 }
597 }
598
Simon Hunta17fa672015-08-19 18:42:22 -0700599 // ========= -----------------------------------------------------------------
Simon Huntd2747a02015-04-30 22:41:16 -0700600
Simon Hunt21281fd2017-03-30 22:28:28 -0700601 private final class ReqAllTraffic extends RequestHandler {
602 private ReqAllTraffic() {
603 super(REQ_ALL_TRAFFIC);
Simon Huntd2747a02015-04-30 22:41:16 -0700604 }
605
606 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800607 public void process(ObjectNode payload) {
Simon Hunt21281fd2017-03-30 22:28:28 -0700608 String trafficType = string(payload, TRAFFIC_TYPE, FLOW_STATS_BYTES);
Thomas Vachuskaf0397b52015-05-29 13:50:17 -0700609
Simon Hunt21281fd2017-03-30 22:28:28 -0700610 switch (trafficType) {
611 case FLOW_STATS_BYTES:
612 traffic.monitor(Mode.ALL_FLOW_TRAFFIC_BYTES);
613 break;
614 case PORT_STATS_BIT_SEC:
615 traffic.monitor(Mode.ALL_PORT_TRAFFIC_BIT_PS);
616 break;
617 case PORT_STATS_PKT_SEC:
618 traffic.monitor(Mode.ALL_PORT_TRAFFIC_PKT_PS);
619 break;
620 default:
621 break;
622 }
Simon Huntd2747a02015-04-30 22:41:16 -0700623 }
624 }
625
Simon Hunt1911fe42017-05-02 18:25:58 -0700626 private NodeSelection makeNodeSelection(ObjectNode payload) {
627 return new NodeSelection(payload, services.device(), services.host(),
628 services.link());
629 }
630
631
Simon Huntd2747a02015-04-30 22:41:16 -0700632 private final class ReqDevLinkFlows extends RequestHandler {
633 private ReqDevLinkFlows() {
634 super(REQ_DEV_LINK_FLOWS);
635 }
636
637 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800638 public void process(ObjectNode payload) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700639 traffic.monitor(Mode.DEV_LINK_FLOWS, makeNodeSelection(payload));
Simon Hunta17fa672015-08-19 18:42:22 -0700640 }
641 }
642
643 private final class ReqRelatedIntents extends RequestHandler {
644 private ReqRelatedIntents() {
645 super(REQ_RELATED_INTENTS);
646 }
647
648 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800649 public void process(ObjectNode payload) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700650 traffic.monitor(Mode.RELATED_INTENTS, makeNodeSelection(payload));
Simon Hunta17fa672015-08-19 18:42:22 -0700651 }
652 }
653
654 private final class ReqNextIntent extends RequestHandler {
655 private ReqNextIntent() {
656 super(REQ_NEXT_INTENT);
657 }
658
659 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800660 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700661 traffic.selectNextIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700662 }
663 }
664
665 private final class ReqPrevIntent extends RequestHandler {
666 private ReqPrevIntent() {
667 super(REQ_PREV_INTENT);
668 }
669
670 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800671 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700672 traffic.selectPreviousIntent();
Simon Hunta17fa672015-08-19 18:42:22 -0700673 }
674 }
675
676 private final class ReqSelectedIntentTraffic extends RequestHandler {
677 private ReqSelectedIntentTraffic() {
678 super(REQ_SEL_INTENT_TRAFFIC);
679 }
680
681 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800682 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700683 traffic.monitor(Mode.SELECTED_INTENT);
Simon Huntd2747a02015-04-30 22:41:16 -0700684 }
685 }
686
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700687 private final class SelIntent extends RequestHandler {
688 private SelIntent() {
689 super(SEL_INTENT);
690 }
691
692 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800693 public void process(ObjectNode payload) {
Viswanath KSP0f297702016-08-13 18:02:43 +0530694 Intent intent = findIntentByPayload(payload);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700695 if (intent == null) {
Viswanath KSP0f297702016-08-13 18:02:43 +0530696 log.warn("Unable to find intent from payload {}", payload);
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700697 } else {
Viswanath KSP0f297702016-08-13 18:02:43 +0530698 log.debug("starting to monitor intent {}", intent.key());
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700699 traffic.monitor(intent);
700 }
701 }
702 }
703
Andrea Campanella732ea832017-02-06 09:25:59 -0800704 private final class SelProtectedIntent extends RequestHandler {
705 private SelProtectedIntent() {
706 super(SEL_PROTECTED_INTENT);
707 }
708
709 @Override
710 public void process(ObjectNode payload) {
711 Intent intent = findIntentByPayload(payload);
712 if (intent == null) {
713 log.warn("Unable to find protected intent from payload {}", payload);
714 } else {
715 log.debug("starting to monitor protected intent {}", intent.key());
716 protectedIntentMonitor.monitor(intent);
717 }
718 }
719 }
720
Simon Huntd2747a02015-04-30 22:41:16 -0700721 private final class CancelTraffic extends RequestHandler {
722 private CancelTraffic() {
723 super(CANCEL_TRAFFIC);
724 }
725
726 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800727 public void process(ObjectNode payload) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700728 traffic.stopMonitoring();
Simon Huntd2747a02015-04-30 22:41:16 -0700729 }
730 }
731
Andrea Campanella732ea832017-02-06 09:25:59 -0800732 private final class CancelProtectedIntentHighlight extends RequestHandler {
733 private CancelProtectedIntentHighlight() {
734 super(CANCEL_PROTECTED_INTENT_HIGHLIGHT);
735 }
736
737 @Override
738 public void process(ObjectNode payload) {
739 protectedIntentMonitor.stopMonitoring();
740 }
741 }
742
Simon Huntd2747a02015-04-30 22:41:16 -0700743 //=======================================================================
744
Simon Hunta17fa672015-08-19 18:42:22 -0700745 // Converts highlights to JSON format and sends the message to the client
Sean Condonadeb7162019-04-13 20:56:14 +0100746 @Override
747 public void sendHighlights(Highlights highlights) {
Simon Hunt52560662015-08-27 22:46:44 -0700748 sendMessage(highlightsMessage(highlights));
Thomas Vachuska329af532015-03-10 02:08:33 -0700749 }
750
Simon Huntd2747a02015-04-30 22:41:16 -0700751 // Subscribes for summary messages.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800752 private synchronized void requestSummary() {
753 PropertyPanel pp = summmaryMessage();
Simon Hunt0af1ec32015-07-24 12:17:55 -0700754 overlayCache.currentOverlay().modifySummary(pp);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800755 sendMessage(envelope(SHOW_SUMMARY, json(pp)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700756 }
757
Simon Huntd2747a02015-04-30 22:41:16 -0700758
Thomas Vachuska329af532015-03-10 02:08:33 -0700759 private void cancelAllRequests() {
760 stopSummaryMonitoring();
Simon Hunt4fc86852015-08-20 17:57:52 -0700761 traffic.stopMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700762 }
763
764 // Sends all controller nodes to the client as node-added messages.
765 private void sendAllInstances(String messageType) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700766 List<ControllerNode> nodes = new ArrayList<>(services.cluster().getNodes());
Simon Hunt8a0429a2017-01-06 16:52:47 -0800767 nodes.sort(NODE_COMPARATOR);
Thomas Vachuska329af532015-03-10 02:08:33 -0700768 for (ControllerNode node : nodes) {
769 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node),
Andrea Campanella732ea832017-02-06 09:25:59 -0800770 messageType));
Thomas Vachuska329af532015-03-10 02:08:33 -0700771 }
772 }
773
774 // Sends all devices to the client as device-added messages.
775 private void sendAllDevices() {
776 // Send optical first, others later for layered rendering
Simon Hunt1911fe42017-05-02 18:25:58 -0700777 for (Device device : services.device().getDevices()) {
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200778 if ((device.type() == Device.Type.ROADM) ||
Andrea Campanella1c24fb92018-12-20 16:43:59 +0100779 (device.type() == Device.Type.OTN) ||
780 (device.type() == Device.Type.OLS) ||
781 (device.type() == Device.Type.TERMINAL_DEVICE)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700782 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
783 }
784 }
Simon Hunt1911fe42017-05-02 18:25:58 -0700785 for (Device device : services.device().getDevices()) {
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200786 if ((device.type() != Device.Type.ROADM) &&
Andrea Campanellafa0f6cc2018-12-18 15:17:53 +0100787 (device.type() != Device.Type.OTN) && (device.type() != Device.Type.OLS) &&
Andrea Campanella1c24fb92018-12-20 16:43:59 +0100788 (device.type() != Device.Type.TERMINAL_DEVICE) && (device.type() != Device.Type.CONTROLLER)) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700789 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
790 }
791 }
792 }
793
794 // Sends all links to the client as link-added messages.
795 private void sendAllLinks() {
796 // Send optical first, others later for layered rendering
Simon Hunt1911fe42017-05-02 18:25:58 -0700797 for (Link link : services.link().getLinks()) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700798 if (link.type() == Link.Type.OPTICAL) {
Simon Hunt5c1a9382016-06-01 19:35:35 -0700799 sendMessage(composeLinkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700800 }
801 }
Simon Hunt1911fe42017-05-02 18:25:58 -0700802 for (Link link : services.link().getLinks()) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700803 if (link.type() != Link.Type.OPTICAL) {
Simon Hunt5c1a9382016-06-01 19:35:35 -0700804 sendMessage(composeLinkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700805 }
806 }
807 }
808
Simon Hunt5c1a9382016-06-01 19:35:35 -0700809 // Temporary mechanism to support topology overlays adding their own
810 // properties to the link events.
811 private ObjectNode composeLinkMessage(LinkEvent event) {
812 // start with base message
813 ObjectNode msg = linkMessage(event);
814 Map<String, String> additional =
815 overlayCache.currentOverlay().additionalLinkData(event);
816
817 if (additional != null) {
818 // attach additional key-value pairs as extra data structure
819 ObjectNode payload = (ObjectNode) msg.get(PAYLOAD);
820 payload.set(EXTRA, createExtra(additional));
821 }
822 return msg;
823 }
824
825 private ObjectNode createExtra(Map<String, String> additional) {
826 ObjectNode extra = objectNode();
827 for (Map.Entry<String, String> entry : additional.entrySet()) {
828 extra.put(entry.getKey(), entry.getValue());
829 }
830 return extra;
831 }
832
Thomas Vachuska329af532015-03-10 02:08:33 -0700833 // Sends all hosts to the client as host-added messages.
834 private void sendAllHosts() {
Simon Hunt1911fe42017-05-02 18:25:58 -0700835 for (Host host : services.host().getHosts()) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700836 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
837 }
838 }
839
Ray Milkey27cff8c2018-03-05 14:58:26 -0800840 private Set<FilteredConnectPoint> getHostLocations(Set<HostId> hostIds) {
841 Set<FilteredConnectPoint> points = new HashSet<>();
Thomas Vachuska329af532015-03-10 02:08:33 -0700842 for (HostId hostId : hostIds) {
Ray Milkey27cff8c2018-03-05 14:58:26 -0800843 points.add(new FilteredConnectPoint(getHostLocation(hostId)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700844 }
845 return points;
846 }
847
848 private HostLocation getHostLocation(HostId hostId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700849 return services.host().getHost(hostId).location();
Thomas Vachuska329af532015-03-10 02:08:33 -0700850 }
851
852 // Produces a list of host ids from the specified JSON array.
853 private Set<HostId> getHostIds(ArrayNode ids) {
854 Set<HostId> hostIds = new HashSet<>();
855 for (JsonNode id : ids) {
856 hostIds.add(hostId(id.asText()));
857 }
858 return hostIds;
859 }
860
Simon Hunt4a6b54b2015-10-27 22:08:25 -0700861 private void sendTopoStartDone() {
862 sendMessage(JsonUtils.envelope(TOPO_START_DONE, objectNode()));
863 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700864
Simon Huntd2747a02015-04-30 22:41:16 -0700865 private synchronized void startSummaryMonitoring() {
Thomas Vachuska329af532015-03-10 02:08:33 -0700866 stopSummaryMonitoring();
Thomas Vachuska329af532015-03-10 02:08:33 -0700867 summaryTask = new SummaryMonitor();
Simon Hunta17fa672015-08-19 18:42:22 -0700868 timer.schedule(summaryTask, SUMMARY_PERIOD, SUMMARY_PERIOD);
Simon Huntd2747a02015-04-30 22:41:16 -0700869 summaryRunning = true;
Thomas Vachuska329af532015-03-10 02:08:33 -0700870 }
871
872 private synchronized void stopSummaryMonitoring() {
Simon Huntd2747a02015-04-30 22:41:16 -0700873 if (summaryTask != null) {
Thomas Vachuska329af532015-03-10 02:08:33 -0700874 summaryTask.cancel();
875 summaryTask = null;
Thomas Vachuska329af532015-03-10 02:08:33 -0700876 }
Simon Huntd2747a02015-04-30 22:41:16 -0700877 summaryRunning = false;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700878 }
879
Thomas Vachuska329af532015-03-10 02:08:33 -0700880
881 // Adds all internal listeners.
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700882 private synchronized void addListeners() {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700883 listenersRemoved = false;
Simon Hunt1911fe42017-05-02 18:25:58 -0700884 services.cluster().addListener(clusterListener);
885 services.mastership().addListener(mastershipListener);
886 services.device().addListener(deviceListener);
887 services.link().addListener(linkListener);
888 services.host().addListener(hostListener);
889 services.intent().addListener(intentListener);
890 services.flow().addListener(flowListener);
Thomas Vachuska329af532015-03-10 02:08:33 -0700891 }
892
893 // Removes all internal listeners.
894 private synchronized void removeListeners() {
895 if (!listenersRemoved) {
896 listenersRemoved = true;
Simon Hunt1911fe42017-05-02 18:25:58 -0700897 services.cluster().removeListener(clusterListener);
898 services.mastership().removeListener(mastershipListener);
899 services.device().removeListener(deviceListener);
900 services.link().removeListener(linkListener);
901 services.host().removeListener(hostListener);
902 services.intent().removeListener(intentListener);
903 services.flow().removeListener(flowListener);
Thomas Vachuska329af532015-03-10 02:08:33 -0700904 }
905 }
906
907 // Cluster event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700908 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
909 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700910 private class InternalClusterListener implements ClusterEventListener {
911 @Override
912 public void event(ClusterEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700913 msgSender.execute(() -> sendMessage(instanceMessage(event, null)));
Thomas Vachuska329af532015-03-10 02:08:33 -0700914 }
915 }
916
917 // Mastership change listener
Simon Hunt7092cc42016-04-06 18:40:17 -0700918 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
919 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700920 private class InternalMastershipListener implements MastershipListener {
921 @Override
922 public void event(MastershipEvent event) {
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700923 msgSender.execute(() -> {
Simon Huntb745ca62015-07-28 15:37:11 -0700924 sendAllInstances(UPDATE_INSTANCE);
Simon Hunt1911fe42017-05-02 18:25:58 -0700925 Device device = services.device().getDevice(event.subject());
Thomas Vachuska52c98bd2015-05-27 20:54:02 -0700926 if (device != null) {
927 sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device)));
928 }
929 });
Thomas Vachuska329af532015-03-10 02:08:33 -0700930 }
931 }
932
933 // Device event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700934 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
935 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700936 private class InternalDeviceListener implements DeviceListener {
937 @Override
938 public void event(DeviceEvent event) {
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700939 if (event.type() != PORT_STATS_UPDATED) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700940 msgSender.execute(() -> sendMessage(deviceMessage(event)));
Simon Hunta1f1c022016-03-03 15:54:57 -0800941 msgSender.execute(traffic::pokeIntent);
Thomas Vachuskacb5016f2015-05-18 14:11:43 -0700942 eventAccummulator.add(event);
943 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700944 }
945 }
946
947 // Link event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700948 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
949 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700950 private class InternalLinkListener implements LinkListener {
951 @Override
952 public void event(LinkEvent event) {
Simon Hunt5c1a9382016-06-01 19:35:35 -0700953 msgSender.execute(() -> sendMessage(composeLinkMessage(event)));
Simon Hunta1f1c022016-03-03 15:54:57 -0800954 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700955 eventAccummulator.add(event);
956 }
957 }
958
959 // Host event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700960 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
961 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700962 private class InternalHostListener implements HostListener {
963 @Override
964 public void event(HostEvent event) {
Thomas Vachuskac7f79962015-05-28 09:37:34 -0700965 msgSender.execute(() -> sendMessage(hostMessage(event)));
Simon Hunta1f1c022016-03-03 15:54:57 -0800966 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700967 eventAccummulator.add(event);
968 }
969 }
970
971 // Intent event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700972 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
973 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700974 private class InternalIntentListener implements IntentListener {
975 @Override
976 public void event(IntentEvent event) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700977 msgSender.execute(traffic::pokeIntent);
Thomas Vachuska329af532015-03-10 02:08:33 -0700978 eventAccummulator.add(event);
979 }
980 }
981
982 // Intent event listener.
Simon Hunt7092cc42016-04-06 18:40:17 -0700983 // TODO: Superceded by UiSharedTopologyModel.ModelEventListener
984 @Deprecated
Thomas Vachuska329af532015-03-10 02:08:33 -0700985 private class InternalFlowListener implements FlowRuleListener {
986 @Override
987 public void event(FlowRuleEvent event) {
988 eventAccummulator.add(event);
989 }
990 }
991
Simon Huntd2747a02015-04-30 22:41:16 -0700992
Simon Hunta17fa672015-08-19 18:42:22 -0700993 // === SUMMARY MONITORING
Thomas Vachuska329af532015-03-10 02:08:33 -0700994
995 // Periodic update of the summary information
996 private class SummaryMonitor extends TimerTask {
997 @Override
998 public void run() {
999 try {
Simon Huntd2747a02015-04-30 22:41:16 -07001000 if (summaryRunning) {
Simon Hunt8a0429a2017-01-06 16:52:47 -08001001 msgSender.execute(() -> requestSummary());
Thomas Vachuska329af532015-03-10 02:08:33 -07001002 }
1003 } catch (Exception e) {
1004 log.warn("Unable to handle summary request due to {}", e.getMessage());
1005 log.warn("Boom!", e);
1006 }
1007 }
1008 }
1009
1010 // Accumulates events to drive methodic update of the summary pane.
1011 private class InternalEventAccummulator extends AbstractAccumulator<Event> {
1012 protected InternalEventAccummulator() {
1013 super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS);
1014 }
1015
1016 @Override
1017 public void processItems(List<Event> items) {
Simon Hunta17fa672015-08-19 18:42:22 -07001018 // Start-of-Debugging -- Keep in until ONOS-2572 is fixed for reals
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001019 long now = System.currentTimeMillis();
1020 String me = this.toString();
1021 String miniMe = me.replaceAll("^.*@", "me@");
1022 log.debug("Time: {}; this: {}, processing items ({} events)",
Andrea Campanella732ea832017-02-06 09:25:59 -08001023 now, miniMe, items.size());
Simon Hunt8d22c4b2015-08-06 16:24:43 -07001024 // End-of-Debugging
1025
Thomas Vachuska329af532015-03-10 02:08:33 -07001026 try {
Simon Huntd2747a02015-04-30 22:41:16 -07001027 if (summaryRunning) {
Simon Hunt8a0429a2017-01-06 16:52:47 -08001028 msgSender.execute(() -> requestSummary());
Thomas Vachuska329af532015-03-10 02:08:33 -07001029 }
1030 } catch (Exception e) {
1031 log.warn("Unable to handle summary request due to {}", e.getMessage());
1032 log.debug("Boom!", e);
1033 }
1034 }
1035 }
1036}