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