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