blob: af28086dd5624ebf7af8269396e93ddc60ef8451 [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 Vachuskadea45ff2014-11-12 18:35:46 -080027import org.onlab.onos.net.ConnectPoint;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080028import org.onlab.onos.net.Device;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080029import org.onlab.onos.net.Host;
30import org.onlab.onos.net.HostId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080031import org.onlab.onos.net.Link;
32import org.onlab.onos.net.device.DeviceEvent;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080033import org.onlab.onos.net.device.DeviceListener;
Thomas Vachuska4830d392014-11-09 17:09:56 -080034import org.onlab.onos.net.flow.DefaultTrafficSelector;
35import org.onlab.onos.net.flow.DefaultTrafficTreatment;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080036import org.onlab.onos.net.host.HostEvent;
37import org.onlab.onos.net.host.HostListener;
Thomas Vachuska4830d392014-11-09 17:09:56 -080038import org.onlab.onos.net.intent.HostToHostIntent;
39import org.onlab.onos.net.intent.Intent;
40import org.onlab.onos.net.intent.IntentEvent;
Thomas Vachuska4830d392014-11-09 17:09:56 -080041import org.onlab.onos.net.intent.IntentListener;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080042import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Thomas Vachuska22e34922014-11-14 00:40:55 -080043import org.onlab.onos.net.intent.OpticalConnectivityIntent;
Thomas Vachuska4830d392014-11-09 17:09:56 -080044import org.onlab.onos.net.intent.PathIntent;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080045import org.onlab.onos.net.intent.PointToPointIntent;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080046import org.onlab.onos.net.link.LinkEvent;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080047import org.onlab.onos.net.link.LinkListener;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080048import org.onlab.osgi.ServiceDirectory;
49
50import java.io.IOException;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080051import java.util.HashSet;
Thomas Vachuska4830d392014-11-09 17:09:56 -080052import java.util.List;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080053import java.util.Set;
Thomas Vachuska22e34922014-11-14 00:40:55 -080054import java.util.Timer;
55import java.util.TimerTask;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080056
Thomas Vachuskae7591e52014-11-13 21:31:15 -080057import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080058import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080059import static org.onlab.onos.net.DeviceId.deviceId;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080060import static org.onlab.onos.net.HostId.hostId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080061import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED;
Thomas Vachuska4830d392014-11-09 17:09:56 -080062import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
Thomas Vachuska22e34922014-11-14 00:40:55 -080063import static org.onlab.onos.net.intent.IntentState.INSTALLED;
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
Thomas Vachuska22e34922014-11-14 00:40:55 -080081 private static final long TRAFFIC_FREQUENCY_SEC = 5000;
82
Thomas Vachuska4830d392014-11-09 17:09:56 -080083 private final ApplicationId appId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080084
Thomas Vachuska7d638d32014-11-07 10:24:43 -080085 private Connection connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080086 private FrameConnection control;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080087
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080088 private final ClusterEventListener clusterListener = new InternalClusterListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -080089 private final DeviceListener deviceListener = new InternalDeviceListener();
90 private final LinkListener linkListener = new InternalLinkListener();
91 private final HostListener hostListener = new InternalHostListener();
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 Vachuska22e34922014-11-14 00:40:55 -080095 private ObjectNode monitorRequest;
96 private final Timer timer = new Timer("intent-traffic-monitor");
97 private final TimerTask timerTask = new IntentTrafficMonitor();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080098
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080099 private long lastActive = System.currentTimeMillis();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800100 private boolean listenersRemoved = false;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800101
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800102 /**
103 * Creates a new web-socket for serving data to GUI topology view.
104 *
105 * @param directory service directory
106 */
107 public TopologyWebSocket(ServiceDirectory directory) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800108 super(directory);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800109 appId = directory.get(CoreService.class).registerApplication(APP_ID);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800110 }
111
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800112 /**
113 * Issues a close on the connection.
114 */
115 synchronized void close() {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800116 removeListeners();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800117 if (connection.isOpen()) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800118 connection.close();
119 }
120 }
121
122 /**
123 * Indicates if this connection is idle.
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800124 *
125 * @return true if idle or closed
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800126 */
127 synchronized boolean isIdle() {
128 boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS;
129 if (idle || !connection.isOpen()) {
130 return true;
131 }
132 try {
133 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
134 } catch (IOException e) {
135 log.warn("Unable to send ping message due to: ", e);
136 }
137 return false;
138 }
139
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800140 @Override
141 public void onOpen(Connection connection) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800142 log.info("GUI client connected");
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800143 this.connection = connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800144 this.control = (FrameConnection) connection;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800145 addListeners();
Thomas Vachuska22e34922014-11-14 00:40:55 -0800146 timer.schedule(timerTask, TRAFFIC_FREQUENCY_SEC, TRAFFIC_FREQUENCY_SEC);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800147
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800148 sendAllInstances();
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800149 sendAllDevices();
150 sendAllLinks();
Thomas Vachuska4830d392014-11-09 17:09:56 -0800151 sendAllHosts();
152 }
153
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800154 @Override
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800155 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800156 removeListeners();
Thomas Vachuska22e34922014-11-14 00:40:55 -0800157 timer.cancel();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800158 log.info("GUI client disconnected");
159 }
160
161 @Override
162 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
163 lastActive = System.currentTimeMillis();
164 return true;
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800165 }
166
167 @Override
168 public void onMessage(String data) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800169 lastActive = System.currentTimeMillis();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800170 try {
171 ObjectNode event = (ObjectNode) mapper.reader().readTree(data);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800172 String type = string(event, "event", "unknown");
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800173 if (type.equals("requestDetails")) {
174 requestDetails(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800175 } else if (type.equals("updateMeta")) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800176 updateMetaUi(event);
Praseed Balakrishnanc0029652014-11-14 13:38:49 -0800177 } else if (type.equals("addHostIntent")) {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800178 createHostIntent(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800179 } else if (type.equals("requestTraffic")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800180 requestTraffic(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800181 } else if (type.equals("cancelTraffic")) {
182 cancelTraffic(event);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800183 }
Thomas Vachuska4830d392014-11-09 17:09:56 -0800184 } catch (Exception e) {
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800185 log.warn("Unable to parse GUI request {} due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800186 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800187 }
188
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800189 // Sends the specified data to the client.
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800190 private synchronized void sendMessage(ObjectNode data) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800191 try {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800192 if (connection.isOpen()) {
193 connection.sendMessage(data.toString());
194 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800195 } catch (IOException e) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800196 log.warn("Unable to send message {} to GUI due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800197 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800198 }
199
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800200 // Sends all controller nodes to the client as node-added messages.
201 private void sendAllInstances() {
202 for (ControllerNode node : clusterService.getNodes()) {
203 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node)));
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 devices to the client as device-added messages.
208 private void sendAllDevices() {
209 for (Device device : deviceService.getDevices()) {
210 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800211 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800212 }
213
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800214 // Sends all links to the client as link-added messages.
215 private void sendAllLinks() {
216 for (Link link : linkService.getLinks()) {
217 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800218 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800219 }
220
221 // Sends all hosts to the client as host-added messages.
222 private void sendAllHosts() {
223 for (Host host : hostService.getHosts()) {
224 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
225 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800226 }
227
228 // Sends back device or host details.
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800229 private void requestDetails(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800230 ObjectNode payload = payload(event);
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800231 String type = string(payload, "class", "unknown");
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800232 long sid = number(event, "sid");
233
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800234 if (type.equals("device")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800235 sendMessage(deviceDetails(deviceId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800236 } else if (type.equals("host")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800237 sendMessage(hostDetails(hostId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800238 }
239 }
240
Thomas Vachuska4830d392014-11-09 17:09:56 -0800241 // Creates host-to-host intent.
242 private void createHostIntent(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800243 ObjectNode payload = payload(event);
244 long id = number(event, "sid");
Thomas Vachuska4830d392014-11-09 17:09:56 -0800245 // TODO: add protection against device ids and non-existent hosts.
246 HostId one = hostId(string(payload, "one"));
247 HostId two = hostId(string(payload, "two"));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800248
Thomas Vachuska4830d392014-11-09 17:09:56 -0800249 HostToHostIntent hostIntent = new HostToHostIntent(appId, one, two,
250 DefaultTrafficSelector.builder().build(),
251 DefaultTrafficTreatment.builder().build());
Thomas Vachuska22e34922014-11-14 00:40:55 -0800252 monitorRequest = event;
Thomas Vachuska4830d392014-11-09 17:09:56 -0800253 intentService.submit(hostIntent);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800254 }
255
256 // Sends traffic message.
Thomas Vachuska22e34922014-11-14 00:40:55 -0800257 private synchronized void requestTraffic(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800258 ObjectNode payload = payload(event);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800259 long sid = number(event, "sid");
Thomas Vachuska22e34922014-11-14 00:40:55 -0800260 monitorRequest = event;
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800261
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800262 // Get the set of selected hosts and their intents.
263 Set<Host> hosts = getHosts((ArrayNode) payload.path("ids"));
264 Set<Intent> intents = findPathIntents(hosts);
265
266 // If there is a hover node, include it in the hosts and find intents.
267 String hover = string(payload, "hover");
268 Set<Intent> hoverIntents;
269 if (!isNullOrEmpty(hover)) {
270 addHost(hosts, hostId(hover));
271 hoverIntents = findPathIntents(hosts);
272 intents.removeAll(hoverIntents);
273
274 // Send an initial message to highlight all links of all monitored intents.
275 sendMessage(trafficMessage(sid,
276 new TrafficClass("primary", hoverIntents),
277 new TrafficClass("secondary", intents)));
278
279 } else {
280 // Send an initial message to highlight all links of all monitored intents.
281 sendMessage(trafficMessage(sid, new TrafficClass("primary", intents)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800282 }
283 }
284
285 // Cancels sending traffic messages.
286 private void cancelTraffic(ObjectNode event) {
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800287 sendMessage(trafficMessage(number(event, "sid")));
Thomas Vachuska22e34922014-11-14 00:40:55 -0800288 monitorRequest = null;
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 Vachuska22e34922014-11-14 00:40:55 -0800309 Set<OpticalConnectivityIntent> opticalIntents = new HashSet<>();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800310
Thomas Vachuska22e34922014-11-14 00:40:55 -0800311 for (Intent intent : intentService.getIntents()) {
312 if (intentService.getIntentState(intent.id()) == INSTALLED) {
313 boolean isRelevant = false;
314 if (intent instanceof HostToHostIntent) {
315 isRelevant = isIntentRelevant((HostToHostIntent) intent, hosts);
316 } else if (intent instanceof PointToPointIntent) {
317 isRelevant = isIntentRelevant((PointToPointIntent) intent, edgePoints);
318 } else if (intent instanceof MultiPointToSinglePointIntent) {
319 isRelevant = isIntentRelevant((MultiPointToSinglePointIntent) intent, edgePoints);
320 } else if (intent instanceof OpticalConnectivityIntent) {
321 opticalIntents.add((OpticalConnectivityIntent) intent);
322 }
323 // TODO: add other intents, e.g. SinglePointToMultiPointIntent
324
325 if (isRelevant) {
326 intents.add(intent);
327 }
328 }
329 }
330
331 for (OpticalConnectivityIntent intent : opticalIntents) {
332 if (isIntentRelevant(intent, intents)) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800333 intents.add(intent);
334 }
335 }
336 return intents;
337 }
338
339 // Indicates whether the specified intent involves all of the given hosts.
340 private boolean isIntentRelevant(HostToHostIntent intent, Set<Host> hosts) {
341 for (Host host : hosts) {
342 HostId id = host.id();
343 // Bail if intent does not involve this host.
344 if (!id.equals(intent.one()) && !id.equals(intent.two())) {
345 return false;
346 }
347 }
348 return true;
349 }
350
351 // Indicates whether the specified intent involves all of the given edge points.
352 private boolean isIntentRelevant(PointToPointIntent intent,
353 Set<ConnectPoint> edgePoints) {
354 for (ConnectPoint point : edgePoints) {
355 // Bail if intent does not involve this edge point.
356 if (!point.equals(intent.egressPoint()) &&
357 !point.equals(intent.ingressPoint())) {
358 return false;
359 }
360 }
361 return true;
362 }
363
364 // Indicates whether the specified intent involves all of the given edge points.
365 private boolean isIntentRelevant(MultiPointToSinglePointIntent intent,
366 Set<ConnectPoint> edgePoints) {
367 for (ConnectPoint point : edgePoints) {
368 // Bail if intent does not involve this edge point.
369 if (!point.equals(intent.egressPoint()) &&
370 !intent.ingressPoints().contains(point)) {
371 return false;
372 }
373 }
374 return true;
375 }
376
Thomas Vachuska22e34922014-11-14 00:40:55 -0800377 // Indicates whether the specified intent involves all of the given edge points.
378 private boolean isIntentRelevant(OpticalConnectivityIntent opticalIntent,
379 Set<Intent> intents) {
Thomas Vachuskadea4cb32014-11-14 12:14:30 -0800380 Link ccSrc = getFirstLink(opticalIntent.getSrcConnectPoint(), false);
381 Link ccDst = getFirstLink(opticalIntent.getDst(), true);
382
Thomas Vachuska22e34922014-11-14 00:40:55 -0800383 for (Intent intent : intents) {
384 List<Intent> installables = intentService.getInstallableIntents(intent.id());
385 for (Intent installable : installables) {
386 if (installable instanceof PathIntent) {
Thomas Vachuskadea4cb32014-11-14 12:14:30 -0800387 List<Link> links = ((PathIntent) installable).path().links();
388 if (links.size() == 3) {
389 Link tunnel = links.get(1);
390 if (tunnel.src().equals(ccSrc.src()) &&
391 tunnel.dst().equals(ccDst.dst())) {
392 return true;
393 }
Thomas Vachuska22e34922014-11-14 00:40:55 -0800394 }
395 }
396 }
397 }
398 return false;
399 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800400
Thomas Vachuskadea4cb32014-11-14 12:14:30 -0800401 private Link getFirstLink(ConnectPoint point, boolean ingress) {
402 for (Link link : linkService.getLinks(point)) {
403 if (point.equals(ingress ? link.src() : link.dst())) {
404 return link;
405 }
406 }
407 return null;
408 }
409
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800410 // Produces a set of all host ids listed in the specified JSON array.
411 private Set<Host> getHosts(ArrayNode array) {
412 Set<Host> hosts = new HashSet<>();
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800413 if (array != null) {
414 for (JsonNode node : array) {
415 try {
416 addHost(hosts, hostId(node.asText()));
417 } catch (IllegalArgumentException e) {
418 log.debug("Skipping ID {}", node.asText());
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800419 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800420 }
421 }
422 return hosts;
423 }
424
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800425 private void addHost(Set<Host> hosts, HostId hostId) {
426 Host host = hostService.getHost(hostId);
427 if (host != null) {
428 hosts.add(host);
429 }
430 }
431
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800432 // Produces a set of edge points from the specified set of hosts.
433 private Set<ConnectPoint> getEdgePoints(Set<Host> hosts) {
434 Set<ConnectPoint> edgePoints = new HashSet<>();
435 for (Host host : hosts) {
436 edgePoints.add(host.location());
437 }
438 return edgePoints;
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800439 }
440
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800441
442 // Adds all internal listeners.
443 private void addListeners() {
444 clusterService.addListener(clusterListener);
445 deviceService.addListener(deviceListener);
446 linkService.addListener(linkListener);
447 hostService.addListener(hostListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800448 intentService.addListener(intentListener);
449 }
450
451 // Removes all internal listeners.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800452 private synchronized void removeListeners() {
453 if (!listenersRemoved) {
454 listenersRemoved = true;
455 clusterService.removeListener(clusterListener);
456 deviceService.removeListener(deviceListener);
457 linkService.removeListener(linkListener);
458 hostService.removeListener(hostListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800459 intentService.removeListener(intentListener);
460 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800461 }
462
463 // Cluster event listener.
464 private class InternalClusterListener implements ClusterEventListener {
465 @Override
466 public void event(ClusterEvent event) {
467 sendMessage(instanceMessage(event));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800468 }
469 }
470
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800471 // Device event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800472 private class InternalDeviceListener implements DeviceListener {
473 @Override
474 public void event(DeviceEvent event) {
475 sendMessage(deviceMessage(event));
476 }
477 }
478
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800479 // Link event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800480 private class InternalLinkListener implements LinkListener {
481 @Override
482 public void event(LinkEvent event) {
483 sendMessage(linkMessage(event));
484 }
485 }
486
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800487 // Host event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800488 private class InternalHostListener implements HostListener {
489 @Override
490 public void event(HostEvent event) {
491 sendMessage(hostMessage(event));
492 }
493 }
494
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800495 // Intent event listener.
Thomas Vachuska4830d392014-11-09 17:09:56 -0800496 private class InternalIntentListener implements IntentListener {
497 @Override
498 public void event(IntentEvent event) {
Thomas Vachuska22e34922014-11-14 00:40:55 -0800499 if (monitorRequest != null) {
500 requestTraffic(monitorRequest);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800501 }
502 }
503 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800504
Thomas Vachuska22e34922014-11-14 00:40:55 -0800505 private class IntentTrafficMonitor extends TimerTask {
506 @Override
507 public void run() {
508 if (monitorRequest != null) {
509 requestTraffic(monitorRequest);
510 }
511 }
512 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800513}
514