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