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