blob: 9e2623fce5daad31bdd689a5501ea01aae27c723 [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 Vachuskadea45ff2014-11-12 18:35:46 -080018import com.fasterxml.jackson.databind.JsonNode;
19import 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 Vachuska690e5f62014-11-09 08:26:47 -080027import org.onlab.onos.mastership.MastershipEvent;
28import org.onlab.onos.mastership.MastershipListener;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080029import org.onlab.onos.net.ConnectPoint;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080030import org.onlab.onos.net.Device;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080031import org.onlab.onos.net.Host;
32import org.onlab.onos.net.HostId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080033import org.onlab.onos.net.Link;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080034import org.onlab.onos.net.Path;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080035import org.onlab.onos.net.device.DeviceEvent;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080036import org.onlab.onos.net.device.DeviceListener;
Thomas Vachuska4830d392014-11-09 17:09:56 -080037import org.onlab.onos.net.flow.DefaultTrafficSelector;
38import org.onlab.onos.net.flow.DefaultTrafficTreatment;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080039import org.onlab.onos.net.host.HostEvent;
40import org.onlab.onos.net.host.HostListener;
Thomas Vachuska4830d392014-11-09 17:09:56 -080041import org.onlab.onos.net.intent.HostToHostIntent;
42import org.onlab.onos.net.intent.Intent;
43import org.onlab.onos.net.intent.IntentEvent;
Thomas Vachuska4830d392014-11-09 17:09:56 -080044import org.onlab.onos.net.intent.IntentListener;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080045import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Thomas Vachuska4830d392014-11-09 17:09:56 -080046import org.onlab.onos.net.intent.PathIntent;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080047import org.onlab.onos.net.intent.PointToPointIntent;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080048import org.onlab.onos.net.link.LinkEvent;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080049import org.onlab.onos.net.link.LinkListener;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080050import org.onlab.osgi.ServiceDirectory;
51
52import java.io.IOException;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080053import java.util.HashSet;
Thomas Vachuska4830d392014-11-09 17:09:56 -080054import java.util.List;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080055import java.util.Map;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080056import java.util.Set;
Thomas Vachuska4830d392014-11-09 17:09:56 -080057import java.util.concurrent.ConcurrentHashMap;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080058
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080059import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080060import static org.onlab.onos.net.DeviceId.deviceId;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080061import static org.onlab.onos.net.HostId.hostId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080062import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED;
Thomas Vachuska4830d392014-11-09 17:09:56 -080063import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080064import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080065
Thomas Vachuska7d638d32014-11-07 10:24:43 -080066/**
67 * Web socket capable of interacting with the GUI topology view.
68 */
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080069public class TopologyWebSocket
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080070 extends TopologyMessages
71 implements WebSocket.OnTextMessage, WebSocket.OnControl {
72
73 private static final long MAX_AGE_MS = 15000;
74
75 private static final byte PING = 0x9;
76 private static final byte PONG = 0xA;
77 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
Thomas Vachuska7d638d32014-11-07 10:24:43 -080078
Thomas Vachuska4830d392014-11-09 17:09:56 -080079 private static final String APP_ID = "org.onlab.onos.gui";
Thomas Vachuska4830d392014-11-09 17:09:56 -080080
81 private final ApplicationId appId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080082
Thomas Vachuska7d638d32014-11-07 10:24:43 -080083 private Connection connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080084 private FrameConnection control;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080085
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080086 private final ClusterEventListener clusterListener = new InternalClusterListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -080087 private final DeviceListener deviceListener = new InternalDeviceListener();
88 private final LinkListener linkListener = new InternalLinkListener();
89 private final HostListener hostListener = new InternalHostListener();
90 private final MastershipListener mastershipListener = new InternalMastershipListener();
Thomas Vachuska4830d392014-11-09 17:09:56 -080091 private final IntentListener intentListener = new InternalIntentListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -080092
Thomas Vachuska4830d392014-11-09 17:09:56 -080093 // Intents that are being monitored for the GUI
Thomas Vachuska3266abf2014-11-13 09:28:46 -080094 private Map<Intent, Long> intentsToMonitor = new ConcurrentHashMap<>();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080095
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080096 private long lastActive = System.currentTimeMillis();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080097 private boolean listenersRemoved = false;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080098
Thomas Vachuska7d638d32014-11-07 10:24:43 -080099 /**
100 * Creates a new web-socket for serving data to GUI topology view.
101 *
102 * @param directory service directory
103 */
104 public TopologyWebSocket(ServiceDirectory directory) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800105 super(directory);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800106 appId = directory.get(CoreService.class).registerApplication(APP_ID);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800107 }
108
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800109 /**
110 * Issues a close on the connection.
111 */
112 synchronized void close() {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800113 removeListeners();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800114 if (connection.isOpen()) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800115 connection.close();
116 }
117 }
118
119 /**
120 * Indicates if this connection is idle.
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800121 *
122 * @return true if idle or closed
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800123 */
124 synchronized boolean isIdle() {
125 boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS;
126 if (idle || !connection.isOpen()) {
127 return true;
128 }
129 try {
130 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
131 } catch (IOException e) {
132 log.warn("Unable to send ping message due to: ", e);
133 }
134 return false;
135 }
136
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800137 @Override
138 public void onOpen(Connection connection) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800139 log.info("GUI client connected");
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800140 this.connection = connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800141 this.control = (FrameConnection) connection;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800142 addListeners();
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800143
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800144 sendAllInstances();
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800145 sendAllDevices();
146 sendAllLinks();
Thomas Vachuska4830d392014-11-09 17:09:56 -0800147 sendAllHosts();
148 }
149
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800150 @Override
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800151 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800152 removeListeners();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800153 log.info("GUI client disconnected");
154 }
155
156 @Override
157 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
158 lastActive = System.currentTimeMillis();
159 return true;
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800160 }
161
162 @Override
163 public void onMessage(String data) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800164 lastActive = System.currentTimeMillis();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800165 try {
166 ObjectNode event = (ObjectNode) mapper.reader().readTree(data);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800167 String type = string(event, "event", "unknown");
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800168 if (type.equals("requestDetails")) {
169 requestDetails(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800170 } else if (type.equals("updateMeta")) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800171 updateMetaUi(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800172 } else if (type.equals("requestPath")) {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800173 createHostIntent(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800174 } else if (type.equals("requestTraffic")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800175 requestTraffic(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800176 } else if (type.equals("cancelTraffic")) {
177 cancelTraffic(event);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800178 }
Thomas Vachuska4830d392014-11-09 17:09:56 -0800179 } catch (Exception e) {
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800180 log.warn("Unable to parse GUI request {} due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800181 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800182 }
183
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800184 // Sends the specified data to the client.
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800185 private synchronized void sendMessage(ObjectNode data) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800186 try {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800187 if (connection.isOpen()) {
188 connection.sendMessage(data.toString());
189 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800190 } catch (IOException e) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800191 log.warn("Unable to send message {} to GUI due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800192 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800193 }
194
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800195 // Sends all controller nodes to the client as node-added messages.
196 private void sendAllInstances() {
197 for (ControllerNode node : clusterService.getNodes()) {
198 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800199 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800200 }
201
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800202 // Sends all devices to the client as device-added messages.
203 private void sendAllDevices() {
204 for (Device device : deviceService.getDevices()) {
205 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800206 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800207 }
208
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800209 // Sends all links to the client as link-added messages.
210 private void sendAllLinks() {
211 for (Link link : linkService.getLinks()) {
212 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800213 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800214 }
215
216 // Sends all hosts to the client as host-added messages.
217 private void sendAllHosts() {
218 for (Host host : hostService.getHosts()) {
219 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
220 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800221 }
222
223 // Sends back device or host details.
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800224 private void requestDetails(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800225 ObjectNode payload = payload(event);
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800226 String type = string(payload, "class", "unknown");
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800227 long sid = number(event, "sid");
228
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800229 if (type.equals("device")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800230 sendMessage(deviceDetails(deviceId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800231 } else if (type.equals("host")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800232 sendMessage(hostDetails(hostId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800233 }
234 }
235
Thomas Vachuska4830d392014-11-09 17:09:56 -0800236 // Creates host-to-host intent.
237 private void createHostIntent(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800238 ObjectNode payload = payload(event);
239 long id = number(event, "sid");
Thomas Vachuska4830d392014-11-09 17:09:56 -0800240 // TODO: add protection against device ids and non-existent hosts.
241 HostId one = hostId(string(payload, "one"));
242 HostId two = hostId(string(payload, "two"));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800243
Thomas Vachuska4830d392014-11-09 17:09:56 -0800244 HostToHostIntent hostIntent = new HostToHostIntent(appId, one, two,
245 DefaultTrafficSelector.builder().build(),
246 DefaultTrafficTreatment.builder().build());
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800247 intentsToMonitor.put(hostIntent, number(event, "sid"));
Thomas Vachuska4830d392014-11-09 17:09:56 -0800248 intentService.submit(hostIntent);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800249 }
250
251 // Sends traffic message.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800252 private void requestTraffic(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800253 ObjectNode payload = payload(event);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800254 long sid = number(event, "sid");
255 Set<Intent> intents = findPathIntents(payload);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800256
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800257 // Add all those intents to the list of monitored intents & flows.
258 intentsToMonitor.clear();
259 for (Intent intent : intents) {
260 intentsToMonitor.put(intent, sid);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800261 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800262
263 // Send an initial message to highlight all links of all monitored intents.
264 sendMessage(trafficMessage(intents, sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800265 }
266
267 // Cancels sending traffic messages.
268 private void cancelTraffic(ObjectNode event) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800269 ObjectNode payload = payload(event);
270 long sid = number(event, "sid");
271 Set<Intent> intents = findPathIntents(payload);
272
273 // Remove all those intents from the list of monitored intents & flows.
274 intentsToMonitor.clear(); // TODO: remove when ready
275 for (Intent intent : intents) {
276 intentsToMonitor.remove(intent.id());
277 }
278 sendMessage(trafficMessage(intents, sid));
279 }
280
281 // Finds all path (host-to-host or point-to-point) intents that pertains
282 // to the hosts indicated in the given event payload.
283 private Set<Intent> findPathIntents(ObjectNode payload) {
284 // Get the list of selected hosts.
285 Set<Host> hosts = getHosts((ArrayNode) payload.path("ids"));
286
287 // Derive from this the set of edge connect points.
288 Set<ConnectPoint> edgePoints = getEdgePoints(hosts);
289
290 // Iterate over all intents and produce a set that contains only those
291 // intents that target all selected hosts or derived edge connect points.
292 return getIntents(hosts, edgePoints);
293 }
294
295 // Produces a set of intents that target all selected hosts or connect points.
296 private Set<Intent> getIntents(Set<Host> hosts, Set<ConnectPoint> edgePoints) {
297 Set<Intent> intents = new HashSet<>();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800298 if (hosts.isEmpty()) {
299 return intents;
300 }
301
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800302 for (Intent intent : intentService.getIntents()) {
303 boolean isRelevant = false;
304 if (intent instanceof HostToHostIntent) {
305 isRelevant = isIntentRelevant((HostToHostIntent) intent, hosts);
306 } else if (intent instanceof PointToPointIntent) {
307 isRelevant = isIntentRelevant((PointToPointIntent) intent, edgePoints);
308 } else if (intent instanceof MultiPointToSinglePointIntent) {
309 isRelevant = isIntentRelevant((MultiPointToSinglePointIntent) intent, edgePoints);
310 }
311 // TODO: add other intents, e.g. SinglePointToMultiPointIntent
312
313 if (isRelevant) {
314 intents.add(intent);
315 }
316 }
317 return intents;
318 }
319
320 // Indicates whether the specified intent involves all of the given hosts.
321 private boolean isIntentRelevant(HostToHostIntent intent, Set<Host> hosts) {
322 for (Host host : hosts) {
323 HostId id = host.id();
324 // Bail if intent does not involve this host.
325 if (!id.equals(intent.one()) && !id.equals(intent.two())) {
326 return false;
327 }
328 }
329 return true;
330 }
331
332 // Indicates whether the specified intent involves all of the given edge points.
333 private boolean isIntentRelevant(PointToPointIntent intent,
334 Set<ConnectPoint> edgePoints) {
335 for (ConnectPoint point : edgePoints) {
336 // Bail if intent does not involve this edge point.
337 if (!point.equals(intent.egressPoint()) &&
338 !point.equals(intent.ingressPoint())) {
339 return false;
340 }
341 }
342 return true;
343 }
344
345 // Indicates whether the specified intent involves all of the given edge points.
346 private boolean isIntentRelevant(MultiPointToSinglePointIntent intent,
347 Set<ConnectPoint> edgePoints) {
348 for (ConnectPoint point : edgePoints) {
349 // Bail if intent does not involve this edge point.
350 if (!point.equals(intent.egressPoint()) &&
351 !intent.ingressPoints().contains(point)) {
352 return false;
353 }
354 }
355 return true;
356 }
357
358
359 // Produces a set of all host ids listed in the specified JSON array.
360 private Set<Host> getHosts(ArrayNode array) {
361 Set<Host> hosts = new HashSet<>();
362 for (JsonNode node : array) {
363 try {
364 Host host = hostService.getHost(hostId(node.asText()));
365 if (host != null) {
366 hosts.add(host);
367 }
368 } catch (IllegalArgumentException e) {
369 log.debug("Skipping ID {}", node.asText());
370 }
371 }
372 return hosts;
373 }
374
375 // Produces a set of edge points from the specified set of hosts.
376 private Set<ConnectPoint> getEdgePoints(Set<Host> hosts) {
377 Set<ConnectPoint> edgePoints = new HashSet<>();
378 for (Host host : hosts) {
379 edgePoints.add(host.location());
380 }
381 return edgePoints;
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800382 }
383
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800384
385 // Adds all internal listeners.
386 private void addListeners() {
387 clusterService.addListener(clusterListener);
388 deviceService.addListener(deviceListener);
389 linkService.addListener(linkListener);
390 hostService.addListener(hostListener);
391 mastershipService.addListener(mastershipListener);
392 intentService.addListener(intentListener);
393 }
394
395 // Removes all internal listeners.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800396 private synchronized void removeListeners() {
397 if (!listenersRemoved) {
398 listenersRemoved = true;
399 clusterService.removeListener(clusterListener);
400 deviceService.removeListener(deviceListener);
401 linkService.removeListener(linkListener);
402 hostService.removeListener(hostListener);
403 mastershipService.removeListener(mastershipListener);
404 intentService.removeListener(intentListener);
405 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800406 }
407
408 // Cluster event listener.
409 private class InternalClusterListener implements ClusterEventListener {
410 @Override
411 public void event(ClusterEvent event) {
412 sendMessage(instanceMessage(event));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800413 }
414 }
415
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800416 // Device event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800417 private class InternalDeviceListener implements DeviceListener {
418 @Override
419 public void event(DeviceEvent event) {
420 sendMessage(deviceMessage(event));
421 }
422 }
423
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800424 // Link event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800425 private class InternalLinkListener implements LinkListener {
426 @Override
427 public void event(LinkEvent event) {
428 sendMessage(linkMessage(event));
429 }
430 }
431
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800432 // Host event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800433 private class InternalHostListener implements HostListener {
434 @Override
435 public void event(HostEvent event) {
436 sendMessage(hostMessage(event));
437 }
438 }
439
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800440 // Mastership event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800441 private class InternalMastershipListener implements MastershipListener {
442 @Override
443 public void event(MastershipEvent event) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800444 // TODO: Is DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED the same?
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800445
Thomas Vachuskad472c6e2014-11-07 19:11:05 -0800446 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800447 }
Thomas Vachuska4830d392014-11-09 17:09:56 -0800448
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800449 // Intent event listener.
Thomas Vachuska4830d392014-11-09 17:09:56 -0800450 private class InternalIntentListener implements IntentListener {
451 @Override
452 public void event(IntentEvent event) {
453 Intent intent = event.subject();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800454 Long sid = intentsToMonitor.get(intent);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800455 if (sid != null) {
456 List<Intent> installable = intentService.getInstallableIntents(intent.id());
457 if (installable != null && !installable.isEmpty()) {
458 PathIntent pathIntent = (PathIntent) installable.iterator().next();
459 Path path = pathIntent.path();
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800460 ObjectNode payload = pathMessage(path, "host")
461 .put("intentId", intent.id().toString());
Thomas Vachuska4830d392014-11-09 17:09:56 -0800462 sendMessage(envelope("showPath", sid, payload));
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800463 sendMessage(trafficMessage(intentsToMonitor.keySet(), sid));
Thomas Vachuska4830d392014-11-09 17:09:56 -0800464 }
465 }
466 }
467 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800468
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800469}
470