blob: bd0206598f52fcde7fc37c008fe37aeb3afebda1 [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 */
16package org.onlab.onos.gui;
17
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;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080022import org.onlab.onos.cluster.ClusterEvent;
23import org.onlab.onos.cluster.ClusterEventListener;
24import org.onlab.onos.cluster.ControllerNode;
Thomas Vachuska4830d392014-11-09 17:09:56 -080025import org.onlab.onos.core.ApplicationId;
26import org.onlab.onos.core.CoreService;
Thomas Vachuska5bde31f2014-11-25 15:29:18 -080027import org.onlab.onos.event.AbstractEventAccumulator;
28import org.onlab.onos.event.Event;
29import org.onlab.onos.event.EventAccumulator;
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -080030import org.onlab.onos.mastership.MastershipAdminService;
Thomas Vachuskae02e11c2014-11-24 16:13:52 -080031import org.onlab.onos.mastership.MastershipEvent;
32import org.onlab.onos.mastership.MastershipListener;
Thomas Vachuska9edca302014-11-22 17:06:42 -080033import org.onlab.onos.net.ConnectPoint;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080034import org.onlab.onos.net.Device;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080035import org.onlab.onos.net.Host;
36import org.onlab.onos.net.HostId;
Thomas Vachuska9edca302014-11-22 17:06:42 -080037import org.onlab.onos.net.HostLocation;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080038import org.onlab.onos.net.Link;
39import org.onlab.onos.net.device.DeviceEvent;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080040import org.onlab.onos.net.device.DeviceListener;
Thomas Vachuska4830d392014-11-09 17:09:56 -080041import org.onlab.onos.net.flow.DefaultTrafficSelector;
42import org.onlab.onos.net.flow.DefaultTrafficTreatment;
Thomas Vachuska5bde31f2014-11-25 15:29:18 -080043import org.onlab.onos.net.flow.FlowRuleEvent;
44import org.onlab.onos.net.flow.FlowRuleListener;
Thomas Vachuska9edca302014-11-22 17:06:42 -080045import org.onlab.onos.net.flow.TrafficSelector;
46import org.onlab.onos.net.flow.TrafficTreatment;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080047import org.onlab.onos.net.host.HostEvent;
48import org.onlab.onos.net.host.HostListener;
Thomas Vachuska4830d392014-11-09 17:09:56 -080049import org.onlab.onos.net.intent.HostToHostIntent;
50import org.onlab.onos.net.intent.Intent;
51import org.onlab.onos.net.intent.IntentEvent;
Thomas Vachuska4830d392014-11-09 17:09:56 -080052import org.onlab.onos.net.intent.IntentListener;
Thomas Vachuska9edca302014-11-22 17:06:42 -080053import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080054import org.onlab.onos.net.link.LinkEvent;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080055import org.onlab.onos.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;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080070import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080071import static org.onlab.onos.net.DeviceId.deviceId;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080072import static org.onlab.onos.net.HostId.hostId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080073import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED;
Thomas Vachuskae02e11c2014-11-24 16:13:52 -080074import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_UPDATED;
Thomas Vachuska4830d392014-11-09 17:09:56 -080075import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080076import static org.onlab.onos.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
Thomas Vachuska4830d392014-11-09 17:09:56 -080091 private static final String APP_ID = "org.onlab.onos.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 Vachuskad1be50d2014-11-08 16:10:20 -0800216 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800217 }
218
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800219 // Processes the specified event.
220 private void processMessage(ObjectNode event) {
221 String type = string(event, "event", "unknown");
222 if (type.equals("requestDetails")) {
223 requestDetails(event);
224 } else if (type.equals("updateMeta")) {
225 updateMetaUi(event);
Thomas Vachuska9edca302014-11-22 17:06:42 -0800226
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800227 } else if (type.equals("addHostIntent")) {
228 createHostIntent(event);
Thomas Vachuska9edca302014-11-22 17:06:42 -0800229 } else if (type.equals("addMultiSourceIntent")) {
230 createMultiSourceIntent(event);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800231
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800232 } else if (type.equals("requestRelatedIntents")) {
233 requestRelatedIntents(event);
234 } else if (type.equals("requestNextRelatedIntent")) {
235 requestNextRelatedIntent(event);
236 } else if (type.equals("requestSelectedIntentTraffic")) {
237 requestSelectedIntentTraffic(event);
238
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800239 } else if (type.equals("requestAllTraffic")) {
240 requestAllTraffic(event);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800241 startTrafficMonitoring(event);
242
Thomas Vachuska29617e52014-11-20 03:17:46 -0800243 } else if (type.equals("requestDeviceLinkFlows")) {
244 requestDeviceLinkFlows(event);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800245 startTrafficMonitoring(event);
246
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800247 } else if (type.equals("cancelTraffic")) {
248 cancelTraffic(event);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800249
250 } else if (type.equals("requestSummary")) {
251 requestSummary(event);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800252 startSummaryMonitoring(event);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800253 } else if (type.equals("cancelSummary")) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800254 stopSummaryMonitoring();
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800255
256 } else if (type.equals("equalizeMasters")) {
257 equalizeMasters(event);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800258 }
259 }
260
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800261 // Sends the specified data to the client.
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800262 private synchronized void sendMessage(ObjectNode data) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800263 try {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800264 if (connection.isOpen()) {
265 connection.sendMessage(data.toString());
266 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800267 } catch (IOException e) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800268 log.warn("Unable to send message {} to GUI due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800269 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800270 }
271
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800272 // Sends all controller nodes to the client as node-added messages.
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800273 private void sendAllInstances(String messageType) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800274 List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
275 Collections.sort(nodes, NODE_COMPARATOR);
276 for (ControllerNode node : nodes) {
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800277 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node),
278 messageType));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800279 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800280 }
281
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800282 // Sends all devices to the client as device-added messages.
283 private void sendAllDevices() {
Thomas Vachuska82041f52014-11-30 22:14:02 -0800284 // Send optical first, others later for layered rendering
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800285 for (Device device : deviceService.getDevices()) {
Thomas Vachuska82041f52014-11-30 22:14:02 -0800286 if (device.type() == Device.Type.ROADM) {
287 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
288 }
289 }
290 for (Device device : deviceService.getDevices()) {
291 if (device.type() != Device.Type.ROADM) {
292 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
293 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800294 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800295 }
296
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800297 // Sends all links to the client as link-added messages.
298 private void sendAllLinks() {
Thomas Vachuska82041f52014-11-30 22:14:02 -0800299 // Send optical first, others later for layered rendering
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800300 for (Link link : linkService.getLinks()) {
Thomas Vachuska82041f52014-11-30 22:14:02 -0800301 if (link.type() == Link.Type.OPTICAL) {
302 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
303 }
304 }
305 for (Link link : linkService.getLinks()) {
306 if (link.type() != Link.Type.OPTICAL) {
307 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
308 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800309 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800310 }
311
312 // Sends all hosts to the client as host-added messages.
313 private void sendAllHosts() {
314 for (Host host : hostService.getHosts()) {
315 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
316 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800317 }
318
319 // Sends back device or host details.
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800320 private void requestDetails(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800321 ObjectNode payload = payload(event);
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800322 String type = string(payload, "class", "unknown");
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800323 long sid = number(event, "sid");
324
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800325 if (type.equals("device")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800326 sendMessage(deviceDetails(deviceId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800327 } else if (type.equals("host")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800328 sendMessage(hostDetails(hostId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800329 }
330 }
331
Thomas Vachuska9edca302014-11-22 17:06:42 -0800332
Thomas Vachuska4830d392014-11-09 17:09:56 -0800333 // Creates host-to-host intent.
334 private void createHostIntent(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800335 ObjectNode payload = payload(event);
336 long id = number(event, "sid");
Thomas Vachuska4830d392014-11-09 17:09:56 -0800337 // TODO: add protection against device ids and non-existent hosts.
338 HostId one = hostId(string(payload, "one"));
339 HostId two = hostId(string(payload, "two"));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800340
Thomas Vachuska9edca302014-11-22 17:06:42 -0800341 HostToHostIntent intent =
342 new HostToHostIntent(appId, one, two,
343 DefaultTrafficSelector.builder().build(),
344 DefaultTrafficTreatment.builder().build());
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800345
Thomas Vachuska9edca302014-11-22 17:06:42 -0800346 intentService.submit(intent);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800347 startMonitoringIntent(event, intent);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800348 }
349
Thomas Vachuska9edca302014-11-22 17:06:42 -0800350 // Creates multi-source-to-single-dest intent.
351 private void createMultiSourceIntent(ObjectNode event) {
352 ObjectNode payload = payload(event);
353 long id = number(event, "sid");
354 // TODO: add protection against device ids and non-existent hosts.
355 Set<HostId> src = getHostIds((ArrayNode) payload.path("src"));
356 HostId dst = hostId(string(payload, "dst"));
357 Host dstHost = hostService.getHost(dst);
358
359 Set<ConnectPoint> ingressPoints = getHostLocations(src);
360
361 // FIXME: clearly, this is not enough
362 TrafficSelector selector = DefaultTrafficSelector.builder()
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800363 .matchEthType(Ethernet.TYPE_IPV4)
Thomas Vachuska9edca302014-11-22 17:06:42 -0800364 .matchEthDst(dstHost.mac()).build();
365 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
366
367 MultiPointToSinglePointIntent intent =
368 new MultiPointToSinglePointIntent(appId, selector, treatment,
369 ingressPoints, dstHost.location());
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800370
Thomas Vachuska9edca302014-11-22 17:06:42 -0800371 intentService.submit(intent);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800372 startMonitoringIntent(event, intent);
Thomas Vachuska9edca302014-11-22 17:06:42 -0800373 }
374
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800375
376 private synchronized void startMonitoringIntent(ObjectNode event, Intent intent) {
377 selectedHosts = new HashSet<>();
378 selectedDevices = new HashSet<>();
379 selectedIntents = new ArrayList<>();
380 selectedIntents.add(intent);
381 currentIntentIndex = -1;
382 requestNextRelatedIntent(event);
383 requestSelectedIntentTraffic(event);
384 }
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 Vachuskae7591e52014-11-13 21:31:15 -0800462 String hover = string(payload, "hover");
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800463 if (haveSelectedIntents()) {
464 // Get the set of selected hosts and their intents.
465 ArrayNode ids = (ArrayNode) payload.path("ids");
466 selectedHosts = getHosts(ids);
467 selectedDevices = getDevices(ids);
468 selectedIntents = intentFilter.findPathIntents(selectedHosts, selectedDevices,
469 intentService.getIntents());
470 currentIntentIndex = -1;
471
472 // Send a message to highlight all links of all monitored intents.
473 sendMessage(trafficMessage(sid, new TrafficClass("primary", selectedIntents)));
474 }
475
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800476 if (!isNullOrEmpty(hover)) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800477 // If there is a hover node, include it in the selection and find intents.
478 processExtendedSelection(sid, hover);
479 }
480 }
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800481
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800482 private boolean haveSelectedIntents() {
483 return selectedIntents != null && !selectedIntents.isEmpty();
484 }
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800485
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800486 private void processExtendedSelection(long sid, String hover) {
487 Set<Host> hoverSelHosts = new HashSet<>(selectedHosts);
488 Set<Device> hoverSelDevices = new HashSet<>(selectedDevices);
489 addHover(hoverSelHosts, hoverSelDevices, hover);
490
491 List<Intent> primary =
492 intentFilter.findPathIntents(hoverSelHosts, hoverSelDevices,
493 selectedIntents);
494 Set<Intent> secondary = new HashSet<>(selectedIntents);
495 secondary.removeAll(primary);
496
497 // Send a message to highlight all links of all monitored intents.
498 sendMessage(trafficMessage(sid, new TrafficClass("primary", primary),
499 new TrafficClass("secondary", secondary)));
500 }
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()) {
525 Intent selectedIntent = selectedIntents.get(currentIntentIndex);
526 log.info("Requested traffic for selected {}", selectedIntent.id());
527
528 Set<Intent> primary = new HashSet<>();
529 primary.add(selectedIntent);
530
531 // Send a message to highlight all links of the selected intent.
532 sendMessage(trafficMessage(number(event, "sid"),
533 new TrafficClass("primary", primary, true)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800534 }
535 }
536
537 // Cancels sending traffic messages.
538 private void cancelTraffic(ObjectNode event) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800539 selectedIntents = null;
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800540 sendMessage(trafficMessage(number(event, "sid")));
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800541 stopTrafficMonitoring();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800542 }
543
Thomas Vachuska47635c62014-11-22 01:21:36 -0800544
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800545 private synchronized long startSummaryMonitoring(ObjectNode event) {
546 stopSummaryMonitoring();
547 summaryEvent = event;
548 summaryTask = new SummaryMonitor();
549 timer.schedule(summaryTask, SUMMARY_FREQUENCY, SUMMARY_FREQUENCY);
550 return number(event, "sid");
551 }
552
553 private synchronized void stopSummaryMonitoring() {
554 if (summaryEvent != null) {
555 summaryTask.cancel();
556 summaryTask = null;
557 summaryEvent = null;
558 }
559 }
560
Thomas Vachuska47635c62014-11-22 01:21:36 -0800561 // Subscribes for summary messages.
562 private synchronized void requestSummary(ObjectNode event) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800563 sendMessage(summmaryMessage(number(event, "sid")));
564 }
565
Thomas Vachuska47635c62014-11-22 01:21:36 -0800566
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800567 // Forces mastership role rebalancing.
568 private void equalizeMasters(ObjectNode event) {
569 directory.get(MastershipAdminService.class).balanceRoles();
570 }
571
572
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800573 // Adds all internal listeners.
574 private void addListeners() {
575 clusterService.addListener(clusterListener);
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800576 mastershipService.addListener(mastershipListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800577 deviceService.addListener(deviceListener);
578 linkService.addListener(linkListener);
579 hostService.addListener(hostListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800580 intentService.addListener(intentListener);
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800581 flowService.addListener(flowListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800582 }
583
584 // Removes all internal listeners.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800585 private synchronized void removeListeners() {
586 if (!listenersRemoved) {
587 listenersRemoved = true;
588 clusterService.removeListener(clusterListener);
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800589 mastershipService.removeListener(mastershipListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800590 deviceService.removeListener(deviceListener);
591 linkService.removeListener(linkListener);
592 hostService.removeListener(hostListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800593 intentService.removeListener(intentListener);
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800594 flowService.removeListener(flowListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800595 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800596 }
597
598 // Cluster event listener.
599 private class InternalClusterListener implements ClusterEventListener {
600 @Override
601 public void event(ClusterEvent event) {
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800602 sendMessage(instanceMessage(event, null));
603 }
604 }
605
606 // Mastership change listener
607 private class InternalMastershipListener implements MastershipListener {
608 @Override
609 public void event(MastershipEvent event) {
610 sendAllInstances("updateInstance");
611 Device device = deviceService.getDevice(event.subject());
612 sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800613 }
614 }
615
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800616 // Device event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800617 private class InternalDeviceListener implements DeviceListener {
618 @Override
619 public void event(DeviceEvent event) {
620 sendMessage(deviceMessage(event));
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800621 eventAccummulator.add(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800622 }
623 }
624
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800625 // Link event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800626 private class InternalLinkListener implements LinkListener {
627 @Override
628 public void event(LinkEvent event) {
629 sendMessage(linkMessage(event));
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800630 eventAccummulator.add(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800631 }
632 }
633
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800634 // Host event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800635 private class InternalHostListener implements HostListener {
636 @Override
637 public void event(HostEvent event) {
638 sendMessage(hostMessage(event));
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800639 eventAccummulator.add(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800640 }
641 }
642
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800643 // Intent event listener.
Thomas Vachuska4830d392014-11-09 17:09:56 -0800644 private class InternalIntentListener implements IntentListener {
645 @Override
646 public void event(IntentEvent event) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800647 if (trafficEvent != null) {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800648 requestSelectedIntentTraffic(trafficEvent);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800649 }
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800650 eventAccummulator.add(event);
651 }
652 }
653
654 // Intent event listener.
655 private class InternalFlowListener implements FlowRuleListener {
656 @Override
657 public void event(FlowRuleEvent event) {
658 eventAccummulator.add(event);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800659 }
660 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800661
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800662 // Periodic update of the traffic information
Thomas Vachuska47635c62014-11-22 01:21:36 -0800663 private class TrafficMonitor extends TimerTask {
Thomas Vachuska22e34922014-11-14 00:40:55 -0800664 @Override
665 public void run() {
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800666 try {
667 if (trafficEvent != null) {
668 String type = string(trafficEvent, "event", "unknown");
669 if (type.equals("requestAllTraffic")) {
670 requestAllTraffic(trafficEvent);
671 } else if (type.equals("requestDeviceLinkFlows")) {
672 requestDeviceLinkFlows(trafficEvent);
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800673 } else if (type.equals("requestSelectedIntentTraffic")) {
674 requestSelectedIntentTraffic(trafficEvent);
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800675 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800676 }
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800677 } catch (Exception e) {
678 log.warn("Unable to handle traffic request due to {}", e.getMessage());
Thomas Vachuska82041f52014-11-30 22:14:02 -0800679 log.warn("Boom!", e);
Thomas Vachuska22e34922014-11-14 00:40:55 -0800680 }
681 }
682 }
Thomas Vachuska47635c62014-11-22 01:21:36 -0800683
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800684 // Periodic update of the summary information
685 private class SummaryMonitor extends TimerTask {
686 @Override
687 public void run() {
688 try {
689 if (summaryEvent != null) {
690 requestSummary(summaryEvent);
691 }
692 } catch (Exception e) {
693 log.warn("Unable to handle summary request due to {}", e.getMessage());
694 }
695 }
696 }
697
Thomas Vachuska5dd52f72014-11-28 19:27:45 -0800698 // Accumulates events to drive methodic update of the summary pane.
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800699 private class InternalEventAccummulator extends AbstractEventAccumulator {
700 protected InternalEventAccummulator() {
701 super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS);
702 }
703
Thomas Vachuska47635c62014-11-22 01:21:36 -0800704 @Override
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800705 public void processEvents(List<Event> events) {
706 try {
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800707 if (summaryEvent != null) {
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800708 sendMessage(summmaryMessage(0));
709 }
710 } catch (Exception e) {
711 log.warn("Unable to handle summary request due to {}", e.getMessage());
Thomas Vachuska47635c62014-11-22 01:21:36 -0800712 }
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800713
Thomas Vachuska47635c62014-11-22 01:21:36 -0800714 }
715 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800716}
717