blob: 608031cb816eb528c240b3acbff4b7f874284ae0 [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;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070043import org.onosproject.ui.impl.topo.UiTopoSession;
44import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
Simon Hunt879ce452017-08-10 23:32:00 -070045import org.onosproject.ui.lion.LionBundle;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070046import org.onosproject.ui.model.topo.UiTopoLayout;
Thomas Vachuska3553b302015-03-07 14:49:43 -080047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import java.io.IOException;
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070051import java.nio.ByteBuffer;
Thomas Vachuska3553b302015-03-07 14:49:43 -080052import java.util.HashMap;
Simon Hunt1f4365d2017-06-21 17:25:09 -070053import java.util.Locale;
Thomas Vachuska3553b302015-03-07 14:49:43 -080054import java.util.Map;
55
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070056import static org.onosproject.ui.impl.UiWebSocketServlet.PING_DELAY_MS;
57
Thomas Vachuska3553b302015-03-07 14:49:43 -080058/**
Simon Hunt7092cc42016-04-06 18:40:17 -070059 * Web socket capable of interacting with the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -080060 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070062public class UiWebSocket extends WebSocketAdapter implements UiConnection {
Thomas Vachuska3553b302015-03-07 14:49:43 -080063
64 private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class);
65
Simon Hunt7715e892016-04-12 19:55:32 -070066 private static final String EVENT = "event";
Simon Hunt7715e892016-04-12 19:55:32 -070067 private static final String PAYLOAD = "payload";
68 private static final String UNKNOWN = "unknown";
Simon Hunt1169c952017-06-05 11:20:11 -070069 private static final String AUTHENTICATION = "authentication";
70 private static final String TOKEN = "token";
71 private static final String ERROR = "error";
Simon Hunt7715e892016-04-12 19:55:32 -070072
73 private static final String ID = "id";
74 private static final String IP = "ip";
75 private static final String CLUSTER_NODES = "clusterNodes";
76 private static final String USER = "user";
77 private static final String BOOTSTRAP = "bootstrap";
78
Simon Huntd6d3ad32017-06-21 15:27:06 -070079 private static final String UBERLION = "uberlion";
80 private static final String LION = "lion";
Simon Hunt1f4365d2017-06-21 17:25:09 -070081 private static final String LOCALE = "locale";
Simon Huntd6d3ad32017-06-21 15:27:06 -070082
Simon Huntd7395c82016-10-20 17:54:01 -070083 private static final String TOPO = "topo";
Thomas Vachuska92b016b2016-05-20 11:37:57 -070084
Laszlo Papp759f0d32018-03-05 13:24:30 +000085 private static final String GLYPHS = "glyphs";
86
Thomas Vachuska1a989c12015-06-09 18:29:22 -070087 private static final long MAX_AGE_MS = 30_000;
Thomas Vachuska3553b302015-03-07 14:49:43 -080088
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070089 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
90 private static final ByteBuffer PING = ByteBuffer.wrap(PING_DATA);
91
Simon Hunt7092cc42016-04-06 18:40:17 -070092 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuska3553b302015-03-07 14:49:43 -080093 private final ServiceDirectory directory;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070094 private final UiTopoSession topoSession;
Thomas Vachuska3553b302015-03-07 14:49:43 -080095
Thomas Vachuska0af26912016-03-21 21:37:30 -070096 private String userName;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070097 private String currentView;
Thomas Vachuska3553b302015-03-07 14:49:43 -080098
Thomas Vachuska3553b302015-03-07 14:49:43 -080099 private long lastActive = System.currentTimeMillis();
100
Simon Hunta0ddb022015-05-01 09:53:01 -0700101 private Map<String, UiMessageHandler> handlers;
Simon Hunte05cae42015-07-23 17:35:24 -0700102 private TopoOverlayCache overlayCache;
Simon Hunt22c35df2017-04-26 17:28:42 -0700103 private Topo2OverlayCache overlay2Cache;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800104
Simon Hunt879ce452017-08-10 23:32:00 -0700105 private Map<String, LionBundle> lionBundleMap;
106
Simon Hunt1169c952017-06-05 11:20:11 -0700107 private UiSessionToken sessionToken;
108
109
Thomas Vachuska3553b302015-03-07 14:49:43 -0800110 /**
Simon Huntcda9c032016-04-11 10:32:54 -0700111 * Creates a new web-socket for serving data to the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -0800112 *
113 * @param directory service directory
Simon Hunt7715e892016-04-12 19:55:32 -0700114 * @param userName user name of the logged-in user
Thomas Vachuska3553b302015-03-07 14:49:43 -0800115 */
Thomas Vachuska0af26912016-03-21 21:37:30 -0700116 public UiWebSocket(ServiceDirectory directory, String userName) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800117 this.directory = directory;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700118 this.userName = userName;
Simon Hunt537bc762016-12-20 12:15:13 -0800119
Simon Hunt95f4b422017-03-03 13:49:05 -0800120 Topo2Jsonifier t2json = new Topo2Jsonifier(directory, userName);
Simon Hunt537bc762016-12-20 12:15:13 -0800121 UiSharedTopologyModel sharedModel = directory.get(UiSharedTopologyModel.class);
122 UiTopoLayoutService layoutService = directory.get(UiTopoLayoutService.class);
Simon Huntbbd0f462017-01-10 14:50:22 -0800123
Simon Hunt8eac4ae2017-01-20 12:56:45 -0800124 sharedModel.injectJsonifier(t2json);
Simon Huntbbd0f462017-01-10 14:50:22 -0800125 topoSession = new UiTopoSession(this, t2json, sharedModel, layoutService);
Simon Hunt1169c952017-06-05 11:20:11 -0700126 sessionToken = null;
Thomas Vachuska0af26912016-03-21 21:37:30 -0700127 }
128
129 @Override
130 public String userName() {
131 return userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800132 }
133
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700134 @Override
135 public UiTopoLayout currentLayout() {
136 return topoSession.currentLayout();
137 }
138
139 @Override
140 public void setCurrentLayout(UiTopoLayout topoLayout) {
141 topoSession.setCurrentLayout(topoLayout);
142 }
143
144 @Override
145 public String currentView() {
146 return currentView;
147 }
148
149 @Override
150 public void setCurrentView(String viewId) {
151 currentView = viewId;
152 topoSession.enableEvent(viewId.equals(TOPO));
153 }
154
Simon Huntd6d3ad32017-06-21 15:27:06 -0700155 private ObjectNode objectNode() {
156 return mapper.createObjectNode();
157 }
158
159 private ArrayNode arrayNode() {
160 return mapper.createArrayNode();
161 }
162
Thomas Vachuska3553b302015-03-07 14:49:43 -0800163 /**
Simon Huntd5b96732016-07-08 13:22:27 -0700164 * Provides a reference to the topology session.
165 *
166 * @return topo session reference
167 */
168 public UiTopoSession topoSession() {
169 return topoSession;
170 }
171
172 /**
Thomas Vachuska3553b302015-03-07 14:49:43 -0800173 * Issues a close on the connection.
174 */
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700175 void close() {
Simon Hunte05cae42015-07-23 17:35:24 -0700176 destroyHandlersAndOverlays();
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700177 if (isConnected()) {
178 getSession().close();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800179 }
180 }
181
182 /**
183 * Indicates if this connection is idle.
184 *
185 * @return true if idle or closed
186 */
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700187 boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -0700188 long quietFor = System.currentTimeMillis() - lastActive;
189 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700190 if (idle || isNotConnected()) {
Simon Huntda580882015-05-12 20:58:18 -0700191 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800192 return true;
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700193
194 } else if (isConnected() && quietFor > PING_DELAY_MS) {
195 try {
196 getRemote().sendPing(PING);
197 lastActive = System.currentTimeMillis();
198 } catch (IOException e) {
199 log.warn("Unable to send ping message due to: ", e);
200 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800201 }
202 return false;
203 }
204
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700205 @Override
206 public void onWebSocketConnect(Session session) {
207 super.onWebSocketConnect(session);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700208 try {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700209 topoSession.init();
Simon Hunte05cae42015-07-23 17:35:24 -0700210 createHandlersAndOverlays();
Simon Hunt7715e892016-04-12 19:55:32 -0700211 sendBootstrapData();
Simon Huntd6d3ad32017-06-21 15:27:06 -0700212 sendUberLionBundle();
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700213 lastActive = System.currentTimeMillis();
Simon Hunt7715e892016-04-12 19:55:32 -0700214 log.info("GUI client connected -- user <{}>", userName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700215
216 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400217 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700218 getSession().close();
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700219 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800220 }
221
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700222 @Override
223 public void onWebSocketClose(int closeCode, String reason) {
dvaddireb09fdf32017-07-04 11:42:53 +0530224 try {
Thomas Vachuska3ed75852018-01-17 11:53:21 -0800225 try {
226 tokenService().revokeToken(sessionToken);
227 log.info("Session token revoked");
228 } catch (ServiceNotFoundException e) {
229 log.error("Unable to reference UiTokenService");
230 }
231 sessionToken = null;
Simon Hunt1169c952017-06-05 11:20:11 -0700232
Thomas Vachuska3ed75852018-01-17 11:53:21 -0800233 topoSession.destroy();
234 destroyHandlersAndOverlays();
235 } catch (Exception e) {
236 log.warn("Unexpected error", e);
237 }
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700238 super.onWebSocketClose(closeCode, reason);
Simon Huntda580882015-05-12 20:58:18 -0700239 log.info("GUI client disconnected [close-code={}, message={}]",
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700240 closeCode, reason);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800241 }
242
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700243 @Override
244 public void onWebSocketText(String data) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800245 lastActive = System.currentTimeMillis();
246 try {
247 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Simon Hunt7715e892016-04-12 19:55:32 -0700248 String type = message.path(EVENT).asText(UNKNOWN);
Simon Hunt1169c952017-06-05 11:20:11 -0700249
Thomas Vachuskad3585742018-08-14 10:06:39 -0700250 if (sessionToken == null) {
251 authenticate(type, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800252 } else {
Thomas Vachuskad3585742018-08-14 10:06:39 -0700253 UiMessageHandler handler = handlers.get(type);
254 if (handler != null) {
255 log.debug("RX message: {}", message);
256 handler.process(message);
257 } else {
258 log.warn("No GUI message handler for type {}", type);
259 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800260 }
Simon Hunt1169c952017-06-05 11:20:11 -0700261
Thomas Vachuskaaddf6342019-03-11 12:50:01 -0700262 } catch (Error | Exception e) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800263 log.warn("Unable to parse GUI message {} due to {}", data, e);
264 log.debug("Boom!!!", e);
265 }
266 }
267
268 @Override
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700269 public void onWebSocketBinary(byte[] payload, int offset, int length) {
270 lastActive = System.currentTimeMillis();
271 log.warn("Binary messages are currently not supported");
272 }
273
274 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700275 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800276 try {
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700277 if (isConnected()) {
278 getRemote().sendString(message.toString());
Simon Huntd5b96732016-07-08 13:22:27 -0700279 log.debug("TX message: {}", message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800280 }
281 } catch (IOException e) {
282 log.warn("Unable to send message {} to GUI due to {}", message, e);
283 log.debug("Boom!!!", e);
284 }
285 }
286
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700287 @Override
Simon Huntc5368182017-01-10 13:32:04 -0800288 public synchronized void sendMessage(String type, ObjectNode payload) {
Simon Huntd6d3ad32017-06-21 15:27:06 -0700289 ObjectNode message = objectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700290 message.put(EVENT, type);
Simon Huntd6d3ad32017-06-21 15:27:06 -0700291 message.set(PAYLOAD, payload != null ? payload : objectNode());
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700292 sendMessage(message);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700293 }
294
Thomas Vachuska3553b302015-03-07 14:49:43 -0800295 // Creates new message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700296 private synchronized void createHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700297 log.debug("Creating handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800298 handlers = new HashMap<>();
Simon Hunte05cae42015-07-23 17:35:24 -0700299 overlayCache = new TopoOverlayCache();
Simon Hunt22c35df2017-04-26 17:28:42 -0700300 overlay2Cache = new Topo2OverlayCache();
Simon Hunte05cae42015-07-23 17:35:24 -0700301
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700302 Map<Class<?>, UiMessageHandler> handlerInstances = new HashMap<>();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800303 UiExtensionService service = directory.get(UiExtensionService.class);
Simon Hunt879ce452017-08-10 23:32:00 -0700304 lionBundleMap = generateLionMap(service);
305
Thomas Vachuska329af532015-03-10 02:08:33 -0700306 service.getExtensions().forEach(ext -> {
307 UiMessageHandlerFactory factory = ext.messageHandlerFactory();
308 if (factory != null) {
309 factory.newHandlers().forEach(handler -> {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800310 try {
311 handler.init(this, directory);
Simon Hunt879ce452017-08-10 23:32:00 -0700312 injectLionBundles(handler, lionBundleMap);
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800313 handler.messageTypes().forEach(type -> handlers.put(type, handler));
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700314 handlerInstances.put(handler.getClass(), handler);
Simon Hunte05cae42015-07-23 17:35:24 -0700315
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800316 } catch (Exception e) {
317 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700318 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700319 });
320 }
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700321 registerOverlays(ext);
Thomas Vachuska329af532015-03-10 02:08:33 -0700322 });
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700323
324 handlerCrossConnects(handlerInstances);
325
Simon Hunt879ce452017-08-10 23:32:00 -0700326 log.debug("#handlers = {}, #overlays = {}",
327 handlers.size(), overlayCache.size());
328 }
329
330 private Map<String, LionBundle> generateLionMap(UiExtensionService service) {
331 Map<String, LionBundle> bundles = new HashMap<>();
332 service.getExtensions().forEach(ext -> {
333 ext.lionBundles().forEach(lb -> bundles.put(lb.id(), lb));
334 });
335 return bundles;
336 }
337
338 private void injectLionBundles(UiMessageHandler handler,
339 Map<String, LionBundle> lionBundleMap) {
340 handler.requiredLionBundles().forEach(lbid -> {
341 LionBundle lb = lionBundleMap.get(lbid);
342 if (lb != null) {
343 handler.cacheLionBundle(lb);
344 } else {
345 log.warn("handler {}: Lion bundle {} non existent!",
346 handler.getClass().getName(), lbid);
347 }
348 });
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700349 }
350
Simon Hunt1169c952017-06-05 11:20:11 -0700351 private void authenticate(String type, ObjectNode message) {
352 if (!AUTHENTICATION.equals(type)) {
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -0400353 log.warn("WebSocket not authenticated: {}", message);
Thomas Vachuskad3585742018-08-14 10:06:39 -0700354 sendMessage(ERROR, notAuthorized(null));
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -0400355 close();
Simon Hunt1169c952017-06-05 11:20:11 -0700356 return;
357 }
358
359 String tokstr = message.path(PAYLOAD).path(TOKEN).asText(UNKNOWN);
360 UiSessionToken token = new UiSessionToken(tokstr);
361
362 if (tokenService().isTokenValid(token)) {
363 sessionToken = token;
364 log.info("Session token authenticated");
365 log.debug("WebSocket authenticated: {}", message);
366 } else {
367 log.warn("Invalid Authentication Token: {}", message);
368 sendMessage(ERROR, notAuthorized(token));
369 }
370 }
371
372 private ObjectNode notAuthorized(UiSessionToken token) {
Simon Huntd6d3ad32017-06-21 15:27:06 -0700373 return objectNode()
Simon Hunt1169c952017-06-05 11:20:11 -0700374 .put("message", "invalid authentication token")
Thomas Vachuskad3585742018-08-14 10:06:39 -0700375 .put("badToken", token != null ? token.toString() : "null");
Simon Hunt1169c952017-06-05 11:20:11 -0700376 }
377
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700378 private void registerOverlays(UiExtension ext) {
379 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
380 if (overlayFactory != null) {
381 overlayFactory.newOverlays().forEach(overlayCache::add);
382 }
383
384 UiTopo2OverlayFactory overlay2Factory = ext.topo2OverlayFactory();
385 if (overlay2Factory != null) {
386 overlay2Factory.newOverlays().forEach(overlay2Cache::add);
387 }
388 }
389
390 private void handlerCrossConnects(Map<Class<?>, UiMessageHandler> handlers) {
391 TopologyViewMessageHandler topomh = (TopologyViewMessageHandler)
392 handlers.get(TopologyViewMessageHandler.class);
393 if (topomh != null) {
394 topomh.setOverlayCache(overlayCache);
395 }
396
397 Topo2ViewMessageHandler topo2mh = (Topo2ViewMessageHandler)
398 handlers.get(Topo2ViewMessageHandler.class);
399 if (topo2mh != null) {
400 topo2mh.setOverlayCache(overlay2Cache);
401
402 // We also need a link to Topo2Traffic
403 Topo2TrafficMessageHandler topo2traffic = (Topo2TrafficMessageHandler)
404 handlers.get(Topo2TrafficMessageHandler.class);
405 if (topo2traffic != null) {
406 topo2mh.setTrafficHandler(topo2traffic);
407 } else {
408 log.error("No topo2 traffic handler found");
409 }
410 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800411 }
412
413 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700414 private synchronized void destroyHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700415 log.debug("Destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800416 handlers.forEach((type, handler) -> handler.destroy());
417 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700418
419 if (overlayCache != null) {
420 overlayCache.destroy();
421 overlayCache = null;
422 }
Simon Hunte6f64612017-04-28 00:01:48 -0700423 if (overlay2Cache != null) {
424 overlay2Cache.destroy();
425 overlay2Cache = null;
426 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800427 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700428
Simon Hunt7715e892016-04-12 19:55:32 -0700429 // Sends initial information (username and cluster member information)
430 // to allow GUI to display logged-in user, and to be able to
431 // fail-over to an alternate cluster member if necessary.
432 private void sendBootstrapData() {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700433 ClusterService service = directory.get(ClusterService.class);
Simon Huntd6d3ad32017-06-21 15:27:06 -0700434 ArrayNode instances = arrayNode();
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700435
436 for (ControllerNode node : service.getNodes()) {
Simon Huntd6d3ad32017-06-21 15:27:06 -0700437 ObjectNode instance = objectNode()
Simon Hunt7715e892016-04-12 19:55:32 -0700438 .put(ID, node.id().toString())
439 .put(IP, node.ip().toString())
Simon Hunt8add9ee2016-09-20 17:05:07 -0700440 .put(GlyphConstants.UI_ATTACHED,
Simon Hunt22c35df2017-04-26 17:28:42 -0700441 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700442 instances.add(instance);
443 }
444
Laszlo Papp759f0d32018-03-05 13:24:30 +0000445 ArrayNode glyphInstances = arrayNode();
446 UiExtensionService uiExtensionService = directory.get(UiExtensionService.class);
447 for (UiGlyph glyph : uiExtensionService.getGlyphs()) {
448 ObjectNode glyphInstance = objectNode()
449 .put(GlyphConstants.ID, glyph.id())
450 .put(GlyphConstants.VIEWBOX, glyph.viewbox())
451 .put(GlyphConstants.PATH, glyph.path());
452 glyphInstances.add(glyphInstance);
453 }
454
Simon Huntd6d3ad32017-06-21 15:27:06 -0700455 ObjectNode payload = objectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700456 payload.set(CLUSTER_NODES, instances);
Laszlo Papp759f0d32018-03-05 13:24:30 +0000457 payload.set(GLYPHS, glyphInstances);
Simon Hunt7715e892016-04-12 19:55:32 -0700458 payload.put(USER, userName);
Laszlo Papp759f0d32018-03-05 13:24:30 +0000459
Simon Huntc5368182017-01-10 13:32:04 -0800460 sendMessage(BOOTSTRAP, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700461 }
462
Simon Hunt1169c952017-06-05 11:20:11 -0700463 private UiTokenService tokenService() {
dvaddireb09fdf32017-07-04 11:42:53 +0530464 return directory.get(UiTokenService.class);
Simon Hunt1169c952017-06-05 11:20:11 -0700465 }
Simon Huntd6d3ad32017-06-21 15:27:06 -0700466
467 // sends the collated localization bundle data up to the client.
468 private void sendUberLionBundle() {
469 UiExtensionService service = directory.get(UiExtensionService.class);
470 ObjectNode lion = objectNode();
471
472 service.getExtensions().forEach(ext -> {
473 ext.lionBundles().forEach(lb -> {
474 ObjectNode lionMap = objectNode();
Simon Hunt879ce452017-08-10 23:32:00 -0700475 lb.getItems().forEach(item -> lionMap.put(item.key(), item.value()));
Simon Huntd6d3ad32017-06-21 15:27:06 -0700476 lion.set(lb.id(), lionMap);
477 });
478 });
479
480 ObjectNode payload = objectNode();
481 payload.set(LION, lion);
Simon Hunt1f4365d2017-06-21 17:25:09 -0700482 payload.put(LOCALE, Locale.getDefault().toString());
Simon Huntd6d3ad32017-06-21 15:27:06 -0700483 sendMessage(UBERLION, payload);
484 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800485}