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