blob: 46bc930501d16c990cf2ab6210c931ab945ac2f3 [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 Vachuskadea45ff2014-11-12 18:35:46 -080094 private static 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.
121 */
122 synchronized boolean isIdle() {
123 boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS;
124 if (idle || !connection.isOpen()) {
125 return true;
126 }
127 try {
128 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
129 } catch (IOException e) {
130 log.warn("Unable to send ping message due to: ", e);
131 }
132 return false;
133 }
134
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800135 @Override
136 public void onOpen(Connection connection) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800137 log.info("GUI client connected");
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800138 this.connection = connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800139 this.control = (FrameConnection) connection;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800140 addListeners();
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800141
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800142 sendAllInstances();
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800143 sendAllDevices();
144 sendAllLinks();
Thomas Vachuska4830d392014-11-09 17:09:56 -0800145 sendAllHosts();
146 }
147
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800148 @Override
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800149 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800150 removeListeners();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800151 log.info("GUI client disconnected");
152 }
153
154 @Override
155 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
156 lastActive = System.currentTimeMillis();
157 return true;
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800158 }
159
160 @Override
161 public void onMessage(String data) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800162 lastActive = System.currentTimeMillis();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800163 try {
164 ObjectNode event = (ObjectNode) mapper.reader().readTree(data);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800165 String type = string(event, "event", "unknown");
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800166 if (type.equals("requestDetails")) {
167 requestDetails(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800168 } else if (type.equals("updateMeta")) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800169 updateMetaUi(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800170 } else if (type.equals("requestPath")) {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800171 createHostIntent(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800172 } else if (type.equals("requestTraffic")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800173 requestTraffic(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800174 } else if (type.equals("cancelTraffic")) {
175 cancelTraffic(event);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800176 }
Thomas Vachuska4830d392014-11-09 17:09:56 -0800177 } catch (Exception e) {
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800178 log.warn("Unable to parse GUI request {} due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800179 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800180 }
181
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800182 // Sends the specified data to the client.
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800183 private synchronized void sendMessage(ObjectNode data) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800184 try {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800185 if (connection.isOpen()) {
186 connection.sendMessage(data.toString());
187 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800188 } catch (IOException e) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800189 log.warn("Unable to send message {} to GUI due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800190 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800191 }
192
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800193 // Sends all controller nodes to the client as node-added messages.
194 private void sendAllInstances() {
195 for (ControllerNode node : clusterService.getNodes()) {
196 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800197 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800198 }
199
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800200 // Sends all devices to the client as device-added messages.
201 private void sendAllDevices() {
202 for (Device device : deviceService.getDevices()) {
203 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800204 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800205 }
206
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800207 // Sends all links to the client as link-added messages.
208 private void sendAllLinks() {
209 for (Link link : linkService.getLinks()) {
210 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800211 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800212 }
213
214 // Sends all hosts to the client as host-added messages.
215 private void sendAllHosts() {
216 for (Host host : hostService.getHosts()) {
217 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
218 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800219 }
220
221 // Sends back device or host details.
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800222 private void requestDetails(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800223 ObjectNode payload = payload(event);
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800224 String type = string(payload, "class", "unknown");
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800225 long sid = number(event, "sid");
226
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800227 if (type.equals("device")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800228 sendMessage(deviceDetails(deviceId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800229 } else if (type.equals("host")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800230 sendMessage(hostDetails(hostId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800231 }
232 }
233
Thomas Vachuska4830d392014-11-09 17:09:56 -0800234 // Creates host-to-host intent.
235 private void createHostIntent(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800236 ObjectNode payload = payload(event);
237 long id = number(event, "sid");
Thomas Vachuska4830d392014-11-09 17:09:56 -0800238 // TODO: add protection against device ids and non-existent hosts.
239 HostId one = hostId(string(payload, "one"));
240 HostId two = hostId(string(payload, "two"));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800241
Thomas Vachuska4830d392014-11-09 17:09:56 -0800242 HostToHostIntent hostIntent = new HostToHostIntent(appId, one, two,
243 DefaultTrafficSelector.builder().build(),
244 DefaultTrafficTreatment.builder().build());
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800245 intentsToMonitor.put(hostIntent, number(event, "sid"));
Thomas Vachuska4830d392014-11-09 17:09:56 -0800246 intentService.submit(hostIntent);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800247 }
248
249 // Sends traffic message.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800250 private void requestTraffic(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800251 ObjectNode payload = payload(event);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800252 long sid = number(event, "sid");
253 Set<Intent> intents = findPathIntents(payload);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800254
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800255 // Add all those intents to the list of monitored intents & flows.
256 intentsToMonitor.clear();
257 for (Intent intent : intents) {
258 intentsToMonitor.put(intent, sid);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800259 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800260
261 // Send an initial message to highlight all links of all monitored intents.
262 sendMessage(trafficMessage(intents, sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800263 }
264
265 // Cancels sending traffic messages.
266 private void cancelTraffic(ObjectNode event) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800267 ObjectNode payload = payload(event);
268 long sid = number(event, "sid");
269 Set<Intent> intents = findPathIntents(payload);
270
271 // Remove all those intents from the list of monitored intents & flows.
272 intentsToMonitor.clear(); // TODO: remove when ready
273 for (Intent intent : intents) {
274 intentsToMonitor.remove(intent.id());
275 }
276 sendMessage(trafficMessage(intents, sid));
277 }
278
279 // Finds all path (host-to-host or point-to-point) intents that pertains
280 // to the hosts indicated in the given event payload.
281 private Set<Intent> findPathIntents(ObjectNode payload) {
282 // Get the list of selected hosts.
283 Set<Host> hosts = getHosts((ArrayNode) payload.path("ids"));
284
285 // Derive from this the set of edge connect points.
286 Set<ConnectPoint> edgePoints = getEdgePoints(hosts);
287
288 // Iterate over all intents and produce a set that contains only those
289 // intents that target all selected hosts or derived edge connect points.
290 return getIntents(hosts, edgePoints);
291 }
292
293 // Produces a set of intents that target all selected hosts or connect points.
294 private Set<Intent> getIntents(Set<Host> hosts, Set<ConnectPoint> edgePoints) {
295 Set<Intent> intents = new HashSet<>();
296 for (Intent intent : intentService.getIntents()) {
297 boolean isRelevant = false;
298 if (intent instanceof HostToHostIntent) {
299 isRelevant = isIntentRelevant((HostToHostIntent) intent, hosts);
300 } else if (intent instanceof PointToPointIntent) {
301 isRelevant = isIntentRelevant((PointToPointIntent) intent, edgePoints);
302 } else if (intent instanceof MultiPointToSinglePointIntent) {
303 isRelevant = isIntentRelevant((MultiPointToSinglePointIntent) intent, edgePoints);
304 }
305 // TODO: add other intents, e.g. SinglePointToMultiPointIntent
306
307 if (isRelevant) {
308 intents.add(intent);
309 }
310 }
311 return intents;
312 }
313
314 // Indicates whether the specified intent involves all of the given hosts.
315 private boolean isIntentRelevant(HostToHostIntent intent, Set<Host> hosts) {
316 for (Host host : hosts) {
317 HostId id = host.id();
318 // Bail if intent does not involve this host.
319 if (!id.equals(intent.one()) && !id.equals(intent.two())) {
320 return false;
321 }
322 }
323 return true;
324 }
325
326 // Indicates whether the specified intent involves all of the given edge points.
327 private boolean isIntentRelevant(PointToPointIntent intent,
328 Set<ConnectPoint> edgePoints) {
329 for (ConnectPoint point : edgePoints) {
330 // Bail if intent does not involve this edge point.
331 if (!point.equals(intent.egressPoint()) &&
332 !point.equals(intent.ingressPoint())) {
333 return false;
334 }
335 }
336 return true;
337 }
338
339 // Indicates whether the specified intent involves all of the given edge points.
340 private boolean isIntentRelevant(MultiPointToSinglePointIntent intent,
341 Set<ConnectPoint> edgePoints) {
342 for (ConnectPoint point : edgePoints) {
343 // Bail if intent does not involve this edge point.
344 if (!point.equals(intent.egressPoint()) &&
345 !intent.ingressPoints().contains(point)) {
346 return false;
347 }
348 }
349 return true;
350 }
351
352
353 // Produces a set of all host ids listed in the specified JSON array.
354 private Set<Host> getHosts(ArrayNode array) {
355 Set<Host> hosts = new HashSet<>();
356 for (JsonNode node : array) {
357 try {
358 Host host = hostService.getHost(hostId(node.asText()));
359 if (host != null) {
360 hosts.add(host);
361 }
362 } catch (IllegalArgumentException e) {
363 log.debug("Skipping ID {}", node.asText());
364 }
365 }
366 return hosts;
367 }
368
369 // Produces a set of edge points from the specified set of hosts.
370 private Set<ConnectPoint> getEdgePoints(Set<Host> hosts) {
371 Set<ConnectPoint> edgePoints = new HashSet<>();
372 for (Host host : hosts) {
373 edgePoints.add(host.location());
374 }
375 return edgePoints;
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800376 }
377
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800378
379 // Adds all internal listeners.
380 private void addListeners() {
381 clusterService.addListener(clusterListener);
382 deviceService.addListener(deviceListener);
383 linkService.addListener(linkListener);
384 hostService.addListener(hostListener);
385 mastershipService.addListener(mastershipListener);
386 intentService.addListener(intentListener);
387 }
388
389 // Removes all internal listeners.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800390 private synchronized void removeListeners() {
391 if (!listenersRemoved) {
392 listenersRemoved = true;
393 clusterService.removeListener(clusterListener);
394 deviceService.removeListener(deviceListener);
395 linkService.removeListener(linkListener);
396 hostService.removeListener(hostListener);
397 mastershipService.removeListener(mastershipListener);
398 intentService.removeListener(intentListener);
399 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800400 }
401
402 // Cluster event listener.
403 private class InternalClusterListener implements ClusterEventListener {
404 @Override
405 public void event(ClusterEvent event) {
406 sendMessage(instanceMessage(event));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800407 }
408 }
409
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800410 // Device event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800411 private class InternalDeviceListener implements DeviceListener {
412 @Override
413 public void event(DeviceEvent event) {
414 sendMessage(deviceMessage(event));
415 }
416 }
417
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800418 // Link event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800419 private class InternalLinkListener implements LinkListener {
420 @Override
421 public void event(LinkEvent event) {
422 sendMessage(linkMessage(event));
423 }
424 }
425
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800426 // Host event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800427 private class InternalHostListener implements HostListener {
428 @Override
429 public void event(HostEvent event) {
430 sendMessage(hostMessage(event));
431 }
432 }
433
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800434 // Mastership event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800435 private class InternalMastershipListener implements MastershipListener {
436 @Override
437 public void event(MastershipEvent event) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800438 // TODO: Is DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED the same?
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800439
Thomas Vachuskad472c6e2014-11-07 19:11:05 -0800440 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800441 }
Thomas Vachuska4830d392014-11-09 17:09:56 -0800442
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800443 // Intent event listener.
Thomas Vachuska4830d392014-11-09 17:09:56 -0800444 private class InternalIntentListener implements IntentListener {
445 @Override
446 public void event(IntentEvent event) {
447 Intent intent = event.subject();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800448 Long sid = intentsToMonitor.get(intent);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800449 if (sid != null) {
450 List<Intent> installable = intentService.getInstallableIntents(intent.id());
451 if (installable != null && !installable.isEmpty()) {
452 PathIntent pathIntent = (PathIntent) installable.iterator().next();
453 Path path = pathIntent.path();
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800454 ObjectNode payload = pathMessage(path, "host")
455 .put("intentId", intent.id().toString());
Thomas Vachuska4830d392014-11-09 17:09:56 -0800456 sendMessage(envelope("showPath", sid, payload));
457 }
458 }
459 }
460 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800461
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800462}
463