blob: 2e7e643ead61fe16b2079cef13eafd5b21ba27fe [file] [log] [blame]
Thomas Vachuska3553b302015-03-07 14:49:43 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska3553b302015-03-07 14:49:43 -08003 *
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 */
16package org.onosproject.ui.impl;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuska3553b302015-03-07 14:49:43 -080020import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.eclipse.jetty.websocket.api.Session;
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070022import org.eclipse.jetty.websocket.api.WebSocketAdapter;
Thomas Vachuska3553b302015-03-07 14:49:43 -080023import org.onlab.osgi.ServiceDirectory;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -070024import org.onlab.osgi.ServiceNotFoundException;
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -070025import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.ControllerNode;
Simon Hunt8add9ee2016-09-20 17:05:07 -070027import org.onosproject.ui.GlyphConstants;
Thomas Vachuska3553b302015-03-07 14:49:43 -080028import org.onosproject.ui.UiConnection;
Simon Hunt2d7cd6f2017-05-04 13:04:50 -070029import org.onosproject.ui.UiExtension;
Thomas Vachuska3553b302015-03-07 14:49:43 -080030import org.onosproject.ui.UiExtensionService;
Laszlo Papp759f0d32018-03-05 13:24:30 +000031import org.onosproject.ui.UiGlyph;
Simon Hunta0ddb022015-05-01 09:53:01 -070032import org.onosproject.ui.UiMessageHandler;
Simon Hunt7715e892016-04-12 19:55:32 -070033import org.onosproject.ui.UiMessageHandlerFactory;
Simon Hunt1169c952017-06-05 11:20:11 -070034import org.onosproject.ui.UiSessionToken;
35import org.onosproject.ui.UiTokenService;
Simon Hunt22c35df2017-04-26 17:28:42 -070036import org.onosproject.ui.UiTopo2OverlayFactory;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070037import org.onosproject.ui.UiTopoLayoutService;
Simon Hunte05cae42015-07-23 17:35:24 -070038import org.onosproject.ui.UiTopoOverlayFactory;
Simon Hunt537bc762016-12-20 12:15:13 -080039import org.onosproject.ui.impl.topo.Topo2Jsonifier;
Simon Hunt22c35df2017-04-26 17:28:42 -070040import org.onosproject.ui.impl.topo.Topo2OverlayCache;
Simon Hunt2d7cd6f2017-05-04 13:04:50 -070041import org.onosproject.ui.impl.topo.Topo2TrafficMessageHandler;
Simon Hunt22c35df2017-04-26 17:28:42 -070042import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler;
Sean Condonadeb7162019-04-13 20:56:14 +010043import org.onosproject.ui.impl.topo.Traffic2Overlay;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070044import org.onosproject.ui.impl.topo.UiTopoSession;
45import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
Simon Hunt879ce452017-08-10 23:32:00 -070046import org.onosproject.ui.lion.LionBundle;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070047import org.onosproject.ui.model.topo.UiTopoLayout;
Thomas Vachuska3553b302015-03-07 14:49:43 -080048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
51import java.io.IOException;
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070052import java.nio.ByteBuffer;
Thomas Vachuska3553b302015-03-07 14:49:43 -080053import java.util.HashMap;
Simon Hunt1f4365d2017-06-21 17:25:09 -070054import java.util.Locale;
Thomas Vachuska3553b302015-03-07 14:49:43 -080055import java.util.Map;
56
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070057import static org.onosproject.ui.impl.UiWebSocketServlet.PING_DELAY_MS;
58
Thomas Vachuska3553b302015-03-07 14:49:43 -080059/**
Simon Hunt7092cc42016-04-06 18:40:17 -070060 * Web socket capable of interacting with the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -080061 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070063public class UiWebSocket extends WebSocketAdapter implements UiConnection {
Thomas Vachuska3553b302015-03-07 14:49:43 -080064
65 private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class);
66
Simon Hunt7715e892016-04-12 19:55:32 -070067 private static final String EVENT = "event";
Simon Hunt7715e892016-04-12 19:55:32 -070068 private static final String PAYLOAD = "payload";
69 private static final String UNKNOWN = "unknown";
Simon Hunt1169c952017-06-05 11:20:11 -070070 private static final String AUTHENTICATION = "authentication";
71 private static final String TOKEN = "token";
72 private static final String ERROR = "error";
Simon Hunt7715e892016-04-12 19:55:32 -070073
74 private static final String ID = "id";
75 private static final String IP = "ip";
76 private static final String CLUSTER_NODES = "clusterNodes";
77 private static final String USER = "user";
78 private static final String BOOTSTRAP = "bootstrap";
79
Simon Huntd6d3ad32017-06-21 15:27:06 -070080 private static final String UBERLION = "uberlion";
81 private static final String LION = "lion";
Simon Hunt1f4365d2017-06-21 17:25:09 -070082 private static final String LOCALE = "locale";
Simon Huntd6d3ad32017-06-21 15:27:06 -070083
Simon Huntd7395c82016-10-20 17:54:01 -070084 private static final String TOPO = "topo";
Thomas Vachuska92b016b2016-05-20 11:37:57 -070085
Laszlo Papp759f0d32018-03-05 13:24:30 +000086 private static final String GLYPHS = "glyphs";
87
Thomas Vachuska1a989c12015-06-09 18:29:22 -070088 private static final long MAX_AGE_MS = 30_000;
Thomas Vachuska3553b302015-03-07 14:49:43 -080089
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070090 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
91 private static final ByteBuffer PING = ByteBuffer.wrap(PING_DATA);
92
Simon Hunt7092cc42016-04-06 18:40:17 -070093 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuska3553b302015-03-07 14:49:43 -080094 private final ServiceDirectory directory;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070095 private final UiTopoSession topoSession;
Thomas Vachuska3553b302015-03-07 14:49:43 -080096
Thomas Vachuska0af26912016-03-21 21:37:30 -070097 private String userName;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070098 private String currentView;
Thomas Vachuska3553b302015-03-07 14:49:43 -080099
Thomas Vachuska3553b302015-03-07 14:49:43 -0800100 private long lastActive = System.currentTimeMillis();
101
Simon Hunta0ddb022015-05-01 09:53:01 -0700102 private Map<String, UiMessageHandler> handlers;
Simon Hunte05cae42015-07-23 17:35:24 -0700103 private TopoOverlayCache overlayCache;
Simon Hunt22c35df2017-04-26 17:28:42 -0700104 private Topo2OverlayCache overlay2Cache;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800105
Simon Hunt879ce452017-08-10 23:32:00 -0700106 private Map<String, LionBundle> lionBundleMap;
107
Simon Hunt1169c952017-06-05 11:20:11 -0700108 private UiSessionToken sessionToken;
109
110
Thomas Vachuska3553b302015-03-07 14:49:43 -0800111 /**
Simon Huntcda9c032016-04-11 10:32:54 -0700112 * Creates a new web-socket for serving data to the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -0800113 *
114 * @param directory service directory
Simon Hunt7715e892016-04-12 19:55:32 -0700115 * @param userName user name of the logged-in user
Thomas Vachuska3553b302015-03-07 14:49:43 -0800116 */
Thomas Vachuska0af26912016-03-21 21:37:30 -0700117 public UiWebSocket(ServiceDirectory directory, String userName) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800118 this.directory = directory;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700119 this.userName = userName;
Simon Hunt537bc762016-12-20 12:15:13 -0800120
Simon Hunt95f4b422017-03-03 13:49:05 -0800121 Topo2Jsonifier t2json = new Topo2Jsonifier(directory, userName);
Simon Hunt537bc762016-12-20 12:15:13 -0800122 UiSharedTopologyModel sharedModel = directory.get(UiSharedTopologyModel.class);
123 UiTopoLayoutService layoutService = directory.get(UiTopoLayoutService.class);
Simon Huntbbd0f462017-01-10 14:50:22 -0800124
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800125 sharedModel.injectJsonifier(t2json);
Simon Huntbbd0f462017-01-10 14:50:22 -0800126 topoSession = new UiTopoSession(this, t2json, sharedModel, layoutService);
Simon Hunt1169c952017-06-05 11:20:11 -0700127 sessionToken = null;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700128 }
129
130 @Override
131 public String userName() {
132 return userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800133 }
134
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700135 @Override
136 public UiTopoLayout currentLayout() {
137 return topoSession.currentLayout();
138 }
139
140 @Override
141 public void setCurrentLayout(UiTopoLayout topoLayout) {
142 topoSession.setCurrentLayout(topoLayout);
143 }
144
145 @Override
146 public String currentView() {
147 return currentView;
148 }
149
150 @Override
151 public void setCurrentView(String viewId) {
152 currentView = viewId;
153 topoSession.enableEvent(viewId.equals(TOPO));
154 }
155
Simon Huntd6d3ad32017-06-21 15:27:06 -0700156 private ObjectNode objectNode() {
157 return mapper.createObjectNode();
158 }
159
160 private ArrayNode arrayNode() {
161 return mapper.createArrayNode();
162 }
163
Thomas Vachuska3553b302015-03-07 14:49:43 -0800164 /**
Simon Huntd5b96732016-07-08 13:22:27 -0700165 * Provides a reference to the topology session.
166 *
167 * @return topo session reference
168 */
169 public UiTopoSession topoSession() {
170 return topoSession;
171 }
172
173 /**
Thomas Vachuska3553b302015-03-07 14:49:43 -0800174 * Issues a close on the connection.
175 */
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700176 void close() {
Simon Hunte05cae42015-07-23 17:35:24 -0700177 destroyHandlersAndOverlays();
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700178 if (isConnected()) {
179 getSession().close();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800180 }
181 }
182
183 /**
184 * Indicates if this connection is idle.
185 *
186 * @return true if idle or closed
187 */
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700188 boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -0700189 long quietFor = System.currentTimeMillis() - lastActive;
190 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700191 if (idle || isNotConnected()) {
Simon Huntda580882015-05-12 20:58:18 -0700192 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800193 return true;
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700194
195 } else if (isConnected() && quietFor > PING_DELAY_MS) {
196 try {
197 getRemote().sendPing(PING);
198 lastActive = System.currentTimeMillis();
199 } catch (IOException e) {
200 log.warn("Unable to send ping message due to: ", e);
201 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800202 }
203 return false;
204 }
205
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700206 @Override
207 public void onWebSocketConnect(Session session) {
208 super.onWebSocketConnect(session);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700209 try {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700210 topoSession.init();
Simon Hunte05cae42015-07-23 17:35:24 -0700211 createHandlersAndOverlays();
Simon Hunt7715e892016-04-12 19:55:32 -0700212 sendBootstrapData();
Simon Huntd6d3ad32017-06-21 15:27:06 -0700213 sendUberLionBundle();
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700214 lastActive = System.currentTimeMillis();
Simon Hunt7715e892016-04-12 19:55:32 -0700215 log.info("GUI client connected -- user <{}>", userName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700216
217 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400218 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700219 getSession().close();
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700220 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800221 }
222
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700223 @Override
224 public void onWebSocketClose(int closeCode, String reason) {
dvaddireb09fdf32017-07-04 11:42:53 +0530225 try {
Thomas Vachuska3ed75852018-01-17 11:53:21 -0800226 try {
227 tokenService().revokeToken(sessionToken);
228 log.info("Session token revoked");
229 } catch (ServiceNotFoundException e) {
230 log.error("Unable to reference UiTokenService");
231 }
232 sessionToken = null;
Simon Hunt1169c952017-06-05 11:20:11 -0700233
Thomas Vachuska3ed75852018-01-17 11:53:21 -0800234 topoSession.destroy();
235 destroyHandlersAndOverlays();
236 } catch (Exception e) {
237 log.warn("Unexpected error", e);
238 }
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700239 super.onWebSocketClose(closeCode, reason);
Simon Huntda580882015-05-12 20:58:18 -0700240 log.info("GUI client disconnected [close-code={}, message={}]",
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700241 closeCode, reason);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800242 }
243
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700244 @Override
245 public void onWebSocketText(String data) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800246 lastActive = System.currentTimeMillis();
247 try {
248 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Simon Hunt7715e892016-04-12 19:55:32 -0700249 String type = message.path(EVENT).asText(UNKNOWN);
Simon Hunt1169c952017-06-05 11:20:11 -0700250
Thomas Vachuskad3585742018-08-14 10:06:39 -0700251 if (sessionToken == null) {
252 authenticate(type, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800253 } else {
Thomas Vachuskad3585742018-08-14 10:06:39 -0700254 UiMessageHandler handler = handlers.get(type);
255 if (handler != null) {
256 log.debug("RX message: {}", message);
257 handler.process(message);
258 } else {
259 log.warn("No GUI message handler for type {}", type);
260 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800261 }
Simon Hunt1169c952017-06-05 11:20:11 -0700262
Thomas Vachuskaaddf6342019-03-11 12:50:01 -0700263 } catch (Error | Exception e) {
Andrea Campanellabdeeda12019-08-02 16:12:05 +0200264 log.warn("Unable to parse GUI message {} due to", data, e);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800265 log.debug("Boom!!!", e);
266 }
267 }
268
269 @Override
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700270 public void onWebSocketBinary(byte[] payload, int offset, int length) {
271 lastActive = System.currentTimeMillis();
272 log.warn("Binary messages are currently not supported");
273 }
274
275 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700276 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800277 try {
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700278 if (isConnected()) {
279 getRemote().sendString(message.toString());
Simon Huntd5b96732016-07-08 13:22:27 -0700280 log.debug("TX message: {}", message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800281 }
282 } catch (IOException e) {
283 log.warn("Unable to send message {} to GUI due to {}", message, e);
284 log.debug("Boom!!!", e);
285 }
286 }
287
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700288 @Override
Simon Huntc5368182017-01-10 13:32:04 -0800289 public synchronized void sendMessage(String type, ObjectNode payload) {
Simon Huntd6d3ad32017-06-21 15:27:06 -0700290 ObjectNode message = objectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700291 message.put(EVENT, type);
Simon Huntd6d3ad32017-06-21 15:27:06 -0700292 message.set(PAYLOAD, payload != null ? payload : objectNode());
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700293 sendMessage(message);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700294 }
295
Thomas Vachuska3553b302015-03-07 14:49:43 -0800296 // Creates new message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700297 private synchronized void createHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700298 log.debug("Creating handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800299 handlers = new HashMap<>();
Simon Hunte05cae42015-07-23 17:35:24 -0700300 overlayCache = new TopoOverlayCache();
Simon Hunt22c35df2017-04-26 17:28:42 -0700301 overlay2Cache = new Topo2OverlayCache();
Simon Hunte05cae42015-07-23 17:35:24 -0700302
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700303 Map<Class<?>, UiMessageHandler> handlerInstances = new HashMap<>();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800304 UiExtensionService service = directory.get(UiExtensionService.class);
Simon Hunt879ce452017-08-10 23:32:00 -0700305 lionBundleMap = generateLionMap(service);
306
Thomas Vachuska329af532015-03-10 02:08:33 -0700307 service.getExtensions().forEach(ext -> {
308 UiMessageHandlerFactory factory = ext.messageHandlerFactory();
309 if (factory != null) {
310 factory.newHandlers().forEach(handler -> {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800311 try {
312 handler.init(this, directory);
Simon Hunt879ce452017-08-10 23:32:00 -0700313 injectLionBundles(handler, lionBundleMap);
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800314 handler.messageTypes().forEach(type -> handlers.put(type, handler));
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700315 handlerInstances.put(handler.getClass(), handler);
Simon Hunte05cae42015-07-23 17:35:24 -0700316
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800317 } catch (Exception e) {
318 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700319 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700320 });
321 }
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700322 registerOverlays(ext);
Thomas Vachuska329af532015-03-10 02:08:33 -0700323 });
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700324
325 handlerCrossConnects(handlerInstances);
326
Sean Condonadeb7162019-04-13 20:56:14 +0100327 overlay2Cache.switchOverlay(null, Traffic2Overlay.OVERLAY_ID);
328
329 log.debug("#handlers = {}, #overlays = Topo: {}, Topo2: {}",
330 handlers.size(), overlayCache.size(), overlay2Cache.size());
Simon Hunt879ce452017-08-10 23:32:00 -0700331 }
332
333 private Map<String, LionBundle> generateLionMap(UiExtensionService service) {
334 Map<String, LionBundle> bundles = new HashMap<>();
335 service.getExtensions().forEach(ext -> {
336 ext.lionBundles().forEach(lb -> bundles.put(lb.id(), lb));
337 });
338 return bundles;
339 }
340
341 private void injectLionBundles(UiMessageHandler handler,
342 Map<String, LionBundle> lionBundleMap) {
343 handler.requiredLionBundles().forEach(lbid -> {
344 LionBundle lb = lionBundleMap.get(lbid);
345 if (lb != null) {
346 handler.cacheLionBundle(lb);
347 } else {
348 log.warn("handler {}: Lion bundle {} non existent!",
349 handler.getClass().getName(), lbid);
350 }
351 });
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700352 }
353
Simon Hunt1169c952017-06-05 11:20:11 -0700354 private void authenticate(String type, ObjectNode message) {
355 if (!AUTHENTICATION.equals(type)) {
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -0400356 log.warn("WebSocket not authenticated: {}", message);
Thomas Vachuskad3585742018-08-14 10:06:39 -0700357 sendMessage(ERROR, notAuthorized(null));
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -0400358 close();
Simon Hunt1169c952017-06-05 11:20:11 -0700359 return;
360 }
361
362 String tokstr = message.path(PAYLOAD).path(TOKEN).asText(UNKNOWN);
363 UiSessionToken token = new UiSessionToken(tokstr);
364
365 if (tokenService().isTokenValid(token)) {
366 sessionToken = token;
367 log.info("Session token authenticated");
368 log.debug("WebSocket authenticated: {}", message);
369 } else {
370 log.warn("Invalid Authentication Token: {}", message);
371 sendMessage(ERROR, notAuthorized(token));
372 }
373 }
374
375 private ObjectNode notAuthorized(UiSessionToken token) {
Simon Huntd6d3ad32017-06-21 15:27:06 -0700376 return objectNode()
Simon Hunt1169c952017-06-05 11:20:11 -0700377 .put("message", "invalid authentication token")
Thomas Vachuskad3585742018-08-14 10:06:39 -0700378 .put("badToken", token != null ? token.toString() : "null");
Simon Hunt1169c952017-06-05 11:20:11 -0700379 }
380
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700381 private void registerOverlays(UiExtension ext) {
382 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
383 if (overlayFactory != null) {
384 overlayFactory.newOverlays().forEach(overlayCache::add);
385 }
386
387 UiTopo2OverlayFactory overlay2Factory = ext.topo2OverlayFactory();
388 if (overlay2Factory != null) {
389 overlay2Factory.newOverlays().forEach(overlay2Cache::add);
390 }
391 }
392
393 private void handlerCrossConnects(Map<Class<?>, UiMessageHandler> handlers) {
394 TopologyViewMessageHandler topomh = (TopologyViewMessageHandler)
395 handlers.get(TopologyViewMessageHandler.class);
396 if (topomh != null) {
397 topomh.setOverlayCache(overlayCache);
398 }
399
400 Topo2ViewMessageHandler topo2mh = (Topo2ViewMessageHandler)
401 handlers.get(Topo2ViewMessageHandler.class);
402 if (topo2mh != null) {
403 topo2mh.setOverlayCache(overlay2Cache);
404
405 // We also need a link to Topo2Traffic
406 Topo2TrafficMessageHandler topo2traffic = (Topo2TrafficMessageHandler)
407 handlers.get(Topo2TrafficMessageHandler.class);
408 if (topo2traffic != null) {
409 topo2mh.setTrafficHandler(topo2traffic);
Sean Condonadeb7162019-04-13 20:56:14 +0100410 topo2traffic.setOverlayCache(overlay2Cache);
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700411 } else {
412 log.error("No topo2 traffic handler found");
413 }
414 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800415 }
416
417 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700418 private synchronized void destroyHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700419 log.debug("Destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800420 handlers.forEach((type, handler) -> handler.destroy());
421 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700422
423 if (overlayCache != null) {
424 overlayCache.destroy();
425 overlayCache = null;
426 }
Simon Hunte6f64612017-04-28 00:01:48 -0700427 if (overlay2Cache != null) {
428 overlay2Cache.destroy();
429 overlay2Cache = null;
430 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800431 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700432
Simon Hunt7715e892016-04-12 19:55:32 -0700433 // Sends initial information (username and cluster member information)
434 // to allow GUI to display logged-in user, and to be able to
435 // fail-over to an alternate cluster member if necessary.
436 private void sendBootstrapData() {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700437 ClusterService service = directory.get(ClusterService.class);
Simon Huntd6d3ad32017-06-21 15:27:06 -0700438 ArrayNode instances = arrayNode();
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700439
440 for (ControllerNode node : service.getNodes()) {
Simon Huntd6d3ad32017-06-21 15:27:06 -0700441 ObjectNode instance = objectNode()
Simon Hunt7715e892016-04-12 19:55:32 -0700442 .put(ID, node.id().toString())
443 .put(IP, node.ip().toString())
Simon Hunt8add9ee2016-09-20 17:05:07 -0700444 .put(GlyphConstants.UI_ATTACHED,
Simon Hunt22c35df2017-04-26 17:28:42 -0700445 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700446 instances.add(instance);
447 }
448
Laszlo Papp759f0d32018-03-05 13:24:30 +0000449 ArrayNode glyphInstances = arrayNode();
450 UiExtensionService uiExtensionService = directory.get(UiExtensionService.class);
451 for (UiGlyph glyph : uiExtensionService.getGlyphs()) {
452 ObjectNode glyphInstance = objectNode()
453 .put(GlyphConstants.ID, glyph.id())
454 .put(GlyphConstants.VIEWBOX, glyph.viewbox())
455 .put(GlyphConstants.PATH, glyph.path());
456 glyphInstances.add(glyphInstance);
457 }
458
Simon Huntd6d3ad32017-06-21 15:27:06 -0700459 ObjectNode payload = objectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700460 payload.set(CLUSTER_NODES, instances);
Laszlo Papp759f0d32018-03-05 13:24:30 +0000461 payload.set(GLYPHS, glyphInstances);
Simon Hunt7715e892016-04-12 19:55:32 -0700462 payload.put(USER, userName);
Laszlo Papp759f0d32018-03-05 13:24:30 +0000463
Simon Huntc5368182017-01-10 13:32:04 -0800464 sendMessage(BOOTSTRAP, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700465 }
466
Simon Hunt1169c952017-06-05 11:20:11 -0700467 private UiTokenService tokenService() {
dvaddireb09fdf32017-07-04 11:42:53 +0530468 return directory.get(UiTokenService.class);
Simon Hunt1169c952017-06-05 11:20:11 -0700469 }
Simon Huntd6d3ad32017-06-21 15:27:06 -0700470
471 // sends the collated localization bundle data up to the client.
472 private void sendUberLionBundle() {
473 UiExtensionService service = directory.get(UiExtensionService.class);
474 ObjectNode lion = objectNode();
475
476 service.getExtensions().forEach(ext -> {
477 ext.lionBundles().forEach(lb -> {
478 ObjectNode lionMap = objectNode();
Simon Hunt879ce452017-08-10 23:32:00 -0700479 lb.getItems().forEach(item -> lionMap.put(item.key(), item.value()));
Simon Huntd6d3ad32017-06-21 15:27:06 -0700480 lion.set(lb.id(), lionMap);
481 });
482 });
483
484 ObjectNode payload = objectNode();
485 payload.set(LION, lion);
Simon Hunt1f4365d2017-06-21 17:25:09 -0700486 payload.put(LOCALE, Locale.getDefault().toString());
Simon Huntd6d3ad32017-06-21 15:27:06 -0700487 sendMessage(UBERLION, payload);
488 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800489}