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