Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 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.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.eclipse.jetty.websocket.WebSocket; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 23 | import org.onlab.osgi.ServiceNotFoundException; |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.ControllerNode; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 26 | import org.onosproject.ui.UiConnection; |
| 27 | import org.onosproject.ui.UiExtensionService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.UiMessageHandlerFactory; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.UiTopoOverlayFactory; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.impl.topo.UiTopoSession; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.topo.TopoConstants; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.util.HashMap; |
| 39 | import java.util.Map; |
| 40 | |
| 41 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 42 | * Web socket capable of interacting with the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 43 | */ |
| 44 | public class UiWebSocket |
| 45 | implements UiConnection, WebSocket.OnTextMessage, WebSocket.OnControl { |
| 46 | |
| 47 | private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class); |
| 48 | |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 49 | private static final long MAX_AGE_MS = 30_000; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 50 | |
| 51 | private static final byte PING = 0x9; |
| 52 | private static final byte PONG = 0xA; |
| 53 | private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad}; |
| 54 | |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 55 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 56 | private final ServiceDirectory directory; |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 57 | private final UiTopoSession topoSession; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 58 | |
| 59 | private Connection connection; |
| 60 | private FrameConnection control; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 61 | private String userName; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 62 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 63 | private long lastActive = System.currentTimeMillis(); |
| 64 | |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 65 | private Map<String, UiMessageHandler> handlers; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 66 | private TopoOverlayCache overlayCache; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 69 | * Creates a new web-socket for serving data to the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 70 | * |
| 71 | * @param directory service directory |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 72 | * @param userName user name of the logged-in user |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 73 | */ |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 74 | public UiWebSocket(ServiceDirectory directory, String userName) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 75 | this.directory = directory; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 76 | this.userName = userName; |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 77 | this.topoSession = |
| 78 | new UiTopoSession(this, directory.get(UiSharedTopologyModel.class)); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public String userName() { |
| 83 | return userName; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Issues a close on the connection. |
| 88 | */ |
| 89 | synchronized void close() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 90 | destroyHandlersAndOverlays(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 91 | if (connection.isOpen()) { |
| 92 | connection.close(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Indicates if this connection is idle. |
| 98 | * |
| 99 | * @return true if idle or closed |
| 100 | */ |
| 101 | synchronized boolean isIdle() { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 102 | long quietFor = System.currentTimeMillis() - lastActive; |
| 103 | boolean idle = quietFor > MAX_AGE_MS; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 104 | if (idle || (connection != null && !connection.isOpen())) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 105 | log.debug("IDLE (or closed) websocket [{} ms]", quietFor); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 106 | return true; |
| 107 | } else if (connection != null) { |
| 108 | try { |
| 109 | control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 110 | } catch (IOException e) { |
| 111 | log.warn("Unable to send ping message due to: ", e); |
| 112 | } |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | @Override |
Satish K | 598c28d | 2015-11-24 17:20:40 +0530 | [diff] [blame] | 118 | public synchronized void onOpen(Connection connection) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 119 | this.connection = connection; |
| 120 | this.control = (FrameConnection) connection; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 121 | try { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 122 | topoSession.init(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 123 | createHandlersAndOverlays(); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 124 | sendInstanceData(); |
| 125 | log.info("GUI client connected"); |
| 126 | |
| 127 | } catch (ServiceNotFoundException e) { |
Brian O'Connor | 75deea6 | 2015-06-24 16:09:17 -0400 | [diff] [blame] | 128 | log.warn("Unable to open GUI connection; services have been shut-down", e); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 129 | this.connection.close(); |
| 130 | this.connection = null; |
| 131 | this.control = null; |
| 132 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public synchronized void onClose(int closeCode, String message) { |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 137 | topoSession.destroy(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 138 | destroyHandlersAndOverlays(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 139 | log.info("GUI client disconnected [close-code={}, message={}]", |
| 140 | closeCode, message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public boolean onControl(byte controlCode, byte[] data, int offset, int length) { |
| 145 | lastActive = System.currentTimeMillis(); |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void onMessage(String data) { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 151 | log.debug("onMessage: {}", data); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 152 | lastActive = System.currentTimeMillis(); |
| 153 | try { |
| 154 | ObjectNode message = (ObjectNode) mapper.reader().readTree(data); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 155 | String type = message.path("event").asText("unknown"); |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 156 | UiMessageHandler handler = handlers.get(type); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 157 | if (handler != null) { |
| 158 | handler.process(message); |
| 159 | } else { |
| 160 | log.warn("No GUI message handler for type {}", type); |
| 161 | } |
| 162 | } catch (Exception e) { |
| 163 | log.warn("Unable to parse GUI message {} due to {}", data, e); |
| 164 | log.debug("Boom!!!", e); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | @Override |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 169 | public synchronized void sendMessage(ObjectNode message) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 170 | try { |
| 171 | if (connection.isOpen()) { |
| 172 | connection.sendMessage(message.toString()); |
| 173 | } |
| 174 | } catch (IOException e) { |
| 175 | log.warn("Unable to send message {} to GUI due to {}", message, e); |
| 176 | log.debug("Boom!!!", e); |
| 177 | } |
| 178 | } |
| 179 | |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 180 | @Override |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 181 | public synchronized void sendMessage(String type, long sid, ObjectNode payload) { |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 182 | ObjectNode message = mapper.createObjectNode(); |
| 183 | message.put("event", type); |
| 184 | if (sid > 0) { |
| 185 | message.put("sid", sid); |
| 186 | } |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 187 | message.set("payload", payload != null ? payload : mapper.createObjectNode()); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 188 | sendMessage(message); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 191 | // Creates new message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 192 | private synchronized void createHandlersAndOverlays() { |
| 193 | log.debug("creating handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 194 | handlers = new HashMap<>(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 195 | overlayCache = new TopoOverlayCache(); |
| 196 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 197 | UiExtensionService service = directory.get(UiExtensionService.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 198 | service.getExtensions().forEach(ext -> { |
| 199 | UiMessageHandlerFactory factory = ext.messageHandlerFactory(); |
| 200 | if (factory != null) { |
| 201 | factory.newHandlers().forEach(handler -> { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 202 | try { |
| 203 | handler.init(this, directory); |
| 204 | handler.messageTypes().forEach(type -> handlers.put(type, handler)); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 205 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 206 | // need to inject the overlay cache into topology message handler |
| 207 | if (handler instanceof TopologyViewMessageHandler) { |
| 208 | ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache); |
| 209 | } |
| 210 | } catch (Exception e) { |
| 211 | log.warn("Unable to setup handler {} due to", handler, e); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 212 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 213 | }); |
| 214 | } |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 215 | |
| 216 | UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory(); |
| 217 | if (overlayFactory != null) { |
| 218 | overlayFactory.newOverlays().forEach(overlayCache::add); |
| 219 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 220 | }); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 221 | log.debug("#handlers = {}, #overlays = {}", handlers.size(), |
| 222 | overlayCache.size()); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Destroys message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 226 | private synchronized void destroyHandlersAndOverlays() { |
| 227 | log.debug("destroying handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 228 | handlers.forEach((type, handler) -> handler.destroy()); |
| 229 | handlers.clear(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 230 | |
| 231 | if (overlayCache != null) { |
| 232 | overlayCache.destroy(); |
| 233 | overlayCache = null; |
| 234 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 235 | } |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 236 | |
| 237 | // Sends cluster node/instance information to allow GUI to fail-over. |
| 238 | private void sendInstanceData() { |
| 239 | ClusterService service = directory.get(ClusterService.class); |
| 240 | ArrayNode instances = mapper.createArrayNode(); |
| 241 | |
| 242 | for (ControllerNode node : service.getNodes()) { |
| 243 | ObjectNode instance = mapper.createObjectNode() |
| 244 | .put("id", node.id().toString()) |
| 245 | .put("ip", node.ip().toString()) |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 246 | .put(TopoConstants.Glyphs.UI_ATTACHED, |
| 247 | node.equals(service.getLocalNode())); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 248 | instances.add(instance); |
| 249 | } |
| 250 | |
| 251 | ObjectNode payload = mapper.createObjectNode(); |
Thomas Vachuska | 20084b7 | 2015-03-11 13:46:50 -0700 | [diff] [blame] | 252 | payload.set("clusterNodes", instances); |
| 253 | sendMessage("bootstrap", 0, payload); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 256 | } |
| 257 | |