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