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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.eclipse.jetty.websocket.api.Session; |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 22 | import org.eclipse.jetty.websocket.api.WebSocketAdapter; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 23 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 24 | import org.onlab.osgi.ServiceNotFoundException; |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.ControllerNode; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.GlyphConstants; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 28 | import org.onosproject.ui.UiConnection; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.UiExtension; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 30 | import org.onosproject.ui.UiExtensionService; |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 31 | import org.onosproject.ui.UiGlyph; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.UiMessageHandlerFactory; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.UiSessionToken; |
| 35 | import org.onosproject.ui.UiTokenService; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.UiTopo2OverlayFactory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 38 | import org.onosproject.ui.UiTopoOverlayFactory; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 39 | import org.onosproject.ui.impl.topo.Topo2Jsonifier; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 40 | import org.onosproject.ui.impl.topo.Topo2OverlayCache; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 41 | import org.onosproject.ui.impl.topo.Topo2TrafficMessageHandler; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 42 | import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler; |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 43 | import org.onosproject.ui.impl.topo.Traffic2Overlay; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 44 | import org.onosproject.ui.impl.topo.UiTopoSession; |
| 45 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 46 | import org.onosproject.ui.lion.LionBundle; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 47 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
| 51 | import java.io.IOException; |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 52 | import java.nio.ByteBuffer; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 53 | import java.util.HashMap; |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 54 | import java.util.Locale; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 55 | import java.util.Map; |
| 56 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 57 | import static org.onosproject.ui.impl.UiWebSocketServlet.PING_DELAY_MS; |
| 58 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 59 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 60 | * Web socket capable of interacting with the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 61 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 63 | public class UiWebSocket extends WebSocketAdapter implements UiConnection { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 64 | |
| 65 | private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class); |
| 66 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 67 | private static final String EVENT = "event"; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 68 | private static final String PAYLOAD = "payload"; |
| 69 | private static final String UNKNOWN = "unknown"; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 70 | private static final String AUTHENTICATION = "authentication"; |
| 71 | private static final String TOKEN = "token"; |
| 72 | private static final String ERROR = "error"; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 73 | |
| 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 Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 80 | private static final String UBERLION = "uberlion"; |
| 81 | private static final String LION = "lion"; |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 82 | private static final String LOCALE = "locale"; |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 83 | |
Simon Hunt | d7395c8 | 2016-10-20 17:54:01 -0700 | [diff] [blame] | 84 | private static final String TOPO = "topo"; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 85 | |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 86 | private static final String GLYPHS = "glyphs"; |
| 87 | |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 88 | private static final long MAX_AGE_MS = 30_000; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 89 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 90 | private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad}; |
| 91 | private static final ByteBuffer PING = ByteBuffer.wrap(PING_DATA); |
| 92 | |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 93 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 94 | private final ServiceDirectory directory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 95 | private final UiTopoSession topoSession; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 96 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 97 | private String userName; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 98 | private String currentView; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 99 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 100 | private long lastActive = System.currentTimeMillis(); |
| 101 | |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 102 | private Map<String, UiMessageHandler> handlers; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 103 | private TopoOverlayCache overlayCache; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 104 | private Topo2OverlayCache overlay2Cache; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 105 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 106 | private Map<String, LionBundle> lionBundleMap; |
| 107 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 108 | private UiSessionToken sessionToken; |
| 109 | |
| 110 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 111 | /** |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 112 | * Creates a new web-socket for serving data to the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 113 | * |
| 114 | * @param directory service directory |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 115 | * @param userName user name of the logged-in user |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 116 | */ |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 117 | public UiWebSocket(ServiceDirectory directory, String userName) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 118 | this.directory = directory; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 119 | this.userName = userName; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 120 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 121 | Topo2Jsonifier t2json = new Topo2Jsonifier(directory, userName); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 122 | UiSharedTopologyModel sharedModel = directory.get(UiSharedTopologyModel.class); |
| 123 | UiTopoLayoutService layoutService = directory.get(UiTopoLayoutService.class); |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 124 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 125 | sharedModel.injectJsonifier(t2json); |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 126 | topoSession = new UiTopoSession(this, t2json, sharedModel, layoutService); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 127 | sessionToken = null; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String userName() { |
| 132 | return userName; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 135 | @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 Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 156 | private ObjectNode objectNode() { |
| 157 | return mapper.createObjectNode(); |
| 158 | } |
| 159 | |
| 160 | private ArrayNode arrayNode() { |
| 161 | return mapper.createArrayNode(); |
| 162 | } |
| 163 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 164 | /** |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 165 | * 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 Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 174 | * Issues a close on the connection. |
| 175 | */ |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 176 | void close() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 177 | destroyHandlersAndOverlays(); |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 178 | if (isConnected()) { |
| 179 | getSession().close(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Indicates if this connection is idle. |
| 185 | * |
| 186 | * @return true if idle or closed |
| 187 | */ |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 188 | boolean isIdle() { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 189 | long quietFor = System.currentTimeMillis() - lastActive; |
| 190 | boolean idle = quietFor > MAX_AGE_MS; |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 191 | if (idle || isNotConnected()) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 192 | log.debug("IDLE (or closed) websocket [{} ms]", quietFor); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 193 | return true; |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 194 | |
| 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 Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public void onWebSocketConnect(Session session) { |
| 208 | super.onWebSocketConnect(session); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 209 | try { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 210 | topoSession.init(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 211 | createHandlersAndOverlays(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 212 | sendBootstrapData(); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 213 | sendUberLionBundle(); |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 214 | lastActive = System.currentTimeMillis(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 215 | log.info("GUI client connected -- user <{}>", userName); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 216 | |
| 217 | } catch (ServiceNotFoundException e) { |
Brian O'Connor | 75deea6 | 2015-06-24 16:09:17 -0400 | [diff] [blame] | 218 | log.warn("Unable to open GUI connection; services have been shut-down", e); |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 219 | getSession().close(); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 220 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 223 | @Override |
| 224 | public void onWebSocketClose(int closeCode, String reason) { |
dvaddire | b09fdf3 | 2017-07-04 11:42:53 +0530 | [diff] [blame] | 225 | try { |
Thomas Vachuska | 3ed7585 | 2018-01-17 11:53:21 -0800 | [diff] [blame] | 226 | 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 Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 233 | |
Thomas Vachuska | 3ed7585 | 2018-01-17 11:53:21 -0800 | [diff] [blame] | 234 | topoSession.destroy(); |
| 235 | destroyHandlersAndOverlays(); |
| 236 | } catch (Exception e) { |
| 237 | log.warn("Unexpected error", e); |
| 238 | } |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 239 | super.onWebSocketClose(closeCode, reason); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 240 | log.info("GUI client disconnected [close-code={}, message={}]", |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 241 | closeCode, reason); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 244 | @Override |
| 245 | public void onWebSocketText(String data) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 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 | |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 251 | if (sessionToken == null) { |
| 252 | authenticate(type, message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 253 | } else { |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 254 | 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 Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 261 | } |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 262 | |
Thomas Vachuska | addf634 | 2019-03-11 12:50:01 -0700 | [diff] [blame] | 263 | } catch (Error | Exception e) { |
Andrea Campanella | bdeeda1 | 2019-08-02 16:12:05 +0200 | [diff] [blame] | 264 | log.warn("Unable to parse GUI message {} due to", data, e); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 265 | log.debug("Boom!!!", e); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | @Override |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 270 | 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 Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 276 | public synchronized void sendMessage(ObjectNode message) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 277 | try { |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 278 | if (isConnected()) { |
| 279 | getRemote().sendString(message.toString()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 280 | log.debug("TX message: {}", message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 281 | } |
| 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 Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 288 | @Override |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 289 | public synchronized void sendMessage(String type, ObjectNode payload) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 290 | ObjectNode message = objectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 291 | message.put(EVENT, type); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 292 | message.set(PAYLOAD, payload != null ? payload : objectNode()); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 293 | sendMessage(message); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 296 | // Creates new message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 297 | private synchronized void createHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 298 | log.debug("Creating handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 299 | handlers = new HashMap<>(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 300 | overlayCache = new TopoOverlayCache(); |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 301 | overlay2Cache = new Topo2OverlayCache(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 302 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 303 | Map<Class<?>, UiMessageHandler> handlerInstances = new HashMap<>(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 304 | UiExtensionService service = directory.get(UiExtensionService.class); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 305 | lionBundleMap = generateLionMap(service); |
| 306 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 307 | service.getExtensions().forEach(ext -> { |
| 308 | UiMessageHandlerFactory factory = ext.messageHandlerFactory(); |
| 309 | if (factory != null) { |
| 310 | factory.newHandlers().forEach(handler -> { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 311 | try { |
| 312 | handler.init(this, directory); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 313 | injectLionBundles(handler, lionBundleMap); |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 314 | handler.messageTypes().forEach(type -> handlers.put(type, handler)); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 315 | handlerInstances.put(handler.getClass(), handler); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 316 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 317 | } catch (Exception e) { |
| 318 | log.warn("Unable to setup handler {} due to", handler, e); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 319 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 320 | }); |
| 321 | } |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 322 | registerOverlays(ext); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 323 | }); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 324 | |
| 325 | handlerCrossConnects(handlerInstances); |
| 326 | |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 327 | overlay2Cache.switchOverlay(null, Traffic2Overlay.OVERLAY_ID); |
| 328 | |
| 329 | log.debug("#handlers = {}, #overlays = Topo: {}, Topo2: {}", |
| 330 | handlers.size(), overlayCache.size(), overlay2Cache.size()); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 331 | } |
| 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 Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 354 | private void authenticate(String type, ObjectNode message) { |
| 355 | if (!AUTHENTICATION.equals(type)) { |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 356 | log.warn("WebSocket not authenticated: {}", message); |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 357 | sendMessage(ERROR, notAuthorized(null)); |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 358 | close(); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 359 | 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 Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 376 | return objectNode() |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 377 | .put("message", "invalid authentication token") |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 378 | .put("badToken", token != null ? token.toString() : "null"); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 381 | 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 Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 410 | topo2traffic.setOverlayCache(overlay2Cache); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 411 | } else { |
| 412 | log.error("No topo2 traffic handler found"); |
| 413 | } |
| 414 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | // Destroys message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 418 | private synchronized void destroyHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 419 | log.debug("Destroying handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 420 | handlers.forEach((type, handler) -> handler.destroy()); |
| 421 | handlers.clear(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 422 | |
| 423 | if (overlayCache != null) { |
| 424 | overlayCache.destroy(); |
| 425 | overlayCache = null; |
| 426 | } |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 427 | if (overlay2Cache != null) { |
| 428 | overlay2Cache.destroy(); |
| 429 | overlay2Cache = null; |
| 430 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 431 | } |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 432 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 433 | // 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 Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 437 | ClusterService service = directory.get(ClusterService.class); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 438 | ArrayNode instances = arrayNode(); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 439 | |
| 440 | for (ControllerNode node : service.getNodes()) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 441 | ObjectNode instance = objectNode() |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 442 | .put(ID, node.id().toString()) |
| 443 | .put(IP, node.ip().toString()) |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 444 | .put(GlyphConstants.UI_ATTACHED, |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 445 | node.equals(service.getLocalNode())); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 446 | instances.add(instance); |
| 447 | } |
| 448 | |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 449 | 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 Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 459 | ObjectNode payload = objectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 460 | payload.set(CLUSTER_NODES, instances); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 461 | payload.set(GLYPHS, glyphInstances); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 462 | payload.put(USER, userName); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 463 | |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 464 | sendMessage(BOOTSTRAP, payload); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 467 | private UiTokenService tokenService() { |
dvaddire | b09fdf3 | 2017-07-04 11:42:53 +0530 | [diff] [blame] | 468 | return directory.get(UiTokenService.class); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 469 | } |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 470 | |
| 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 Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 479 | lb.getItems().forEach(item -> lionMap.put(item.key(), item.value())); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 480 | lion.set(lb.id(), lionMap); |
| 481 | }); |
| 482 | }); |
| 483 | |
| 484 | ObjectNode payload = objectNode(); |
| 485 | payload.set(LION, lion); |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 486 | payload.put(LOCALE, Locale.getDefault().toString()); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 487 | sendMessage(UBERLION, payload); |
| 488 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 489 | } |