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