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