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; |
pierventre | fa5dc3c | 2021-11-05 15:37:32 +0100 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.cluster.ControllerNode; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.GlyphConstants; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 29 | import org.onosproject.ui.UiConnection; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.UiExtension; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 31 | import org.onosproject.ui.UiExtensionService; |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 32 | import org.onosproject.ui.UiGlyph; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.UiMessageHandlerFactory; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.UiSessionToken; |
| 36 | import org.onosproject.ui.UiTokenService; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.UiTopo2OverlayFactory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 38 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 39 | import org.onosproject.ui.UiTopoOverlayFactory; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 40 | import org.onosproject.ui.impl.topo.Topo2Jsonifier; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 41 | import org.onosproject.ui.impl.topo.Topo2OverlayCache; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 42 | import org.onosproject.ui.impl.topo.Topo2TrafficMessageHandler; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 43 | import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler; |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 44 | import org.onosproject.ui.impl.topo.Traffic2Overlay; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 45 | import org.onosproject.ui.impl.topo.UiTopoSession; |
| 46 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 47 | import org.onosproject.ui.lion.LionBundle; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 48 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | import org.slf4j.LoggerFactory; |
| 51 | |
| 52 | import java.io.IOException; |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 53 | import java.nio.ByteBuffer; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 54 | import java.util.HashMap; |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 55 | import java.util.Locale; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 56 | import java.util.Map; |
| 57 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 58 | import static org.onosproject.ui.impl.UiWebSocketServlet.PING_DELAY_MS; |
| 59 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 60 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 61 | * Web socket capable of interacting with the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 62 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 64 | public class UiWebSocket extends WebSocketAdapter implements UiConnection { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 65 | |
| 66 | private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class); |
| 67 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 68 | private static final String EVENT = "event"; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 69 | private static final String PAYLOAD = "payload"; |
| 70 | private static final String UNKNOWN = "unknown"; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 71 | private static final String AUTHENTICATION = "authentication"; |
| 72 | private static final String TOKEN = "token"; |
| 73 | private static final String ERROR = "error"; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 74 | |
| 75 | private static final String ID = "id"; |
| 76 | private static final String IP = "ip"; |
| 77 | private static final String CLUSTER_NODES = "clusterNodes"; |
| 78 | private static final String USER = "user"; |
| 79 | private static final String BOOTSTRAP = "bootstrap"; |
| 80 | |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 81 | private static final String UBERLION = "uberlion"; |
| 82 | private static final String LION = "lion"; |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 83 | private static final String LOCALE = "locale"; |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 84 | |
Simon Hunt | d7395c8 | 2016-10-20 17:54:01 -0700 | [diff] [blame] | 85 | private static final String TOPO = "topo"; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 86 | |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 87 | private static final String GLYPHS = "glyphs"; |
| 88 | |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 89 | private static final long MAX_AGE_MS = 30_000; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 90 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 91 | private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad}; |
| 92 | private static final ByteBuffer PING = ByteBuffer.wrap(PING_DATA); |
| 93 | |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 94 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 95 | private final ServiceDirectory directory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 96 | private final UiTopoSession topoSession; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 97 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 98 | private String userName; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 99 | private String currentView; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 100 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 101 | private long lastActive = System.currentTimeMillis(); |
| 102 | |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 103 | private Map<String, UiMessageHandler> handlers; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 104 | private TopoOverlayCache overlayCache; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 105 | private Topo2OverlayCache overlay2Cache; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 106 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 107 | private Map<String, LionBundle> lionBundleMap; |
| 108 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 109 | private UiSessionToken sessionToken; |
| 110 | |
| 111 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 112 | /** |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 113 | * Creates a new web-socket for serving data to the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 114 | * |
| 115 | * @param directory service directory |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 116 | * @param userName user name of the logged-in user |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 117 | */ |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 118 | public UiWebSocket(ServiceDirectory directory, String userName) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 119 | this.directory = directory; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 120 | this.userName = userName; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 121 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 122 | Topo2Jsonifier t2json = new Topo2Jsonifier(directory, userName); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 123 | UiSharedTopologyModel sharedModel = directory.get(UiSharedTopologyModel.class); |
| 124 | UiTopoLayoutService layoutService = directory.get(UiTopoLayoutService.class); |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 125 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 126 | sharedModel.injectJsonifier(t2json); |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 127 | topoSession = new UiTopoSession(this, t2json, sharedModel, layoutService); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 128 | sessionToken = null; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public String userName() { |
| 133 | return userName; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public UiTopoLayout currentLayout() { |
| 138 | return topoSession.currentLayout(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void setCurrentLayout(UiTopoLayout topoLayout) { |
| 143 | topoSession.setCurrentLayout(topoLayout); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public String currentView() { |
| 148 | return currentView; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void setCurrentView(String viewId) { |
| 153 | currentView = viewId; |
| 154 | topoSession.enableEvent(viewId.equals(TOPO)); |
| 155 | } |
| 156 | |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 157 | private ObjectNode objectNode() { |
| 158 | return mapper.createObjectNode(); |
| 159 | } |
| 160 | |
| 161 | private ArrayNode arrayNode() { |
| 162 | return mapper.createArrayNode(); |
| 163 | } |
| 164 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 165 | /** |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 166 | * Provides a reference to the topology session. |
| 167 | * |
| 168 | * @return topo session reference |
| 169 | */ |
| 170 | public UiTopoSession topoSession() { |
| 171 | return topoSession; |
| 172 | } |
| 173 | |
| 174 | /** |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 175 | * Issues a close on the connection. |
| 176 | */ |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 177 | void close() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 178 | destroyHandlersAndOverlays(); |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 179 | if (isConnected()) { |
| 180 | getSession().close(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Indicates if this connection is idle. |
| 186 | * |
| 187 | * @return true if idle or closed |
| 188 | */ |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 189 | boolean isIdle() { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 190 | long quietFor = System.currentTimeMillis() - lastActive; |
| 191 | boolean idle = quietFor > MAX_AGE_MS; |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 192 | if (idle || isNotConnected()) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 193 | log.debug("IDLE (or closed) websocket [{} ms]", quietFor); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 194 | return true; |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 195 | |
| 196 | } else if (isConnected() && quietFor > PING_DELAY_MS) { |
| 197 | try { |
| 198 | getRemote().sendPing(PING); |
| 199 | lastActive = System.currentTimeMillis(); |
| 200 | } catch (IOException e) { |
| 201 | log.warn("Unable to send ping message due to: ", e); |
| 202 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 203 | } |
| 204 | return false; |
| 205 | } |
| 206 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 207 | @Override |
| 208 | public void onWebSocketConnect(Session session) { |
| 209 | super.onWebSocketConnect(session); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 210 | try { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 211 | topoSession.init(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 212 | createHandlersAndOverlays(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 213 | sendBootstrapData(); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 214 | sendUberLionBundle(); |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 215 | lastActive = System.currentTimeMillis(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 216 | log.info("GUI client connected -- user <{}>", userName); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 217 | |
| 218 | } catch (ServiceNotFoundException e) { |
Brian O'Connor | 75deea6 | 2015-06-24 16:09:17 -0400 | [diff] [blame] | 219 | 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] | 220 | getSession().close(); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 221 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 224 | @Override |
| 225 | public void onWebSocketClose(int closeCode, String reason) { |
dvaddire | b09fdf3 | 2017-07-04 11:42:53 +0530 | [diff] [blame] | 226 | try { |
Thomas Vachuska | 3ed7585 | 2018-01-17 11:53:21 -0800 | [diff] [blame] | 227 | try { |
| 228 | tokenService().revokeToken(sessionToken); |
| 229 | log.info("Session token revoked"); |
| 230 | } catch (ServiceNotFoundException e) { |
| 231 | log.error("Unable to reference UiTokenService"); |
| 232 | } |
| 233 | sessionToken = null; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 234 | |
Thomas Vachuska | 3ed7585 | 2018-01-17 11:53:21 -0800 | [diff] [blame] | 235 | topoSession.destroy(); |
| 236 | destroyHandlersAndOverlays(); |
| 237 | } catch (Exception e) { |
| 238 | log.warn("Unexpected error", e); |
| 239 | } |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 240 | super.onWebSocketClose(closeCode, reason); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 241 | log.info("GUI client disconnected [close-code={}, message={}]", |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 242 | closeCode, reason); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 245 | @Override |
| 246 | public void onWebSocketText(String data) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 247 | lastActive = System.currentTimeMillis(); |
| 248 | try { |
| 249 | ObjectNode message = (ObjectNode) mapper.reader().readTree(data); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 250 | String type = message.path(EVENT).asText(UNKNOWN); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 251 | |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 252 | if (sessionToken == null) { |
| 253 | authenticate(type, message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 254 | } else { |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 255 | UiMessageHandler handler = handlers.get(type); |
| 256 | if (handler != null) { |
| 257 | log.debug("RX message: {}", message); |
| 258 | handler.process(message); |
| 259 | } else { |
| 260 | log.warn("No GUI message handler for type {}", type); |
| 261 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 262 | } |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 263 | |
Thomas Vachuska | addf634 | 2019-03-11 12:50:01 -0700 | [diff] [blame] | 264 | } catch (Error | Exception e) { |
Andrea Campanella | bdeeda1 | 2019-08-02 16:12:05 +0200 | [diff] [blame] | 265 | log.warn("Unable to parse GUI message {} due to", data, e); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 266 | log.debug("Boom!!!", e); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | @Override |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 271 | public void onWebSocketBinary(byte[] payload, int offset, int length) { |
| 272 | lastActive = System.currentTimeMillis(); |
| 273 | log.warn("Binary messages are currently not supported"); |
| 274 | } |
| 275 | |
| 276 | @Override |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 277 | public synchronized void sendMessage(ObjectNode message) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 278 | try { |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 279 | if (isConnected()) { |
| 280 | getRemote().sendString(message.toString()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 281 | log.debug("TX message: {}", message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 282 | } |
| 283 | } catch (IOException e) { |
| 284 | log.warn("Unable to send message {} to GUI due to {}", message, e); |
| 285 | log.debug("Boom!!!", e); |
| 286 | } |
| 287 | } |
| 288 | |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 289 | @Override |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 290 | public synchronized void sendMessage(String type, ObjectNode payload) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 291 | ObjectNode message = objectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 292 | message.put(EVENT, type); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 293 | message.set(PAYLOAD, payload != null ? payload : objectNode()); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 294 | sendMessage(message); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 297 | // Creates new message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 298 | private synchronized void createHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 299 | log.debug("Creating handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 300 | handlers = new HashMap<>(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 301 | overlayCache = new TopoOverlayCache(); |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 302 | overlay2Cache = new Topo2OverlayCache(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 303 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 304 | Map<Class<?>, UiMessageHandler> handlerInstances = new HashMap<>(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 305 | UiExtensionService service = directory.get(UiExtensionService.class); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 306 | lionBundleMap = generateLionMap(service); |
| 307 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 308 | service.getExtensions().forEach(ext -> { |
| 309 | UiMessageHandlerFactory factory = ext.messageHandlerFactory(); |
| 310 | if (factory != null) { |
| 311 | factory.newHandlers().forEach(handler -> { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 312 | try { |
| 313 | handler.init(this, directory); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 314 | injectLionBundles(handler, lionBundleMap); |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 315 | handler.messageTypes().forEach(type -> handlers.put(type, handler)); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 316 | handlerInstances.put(handler.getClass(), handler); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 317 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 318 | } catch (Exception e) { |
| 319 | log.warn("Unable to setup handler {} due to", handler, e); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 320 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 321 | }); |
| 322 | } |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 323 | registerOverlays(ext); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 324 | }); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 325 | |
| 326 | handlerCrossConnects(handlerInstances); |
| 327 | |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 328 | overlay2Cache.switchOverlay(null, Traffic2Overlay.OVERLAY_ID); |
| 329 | |
| 330 | log.debug("#handlers = {}, #overlays = Topo: {}, Topo2: {}", |
| 331 | handlers.size(), overlayCache.size(), overlay2Cache.size()); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | private Map<String, LionBundle> generateLionMap(UiExtensionService service) { |
| 335 | Map<String, LionBundle> bundles = new HashMap<>(); |
| 336 | service.getExtensions().forEach(ext -> { |
| 337 | ext.lionBundles().forEach(lb -> bundles.put(lb.id(), lb)); |
| 338 | }); |
| 339 | return bundles; |
| 340 | } |
| 341 | |
| 342 | private void injectLionBundles(UiMessageHandler handler, |
| 343 | Map<String, LionBundle> lionBundleMap) { |
| 344 | handler.requiredLionBundles().forEach(lbid -> { |
| 345 | LionBundle lb = lionBundleMap.get(lbid); |
| 346 | if (lb != null) { |
| 347 | handler.cacheLionBundle(lb); |
| 348 | } else { |
| 349 | log.warn("handler {}: Lion bundle {} non existent!", |
| 350 | handler.getClass().getName(), lbid); |
| 351 | } |
| 352 | }); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 355 | private void authenticate(String type, ObjectNode message) { |
| 356 | if (!AUTHENTICATION.equals(type)) { |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 357 | log.warn("WebSocket not authenticated: {}", message); |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 358 | sendMessage(ERROR, notAuthorized(null)); |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 359 | close(); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 360 | return; |
| 361 | } |
| 362 | |
| 363 | String tokstr = message.path(PAYLOAD).path(TOKEN).asText(UNKNOWN); |
| 364 | UiSessionToken token = new UiSessionToken(tokstr); |
| 365 | |
| 366 | if (tokenService().isTokenValid(token)) { |
| 367 | sessionToken = token; |
| 368 | log.info("Session token authenticated"); |
| 369 | log.debug("WebSocket authenticated: {}", message); |
| 370 | } else { |
| 371 | log.warn("Invalid Authentication Token: {}", message); |
| 372 | sendMessage(ERROR, notAuthorized(token)); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | private ObjectNode notAuthorized(UiSessionToken token) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 377 | return objectNode() |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 378 | .put("message", "invalid authentication token") |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 379 | .put("badToken", token != null ? token.toString() : "null"); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 382 | private void registerOverlays(UiExtension ext) { |
| 383 | UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory(); |
| 384 | if (overlayFactory != null) { |
| 385 | overlayFactory.newOverlays().forEach(overlayCache::add); |
| 386 | } |
| 387 | |
| 388 | UiTopo2OverlayFactory overlay2Factory = ext.topo2OverlayFactory(); |
| 389 | if (overlay2Factory != null) { |
| 390 | overlay2Factory.newOverlays().forEach(overlay2Cache::add); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | private void handlerCrossConnects(Map<Class<?>, UiMessageHandler> handlers) { |
| 395 | TopologyViewMessageHandler topomh = (TopologyViewMessageHandler) |
| 396 | handlers.get(TopologyViewMessageHandler.class); |
| 397 | if (topomh != null) { |
| 398 | topomh.setOverlayCache(overlayCache); |
| 399 | } |
| 400 | |
| 401 | Topo2ViewMessageHandler topo2mh = (Topo2ViewMessageHandler) |
| 402 | handlers.get(Topo2ViewMessageHandler.class); |
| 403 | if (topo2mh != null) { |
| 404 | topo2mh.setOverlayCache(overlay2Cache); |
| 405 | |
| 406 | // We also need a link to Topo2Traffic |
| 407 | Topo2TrafficMessageHandler topo2traffic = (Topo2TrafficMessageHandler) |
| 408 | handlers.get(Topo2TrafficMessageHandler.class); |
| 409 | if (topo2traffic != null) { |
| 410 | topo2mh.setTrafficHandler(topo2traffic); |
Sean Condon | adeb716 | 2019-04-13 20:56:14 +0100 | [diff] [blame] | 411 | topo2traffic.setOverlayCache(overlay2Cache); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 412 | } else { |
| 413 | log.error("No topo2 traffic handler found"); |
| 414 | } |
| 415 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Destroys message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 419 | private synchronized void destroyHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 420 | log.debug("Destroying handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 421 | handlers.forEach((type, handler) -> handler.destroy()); |
| 422 | handlers.clear(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 423 | |
| 424 | if (overlayCache != null) { |
| 425 | overlayCache.destroy(); |
| 426 | overlayCache = null; |
| 427 | } |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 428 | if (overlay2Cache != null) { |
| 429 | overlay2Cache.destroy(); |
| 430 | overlay2Cache = null; |
| 431 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 432 | } |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 433 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 434 | // Sends initial information (username and cluster member information) |
| 435 | // to allow GUI to display logged-in user, and to be able to |
| 436 | // fail-over to an alternate cluster member if necessary. |
| 437 | private void sendBootstrapData() { |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 438 | ClusterService service = directory.get(ClusterService.class); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 439 | ArrayNode instances = arrayNode(); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 440 | |
| 441 | for (ControllerNode node : service.getNodes()) { |
pierventre | fa5dc3c | 2021-11-05 15:37:32 +0100 | [diff] [blame] | 442 | IpAddress nodeIp = node.ip(); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 443 | ObjectNode instance = objectNode() |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 444 | .put(ID, node.id().toString()) |
pierventre | fa5dc3c | 2021-11-05 15:37:32 +0100 | [diff] [blame] | 445 | .put(IP, nodeIp != null ? nodeIp.toString() : node.host()) |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 446 | .put(GlyphConstants.UI_ATTACHED, |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 447 | node.equals(service.getLocalNode())); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 448 | instances.add(instance); |
| 449 | } |
| 450 | |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 451 | ArrayNode glyphInstances = arrayNode(); |
| 452 | UiExtensionService uiExtensionService = directory.get(UiExtensionService.class); |
| 453 | for (UiGlyph glyph : uiExtensionService.getGlyphs()) { |
| 454 | ObjectNode glyphInstance = objectNode() |
| 455 | .put(GlyphConstants.ID, glyph.id()) |
| 456 | .put(GlyphConstants.VIEWBOX, glyph.viewbox()) |
| 457 | .put(GlyphConstants.PATH, glyph.path()); |
| 458 | glyphInstances.add(glyphInstance); |
| 459 | } |
| 460 | |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 461 | ObjectNode payload = objectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 462 | payload.set(CLUSTER_NODES, instances); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 463 | payload.set(GLYPHS, glyphInstances); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 464 | payload.put(USER, userName); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 465 | |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 466 | sendMessage(BOOTSTRAP, payload); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 469 | private UiTokenService tokenService() { |
dvaddire | b09fdf3 | 2017-07-04 11:42:53 +0530 | [diff] [blame] | 470 | return directory.get(UiTokenService.class); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 471 | } |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 472 | |
| 473 | // sends the collated localization bundle data up to the client. |
| 474 | private void sendUberLionBundle() { |
| 475 | UiExtensionService service = directory.get(UiExtensionService.class); |
| 476 | ObjectNode lion = objectNode(); |
| 477 | |
| 478 | service.getExtensions().forEach(ext -> { |
| 479 | ext.lionBundles().forEach(lb -> { |
| 480 | ObjectNode lionMap = objectNode(); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 481 | lb.getItems().forEach(item -> lionMap.put(item.key(), item.value())); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 482 | lion.set(lb.id(), lionMap); |
| 483 | }); |
| 484 | }); |
| 485 | |
| 486 | ObjectNode payload = objectNode(); |
| 487 | payload.set(LION, lion); |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 488 | payload.put(LOCALE, Locale.getDefault().toString()); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 489 | sendMessage(UBERLION, payload); |
| 490 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 491 | } |