blob: 7477e605dbcbbcc71534d128af967e4df7c495e4 [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 Vachuska5bde31f2014-11-25 15:29:18 -080093 private static final long TRAFFIC_FREQUENCY_SEC = 2000;
Thomas Vachuska22e34922014-11-14 00:40:55 -080094
Thomas Vachuska47635c62014-11-22 01:21:36 -080095 private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
96 new Comparator<ControllerNode>() {
97 @Override
98 public int compare(ControllerNode o1, ControllerNode o2) {
99 return o1.id().toString().compareTo(o2.id().toString());
100 }
101 };
102
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800103
104 private final Timer timer = new Timer("topology-view");
105
106 private static final int MAX_EVENTS = 500;
107 private static final int MAX_BATCH_MS = 1000;
108 private static final int MAX_IDLE_MS = 500;
109
Thomas Vachuska4830d392014-11-09 17:09:56 -0800110 private final ApplicationId appId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -0800111
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800112 private Connection connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800113 private FrameConnection control;
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800114
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800115 private final ClusterEventListener clusterListener = new InternalClusterListener();
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800116 private final MastershipListener mastershipListener = new InternalMastershipListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800117 private final DeviceListener deviceListener = new InternalDeviceListener();
118 private final LinkListener linkListener = new InternalLinkListener();
119 private final HostListener hostListener = new InternalHostListener();
Thomas Vachuska4830d392014-11-09 17:09:56 -0800120 private final IntentListener intentListener = new InternalIntentListener();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800121 private final FlowRuleListener flowListener = new InternalFlowListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800122
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800123 private final EventAccumulator eventAccummulator = new InternalEventAccummulator();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800124
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800125 private boolean summaryEnabled = true;
Thomas Vachuska47635c62014-11-22 01:21:36 -0800126 private TimerTask trafficTask;
127 private ObjectNode trafficEvent;
128
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800129 private long lastActive = System.currentTimeMillis();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800130 private boolean listenersRemoved = false;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800131
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800132 private TopologyViewIntentFilter intentFilter;
133
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800134 /**
135 * Creates a new web-socket for serving data to GUI topology view.
136 *
137 * @param directory service directory
138 */
Thomas Vachuska7c27ad72014-11-14 16:20:10 -0800139 public TopologyViewWebSocket(ServiceDirectory directory) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800140 super(directory);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800141 intentFilter = new TopologyViewIntentFilter(intentService, deviceService,
Thomas Vachuska9edca302014-11-22 17:06:42 -0800142 hostService, linkService);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800143 appId = directory.get(CoreService.class).registerApplication(APP_ID);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800144 }
145
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800146 /**
147 * Issues a close on the connection.
148 */
149 synchronized void close() {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800150 removeListeners();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800151 if (connection.isOpen()) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800152 connection.close();
153 }
154 }
155
156 /**
157 * Indicates if this connection is idle.
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800158 *
159 * @return true if idle or closed
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800160 */
161 synchronized boolean isIdle() {
162 boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS;
163 if (idle || !connection.isOpen()) {
164 return true;
165 }
166 try {
167 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
168 } catch (IOException e) {
169 log.warn("Unable to send ping message due to: ", e);
170 }
171 return false;
172 }
173
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800174 @Override
175 public void onOpen(Connection connection) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800176 log.info("GUI client connected");
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800177 this.connection = connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800178 this.control = (FrameConnection) connection;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800179 addListeners();
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800180
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800181 sendAllInstances(null);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800182 sendAllDevices();
183 sendAllLinks();
Thomas Vachuska4830d392014-11-09 17:09:56 -0800184 sendAllHosts();
185 }
186
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800187 @Override
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800188 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800189 removeListeners();
Thomas Vachuska22e34922014-11-14 00:40:55 -0800190 timer.cancel();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800191 log.info("GUI client disconnected");
192 }
193
194 @Override
195 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
196 lastActive = System.currentTimeMillis();
197 return true;
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800198 }
199
200 @Override
201 public void onMessage(String data) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800202 lastActive = System.currentTimeMillis();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800203 try {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800204 processMessage((ObjectNode) mapper.reader().readTree(data));
Thomas Vachuska4830d392014-11-09 17:09:56 -0800205 } catch (Exception e) {
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800206 log.warn("Unable to parse GUI request {} due to {}", data, e);
Thomas Vachuska29617e52014-11-20 03:17:46 -0800207 log.warn("Boom!!!!", e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800208 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800209 }
210
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800211 // Processes the specified event.
212 private void processMessage(ObjectNode event) {
213 String type = string(event, "event", "unknown");
214 if (type.equals("requestDetails")) {
215 requestDetails(event);
216 } else if (type.equals("updateMeta")) {
217 updateMetaUi(event);
Thomas Vachuska9edca302014-11-22 17:06:42 -0800218
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800219 } else if (type.equals("addHostIntent")) {
220 createHostIntent(event);
Thomas Vachuska9edca302014-11-22 17:06:42 -0800221 } else if (type.equals("addMultiSourceIntent")) {
222 createMultiSourceIntent(event);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800223
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800224 } else if (type.equals("requestTraffic")) {
225 requestTraffic(event);
226 } else if (type.equals("requestAllTraffic")) {
227 requestAllTraffic(event);
Thomas Vachuska29617e52014-11-20 03:17:46 -0800228 } else if (type.equals("requestDeviceLinkFlows")) {
229 requestDeviceLinkFlows(event);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800230 } else if (type.equals("cancelTraffic")) {
231 cancelTraffic(event);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800232
233 } else if (type.equals("requestSummary")) {
234 requestSummary(event);
235 } else if (type.equals("cancelSummary")) {
236 cancelSummary(event);
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800237
238 } else if (type.equals("equalizeMasters")) {
239 equalizeMasters(event);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800240 }
241 }
242
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800243 // Sends the specified data to the client.
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800244 private synchronized void sendMessage(ObjectNode data) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800245 try {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800246 if (connection.isOpen()) {
247 connection.sendMessage(data.toString());
248 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800249 } catch (IOException e) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800250 log.warn("Unable to send message {} to GUI due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800251 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800252 }
253
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800254 // Sends all controller nodes to the client as node-added messages.
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800255 private void sendAllInstances(String messageType) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800256 List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
257 Collections.sort(nodes, NODE_COMPARATOR);
258 for (ControllerNode node : nodes) {
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800259 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node),
260 messageType));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800261 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800262 }
263
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800264 // Sends all devices to the client as device-added messages.
265 private void sendAllDevices() {
266 for (Device device : deviceService.getDevices()) {
267 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800268 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800269 }
270
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800271 // Sends all links to the client as link-added messages.
272 private void sendAllLinks() {
273 for (Link link : linkService.getLinks()) {
274 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800275 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800276 }
277
278 // Sends all hosts to the client as host-added messages.
279 private void sendAllHosts() {
280 for (Host host : hostService.getHosts()) {
281 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
282 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800283 }
284
285 // Sends back device or host details.
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800286 private void requestDetails(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800287 ObjectNode payload = payload(event);
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800288 String type = string(payload, "class", "unknown");
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800289 long sid = number(event, "sid");
290
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800291 if (type.equals("device")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800292 sendMessage(deviceDetails(deviceId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800293 } else if (type.equals("host")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800294 sendMessage(hostDetails(hostId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800295 }
296 }
297
Thomas Vachuska9edca302014-11-22 17:06:42 -0800298
Thomas Vachuska4830d392014-11-09 17:09:56 -0800299 // Creates host-to-host intent.
300 private void createHostIntent(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800301 ObjectNode payload = payload(event);
302 long id = number(event, "sid");
Thomas Vachuska4830d392014-11-09 17:09:56 -0800303 // TODO: add protection against device ids and non-existent hosts.
304 HostId one = hostId(string(payload, "one"));
305 HostId two = hostId(string(payload, "two"));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800306
Thomas Vachuska9edca302014-11-22 17:06:42 -0800307 HostToHostIntent intent =
308 new HostToHostIntent(appId, one, two,
309 DefaultTrafficSelector.builder().build(),
310 DefaultTrafficTreatment.builder().build());
311 startMonitoring(event);
312 intentService.submit(intent);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800313 }
314
Thomas Vachuska9edca302014-11-22 17:06:42 -0800315 // Creates multi-source-to-single-dest intent.
316 private void createMultiSourceIntent(ObjectNode event) {
317 ObjectNode payload = payload(event);
318 long id = number(event, "sid");
319 // TODO: add protection against device ids and non-existent hosts.
320 Set<HostId> src = getHostIds((ArrayNode) payload.path("src"));
321 HostId dst = hostId(string(payload, "dst"));
322 Host dstHost = hostService.getHost(dst);
323
324 Set<ConnectPoint> ingressPoints = getHostLocations(src);
325
326 // FIXME: clearly, this is not enough
327 TrafficSelector selector = DefaultTrafficSelector.builder()
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800328 .matchEthType(Ethernet.TYPE_IPV4)
Thomas Vachuska9edca302014-11-22 17:06:42 -0800329 .matchEthDst(dstHost.mac()).build();
330 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
331
332 MultiPointToSinglePointIntent intent =
333 new MultiPointToSinglePointIntent(appId, selector, treatment,
334 ingressPoints, dstHost.location());
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800335 startMonitoring(event);
Thomas Vachuska9edca302014-11-22 17:06:42 -0800336 intentService.submit(intent);
337 }
338
339 private Set<ConnectPoint> getHostLocations(Set<HostId> hostIds) {
340 Set<ConnectPoint> points = new HashSet<>();
341 for (HostId hostId : hostIds) {
342 points.add(getHostLocation(hostId));
Thomas Vachuska47635c62014-11-22 01:21:36 -0800343 }
Thomas Vachuska9edca302014-11-22 17:06:42 -0800344 return points;
345 }
346
347 private HostLocation getHostLocation(HostId hostId) {
348 return hostService.getHost(hostId).location();
349 }
350
351 // Produces a list of host ids from the specified JSON array.
352 private Set<HostId> getHostIds(ArrayNode ids) {
353 Set<HostId> hostIds = new HashSet<>();
354 for (JsonNode id : ids) {
355 hostIds.add(hostId(id.asText()));
356 }
357 return hostIds;
358 }
359
360
361 private synchronized long startMonitoring(ObjectNode event) {
362 if (trafficTask != null) {
363 stopMonitoring();
364 }
365 trafficEvent = event;
366 trafficTask = new TrafficMonitor();
367 timer.schedule(trafficTask, TRAFFIC_FREQUENCY_SEC, TRAFFIC_FREQUENCY_SEC);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800368 return number(event, "sid");
369 }
370
371 private synchronized void stopMonitoring() {
372 if (trafficTask != null) {
373 trafficTask.cancel();
374 trafficTask = null;
375 trafficEvent = null;
376 }
377 }
378
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800379 // Subscribes for host traffic messages.
380 private synchronized void requestAllTraffic(ObjectNode event) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800381 long sid = startMonitoring(event);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800382 sendMessage(trafficSummaryMessage(sid));
383 }
384
Thomas Vachuska29617e52014-11-20 03:17:46 -0800385 private void requestDeviceLinkFlows(ObjectNode event) {
386 ObjectNode payload = payload(event);
Thomas Vachuska47635c62014-11-22 01:21:36 -0800387 long sid = startMonitoring(event);
Thomas Vachuska29617e52014-11-20 03:17:46 -0800388
389 // Get the set of selected hosts and their intents.
390 ArrayNode ids = (ArrayNode) payload.path("ids");
391 Set<Host> hosts = new HashSet<>();
392 Set<Device> devices = getDevices(ids);
393
394 // If there is a hover node, include it in the hosts and find intents.
395 String hover = string(payload, "hover");
Thomas Vachuska29617e52014-11-20 03:17:46 -0800396 if (!isNullOrEmpty(hover)) {
397 addHover(hosts, devices, hover);
398 }
399 sendMessage(flowSummaryMessage(sid, devices));
400 }
401
402
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800403 // Subscribes for host traffic messages.
Thomas Vachuska22e34922014-11-14 00:40:55 -0800404 private synchronized void requestTraffic(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800405 ObjectNode payload = payload(event);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800406 if (!payload.has("ids")) {
407 return;
408 }
409
Thomas Vachuska47635c62014-11-22 01:21:36 -0800410 long sid = startMonitoring(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800411
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800412 // Get the set of selected hosts and their intents.
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800413 ArrayNode ids = (ArrayNode) payload.path("ids");
414 Set<Host> hosts = getHosts(ids);
415 Set<Device> devices = getDevices(ids);
416 Set<Intent> intents = intentFilter.findPathIntents(hosts, devices);
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800417
418 // If there is a hover node, include it in the hosts and find intents.
419 String hover = string(payload, "hover");
420 Set<Intent> hoverIntents;
421 if (!isNullOrEmpty(hover)) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800422 addHover(hosts, devices, hover);
423 hoverIntents = intentFilter.findPathIntents(hosts, devices);
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800424 intents.removeAll(hoverIntents);
425
426 // Send an initial message to highlight all links of all monitored intents.
427 sendMessage(trafficMessage(sid,
428 new TrafficClass("primary", hoverIntents),
429 new TrafficClass("secondary", intents)));
430
431 } else {
432 // Send an initial message to highlight all links of all monitored intents.
433 sendMessage(trafficMessage(sid, new TrafficClass("primary", intents)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800434 }
435 }
436
437 // Cancels sending traffic messages.
438 private void cancelTraffic(ObjectNode event) {
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800439 sendMessage(trafficMessage(number(event, "sid")));
Thomas Vachuska47635c62014-11-22 01:21:36 -0800440 stopMonitoring();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800441 }
442
Thomas Vachuska47635c62014-11-22 01:21:36 -0800443
444 // Subscribes for summary messages.
445 private synchronized void requestSummary(ObjectNode event) {
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800446 summaryEnabled = true;
Thomas Vachuska47635c62014-11-22 01:21:36 -0800447 sendMessage(summmaryMessage(number(event, "sid")));
448 }
449
450 // Cancels sending summary messages.
451 private synchronized void cancelSummary(ObjectNode event) {
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800452 summaryEnabled = false;
Thomas Vachuska47635c62014-11-22 01:21:36 -0800453 }
454
455
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800456 // Forces mastership role rebalancing.
457 private void equalizeMasters(ObjectNode event) {
458 directory.get(MastershipAdminService.class).balanceRoles();
459 }
460
461
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800462 // Adds all internal listeners.
463 private void addListeners() {
464 clusterService.addListener(clusterListener);
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800465 mastershipService.addListener(mastershipListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800466 deviceService.addListener(deviceListener);
467 linkService.addListener(linkListener);
468 hostService.addListener(hostListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800469 intentService.addListener(intentListener);
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800470 flowService.addListener(flowListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800471 }
472
473 // Removes all internal listeners.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800474 private synchronized void removeListeners() {
475 if (!listenersRemoved) {
476 listenersRemoved = true;
477 clusterService.removeListener(clusterListener);
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800478 mastershipService.removeListener(mastershipListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800479 deviceService.removeListener(deviceListener);
480 linkService.removeListener(linkListener);
481 hostService.removeListener(hostListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800482 intentService.removeListener(intentListener);
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800483 flowService.removeListener(flowListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800484 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800485 }
486
487 // Cluster event listener.
488 private class InternalClusterListener implements ClusterEventListener {
489 @Override
490 public void event(ClusterEvent event) {
Thomas Vachuskae02e11c2014-11-24 16:13:52 -0800491 sendMessage(instanceMessage(event, null));
492 }
493 }
494
495 // Mastership change listener
496 private class InternalMastershipListener implements MastershipListener {
497 @Override
498 public void event(MastershipEvent event) {
499 sendAllInstances("updateInstance");
500 Device device = deviceService.getDevice(event.subject());
501 sendMessage(deviceMessage(new DeviceEvent(DEVICE_UPDATED, device)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800502 }
503 }
504
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800505 // Device event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800506 private class InternalDeviceListener implements DeviceListener {
507 @Override
508 public void event(DeviceEvent event) {
509 sendMessage(deviceMessage(event));
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800510 eventAccummulator.add(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800511 }
512 }
513
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800514 // Link event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800515 private class InternalLinkListener implements LinkListener {
516 @Override
517 public void event(LinkEvent event) {
518 sendMessage(linkMessage(event));
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800519 eventAccummulator.add(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800520 }
521 }
522
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800523 // Host event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800524 private class InternalHostListener implements HostListener {
525 @Override
526 public void event(HostEvent event) {
527 sendMessage(hostMessage(event));
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800528 eventAccummulator.add(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800529 }
530 }
531
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800532 // Intent event listener.
Thomas Vachuska4830d392014-11-09 17:09:56 -0800533 private class InternalIntentListener implements IntentListener {
534 @Override
535 public void event(IntentEvent event) {
Thomas Vachuska47635c62014-11-22 01:21:36 -0800536 if (trafficEvent != null) {
537 requestTraffic(trafficEvent);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800538 }
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800539 eventAccummulator.add(event);
540 }
541 }
542
543 // Intent event listener.
544 private class InternalFlowListener implements FlowRuleListener {
545 @Override
546 public void event(FlowRuleEvent event) {
547 eventAccummulator.add(event);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800548 }
549 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800550
Thomas Vachuska47635c62014-11-22 01:21:36 -0800551 private class TrafficMonitor extends TimerTask {
Thomas Vachuska22e34922014-11-14 00:40:55 -0800552 @Override
553 public void run() {
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800554 try {
555 if (trafficEvent != null) {
556 String type = string(trafficEvent, "event", "unknown");
557 if (type.equals("requestAllTraffic")) {
558 requestAllTraffic(trafficEvent);
559 } else if (type.equals("requestDeviceLinkFlows")) {
560 requestDeviceLinkFlows(trafficEvent);
561 } else {
562 requestTraffic(trafficEvent);
563 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800564 }
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800565 } catch (Exception e) {
566 log.warn("Unable to handle traffic request due to {}", e.getMessage());
Thomas Vachuska22e34922014-11-14 00:40:55 -0800567 }
568 }
569 }
Thomas Vachuska47635c62014-11-22 01:21:36 -0800570
Thomas Vachuska5dd52f72014-11-28 19:27:45 -0800571 // Accumulates events to drive methodic update of the summary pane.
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800572 private class InternalEventAccummulator extends AbstractEventAccumulator {
573 protected InternalEventAccummulator() {
574 super(new Timer("topo-summary"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS);
575 }
576
Thomas Vachuska47635c62014-11-22 01:21:36 -0800577 @Override
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800578 public void processEvents(List<Event> events) {
579 try {
580 if (summaryEnabled) {
581 sendMessage(summmaryMessage(0));
582 }
583 } catch (Exception e) {
584 log.warn("Unable to handle summary request due to {}", e.getMessage());
Thomas Vachuska47635c62014-11-22 01:21:36 -0800585 }
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800586
Thomas Vachuska47635c62014-11-22 01:21:36 -0800587 }
588 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800589}
590