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