Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onlab.onos.gui; |
| 17 | |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 20 | import org.eclipse.jetty.websocket.WebSocket; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 21 | import org.onlab.onos.cluster.ClusterEvent; |
| 22 | import org.onlab.onos.cluster.ClusterEventListener; |
| 23 | import org.onlab.onos.cluster.ControllerNode; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 24 | import org.onlab.onos.core.ApplicationId; |
| 25 | import org.onlab.onos.core.CoreService; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 26 | import org.onlab.onos.net.Device; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 27 | import org.onlab.onos.net.Host; |
| 28 | import org.onlab.onos.net.HostId; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 29 | import org.onlab.onos.net.Link; |
| 30 | import org.onlab.onos.net.device.DeviceEvent; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 31 | import org.onlab.onos.net.device.DeviceListener; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 32 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 33 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 34 | import org.onlab.onos.net.host.HostEvent; |
| 35 | import org.onlab.onos.net.host.HostListener; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 36 | import org.onlab.onos.net.intent.HostToHostIntent; |
| 37 | import org.onlab.onos.net.intent.Intent; |
| 38 | import org.onlab.onos.net.intent.IntentEvent; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 39 | import org.onlab.onos.net.intent.IntentListener; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 40 | import org.onlab.onos.net.link.LinkEvent; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 41 | import org.onlab.onos.net.link.LinkListener; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 42 | import org.onlab.osgi.ServiceDirectory; |
| 43 | |
| 44 | import java.io.IOException; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 45 | import java.util.Set; |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 46 | import java.util.Timer; |
| 47 | import java.util.TimerTask; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 48 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 49 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 50 | import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 51 | import static org.onlab.onos.net.DeviceId.deviceId; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 52 | import static org.onlab.onos.net.HostId.hostId; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 53 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 54 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 55 | import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 56 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Web socket capable of interacting with the GUI topology view. |
| 59 | */ |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 60 | public class TopologyViewWebSocket |
| 61 | extends TopologyViewMessages |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 62 | implements WebSocket.OnTextMessage, WebSocket.OnControl { |
| 63 | |
| 64 | private static final long MAX_AGE_MS = 15000; |
| 65 | |
| 66 | private static final byte PING = 0x9; |
| 67 | private static final byte PONG = 0xA; |
| 68 | private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad}; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 69 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 70 | private static final String APP_ID = "org.onlab.onos.gui"; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 71 | |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 72 | private static final long TRAFFIC_FREQUENCY_SEC = 1000; |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 73 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 74 | private final ApplicationId appId; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 75 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 76 | private Connection connection; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 77 | private FrameConnection control; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 78 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 79 | private final ClusterEventListener clusterListener = new InternalClusterListener(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 80 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 81 | private final LinkListener linkListener = new InternalLinkListener(); |
| 82 | private final HostListener hostListener = new InternalHostListener(); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 83 | private final IntentListener intentListener = new InternalIntentListener(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 84 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 85 | // Intents that are being monitored for the GUI |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 86 | private ObjectNode monitorRequest; |
| 87 | private final Timer timer = new Timer("intent-traffic-monitor"); |
| 88 | private final TimerTask timerTask = new IntentTrafficMonitor(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 89 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 90 | private long lastActive = System.currentTimeMillis(); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 91 | private boolean listenersRemoved = false; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 92 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame^] | 93 | private TopologyViewIntentFilter intentFilter; |
| 94 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Creates a new web-socket for serving data to GUI topology view. |
| 97 | * |
| 98 | * @param directory service directory |
| 99 | */ |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 100 | public TopologyViewWebSocket(ServiceDirectory directory) { |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 101 | super(directory); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame^] | 102 | |
| 103 | intentFilter = new TopologyViewIntentFilter(intentService, deviceService, |
| 104 | hostService, linkService); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 105 | appId = directory.get(CoreService.class).registerApplication(APP_ID); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 108 | /** |
| 109 | * Issues a close on the connection. |
| 110 | */ |
| 111 | synchronized void close() { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 112 | removeListeners(); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 113 | if (connection.isOpen()) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 114 | connection.close(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Indicates if this connection is idle. |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 120 | * |
| 121 | * @return true if idle or closed |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 122 | */ |
| 123 | synchronized boolean isIdle() { |
| 124 | boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS; |
| 125 | if (idle || !connection.isOpen()) { |
| 126 | return true; |
| 127 | } |
| 128 | try { |
| 129 | control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 130 | } catch (IOException e) { |
| 131 | log.warn("Unable to send ping message due to: ", e); |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 136 | @Override |
| 137 | public void onOpen(Connection connection) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 138 | log.info("GUI client connected"); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 139 | this.connection = connection; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 140 | this.control = (FrameConnection) connection; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 141 | addListeners(); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 142 | timer.schedule(timerTask, TRAFFIC_FREQUENCY_SEC, TRAFFIC_FREQUENCY_SEC); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 143 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 144 | sendAllInstances(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 145 | sendAllDevices(); |
| 146 | sendAllLinks(); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 147 | sendAllHosts(); |
| 148 | } |
| 149 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 150 | @Override |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 151 | public synchronized void onClose(int closeCode, String message) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 152 | removeListeners(); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 153 | timer.cancel(); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 154 | 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 Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void onMessage(String data) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 165 | lastActive = System.currentTimeMillis(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 166 | try { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame^] | 167 | processMessage((ObjectNode) mapper.reader().readTree(data)); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 168 | } catch (Exception e) { |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 169 | log.warn("Unable to parse GUI request {} due to {}", data, e); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 170 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame^] | 173 | // Processes the specified event. |
| 174 | private void processMessage(ObjectNode event) { |
| 175 | String type = string(event, "event", "unknown"); |
| 176 | if (type.equals("requestDetails")) { |
| 177 | requestDetails(event); |
| 178 | } else if (type.equals("updateMeta")) { |
| 179 | updateMetaUi(event); |
| 180 | } else if (type.equals("addHostIntent")) { |
| 181 | createHostIntent(event); |
| 182 | } else if (type.equals("requestTraffic")) { |
| 183 | requestTraffic(event); |
| 184 | } else if (type.equals("requestAllTraffic")) { |
| 185 | requestAllTraffic(event); |
| 186 | } else if (type.equals("cancelTraffic")) { |
| 187 | cancelTraffic(event); |
| 188 | } |
| 189 | } |
| 190 | |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 191 | // Sends the specified data to the client. |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 192 | private synchronized void sendMessage(ObjectNode data) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 193 | try { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 194 | if (connection.isOpen()) { |
| 195 | connection.sendMessage(data.toString()); |
| 196 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 197 | } catch (IOException e) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 198 | log.warn("Unable to send message {} to GUI due to {}", data, e); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 199 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 202 | // Sends all controller nodes to the client as node-added messages. |
| 203 | private void sendAllInstances() { |
| 204 | for (ControllerNode node : clusterService.getNodes()) { |
| 205 | sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 206 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 209 | // Sends all devices to the client as device-added messages. |
| 210 | private void sendAllDevices() { |
| 211 | for (Device device : deviceService.getDevices()) { |
| 212 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 213 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 216 | // Sends all links to the client as link-added messages. |
| 217 | private void sendAllLinks() { |
| 218 | for (Link link : linkService.getLinks()) { |
| 219 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 220 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Sends all hosts to the client as host-added messages. |
| 224 | private void sendAllHosts() { |
| 225 | for (Host host : hostService.getHosts()) { |
| 226 | sendMessage(hostMessage(new HostEvent(HOST_ADDED, host))); |
| 227 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Sends back device or host details. |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 231 | private void requestDetails(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 232 | ObjectNode payload = payload(event); |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 233 | String type = string(payload, "class", "unknown"); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 234 | long sid = number(event, "sid"); |
| 235 | |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 236 | if (type.equals("device")) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 237 | sendMessage(deviceDetails(deviceId(string(payload, "id")), sid)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 238 | } else if (type.equals("host")) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 239 | sendMessage(hostDetails(hostId(string(payload, "id")), sid)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 243 | // Creates host-to-host intent. |
| 244 | private void createHostIntent(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 245 | ObjectNode payload = payload(event); |
| 246 | long id = number(event, "sid"); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 247 | // TODO: add protection against device ids and non-existent hosts. |
| 248 | HostId one = hostId(string(payload, "one")); |
| 249 | HostId two = hostId(string(payload, "two")); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 250 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 251 | HostToHostIntent hostIntent = new HostToHostIntent(appId, one, two, |
| 252 | DefaultTrafficSelector.builder().build(), |
| 253 | DefaultTrafficTreatment.builder().build()); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 254 | monitorRequest = event; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 255 | intentService.submit(hostIntent); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame^] | 258 | // Subscribes for host traffic messages. |
| 259 | private synchronized void requestAllTraffic(ObjectNode event) { |
| 260 | ObjectNode payload = payload(event); |
| 261 | long sid = number(event, "sid"); |
| 262 | monitorRequest = event; |
| 263 | sendMessage(trafficSummaryMessage(sid)); |
| 264 | } |
| 265 | |
| 266 | // Subscribes for host traffic messages. |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 267 | private synchronized void requestTraffic(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 268 | ObjectNode payload = payload(event); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame^] | 269 | if (!payload.has("ids")) { |
| 270 | return; |
| 271 | } |
| 272 | |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 273 | long sid = number(event, "sid"); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 274 | monitorRequest = event; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 275 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 276 | // Get the set of selected hosts and their intents. |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame^] | 277 | ArrayNode ids = (ArrayNode) payload.path("ids"); |
| 278 | Set<Host> hosts = getHosts(ids); |
| 279 | Set<Device> devices = getDevices(ids); |
| 280 | Set<Intent> intents = intentFilter.findPathIntents(hosts, devices); |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 281 | |
| 282 | // If there is a hover node, include it in the hosts and find intents. |
| 283 | String hover = string(payload, "hover"); |
| 284 | Set<Intent> hoverIntents; |
| 285 | if (!isNullOrEmpty(hover)) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame^] | 286 | addHover(hosts, devices, hover); |
| 287 | hoverIntents = intentFilter.findPathIntents(hosts, devices); |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 288 | intents.removeAll(hoverIntents); |
| 289 | |
| 290 | // Send an initial message to highlight all links of all monitored intents. |
| 291 | sendMessage(trafficMessage(sid, |
| 292 | new TrafficClass("primary", hoverIntents), |
| 293 | new TrafficClass("secondary", intents))); |
| 294 | |
| 295 | } else { |
| 296 | // Send an initial message to highlight all links of all monitored intents. |
| 297 | sendMessage(trafficMessage(sid, new TrafficClass("primary", intents))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | // Cancels sending traffic messages. |
| 302 | private void cancelTraffic(ObjectNode event) { |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 303 | sendMessage(trafficMessage(number(event, "sid"))); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 304 | monitorRequest = null; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 307 | // Adds all internal listeners. |
| 308 | private void addListeners() { |
| 309 | clusterService.addListener(clusterListener); |
| 310 | deviceService.addListener(deviceListener); |
| 311 | linkService.addListener(linkListener); |
| 312 | hostService.addListener(hostListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 313 | intentService.addListener(intentListener); |
| 314 | } |
| 315 | |
| 316 | // Removes all internal listeners. |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 317 | private synchronized void removeListeners() { |
| 318 | if (!listenersRemoved) { |
| 319 | listenersRemoved = true; |
| 320 | clusterService.removeListener(clusterListener); |
| 321 | deviceService.removeListener(deviceListener); |
| 322 | linkService.removeListener(linkListener); |
| 323 | hostService.removeListener(hostListener); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 324 | intentService.removeListener(intentListener); |
| 325 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // Cluster event listener. |
| 329 | private class InternalClusterListener implements ClusterEventListener { |
| 330 | @Override |
| 331 | public void event(ClusterEvent event) { |
| 332 | sendMessage(instanceMessage(event)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 336 | // Device event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 337 | private class InternalDeviceListener implements DeviceListener { |
| 338 | @Override |
| 339 | public void event(DeviceEvent event) { |
| 340 | sendMessage(deviceMessage(event)); |
| 341 | } |
| 342 | } |
| 343 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 344 | // Link event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 345 | private class InternalLinkListener implements LinkListener { |
| 346 | @Override |
| 347 | public void event(LinkEvent event) { |
| 348 | sendMessage(linkMessage(event)); |
| 349 | } |
| 350 | } |
| 351 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 352 | // Host event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 353 | private class InternalHostListener implements HostListener { |
| 354 | @Override |
| 355 | public void event(HostEvent event) { |
| 356 | sendMessage(hostMessage(event)); |
| 357 | } |
| 358 | } |
| 359 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 360 | // Intent event listener. |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 361 | private class InternalIntentListener implements IntentListener { |
| 362 | @Override |
| 363 | public void event(IntentEvent event) { |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 364 | if (monitorRequest != null) { |
| 365 | requestTraffic(monitorRequest); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 369 | |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 370 | private class IntentTrafficMonitor extends TimerTask { |
| 371 | @Override |
| 372 | public void run() { |
| 373 | if (monitorRequest != null) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame^] | 374 | String type = string(monitorRequest, "event", "unknown"); |
| 375 | if (type.equals("requestAllTraffic")) { |
| 376 | requestAllTraffic(monitorRequest); |
| 377 | } else { |
| 378 | requestTraffic(monitorRequest); |
| 379 | } |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 383 | } |
| 384 | |