blob: 10b578595b65089e5f5f58a650b841acbd824d01 [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;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080032import org.onlab.onos.net.Path;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080033import org.onlab.onos.net.device.DeviceEvent;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080034import org.onlab.onos.net.device.DeviceListener;
Thomas Vachuska4830d392014-11-09 17:09:56 -080035import org.onlab.onos.net.flow.DefaultTrafficSelector;
36import org.onlab.onos.net.flow.DefaultTrafficTreatment;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080037import org.onlab.onos.net.host.HostEvent;
38import org.onlab.onos.net.host.HostListener;
Thomas Vachuska4830d392014-11-09 17:09:56 -080039import org.onlab.onos.net.intent.HostToHostIntent;
40import org.onlab.onos.net.intent.Intent;
41import org.onlab.onos.net.intent.IntentEvent;
Thomas Vachuska4830d392014-11-09 17:09:56 -080042import org.onlab.onos.net.intent.IntentListener;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080043import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Thomas Vachuska22e34922014-11-14 00:40:55 -080044import org.onlab.onos.net.intent.OpticalConnectivityIntent;
Thomas Vachuska4830d392014-11-09 17:09:56 -080045import org.onlab.onos.net.intent.PathIntent;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080046import org.onlab.onos.net.intent.PointToPointIntent;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080047import org.onlab.onos.net.link.LinkEvent;
Thomas Vachuska690e5f62014-11-09 08:26:47 -080048import org.onlab.onos.net.link.LinkListener;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080049import org.onlab.osgi.ServiceDirectory;
50
51import java.io.IOException;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080052import java.util.HashSet;
Thomas Vachuska4830d392014-11-09 17:09:56 -080053import java.util.List;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080054import java.util.Set;
Thomas Vachuska22e34922014-11-14 00:40:55 -080055import java.util.Timer;
56import java.util.TimerTask;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080057
Thomas Vachuskae7591e52014-11-13 21:31:15 -080058import static com.google.common.base.Strings.isNullOrEmpty;
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 Vachuska22e34922014-11-14 00:40:55 -080064import static org.onlab.onos.net.intent.IntentState.INSTALLED;
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
Thomas Vachuska22e34922014-11-14 00:40:55 -080082 private static final long TRAFFIC_FREQUENCY_SEC = 5000;
83
Thomas Vachuska4830d392014-11-09 17:09:56 -080084 private final ApplicationId appId;
Thomas Vachuskad472c6e2014-11-07 19:11:05 -080085
Thomas Vachuska7d638d32014-11-07 10:24:43 -080086 private Connection connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080087 private FrameConnection control;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080088
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080089 private final ClusterEventListener clusterListener = new InternalClusterListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -080090 private final DeviceListener deviceListener = new InternalDeviceListener();
91 private final LinkListener linkListener = new InternalLinkListener();
92 private final HostListener hostListener = new InternalHostListener();
Thomas Vachuska4830d392014-11-09 17:09:56 -080093 private final IntentListener intentListener = new InternalIntentListener();
Thomas Vachuska690e5f62014-11-09 08:26:47 -080094
Thomas Vachuska4830d392014-11-09 17:09:56 -080095 // Intents that are being monitored for the GUI
Thomas Vachuska22e34922014-11-14 00:40:55 -080096 private ObjectNode monitorRequest;
97 private final Timer timer = new Timer("intent-traffic-monitor");
98 private final TimerTask timerTask = new IntentTrafficMonitor();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080099
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800100 private long lastActive = System.currentTimeMillis();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800101 private boolean listenersRemoved = false;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800102
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800103 /**
104 * Creates a new web-socket for serving data to GUI topology view.
105 *
106 * @param directory service directory
107 */
108 public TopologyWebSocket(ServiceDirectory directory) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800109 super(directory);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800110 appId = directory.get(CoreService.class).registerApplication(APP_ID);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800111 }
112
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800113 /**
114 * Issues a close on the connection.
115 */
116 synchronized void close() {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800117 removeListeners();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800118 if (connection.isOpen()) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800119 connection.close();
120 }
121 }
122
123 /**
124 * Indicates if this connection is idle.
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800125 *
126 * @return true if idle or closed
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800127 */
128 synchronized boolean isIdle() {
129 boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS;
130 if (idle || !connection.isOpen()) {
131 return true;
132 }
133 try {
134 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
135 } catch (IOException e) {
136 log.warn("Unable to send ping message due to: ", e);
137 }
138 return false;
139 }
140
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800141 @Override
142 public void onOpen(Connection connection) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800143 log.info("GUI client connected");
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800144 this.connection = connection;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800145 this.control = (FrameConnection) connection;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800146 addListeners();
Thomas Vachuska22e34922014-11-14 00:40:55 -0800147 timer.schedule(timerTask, TRAFFIC_FREQUENCY_SEC, TRAFFIC_FREQUENCY_SEC);
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800148
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800149 sendAllInstances();
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800150 sendAllDevices();
151 sendAllLinks();
Thomas Vachuska4830d392014-11-09 17:09:56 -0800152 sendAllHosts();
153 }
154
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800155 @Override
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800156 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800157 removeListeners();
Thomas Vachuska22e34922014-11-14 00:40:55 -0800158 timer.cancel();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800159 log.info("GUI client disconnected");
160 }
161
162 @Override
163 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
164 lastActive = System.currentTimeMillis();
165 return true;
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800166 }
167
168 @Override
169 public void onMessage(String data) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800170 lastActive = System.currentTimeMillis();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800171 try {
172 ObjectNode event = (ObjectNode) mapper.reader().readTree(data);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800173 String type = string(event, "event", "unknown");
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800174 if (type.equals("requestDetails")) {
175 requestDetails(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800176 } else if (type.equals("updateMeta")) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800177 updateMetaUi(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800178 } else if (type.equals("requestPath")) {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800179 createHostIntent(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800180 } else if (type.equals("requestTraffic")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800181 requestTraffic(event);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800182 } else if (type.equals("cancelTraffic")) {
183 cancelTraffic(event);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800184 }
Thomas Vachuska4830d392014-11-09 17:09:56 -0800185 } catch (Exception e) {
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800186 log.warn("Unable to parse GUI request {} due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800187 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800188 }
189
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800190 // Sends the specified data to the client.
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800191 private synchronized void sendMessage(ObjectNode data) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800192 try {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800193 if (connection.isOpen()) {
194 connection.sendMessage(data.toString());
195 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800196 } catch (IOException e) {
Thomas Vachuskaba5621e2014-11-12 01:47:19 -0800197 log.warn("Unable to send message {} to GUI due to {}", data, e);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800198 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800199 }
200
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800201 // Sends all controller nodes to the client as node-added messages.
202 private void sendAllInstances() {
203 for (ControllerNode node : clusterService.getNodes()) {
204 sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800205 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800206 }
207
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800208 // Sends all devices to the client as device-added messages.
209 private void sendAllDevices() {
210 for (Device device : deviceService.getDevices()) {
211 sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800212 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800213 }
214
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800215 // Sends all links to the client as link-added messages.
216 private void sendAllLinks() {
217 for (Link link : linkService.getLinks()) {
218 sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link)));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800219 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800220 }
221
222 // Sends all hosts to the client as host-added messages.
223 private void sendAllHosts() {
224 for (Host host : hostService.getHosts()) {
225 sendMessage(hostMessage(new HostEvent(HOST_ADDED, host)));
226 }
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800227 }
228
229 // Sends back device or host details.
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800230 private void requestDetails(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800231 ObjectNode payload = payload(event);
Thomas Vachuskaf1fae002014-11-11 18:22:02 -0800232 String type = string(payload, "class", "unknown");
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800233 long sid = number(event, "sid");
234
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800235 if (type.equals("device")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800236 sendMessage(deviceDetails(deviceId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800237 } else if (type.equals("host")) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800238 sendMessage(hostDetails(hostId(string(payload, "id")), sid));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800239 }
240 }
241
Thomas Vachuska4830d392014-11-09 17:09:56 -0800242 // Creates host-to-host intent.
243 private void createHostIntent(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800244 ObjectNode payload = payload(event);
245 long id = number(event, "sid");
Thomas Vachuska4830d392014-11-09 17:09:56 -0800246 // TODO: add protection against device ids and non-existent hosts.
247 HostId one = hostId(string(payload, "one"));
248 HostId two = hostId(string(payload, "two"));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800249
Thomas Vachuska4830d392014-11-09 17:09:56 -0800250 HostToHostIntent hostIntent = new HostToHostIntent(appId, one, two,
251 DefaultTrafficSelector.builder().build(),
252 DefaultTrafficTreatment.builder().build());
Thomas Vachuska22e34922014-11-14 00:40:55 -0800253 monitorRequest = event;
Thomas Vachuska4830d392014-11-09 17:09:56 -0800254 intentService.submit(hostIntent);
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800255 }
256
257 // Sends traffic message.
Thomas Vachuska22e34922014-11-14 00:40:55 -0800258 private synchronized void requestTraffic(ObjectNode event) {
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800259 ObjectNode payload = payload(event);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800260 long sid = number(event, "sid");
Thomas Vachuska22e34922014-11-14 00:40:55 -0800261 monitorRequest = event;
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800262
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800263 // Get the set of selected hosts and their intents.
264 Set<Host> hosts = getHosts((ArrayNode) payload.path("ids"));
265 Set<Intent> intents = findPathIntents(hosts);
266
267 // If there is a hover node, include it in the hosts and find intents.
268 String hover = string(payload, "hover");
269 Set<Intent> hoverIntents;
270 if (!isNullOrEmpty(hover)) {
271 addHost(hosts, hostId(hover));
272 hoverIntents = findPathIntents(hosts);
273 intents.removeAll(hoverIntents);
274
275 // Send an initial message to highlight all links of all monitored intents.
276 sendMessage(trafficMessage(sid,
277 new TrafficClass("primary", hoverIntents),
278 new TrafficClass("secondary", intents)));
279
280 } else {
281 // Send an initial message to highlight all links of all monitored intents.
282 sendMessage(trafficMessage(sid, new TrafficClass("primary", intents)));
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 Vachuska22e34922014-11-14 00:40:55 -0800289 monitorRequest = null;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800290 }
291
292 // Finds all path (host-to-host or point-to-point) intents that pertains
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800293 // to the given hosts.
294 private Set<Intent> findPathIntents(Set<Host> hosts) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800295 // Derive from this the set of edge connect points.
296 Set<ConnectPoint> edgePoints = getEdgePoints(hosts);
297
298 // Iterate over all intents and produce a set that contains only those
299 // intents that target all selected hosts or derived edge connect points.
300 return getIntents(hosts, edgePoints);
301 }
302
303 // Produces a set of intents that target all selected hosts or connect points.
304 private Set<Intent> getIntents(Set<Host> hosts, Set<ConnectPoint> edgePoints) {
305 Set<Intent> intents = new HashSet<>();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800306 if (hosts.isEmpty()) {
307 return intents;
308 }
309
Thomas Vachuska22e34922014-11-14 00:40:55 -0800310 Set<OpticalConnectivityIntent> opticalIntents = new HashSet<>();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800311
Thomas Vachuska22e34922014-11-14 00:40:55 -0800312 for (Intent intent : intentService.getIntents()) {
313 if (intentService.getIntentState(intent.id()) == INSTALLED) {
314 boolean isRelevant = false;
315 if (intent instanceof HostToHostIntent) {
316 isRelevant = isIntentRelevant((HostToHostIntent) intent, hosts);
317 } else if (intent instanceof PointToPointIntent) {
318 isRelevant = isIntentRelevant((PointToPointIntent) intent, edgePoints);
319 } else if (intent instanceof MultiPointToSinglePointIntent) {
320 isRelevant = isIntentRelevant((MultiPointToSinglePointIntent) intent, edgePoints);
321 } else if (intent instanceof OpticalConnectivityIntent) {
322 opticalIntents.add((OpticalConnectivityIntent) intent);
323 }
324 // TODO: add other intents, e.g. SinglePointToMultiPointIntent
325
326 if (isRelevant) {
327 intents.add(intent);
328 }
329 }
330 }
331
332 for (OpticalConnectivityIntent intent : opticalIntents) {
333 if (isIntentRelevant(intent, intents)) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800334 intents.add(intent);
335 }
336 }
337 return intents;
338 }
339
340 // Indicates whether the specified intent involves all of the given hosts.
341 private boolean isIntentRelevant(HostToHostIntent intent, Set<Host> hosts) {
342 for (Host host : hosts) {
343 HostId id = host.id();
344 // Bail if intent does not involve this host.
345 if (!id.equals(intent.one()) && !id.equals(intent.two())) {
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(PointToPointIntent 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 !point.equals(intent.ingressPoint())) {
359 return false;
360 }
361 }
362 return true;
363 }
364
365 // Indicates whether the specified intent involves all of the given edge points.
366 private boolean isIntentRelevant(MultiPointToSinglePointIntent intent,
367 Set<ConnectPoint> edgePoints) {
368 for (ConnectPoint point : edgePoints) {
369 // Bail if intent does not involve this edge point.
370 if (!point.equals(intent.egressPoint()) &&
371 !intent.ingressPoints().contains(point)) {
372 return false;
373 }
374 }
375 return true;
376 }
377
Thomas Vachuska22e34922014-11-14 00:40:55 -0800378 // Indicates whether the specified intent involves all of the given edge points.
379 private boolean isIntentRelevant(OpticalConnectivityIntent opticalIntent,
380 Set<Intent> intents) {
381 for (Intent intent : intents) {
382 List<Intent> installables = intentService.getInstallableIntents(intent.id());
383 for (Intent installable : installables) {
384 if (installable instanceof PathIntent) {
385 Path path = ((PathIntent) installable).path();
386 if (opticalIntent.getSrcConnectPoint().equals(path.src()) &&
387 opticalIntent.getDst().equals(path.dst())) {
388 return true;
389 }
390 }
391 }
392 }
393 return false;
394 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800395
396 // Produces a set of all host ids listed in the specified JSON array.
397 private Set<Host> getHosts(ArrayNode array) {
398 Set<Host> hosts = new HashSet<>();
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800399 if (array != null) {
400 for (JsonNode node : array) {
401 try {
402 addHost(hosts, hostId(node.asText()));
403 } catch (IllegalArgumentException e) {
404 log.debug("Skipping ID {}", node.asText());
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800405 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800406 }
407 }
408 return hosts;
409 }
410
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800411 private void addHost(Set<Host> hosts, HostId hostId) {
412 Host host = hostService.getHost(hostId);
413 if (host != null) {
414 hosts.add(host);
415 }
416 }
417
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800418 // Produces a set of edge points from the specified set of hosts.
419 private Set<ConnectPoint> getEdgePoints(Set<Host> hosts) {
420 Set<ConnectPoint> edgePoints = new HashSet<>();
421 for (Host host : hosts) {
422 edgePoints.add(host.location());
423 }
424 return edgePoints;
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800425 }
426
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800427
428 // Adds all internal listeners.
429 private void addListeners() {
430 clusterService.addListener(clusterListener);
431 deviceService.addListener(deviceListener);
432 linkService.addListener(linkListener);
433 hostService.addListener(hostListener);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800434 intentService.addListener(intentListener);
435 }
436
437 // Removes all internal listeners.
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800438 private synchronized void removeListeners() {
439 if (!listenersRemoved) {
440 listenersRemoved = true;
441 clusterService.removeListener(clusterListener);
442 deviceService.removeListener(deviceListener);
443 linkService.removeListener(linkListener);
444 hostService.removeListener(hostListener);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800445 intentService.removeListener(intentListener);
446 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800447 }
448
449 // Cluster event listener.
450 private class InternalClusterListener implements ClusterEventListener {
451 @Override
452 public void event(ClusterEvent event) {
453 sendMessage(instanceMessage(event));
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800454 }
455 }
456
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800457 // Device event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800458 private class InternalDeviceListener implements DeviceListener {
459 @Override
460 public void event(DeviceEvent event) {
461 sendMessage(deviceMessage(event));
462 }
463 }
464
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800465 // Link event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800466 private class InternalLinkListener implements LinkListener {
467 @Override
468 public void event(LinkEvent event) {
469 sendMessage(linkMessage(event));
470 }
471 }
472
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800473 // Host event listener.
Thomas Vachuska690e5f62014-11-09 08:26:47 -0800474 private class InternalHostListener implements HostListener {
475 @Override
476 public void event(HostEvent event) {
477 sendMessage(hostMessage(event));
478 }
479 }
480
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800481 // Intent event listener.
Thomas Vachuska4830d392014-11-09 17:09:56 -0800482 private class InternalIntentListener implements IntentListener {
483 @Override
484 public void event(IntentEvent event) {
Thomas Vachuska22e34922014-11-14 00:40:55 -0800485 if (monitorRequest != null) {
486 requestTraffic(monitorRequest);
Thomas Vachuska4830d392014-11-09 17:09:56 -0800487 }
488 }
489 }
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800490
Thomas Vachuska22e34922014-11-14 00:40:55 -0800491 private class IntentTrafficMonitor extends TimerTask {
492 @Override
493 public void run() {
494 if (monitorRequest != null) {
495 requestTraffic(monitorRequest);
496 }
497 }
498 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800499}
500