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