blob: 0353a25d0265b0465726dd0010b52604103c7572 [file] [log] [blame]
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.gui;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080017
Thomas Vachuska9edca302014-11-22 17:06:42 -080018import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080019import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080020import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080021import org.eclipse.jetty.websocket.WebSocket;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.cluster.ClusterEvent;
23import org.onosproject.cluster.ClusterEventListener;
24import org.onosproject.cluster.ControllerNode;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.event.AbstractEventAccumulator;
28import org.onosproject.event.Event;
29import org.onosproject.event.EventAccumulator;
30import org.onosproject.mastership.MastershipAdminService;
31import org.onosproject.mastership.MastershipEvent;
32import org.onosproject.mastership.MastershipListener;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.Device;
35import org.onosproject.net.Host;
36import org.onosproject.net.HostId;
37import org.onosproject.net.HostLocation;
38import org.onosproject.net.Link;
39import org.onosproject.net.device.DeviceEvent;
40import org.onosproject.net.device.DeviceListener;
41import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
43import org.onosproject.net.flow.FlowRuleEvent;
44import org.onosproject.net.flow.FlowRuleListener;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
47import org.onosproject.net.host.HostEvent;
48import org.onosproject.net.host.HostListener;
49import org.onosproject.net.intent.HostToHostIntent;
50import org.onosproject.net.intent.Intent;
51import org.onosproject.net.intent.IntentEvent;
52import org.onosproject.net.intent.IntentListener;
53import org.onosproject.net.intent.MultiPointToSinglePointIntent;
54import org.onosproject.net.link.LinkEvent;
55import org.onosproject.net.link.LinkListener;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080056import org.onlab.osgi.ServiceDirectory;
Thomas Vachuskae02e11c2014-11-24 16:13:52 -080057import org.onlab.packet.Ethernet;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080058
59import java.io.IOException;
Thomas Vachuska47635c62014-11-22 01:21:36 -080060import java.util.ArrayList;
61import java.util.Collections;
62import java.util.Comparator;
Thomas Vachuska29617e52014-11-20 03:17:46 -080063import java.util.HashSet;
Thomas Vachuska47635c62014-11-22 01:21:36 -080064import java.util.List;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080065import java.util.Set;
Thomas Vachuska22e34922014-11-14 00:40:55 -080066import java.util.Timer;
67import java.util.TimerTask;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080068
Thomas Vachuskae7591e52014-11-13 21:31:15 -080069import static com.google.common.base.Strings.isNullOrEmpty;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED;
71import static org.onosproject.net.DeviceId.deviceId;
72import static org.onosproject.net.HostId.hostId;
73import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
74import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED;
75import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
76import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080077
Thomas Vachuska7d638d32014-11-07 10:24:43 -080078/**
79 * Web socket capable of interacting with the GUI topology view.
80 */
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080081public class TopologyViewWebSocket
82 extends TopologyViewMessages
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080083 implements WebSocket.OnTextMessage, WebSocket.OnControl {
84
85 private static final long MAX_AGE_MS = 15000;
86
87 private static final byte PING = 0x9;
88 private static final byte PONG = 0xA;
89 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
Thomas Vachuska7d638d32014-11-07 10:24:43 -080090
Brian O'Connorabafb502014-12-02 22:26:20 -080091 private static final String APP_ID = "org.onosproject.gui";
Thomas Vachuska4830d392014-11-09 17:09:56 -080092
Thomas Vachuska164fa5c2014-12-02 21:59:41 -080093 private static final long TRAFFIC_FREQUENCY = 2000;
94 private static final long SUMMARY_FREQUENCY = 30000;
Thomas Vachuska22e34922014-11-14 00:40:55 -080095
Thomas Vachuska47635c62014-11-22 01:21:36 -080096 private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
97 new Comparator<ControllerNode>() {
98 @Override
99 public int compare(ControllerNode o1, ControllerNode o2) {
100 return o1.id().toString().compareTo(o2.id().toString());
101 }
102 };
103
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800104
105 private final Timer timer = new Timer("topology-view");
106
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800107 private static final int MAX_EVENTS = 1000;
108 private static final int MAX_BATCH_MS = 5000;
109 private static final int MAX_IDLE_MS = 1000;
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800110
Thomas Vachuska4830d392014-11-09 17:09:56 -0800111 private final ApplicationId appId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -0800112
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800113 private Connection connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800114 private FrameConnection control;
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800115
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800116 private final ClusterEventListener clusterListener = new InternalClusterListener();
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800117 private final MastershipListener mastershipListener = new InternalMastershipListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800118 private final DeviceListener deviceListener = new InternalDeviceListener();
119 private final LinkListener linkListener = new InternalLinkListener();
120 private final HostListener hostListener = new InternalHostListener();
Thomas Vachuska4830d392014-11-09 17:09:56 -0800121 private final IntentListener intentListener = new InternalIntentListener();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800122 private final FlowRuleListener flowListener = new InternalFlowListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800123
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800124 private final EventAccumulator eventAccummulator = new InternalEventAccummulator();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800125
126 private TimerTask trafficTask;
127 private ObjectNode trafficEvent;
128
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800129 private TimerTask summaryTask;
130 private ObjectNode summaryEvent;
131
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800132 private long lastActive = System.currentTimeMillis();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800133 private boolean listenersRemoved = false;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800134
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800135 private TopologyViewIntentFilter intentFilter;
136
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800137 // Current selection context
138 private Set<Host> selectedHosts;
139 private Set<Device> selectedDevices;
140 private List<Intent> selectedIntents;
141 private int currentIntentIndex = -1;
142
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800143 /**
144 * Creates a new web-socket for serving data to GUI topology view.
145 *
146 * @param directory service directory
147 */
Thomas Vachuska7c27ad72014-11-14 16:20:10 -0800148 public TopologyViewWebSocket(ServiceDirectory directory) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800149 super(directory);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800150 intentFilter = new TopologyViewIntentFilter(intentService, deviceService,
Thomas Vachuska9edca302014-11-22 17:06:42 -0800151 hostService, linkService);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800152 appId = directory.get(CoreService.class).registerApplication(APP_ID);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800153 }
154
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800155 /**
156 * Issues a close on the connection.
157 */
158 synchronized void close() {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800159 removeListeners();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800160 if (connection.isOpen()) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800161 connection.close();
162 }
163 }
164
165 /**
166 * Indicates if this connection is idle.
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800167 *
168 * @return true if idle or closed
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800169 */
170 synchronized boolean isIdle() {
171 boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS;
172 if (idle || !connection.isOpen()) {
173 return true;
174 }
175 try {
176 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
177 } catch (IOException e) {
178 log.warn("Unable to send ping message due to: ", e);
179 }
180 return false;
181 }
182
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800183 @Override
184 public void onOpen(Connection connection) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800185 log.info("GUI client connected");
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800186 this.connection = connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800187 this.control = (FrameConnection) connection;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800188 addListeners();
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800189
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800190 sendAllInstances(null);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800191 sendAllDevices();
192 sendAllLinks();
Thomas Vachuska4830d392014-11-09 17:09:56 -0800193 sendAllHosts();
194 }
195
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800196 @Override
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800197 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800198 removeListeners();
Thomas Vachuska22e34922014-11-14 00:40:55 -0800199 timer.cancel();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800200 log.info("GUI client disconnected");
201 }
202
203 @Override
204 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
205 lastActive = System.currentTimeMillis();
206 return true;
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800207 }
208
209 @Override
210 public void onMessage(String data) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800211 lastActive = System.currentTimeMillis();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800212 try {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800213 processMessage((ObjectNode) mapper.reader().readTree(data));
Thomas Vachuska4830d392014-11-09 17:09:56 -0800214 } catch (Exception e) {
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800215 log.warn("Unable to parse GUI request {} due to {}", data, e);
Thomas Vachuska6810b7882014-12-03 00:37:25 -0800216 log.warn("Boom!!!", e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800217 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800218 }
219
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800220 // Processes the specified event.
221 private void processMessage(ObjectNode event) {
222 String type = string(event, "event", "unknown");
223 if (type.equals("requestDetails")) {
224 requestDetails(event);
225 } else if (type.equals("updateMeta")) {
226 updateMetaUi(event);
Thomas Vachuska9edca302014-11-22 17:06:42 -0800227
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800228 } else if (type.equals("addHostIntent")) {
229 createHostIntent(event);
Thomas Vachuska9edca302014-11-22 17:06:42 -0800230 } else if (type.equals("addMultiSourceIntent")) {
231 createMultiSourceIntent(event);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800232
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800233 } else if (type.equals("requestRelatedIntents")) {
234 requestRelatedIntents(event);
235 } else if (type.equals("requestNextRelatedIntent")) {
236 requestNextRelatedIntent(event);
237 } else if (type.equals("requestSelectedIntentTraffic")) {
238 requestSelectedIntentTraffic(event);
239
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800240 } else if (type.equals("requestAllTraffic")) {
241 requestAllTraffic(event);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800242 startTrafficMonitoring(event);
243
Thomas Vachuska29617e52014-11-20 03:17:46 -0800244 } else if (type.equals("requestDeviceLinkFlows")) {
245 requestDeviceLinkFlows(event);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800246 startTrafficMonitoring(event);
247
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800248 } else if (type.equals("cancelTraffic")) {
249 cancelTraffic(event);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800250
251 } else if (type.equals("requestSummary")) {
252 requestSummary(event);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800253 startSummaryMonitoring(event);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800254 } else if (type.equals("cancelSummary")) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800255 stopSummaryMonitoring();
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800256
257 } else if (type.equals("equalizeMasters")) {
258 equalizeMasters(event);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800259 }
260 }
261
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800262 // Sends the specified data to the client.
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800263 private synchronized void sendMessage(ObjectNode data) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800264 try {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800265 if (connection.isOpen()) {
266 connection.sendMessage(data.toString());
267 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800268 } catch (IOException e) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800269 log.warn("Unable to send message {} to GUI due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800270 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800271 }
272
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800273 // Sends all controller nodes to the client as node-added messages.
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800274 private void sendAllInstances(String messageType) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800275 List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
276 Collections.sort(nodes, NODE_COMPARATOR);
277 for (ControllerNode node : nodes) {
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800278 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node),
279 messageType));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800280 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800281 }
282
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800283 // Sends all devices to the client as device-added messages.
284 private void sendAllDevices() {
Thomas Vachuska82041f52014-11-30 22:14:02 -0800285 // Send optical first, others later for layered rendering
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800286 for (Device device : deviceService.getDevices()) {
Thomas Vachuska82041f52014-11-30 22:14:02 -0800287 if (device.type() == Device.Type.ROADM) {
288 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
289 }
290 }
291 for (Device device : deviceService.getDevices()) {
292 if (device.type() != Device.Type.ROADM) {
293 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
294 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800295 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800296 }
297
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800298 // Sends all links to the client as link-added messages.
299 private void sendAllLinks() {
Thomas Vachuska82041f52014-11-30 22:14:02 -0800300 // Send optical first, others later for layered rendering
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800301 for (Link link : linkService.getLinks()) {
Thomas Vachuska82041f52014-11-30 22:14:02 -0800302 if (link.type() == Link.Type.OPTICAL) {
303 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
304 }
305 }
306 for (Link link : linkService.getLinks()) {
307 if (link.type() != Link.Type.OPTICAL) {
308 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
309 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800310 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800311 }
312
313 // Sends all hosts to the client as host-added messages.
314 private void sendAllHosts() {
315 for (Host host : hostService.getHosts()) {
316 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
317 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800318 }
319
320 // Sends back device or host details.
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800321 private void requestDetails(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800322 ObjectNode payload = payload(event);
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800323 String type = string(payload, "class", "unknown");
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800324 long sid = number(event, "sid");
325
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800326 if (type.equals("device")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800327 sendMessage(deviceDetails(deviceId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800328 } else if (type.equals("host")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800329 sendMessage(hostDetails(hostId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800330 }
331 }
332
Thomas Vachuska9edca302014-11-22 17:06:42 -0800333
Thomas Vachuska4830d392014-11-09 17:09:56 -0800334 // Creates host-to-host intent.
335 private void createHostIntent(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800336 ObjectNode payload = payload(event);
337 long id = number(event, "sid");
Thomas Vachuska4830d392014-11-09 17:09:56 -0800338 // TODO: add protection against device ids and non-existent hosts.
339 HostId one = hostId(string(payload, "one"));
340 HostId two = hostId(string(payload, "two"));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800341
Thomas Vachuska9edca302014-11-22 17:06:42 -0800342 HostToHostIntent intent =
343 new HostToHostIntent(appId, one, two,
344 DefaultTrafficSelector.builder().build(),
345 DefaultTrafficTreatment.builder().build());
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800346
Thomas Vachuska9edca302014-11-22 17:06:42 -0800347 intentService.submit(intent);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800348 startMonitoringIntent(event, intent);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800349 }
350
Thomas Vachuska9edca302014-11-22 17:06:42 -0800351 // Creates multi-source-to-single-dest intent.
352 private void createMultiSourceIntent(ObjectNode event) {
353 ObjectNode payload = payload(event);
354 long id = number(event, "sid");
355 // TODO: add protection against device ids and non-existent hosts.
356 Set<HostId> src = getHostIds((ArrayNode) payload.path("src"));
357 HostId dst = hostId(string(payload, "dst"));
358 Host dstHost = hostService.getHost(dst);
359
360 Set<ConnectPoint> ingressPoints = getHostLocations(src);
361
362 // FIXME: clearly, this is not enough
363 TrafficSelector selector = DefaultTrafficSelector.builder()
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800364 .matchEthType(Ethernet.TYPE_IPV4)
Thomas Vachuska9edca302014-11-22 17:06:42 -0800365 .matchEthDst(dstHost.mac()).build();
366 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
367
368 MultiPointToSinglePointIntent intent =
369 new MultiPointToSinglePointIntent(appId, selector, treatment,
370 ingressPoints, dstHost.location());
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800371
Thomas Vachuska9edca302014-11-22 17:06:42 -0800372 intentService.submit(intent);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800373 startMonitoringIntent(event, intent);
Thomas Vachuska9edca302014-11-22 17:06:42 -0800374 }
375
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800376
377 private synchronized void startMonitoringIntent(ObjectNode event, Intent intent) {
378 selectedHosts = new HashSet<>();
379 selectedDevices = new HashSet<>();
380 selectedIntents = new ArrayList<>();
381 selectedIntents.add(intent);
382 currentIntentIndex = -1;
383 requestNextRelatedIntent(event);
384 requestSelectedIntentTraffic(event);
385 }
386
387
Thomas Vachuska9edca302014-11-22 17:06:42 -0800388 private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) {
389 Set<ConnectPoint> points = new HashSet<>();
390 for (HostId hostId : hostIds) {
391 points.add(getHostLocation(hostId));
Thomas Vachuska47635c62014-11-22 01:21:36 -0800392 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800393 return points;
394 }
395
396 private HostLocation getHostLocation(HostId hostId) {
397 return hostService.getHost(hostId).location();
398 }
399
400 // Produces a list of host ids from the specified JSON array.
401 private Set<HostId> getHostIds(ArrayNode ids) {
402 Set<HostId> hostIds = new HashSet<>();
403 for (JsonNode id : ids) {
404 hostIds.add(hostId(id.asText()));
405 }
406 return hostIds;
407 }
408
409
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800410 private synchronized long startTrafficMonitoring(ObjectNode event) {
411 stopTrafficMonitoring();
Thomas Vachuska9edca302014-11-22 17:06:42 -0800412 trafficEvent = event;
413 trafficTask = new TrafficMonitor();
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800414 timer.schedule(trafficTask, TRAFFIC_FREQUENCY, TRAFFIC_FREQUENCY);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800415 return number(event, "sid");
416 }
417
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800418 private synchronized void stopTrafficMonitoring() {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800419 if (trafficTask != null) {
420 trafficTask.cancel();
421 trafficTask = null;
422 trafficEvent = null;
423 }
424 }
425
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800426 // Subscribes for host traffic messages.
427 private synchronized void requestAllTraffic(ObjectNode event) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800428 long sid = startTrafficMonitoring(event);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800429 sendMessage(trafficSummaryMessage(sid));
430 }
431
Thomas Vachuska29617e52014-11-20 03:17:46 -0800432 private void requestDeviceLinkFlows(ObjectNode event) {
433 ObjectNode payload = payload(event);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800434 long sid = startTrafficMonitoring(event);
Thomas Vachuska29617e52014-11-20 03:17:46 -0800435
436 // Get the set of selected hosts and their intents.
437 ArrayNode ids = (ArrayNode) payload.path("ids");
438 Set<Host> hosts = new HashSet<>();
439 Set<Device> devices = getDevices(ids);
440
441 // If there is a hover node, include it in the hosts and find intents.
442 String hover = string(payload, "hover");
Thomas Vachuska29617e52014-11-20 03:17:46 -0800443 if (!isNullOrEmpty(hover)) {
444 addHover(hosts, devices, hover);
445 }
446 sendMessage(flowSummaryMessage(sid, devices));
447 }
448
449
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800450 // Requests related intents message.
451 private synchronized void requestRelatedIntents(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800452 ObjectNode payload = payload(event);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800453 if (!payload.has("ids")) {
454 return;
455 }
456
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800457 long sid = number(event, "sid");
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800458
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800459 // Cancel any other traffic monitoring mode.
460 stopTrafficMonitoring();
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800461
Thomas Vachuska6810b7882014-12-03 00:37:25 -0800462 // Get the set of selected hosts and their intents.
463 ArrayNode ids = (ArrayNode) payload.path("ids");
464 selectedHosts = getHosts(ids);
465 selectedDevices = getDevices(ids);
466 selectedIntents = intentFilter.findPathIntents(selectedHosts, selectedDevices,
467 intentService.getIntents());
468 currentIntentIndex = -1;
469
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800470 String hover = string(payload, "hover");
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800471 if (haveSelectedIntents()) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800472 // Send a message to highlight all links of all monitored intents.
473 sendMessage(trafficMessage(sid, new TrafficClass("primary", selectedIntents)));
Thomas Vachuska6810b7882014-12-03 00:37:25 -0800474 } else if (!isNullOrEmpty(hover)) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800475 // If there is a hover node, include it in the selection and find intents.
476 processExtendedSelection(sid, hover);
477 }
478 }
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800479
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800480 private boolean haveSelectedIntents() {
481 return selectedIntents != null && !selectedIntents.isEmpty();
482 }
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800483
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800484 private void processExtendedSelection(long sid, String hover) {
Thomas Vachuska6810b7882014-12-03 00:37:25 -0800485 if (haveSelectedIntents()) {
486 Set<Host> hoverSelHosts = new HashSet<>(selectedHosts);
487 Set<Device> hoverSelDevices = new HashSet<>(selectedDevices);
488 addHover(hoverSelHosts, hoverSelDevices, hover);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800489
Thomas Vachuska6810b7882014-12-03 00:37:25 -0800490 List<Intent> primary =
491 intentFilter.findPathIntents(hoverSelHosts, hoverSelDevices,
492 selectedIntents);
493 Set<Intent> secondary = new HashSet<>(selectedIntents);
494 secondary.removeAll(primary);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800495
Thomas Vachuska6810b7882014-12-03 00:37:25 -0800496 // Send a message to highlight all links of all monitored intents.
497 sendMessage(trafficMessage(sid, new TrafficClass("primary", primary),
498 new TrafficClass("secondary", secondary)));
499 }
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800500 }
501
502 // Requests next of the related intents.
503 private void requestNextRelatedIntent(ObjectNode event) {
504 if (haveSelectedIntents()) {
505 currentIntentIndex = (currentIntentIndex + 1) % selectedIntents.size();
506 Intent selectedIntent = selectedIntents.get(currentIntentIndex);
507 log.info("Requested next intent {}", selectedIntent.id());
508
509 Set<Intent> primary = new HashSet<>();
510 primary.add(selectedIntent);
511
512 Set<Intent> secondary = new HashSet<>(selectedIntents);
513 secondary.remove(selectedIntent);
514
515 // Send a message to highlight all links of the selected intent.
516 sendMessage(trafficMessage(number(event, "sid"),
517 new TrafficClass("primary", primary),
518 new TrafficClass("secondary", secondary)));
519 }
520 }
521
522 // Requests monitoring of traffic for the selected intent.
523 private void requestSelectedIntentTraffic(ObjectNode event) {
524 if (haveSelectedIntents()) {
Thomas Vachuska6810b7882014-12-03 00:37:25 -0800525 if (currentIntentIndex < 0) {
526 currentIntentIndex = 0;
527 }
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800528 Intent selectedIntent = selectedIntents.get(currentIntentIndex);
529 log.info("Requested traffic for selected {}", selectedIntent.id());
530
531 Set<Intent> primary = new HashSet<>();
532 primary.add(selectedIntent);
533
534 // Send a message to highlight all links of the selected intent.
535 sendMessage(trafficMessage(number(event, "sid"),
536 new TrafficClass("primary", primary, true)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800537 }
538 }
539
540 // Cancels sending traffic messages.
541 private void cancelTraffic(ObjectNode event) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800542 selectedIntents = null;
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800543 sendMessage(trafficMessage(number(event, "sid")));
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800544 stopTrafficMonitoring();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800545 }
546
Thomas Vachuska47635c62014-11-22 01:21:36 -0800547
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800548 private synchronized long startSummaryMonitoring(ObjectNode event) {
549 stopSummaryMonitoring();
550 summaryEvent = event;
551 summaryTask = new SummaryMonitor();
552 timer.schedule(summaryTask, SUMMARY_FREQUENCY, SUMMARY_FREQUENCY);
553 return number(event, "sid");
554 }
555
556 private synchronized void stopSummaryMonitoring() {
557 if (summaryEvent != null) {
558 summaryTask.cancel();
559 summaryTask = null;
560 summaryEvent = null;
561 }
562 }
563
Thomas Vachuska47635c62014-11-22 01:21:36 -0800564 // Subscribes for summary messages.
565 private synchronized void requestSummary(ObjectNode event) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800566 sendMessage(summmaryMessage(number(event, "sid")));
567 }
568
Thomas Vachuska47635c62014-11-22 01:21:36 -0800569
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800570 // Forces mastership role rebalancing.
571 private void equalizeMasters(ObjectNode event) {
572 directory.get(MastershipAdminService.class).balanceRoles();
573 }
574
575
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800576 // Adds all internal listeners.
577 private void addListeners() {
578 clusterService.addListener(clusterListener);
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800579 mastershipService.addListener(mastershipListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800580 deviceService.addListener(deviceListener);
581 linkService.addListener(linkListener);
582 hostService.addListener(hostListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800583 intentService.addListener(intentListener);
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800584 flowService.addListener(flowListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800585 }
586
587 // Removes all internal listeners.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800588 private synchronized void removeListeners() {
589 if (!listenersRemoved) {
590 listenersRemoved = true;
591 clusterService.removeListener(clusterListener);
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800592 mastershipService.removeListener(mastershipListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800593 deviceService.removeListener(deviceListener);
594 linkService.removeListener(linkListener);
595 hostService.removeListener(hostListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800596 intentService.removeListener(intentListener);
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800597 flowService.removeListener(flowListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800598 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800599 }
600
601 // Cluster event listener.
602 private class InternalClusterListener implements ClusterEventListener {
603 @Override
604 public void event(ClusterEvent event) {
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800605 sendMessage(instanceMessage(event, null));
606 }
607 }
608
609 // Mastership change listener
610 private class InternalMastershipListener implements MastershipListener {
611 @Override
612 public void event(MastershipEvent event) {
613 sendAllInstances("updateInstance");
614 Device device = deviceService.getDevice(event.subject());
615 sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800616 }
617 }
618
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800619 // Device event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800620 private class InternalDeviceListener implements DeviceListener {
621 @Override
622 public void event(DeviceEvent event) {
623 sendMessage(deviceMessage(event));
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800624 eventAccummulator.add(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800625 }
626 }
627
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800628 // Link event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800629 private class InternalLinkListener implements LinkListener {
630 @Override
631 public void event(LinkEvent event) {
632 sendMessage(linkMessage(event));
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800633 eventAccummulator.add(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800634 }
635 }
636
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800637 // Host event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800638 private class InternalHostListener implements HostListener {
639 @Override
640 public void event(HostEvent event) {
641 sendMessage(hostMessage(event));
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800642 eventAccummulator.add(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800643 }
644 }
645
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800646 // Intent event listener.
Thomas Vachuska4830d392014-11-09 17:09:56 -0800647 private class InternalIntentListener implements IntentListener {
648 @Override
649 public void event(IntentEvent event) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800650 if (trafficEvent != null) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800651 requestSelectedIntentTraffic(trafficEvent);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800652 }
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800653 eventAccummulator.add(event);
654 }
655 }
656
657 // Intent event listener.
658 private class InternalFlowListener implements FlowRuleListener {
659 @Override
660 public void event(FlowRuleEvent event) {
661 eventAccummulator.add(event);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800662 }
663 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800664
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800665 // Periodic update of the traffic information
Thomas Vachuska47635c62014-11-22 01:21:36 -0800666 private class TrafficMonitor extends TimerTask {
Thomas Vachuska22e34922014-11-14 00:40:55 -0800667 @Override
668 public void run() {
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800669 try {
670 if (trafficEvent != null) {
671 String type = string(trafficEvent, "event", "unknown");
672 if (type.equals("requestAllTraffic")) {
673 requestAllTraffic(trafficEvent);
674 } else if (type.equals("requestDeviceLinkFlows")) {
675 requestDeviceLinkFlows(trafficEvent);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800676 } else if (type.equals("requestSelectedIntentTraffic")) {
677 requestSelectedIntentTraffic(trafficEvent);
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800678 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800679 }
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800680 } catch (Exception e) {
681 log.warn("Unable to handle traffic request due to {}", e.getMessage());
Thomas Vachuska82041f52014-11-30 22:14:02 -0800682 log.warn("Boom!", e);
Thomas Vachuska22e34922014-11-14 00:40:55 -0800683 }
684 }
685 }
Thomas Vachuska47635c62014-11-22 01:21:36 -0800686
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800687 // Periodic update of the summary information
688 private class SummaryMonitor extends TimerTask {
689 @Override
690 public void run() {
691 try {
692 if (summaryEvent != null) {
693 requestSummary(summaryEvent);
694 }
695 } catch (Exception e) {
696 log.warn("Unable to handle summary request due to {}", e.getMessage());
697 }
698 }
699 }
700
Thomas Vachuska5dd52f72014-11-28 19:27:45 -0800701 // Accumulates events to drive methodic update of the summary pane.
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800702 private class InternalEventAccummulator extends AbstractEventAccumulator {
703 protected InternalEventAccummulator() {
704 super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS);
705 }
706
Thomas Vachuska47635c62014-11-22 01:21:36 -0800707 @Override
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800708 public void processEvents(List<Event> events) {
709 try {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800710 if (summaryEvent != null) {
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800711 sendMessage(summmaryMessage(0));
712 }
713 } catch (Exception e) {
714 log.warn("Unable to handle summary request due to {}", e.getMessage());
Thomas Vachuska47635c62014-11-22 01:21:36 -0800715 }
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800716
Thomas Vachuska47635c62014-11-22 01:21:36 -0800717 }
718 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800719}
720