blob: cf8144b54871f68abca4a2e0a4cae7e27f981b0a [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 Vachuskae7591e52014-11-13 21:31:15 -080059import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080060import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080061import static org.onlab.onos.net.DeviceId.deviceId;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080062import static org.onlab.onos.net.HostId.hostId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080063import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED;
Thomas Vachuska4830d392014-11-09 17:09:56 -080064import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080065import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080066
Thomas Vachuska7d638d32014-11-07 10:24:43 -080067/**
68 * Web socket capable of interacting with the GUI topology view.
69 */
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080070public class TopologyWebSocket
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080071 extends TopologyMessages
72 implements WebSocket.OnTextMessage, WebSocket.OnControl {
73
74 private static final long MAX_AGE_MS = 15000;
75
76 private static final byte PING = 0x9;
77 private static final byte PONG = 0xA;
78 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
Thomas Vachuska7d638d32014-11-07 10:24:43 -080079
Thomas Vachuska4830d392014-11-09 17:09:56 -080080 private static final String APP_ID = "org.onlab.onos.gui";
Thomas Vachuska4830d392014-11-09 17:09:56 -080081
82 private final ApplicationId appId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080083
Thomas Vachuska7d638d32014-11-07 10:24:43 -080084 private Connection connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080085 private FrameConnection control;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080086
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080087 private final ClusterEventListener clusterListener = new InternalClusterListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -080088 private final DeviceListener deviceListener = new InternalDeviceListener();
89 private final LinkListener linkListener = new InternalLinkListener();
90 private final HostListener hostListener = new InternalHostListener();
91 private final MastershipListener mastershipListener = new InternalMastershipListener();
Thomas Vachuska4830d392014-11-09 17:09:56 -080092 private final IntentListener intentListener = new InternalIntentListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -080093
Thomas Vachuska4830d392014-11-09 17:09:56 -080094 // Intents that are being monitored for the GUI
Thomas Vachuska3266abf2014-11-13 09:28:46 -080095 private Map<Intent, Long> intentsToMonitor = new ConcurrentHashMap<>();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080096
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080097 private long lastActive = System.currentTimeMillis();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080098 private boolean listenersRemoved = false;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080099
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800100 /**
101 * Creates a new web-socket for serving data to GUI topology view.
102 *
103 * @param directory service directory
104 */
105 public TopologyWebSocket(ServiceDirectory directory) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800106 super(directory);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800107 appId = directory.get(CoreService.class).registerApplication(APP_ID);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800108 }
109
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800110 /**
111 * Issues a close on the connection.
112 */
113 synchronized void close() {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800114 removeListeners();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800115 if (connection.isOpen()) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800116 connection.close();
117 }
118 }
119
120 /**
121 * Indicates if this connection is idle.
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800122 *
123 * @return true if idle or closed
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800124 */
125 synchronized boolean isIdle() {
126 boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS;
127 if (idle || !connection.isOpen()) {
128 return true;
129 }
130 try {
131 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
132 } catch (IOException e) {
133 log.warn("Unable to send ping message due to: ", e);
134 }
135 return false;
136 }
137
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800138 @Override
139 public void onOpen(Connection connection) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800140 log.info("GUI client connected");
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800141 this.connection = connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800142 this.control = (FrameConnection) connection;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800143 addListeners();
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800144
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800145 sendAllInstances();
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800146 sendAllDevices();
147 sendAllLinks();
Thomas Vachuska4830d392014-11-09 17:09:56 -0800148 sendAllHosts();
149 }
150
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800151 @Override
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800152 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800153 removeListeners();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800154 log.info("GUI client disconnected");
155 }
156
157 @Override
158 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
159 lastActive = System.currentTimeMillis();
160 return true;
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800161 }
162
163 @Override
164 public void onMessage(String data) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800165 lastActive = System.currentTimeMillis();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800166 try {
167 ObjectNode event = (ObjectNode) mapper.reader().readTree(data);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800168 String type = string(event, "event", "unknown");
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800169 if (type.equals("requestDetails")) {
170 requestDetails(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800171 } else if (type.equals("updateMeta")) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800172 updateMetaUi(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800173 } else if (type.equals("requestPath")) {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800174 createHostIntent(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800175 } else if (type.equals("requestTraffic")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800176 requestTraffic(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800177 } else if (type.equals("cancelTraffic")) {
178 cancelTraffic(event);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800179 }
Thomas Vachuska4830d392014-11-09 17:09:56 -0800180 } catch (Exception e) {
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800181 log.warn("Unable to parse GUI request {} due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800182 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800183 }
184
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800185 // Sends the specified data to the client.
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800186 private synchronized void sendMessage(ObjectNode data) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800187 try {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800188 if (connection.isOpen()) {
189 connection.sendMessage(data.toString());
190 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800191 } catch (IOException e) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800192 log.warn("Unable to send message {} to GUI due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800193 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800194 }
195
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800196 // Sends all controller nodes to the client as node-added messages.
197 private void sendAllInstances() {
198 for (ControllerNode node : clusterService.getNodes()) {
199 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800200 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800201 }
202
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800203 // Sends all devices to the client as device-added messages.
204 private void sendAllDevices() {
205 for (Device device : deviceService.getDevices()) {
206 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800207 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800208 }
209
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800210 // Sends all links to the client as link-added messages.
211 private void sendAllLinks() {
212 for (Link link : linkService.getLinks()) {
213 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800214 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800215 }
216
217 // Sends all hosts to the client as host-added messages.
218 private void sendAllHosts() {
219 for (Host host : hostService.getHosts()) {
220 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
221 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800222 }
223
224 // Sends back device or host details.
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800225 private void requestDetails(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800226 ObjectNode payload = payload(event);
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800227 String type = string(payload, "class", "unknown");
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800228 long sid = number(event, "sid");
229
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800230 if (type.equals("device")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800231 sendMessage(deviceDetails(deviceId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800232 } else if (type.equals("host")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800233 sendMessage(hostDetails(hostId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800234 }
235 }
236
Thomas Vachuska4830d392014-11-09 17:09:56 -0800237 // Creates host-to-host intent.
238 private void createHostIntent(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800239 ObjectNode payload = payload(event);
240 long id = number(event, "sid");
Thomas Vachuska4830d392014-11-09 17:09:56 -0800241 // TODO: add protection against device ids and non-existent hosts.
242 HostId one = hostId(string(payload, "one"));
243 HostId two = hostId(string(payload, "two"));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800244
Thomas Vachuska4830d392014-11-09 17:09:56 -0800245 HostToHostIntent hostIntent = new HostToHostIntent(appId, one, two,
246 DefaultTrafficSelector.builder().build(),
247 DefaultTrafficTreatment.builder().build());
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800248 intentsToMonitor.put(hostIntent, number(event, "sid"));
Thomas Vachuska4830d392014-11-09 17:09:56 -0800249 intentService.submit(hostIntent);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800250 }
251
252 // Sends traffic message.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800253 private void requestTraffic(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800254 ObjectNode payload = payload(event);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800255 long sid = number(event, "sid");
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800256
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800257 // Get the set of selected hosts and their intents.
258 Set<Host> hosts = getHosts((ArrayNode) payload.path("ids"));
259 Set<Intent> intents = findPathIntents(hosts);
260
261 // If there is a hover node, include it in the hosts and find intents.
262 String hover = string(payload, "hover");
263 Set<Intent> hoverIntents;
264 if (!isNullOrEmpty(hover)) {
265 addHost(hosts, hostId(hover));
266 hoverIntents = findPathIntents(hosts);
267 intents.removeAll(hoverIntents);
268
269 // Send an initial message to highlight all links of all monitored intents.
270 sendMessage(trafficMessage(sid,
271 new TrafficClass("primary", hoverIntents),
272 new TrafficClass("secondary", intents)));
273
274 } else {
275 // Send an initial message to highlight all links of all monitored intents.
276 sendMessage(trafficMessage(sid, new TrafficClass("primary", intents)));
277
278 // Add all those intents to the list of monitored intents & flows.
279 intentsToMonitor.clear();
280 for (Intent intent : intents) {
281 intentsToMonitor.put(intent, sid);
282 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800283 }
284 }
285
286 // Cancels sending traffic messages.
287 private void cancelTraffic(ObjectNode event) {
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800288 sendMessage(trafficMessage(number(event, "sid")));
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800289 }
290
291 // Finds all path (host-to-host or point-to-point) intents that pertains
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800292 // to the given hosts.
293 private Set<Intent> findPathIntents(Set<Host> hosts) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800294 // Derive from this the set of edge connect points.
295 Set<ConnectPoint> edgePoints = getEdgePoints(hosts);
296
297 // Iterate over all intents and produce a set that contains only those
298 // intents that target all selected hosts or derived edge connect points.
299 return getIntents(hosts, edgePoints);
300 }
301
302 // Produces a set of intents that target all selected hosts or connect points.
303 private Set<Intent> getIntents(Set<Host> hosts, Set<ConnectPoint> edgePoints) {
304 Set<Intent> intents = new HashSet<>();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800305 if (hosts.isEmpty()) {
306 return intents;
307 }
308
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800309 for (Intent intent : intentService.getIntents()) {
310 boolean isRelevant = false;
311 if (intent instanceof HostToHostIntent) {
312 isRelevant = isIntentRelevant((HostToHostIntent) intent, hosts);
313 } else if (intent instanceof PointToPointIntent) {
314 isRelevant = isIntentRelevant((PointToPointIntent) intent, edgePoints);
315 } else if (intent instanceof MultiPointToSinglePointIntent) {
316 isRelevant = isIntentRelevant((MultiPointToSinglePointIntent) intent, edgePoints);
317 }
318 // TODO: add other intents, e.g. SinglePointToMultiPointIntent
319
320 if (isRelevant) {
321 intents.add(intent);
322 }
323 }
324 return intents;
325 }
326
327 // Indicates whether the specified intent involves all of the given hosts.
328 private boolean isIntentRelevant(HostToHostIntent intent, Set<Host> hosts) {
329 for (Host host : hosts) {
330 HostId id = host.id();
331 // Bail if intent does not involve this host.
332 if (!id.equals(intent.one()) && !id.equals(intent.two())) {
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(PointToPointIntent 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 !point.equals(intent.ingressPoint())) {
346 return false;
347 }
348 }
349 return true;
350 }
351
352 // Indicates whether the specified intent involves all of the given edge points.
353 private boolean isIntentRelevant(MultiPointToSinglePointIntent intent,
354 Set<ConnectPoint> edgePoints) {
355 for (ConnectPoint point : edgePoints) {
356 // Bail if intent does not involve this edge point.
357 if (!point.equals(intent.egressPoint()) &&
358 !intent.ingressPoints().contains(point)) {
359 return false;
360 }
361 }
362 return true;
363 }
364
365
366 // Produces a set of all host ids listed in the specified JSON array.
367 private Set<Host> getHosts(ArrayNode array) {
368 Set<Host> hosts = new HashSet<>();
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800369 if (array != null) {
370 for (JsonNode node : array) {
371 try {
372 addHost(hosts, hostId(node.asText()));
373 } catch (IllegalArgumentException e) {
374 log.debug("Skipping ID {}", node.asText());
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800375 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800376 }
377 }
378 return hosts;
379 }
380
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800381 private void addHost(Set<Host> hosts, HostId hostId) {
382 Host host = hostService.getHost(hostId);
383 if (host != null) {
384 hosts.add(host);
385 }
386 }
387
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800388 // Produces a set of edge points from the specified set of hosts.
389 private Set<ConnectPoint> getEdgePoints(Set<Host> hosts) {
390 Set<ConnectPoint> edgePoints = new HashSet<>();
391 for (Host host : hosts) {
392 edgePoints.add(host.location());
393 }
394 return edgePoints;
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800395 }
396
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800397
398 // Adds all internal listeners.
399 private void addListeners() {
400 clusterService.addListener(clusterListener);
401 deviceService.addListener(deviceListener);
402 linkService.addListener(linkListener);
403 hostService.addListener(hostListener);
404 mastershipService.addListener(mastershipListener);
405 intentService.addListener(intentListener);
406 }
407
408 // Removes all internal listeners.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800409 private synchronized void removeListeners() {
410 if (!listenersRemoved) {
411 listenersRemoved = true;
412 clusterService.removeListener(clusterListener);
413 deviceService.removeListener(deviceListener);
414 linkService.removeListener(linkListener);
415 hostService.removeListener(hostListener);
416 mastershipService.removeListener(mastershipListener);
417 intentService.removeListener(intentListener);
418 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800419 }
420
421 // Cluster event listener.
422 private class InternalClusterListener implements ClusterEventListener {
423 @Override
424 public void event(ClusterEvent event) {
425 sendMessage(instanceMessage(event));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800426 }
427 }
428
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800429 // Device event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800430 private class InternalDeviceListener implements DeviceListener {
431 @Override
432 public void event(DeviceEvent event) {
433 sendMessage(deviceMessage(event));
434 }
435 }
436
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800437 // Link event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800438 private class InternalLinkListener implements LinkListener {
439 @Override
440 public void event(LinkEvent event) {
441 sendMessage(linkMessage(event));
442 }
443 }
444
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800445 // Host event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800446 private class InternalHostListener implements HostListener {
447 @Override
448 public void event(HostEvent event) {
449 sendMessage(hostMessage(event));
450 }
451 }
452
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800453 // Mastership event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800454 private class InternalMastershipListener implements MastershipListener {
455 @Override
456 public void event(MastershipEvent event) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800457 // TODO: Is DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED the same?
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800458
Thomas Vachuskad472c6e2014-11-07 19:11:05 -0800459 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800460 }
Thomas Vachuska4830d392014-11-09 17:09:56 -0800461
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800462 // Intent event listener.
Thomas Vachuska4830d392014-11-09 17:09:56 -0800463 private class InternalIntentListener implements IntentListener {
464 @Override
465 public void event(IntentEvent event) {
466 Intent intent = event.subject();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800467 Long sid = intentsToMonitor.get(intent);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800468 if (sid != null) {
469 List<Intent> installable = intentService.getInstallableIntents(intent.id());
470 if (installable != null && !installable.isEmpty()) {
471 PathIntent pathIntent = (PathIntent) installable.iterator().next();
472 Path path = pathIntent.path();
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800473 ObjectNode payload = pathMessage(path, "host")
474 .put("intentId", intent.id().toString());
Thomas Vachuska4830d392014-11-09 17:09:56 -0800475 sendMessage(envelope("showPath", sid, payload));
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800476 TrafficClass tc = new TrafficClass("animated", intentsToMonitor.keySet());
477 sendMessage(trafficMessage(sid, tc));
Thomas Vachuska4830d392014-11-09 17:09:56 -0800478 }
479 }
480 }
481 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800482
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800483}
484