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