Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.GlyphConstants; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 27 | import org.onosproject.ui.UiConnection; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.UiExtension; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 29 | import org.onosproject.ui.UiExtensionService; |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 30 | import org.onosproject.ui.UiGlyph; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.UiMessageHandlerFactory; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.UiSessionToken; |
| 34 | import org.onosproject.ui.UiTokenService; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.UiTopo2OverlayFactory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.UiTopoOverlayFactory; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 38 | import org.onosproject.ui.impl.topo.Topo2Jsonifier; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 39 | import org.onosproject.ui.impl.topo.Topo2OverlayCache; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 40 | import org.onosproject.ui.impl.topo.Topo2TrafficMessageHandler; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 41 | import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 42 | import org.onosproject.ui.impl.topo.UiTopoSession; |
| 43 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 44 | import org.onosproject.ui.lion.LionBundle; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 45 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
| 49 | import java.io.IOException; |
| 50 | import java.util.HashMap; |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 51 | import java.util.Locale; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 52 | import java.util.Map; |
| 53 | |
| 54 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 55 | * Web socket capable of interacting with the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 56 | */ |
| 57 | public class UiWebSocket |
| 58 | implements UiConnection, WebSocket.OnTextMessage, WebSocket.OnControl { |
| 59 | |
| 60 | private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class); |
| 61 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 62 | private static final String EVENT = "event"; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 63 | private static final String PAYLOAD = "payload"; |
| 64 | private static final String UNKNOWN = "unknown"; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 65 | private static final String AUTHENTICATION = "authentication"; |
| 66 | private static final String TOKEN = "token"; |
| 67 | private static final String ERROR = "error"; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 68 | |
| 69 | private static final String ID = "id"; |
| 70 | private static final String IP = "ip"; |
| 71 | private static final String CLUSTER_NODES = "clusterNodes"; |
| 72 | private static final String USER = "user"; |
| 73 | private static final String BOOTSTRAP = "bootstrap"; |
| 74 | |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 75 | private static final String UBERLION = "uberlion"; |
| 76 | private static final String LION = "lion"; |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 77 | private static final String LOCALE = "locale"; |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 78 | |
Simon Hunt | d7395c8 | 2016-10-20 17:54:01 -0700 | [diff] [blame] | 79 | private static final String TOPO = "topo"; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 80 | |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 81 | private static final String GLYPHS = "glyphs"; |
| 82 | |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 83 | private static final long MAX_AGE_MS = 30_000; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 84 | |
| 85 | private static final byte PING = 0x9; |
| 86 | private static final byte PONG = 0xA; |
| 87 | private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad}; |
| 88 | |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 89 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 90 | private final ServiceDirectory directory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 91 | private final UiTopoSession topoSession; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 92 | |
| 93 | private Connection connection; |
| 94 | private FrameConnection control; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 95 | private String userName; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 96 | private String currentView; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 97 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 98 | private long lastActive = System.currentTimeMillis(); |
| 99 | |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 100 | private Map<String, UiMessageHandler> handlers; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 101 | private TopoOverlayCache overlayCache; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 102 | private Topo2OverlayCache overlay2Cache; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 103 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 104 | private Map<String, LionBundle> lionBundleMap; |
| 105 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 106 | private UiSessionToken sessionToken; |
| 107 | |
| 108 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 109 | /** |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 110 | * Creates a new web-socket for serving data to the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 111 | * |
| 112 | * @param directory service directory |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 113 | * @param userName user name of the logged-in user |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 114 | */ |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 115 | public UiWebSocket(ServiceDirectory directory, String userName) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 116 | this.directory = directory; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 117 | this.userName = userName; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 118 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 119 | Topo2Jsonifier t2json = new Topo2Jsonifier(directory, userName); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 120 | UiSharedTopologyModel sharedModel = directory.get(UiSharedTopologyModel.class); |
| 121 | UiTopoLayoutService layoutService = directory.get(UiTopoLayoutService.class); |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 122 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 123 | sharedModel.injectJsonifier(t2json); |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 124 | topoSession = new UiTopoSession(this, t2json, sharedModel, layoutService); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 125 | sessionToken = null; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String userName() { |
| 130 | return userName; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public UiTopoLayout currentLayout() { |
| 135 | return topoSession.currentLayout(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void setCurrentLayout(UiTopoLayout topoLayout) { |
| 140 | topoSession.setCurrentLayout(topoLayout); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String currentView() { |
| 145 | return currentView; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void setCurrentView(String viewId) { |
| 150 | currentView = viewId; |
| 151 | topoSession.enableEvent(viewId.equals(TOPO)); |
| 152 | } |
| 153 | |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 154 | private ObjectNode objectNode() { |
| 155 | return mapper.createObjectNode(); |
| 156 | } |
| 157 | |
| 158 | private ArrayNode arrayNode() { |
| 159 | return mapper.createArrayNode(); |
| 160 | } |
| 161 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 162 | /** |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 163 | * Provides a reference to the topology session. |
| 164 | * |
| 165 | * @return topo session reference |
| 166 | */ |
| 167 | public UiTopoSession topoSession() { |
| 168 | return topoSession; |
| 169 | } |
| 170 | |
| 171 | /** |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 172 | * Issues a close on the connection. |
| 173 | */ |
| 174 | synchronized void close() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 175 | destroyHandlersAndOverlays(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 176 | if (connection.isOpen()) { |
| 177 | connection.close(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Indicates if this connection is idle. |
| 183 | * |
| 184 | * @return true if idle or closed |
| 185 | */ |
| 186 | synchronized boolean isIdle() { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 187 | long quietFor = System.currentTimeMillis() - lastActive; |
| 188 | boolean idle = quietFor > MAX_AGE_MS; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 189 | if (idle || (connection != null && !connection.isOpen())) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 190 | log.debug("IDLE (or closed) websocket [{} ms]", quietFor); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 191 | return true; |
| 192 | } else if (connection != null) { |
| 193 | try { |
| 194 | control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 195 | } catch (IOException e) { |
| 196 | log.warn("Unable to send ping message due to: ", e); |
| 197 | } |
| 198 | } |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | @Override |
Satish K | 598c28d | 2015-11-24 17:20:40 +0530 | [diff] [blame] | 203 | public synchronized void onOpen(Connection connection) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 204 | this.connection = connection; |
| 205 | this.control = (FrameConnection) connection; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 206 | try { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 207 | topoSession.init(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 208 | createHandlersAndOverlays(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 209 | sendBootstrapData(); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 210 | sendUberLionBundle(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 211 | log.info("GUI client connected -- user <{}>", userName); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 212 | |
| 213 | } catch (ServiceNotFoundException e) { |
Brian O'Connor | 75deea6 | 2015-06-24 16:09:17 -0400 | [diff] [blame] | 214 | 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] | 215 | this.connection.close(); |
| 216 | this.connection = null; |
| 217 | this.control = null; |
| 218 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public synchronized void onClose(int closeCode, String message) { |
dvaddire | b09fdf3 | 2017-07-04 11:42:53 +0530 | [diff] [blame] | 223 | try { |
Thomas Vachuska | 3ed7585 | 2018-01-17 11:53:21 -0800 | [diff] [blame] | 224 | try { |
| 225 | tokenService().revokeToken(sessionToken); |
| 226 | log.info("Session token revoked"); |
| 227 | } catch (ServiceNotFoundException e) { |
| 228 | log.error("Unable to reference UiTokenService"); |
| 229 | } |
| 230 | sessionToken = null; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 231 | |
Thomas Vachuska | 3ed7585 | 2018-01-17 11:53:21 -0800 | [diff] [blame] | 232 | topoSession.destroy(); |
| 233 | destroyHandlersAndOverlays(); |
| 234 | } catch (Exception e) { |
| 235 | log.warn("Unexpected error", e); |
| 236 | } |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 237 | log.info("GUI client disconnected [close-code={}, message={}]", |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 238 | closeCode, message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public boolean onControl(byte controlCode, byte[] data, int offset, int length) { |
| 243 | lastActive = System.currentTimeMillis(); |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void onMessage(String data) { |
| 249 | lastActive = System.currentTimeMillis(); |
| 250 | try { |
| 251 | ObjectNode message = (ObjectNode) mapper.reader().readTree(data); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 252 | String type = message.path(EVENT).asText(UNKNOWN); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 253 | |
| 254 | if (sessionToken == null) { |
| 255 | authenticate(type, message); |
| 256 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 257 | } else { |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 258 | UiMessageHandler handler = handlers.get(type); |
| 259 | if (handler != null) { |
| 260 | log.debug("RX message: {}", message); |
| 261 | handler.process(message); |
| 262 | } else { |
| 263 | log.warn("No GUI message handler for type {}", type); |
| 264 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 265 | } |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 266 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 267 | } catch (Exception e) { |
| 268 | log.warn("Unable to parse GUI message {} due to {}", data, e); |
| 269 | log.debug("Boom!!!", e); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | @Override |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 274 | public synchronized void sendMessage(ObjectNode message) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 275 | try { |
| 276 | if (connection.isOpen()) { |
| 277 | connection.sendMessage(message.toString()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 278 | log.debug("TX message: {}", message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 279 | } |
| 280 | } catch (IOException e) { |
| 281 | log.warn("Unable to send message {} to GUI due to {}", message, e); |
| 282 | log.debug("Boom!!!", e); |
| 283 | } |
| 284 | } |
| 285 | |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 286 | @Override |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 287 | public synchronized void sendMessage(String type, ObjectNode payload) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 288 | ObjectNode message = objectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 289 | message.put(EVENT, type); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 290 | message.set(PAYLOAD, payload != null ? payload : objectNode()); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 291 | sendMessage(message); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 294 | // Creates new message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 295 | private synchronized void createHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 296 | log.debug("Creating handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 297 | handlers = new HashMap<>(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 298 | overlayCache = new TopoOverlayCache(); |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 299 | overlay2Cache = new Topo2OverlayCache(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 300 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 301 | Map<Class<?>, UiMessageHandler> handlerInstances = new HashMap<>(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 302 | UiExtensionService service = directory.get(UiExtensionService.class); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 303 | lionBundleMap = generateLionMap(service); |
| 304 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 305 | service.getExtensions().forEach(ext -> { |
| 306 | UiMessageHandlerFactory factory = ext.messageHandlerFactory(); |
| 307 | if (factory != null) { |
| 308 | factory.newHandlers().forEach(handler -> { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 309 | try { |
| 310 | handler.init(this, directory); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 311 | injectLionBundles(handler, lionBundleMap); |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 312 | handler.messageTypes().forEach(type -> handlers.put(type, handler)); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 313 | handlerInstances.put(handler.getClass(), handler); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 314 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 315 | } catch (Exception e) { |
| 316 | log.warn("Unable to setup handler {} due to", handler, e); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 317 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 318 | }); |
| 319 | } |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 320 | registerOverlays(ext); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 321 | }); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 322 | |
| 323 | handlerCrossConnects(handlerInstances); |
| 324 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 325 | log.debug("#handlers = {}, #overlays = {}", |
| 326 | handlers.size(), overlayCache.size()); |
| 327 | } |
| 328 | |
| 329 | private Map<String, LionBundle> generateLionMap(UiExtensionService service) { |
| 330 | Map<String, LionBundle> bundles = new HashMap<>(); |
| 331 | service.getExtensions().forEach(ext -> { |
| 332 | ext.lionBundles().forEach(lb -> bundles.put(lb.id(), lb)); |
| 333 | }); |
| 334 | return bundles; |
| 335 | } |
| 336 | |
| 337 | private void injectLionBundles(UiMessageHandler handler, |
| 338 | Map<String, LionBundle> lionBundleMap) { |
| 339 | handler.requiredLionBundles().forEach(lbid -> { |
| 340 | LionBundle lb = lionBundleMap.get(lbid); |
| 341 | if (lb != null) { |
| 342 | handler.cacheLionBundle(lb); |
| 343 | } else { |
| 344 | log.warn("handler {}: Lion bundle {} non existent!", |
| 345 | handler.getClass().getName(), lbid); |
| 346 | } |
| 347 | }); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 350 | private void authenticate(String type, ObjectNode message) { |
| 351 | if (!AUTHENTICATION.equals(type)) { |
Thomas Vachuska | fa06365 | 2018-04-25 15:49:00 -0400 | [diff] [blame^] | 352 | log.warn("WebSocket not authenticated: {}", message); |
| 353 | close(); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 354 | return; |
| 355 | } |
| 356 | |
| 357 | String tokstr = message.path(PAYLOAD).path(TOKEN).asText(UNKNOWN); |
| 358 | UiSessionToken token = new UiSessionToken(tokstr); |
| 359 | |
| 360 | if (tokenService().isTokenValid(token)) { |
| 361 | sessionToken = token; |
| 362 | log.info("Session token authenticated"); |
| 363 | log.debug("WebSocket authenticated: {}", message); |
| 364 | } else { |
| 365 | log.warn("Invalid Authentication Token: {}", message); |
| 366 | sendMessage(ERROR, notAuthorized(token)); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | private ObjectNode notAuthorized(UiSessionToken token) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 371 | return objectNode() |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 372 | .put("message", "invalid authentication token") |
| 373 | .put("badToken", token.toString()); |
| 374 | } |
| 375 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 376 | private void registerOverlays(UiExtension ext) { |
| 377 | UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory(); |
| 378 | if (overlayFactory != null) { |
| 379 | overlayFactory.newOverlays().forEach(overlayCache::add); |
| 380 | } |
| 381 | |
| 382 | UiTopo2OverlayFactory overlay2Factory = ext.topo2OverlayFactory(); |
| 383 | if (overlay2Factory != null) { |
| 384 | overlay2Factory.newOverlays().forEach(overlay2Cache::add); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | private void handlerCrossConnects(Map<Class<?>, UiMessageHandler> handlers) { |
| 389 | TopologyViewMessageHandler topomh = (TopologyViewMessageHandler) |
| 390 | handlers.get(TopologyViewMessageHandler.class); |
| 391 | if (topomh != null) { |
| 392 | topomh.setOverlayCache(overlayCache); |
| 393 | } |
| 394 | |
| 395 | Topo2ViewMessageHandler topo2mh = (Topo2ViewMessageHandler) |
| 396 | handlers.get(Topo2ViewMessageHandler.class); |
| 397 | if (topo2mh != null) { |
| 398 | topo2mh.setOverlayCache(overlay2Cache); |
| 399 | |
| 400 | // We also need a link to Topo2Traffic |
| 401 | Topo2TrafficMessageHandler topo2traffic = (Topo2TrafficMessageHandler) |
| 402 | handlers.get(Topo2TrafficMessageHandler.class); |
| 403 | if (topo2traffic != null) { |
| 404 | topo2mh.setTrafficHandler(topo2traffic); |
| 405 | } else { |
| 406 | log.error("No topo2 traffic handler found"); |
| 407 | } |
| 408 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Destroys message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 412 | private synchronized void destroyHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 413 | log.debug("Destroying handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 414 | handlers.forEach((type, handler) -> handler.destroy()); |
| 415 | handlers.clear(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 416 | |
| 417 | if (overlayCache != null) { |
| 418 | overlayCache.destroy(); |
| 419 | overlayCache = null; |
| 420 | } |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 421 | if (overlay2Cache != null) { |
| 422 | overlay2Cache.destroy(); |
| 423 | overlay2Cache = null; |
| 424 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 425 | } |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 426 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 427 | // Sends initial information (username and cluster member information) |
| 428 | // to allow GUI to display logged-in user, and to be able to |
| 429 | // fail-over to an alternate cluster member if necessary. |
| 430 | private void sendBootstrapData() { |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 431 | ClusterService service = directory.get(ClusterService.class); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 432 | ArrayNode instances = arrayNode(); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 433 | |
| 434 | for (ControllerNode node : service.getNodes()) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 435 | ObjectNode instance = objectNode() |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 436 | .put(ID, node.id().toString()) |
| 437 | .put(IP, node.ip().toString()) |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 438 | .put(GlyphConstants.UI_ATTACHED, |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 439 | node.equals(service.getLocalNode())); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 440 | instances.add(instance); |
| 441 | } |
| 442 | |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 443 | ArrayNode glyphInstances = arrayNode(); |
| 444 | UiExtensionService uiExtensionService = directory.get(UiExtensionService.class); |
| 445 | for (UiGlyph glyph : uiExtensionService.getGlyphs()) { |
| 446 | ObjectNode glyphInstance = objectNode() |
| 447 | .put(GlyphConstants.ID, glyph.id()) |
| 448 | .put(GlyphConstants.VIEWBOX, glyph.viewbox()) |
| 449 | .put(GlyphConstants.PATH, glyph.path()); |
| 450 | glyphInstances.add(glyphInstance); |
| 451 | } |
| 452 | |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 453 | ObjectNode payload = objectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 454 | payload.set(CLUSTER_NODES, instances); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 455 | payload.set(GLYPHS, glyphInstances); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 456 | payload.put(USER, userName); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 457 | |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 458 | sendMessage(BOOTSTRAP, payload); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 461 | private UiTokenService tokenService() { |
dvaddire | b09fdf3 | 2017-07-04 11:42:53 +0530 | [diff] [blame] | 462 | return directory.get(UiTokenService.class); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 463 | } |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 464 | |
| 465 | // sends the collated localization bundle data up to the client. |
| 466 | private void sendUberLionBundle() { |
| 467 | UiExtensionService service = directory.get(UiExtensionService.class); |
| 468 | ObjectNode lion = objectNode(); |
| 469 | |
| 470 | service.getExtensions().forEach(ext -> { |
| 471 | ext.lionBundles().forEach(lb -> { |
| 472 | ObjectNode lionMap = objectNode(); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 473 | lb.getItems().forEach(item -> lionMap.put(item.key(), item.value())); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 474 | lion.set(lb.id(), lionMap); |
| 475 | }); |
| 476 | }); |
| 477 | |
| 478 | ObjectNode payload = objectNode(); |
| 479 | payload.set(LION, lion); |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 480 | payload.put(LOCALE, Locale.getDefault().toString()); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 481 | sendMessage(UBERLION, payload); |
| 482 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 483 | } |