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