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; |
| 22 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; |
| 23 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; |
| 24 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; |
| 25 | import org.eclipse.jetty.websocket.api.annotations.WebSocket; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 26 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 27 | import org.onlab.osgi.ServiceNotFoundException; |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 28 | import org.onosproject.cluster.ClusterService; |
| 29 | import org.onosproject.cluster.ControllerNode; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.GlyphConstants; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 31 | import org.onosproject.ui.UiConnection; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.UiExtension; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 33 | import org.onosproject.ui.UiExtensionService; |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 34 | import org.onosproject.ui.UiGlyph; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.UiMessageHandlerFactory; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.UiSessionToken; |
| 38 | import org.onosproject.ui.UiTokenService; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 39 | import org.onosproject.ui.UiTopo2OverlayFactory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 40 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 41 | import org.onosproject.ui.UiTopoOverlayFactory; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 42 | import org.onosproject.ui.impl.topo.Topo2Jsonifier; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 43 | import org.onosproject.ui.impl.topo.Topo2OverlayCache; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 44 | import org.onosproject.ui.impl.topo.Topo2TrafficMessageHandler; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 45 | import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 46 | import org.onosproject.ui.impl.topo.UiTopoSession; |
| 47 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 48 | import org.onosproject.ui.lion.LionBundle; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 49 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
| 53 | import java.io.IOException; |
| 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 | |
| 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 | |
| 62 | @WebSocket |
Thomas Vachuska | d0d7ea1 | 2018-10-10 10:27:04 -0700 | [diff] [blame] | 63 | public class UiWebSocket 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 | |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 90 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 91 | private final ServiceDirectory directory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 92 | private final UiTopoSession topoSession; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 93 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 94 | private Session session; |
| 95 | //private FrameConnection control; |
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 | */ |
| 175 | synchronized void close() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 176 | destroyHandlersAndOverlays(); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 177 | if (session.isOpen()) { |
| 178 | session.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 | */ |
| 187 | synchronized 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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 190 | if (idle || (session != null && !session.isOpen())) { |
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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 193 | //} else if (session != null) { |
| 194 | // try { |
| 195 | // control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 196 | // } catch (IOException e) { |
| 197 | // log.warn("Unable to send ping message due to: ", e); |
| 198 | // } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 203 | @OnWebSocketConnect |
| 204 | public synchronized void onOpen(Session session) { |
| 205 | this.session = session; |
| 206 | //this.control = (FrameConnection) connection; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 207 | try { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 208 | topoSession.init(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 209 | createHandlersAndOverlays(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 210 | sendBootstrapData(); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 211 | sendUberLionBundle(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 212 | log.info("GUI client connected -- user <{}>", userName); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 213 | |
| 214 | } catch (ServiceNotFoundException e) { |
Brian O'Connor | 75deea6 | 2015-06-24 16:09:17 -0400 | [diff] [blame] | 215 | log.warn("Unable to open GUI connection; services have been shut-down", e); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 216 | this.session.close(); |
| 217 | this.session = null; |
| 218 | //this.control = null; |
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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 222 | @OnWebSocketClose |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 223 | public synchronized void onClose(int closeCode, String message) { |
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 | } |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 238 | log.info("GUI client disconnected [close-code={}, message={}]", |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 239 | closeCode, message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 242 | @OnWebSocketMessage |
Thomas Vachuska | 2fd325f | 2018-10-01 13:21:23 -0700 | [diff] [blame] | 243 | public void onText(Session session, String data) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 244 | lastActive = System.currentTimeMillis(); |
| 245 | try { |
| 246 | ObjectNode message = (ObjectNode) mapper.reader().readTree(data); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 247 | String type = message.path(EVENT).asText(UNKNOWN); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 248 | |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 249 | if (sessionToken == null) { |
| 250 | authenticate(type, message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 251 | } else { |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 252 | UiMessageHandler handler = handlers.get(type); |
| 253 | if (handler != null) { |
| 254 | log.debug("RX message: {}", message); |
| 255 | handler.process(message); |
| 256 | } else { |
| 257 | log.warn("No GUI message handler for type {}", type); |
| 258 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 259 | } |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 260 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 261 | } catch (Exception e) { |
| 262 | log.warn("Unable to parse GUI message {} due to {}", data, e); |
| 263 | log.debug("Boom!!!", e); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | @Override |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 268 | public synchronized void sendMessage(ObjectNode message) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 269 | try { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 270 | if (session.isOpen()) { |
| 271 | session.getRemote().sendString(message.toString()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 272 | log.debug("TX message: {}", message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 273 | } |
| 274 | } catch (IOException e) { |
| 275 | log.warn("Unable to send message {} to GUI due to {}", message, e); |
| 276 | log.debug("Boom!!!", e); |
| 277 | } |
| 278 | } |
| 279 | |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 280 | @Override |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 281 | public synchronized void sendMessage(String type, ObjectNode payload) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 282 | ObjectNode message = objectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 283 | message.put(EVENT, type); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 284 | message.set(PAYLOAD, payload != null ? payload : objectNode()); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 285 | sendMessage(message); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 288 | // Creates new message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 289 | private synchronized void createHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 290 | log.debug("Creating handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 291 | handlers = new HashMap<>(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 292 | overlayCache = new TopoOverlayCache(); |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 293 | overlay2Cache = new Topo2OverlayCache(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 294 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 295 | Map<Class<?>, UiMessageHandler> handlerInstances = new HashMap<>(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 296 | UiExtensionService service = directory.get(UiExtensionService.class); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 297 | lionBundleMap = generateLionMap(service); |
| 298 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 299 | service.getExtensions().forEach(ext -> { |
| 300 | UiMessageHandlerFactory factory = ext.messageHandlerFactory(); |
| 301 | if (factory != null) { |
| 302 | factory.newHandlers().forEach(handler -> { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 303 | try { |
| 304 | handler.init(this, directory); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 305 | injectLionBundles(handler, lionBundleMap); |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 306 | handler.messageTypes().forEach(type -> handlers.put(type, handler)); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 307 | handlerInstances.put(handler.getClass(), handler); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 308 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 309 | } catch (Exception e) { |
| 310 | log.warn("Unable to setup handler {} due to", handler, e); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 311 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 312 | }); |
| 313 | } |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 314 | registerOverlays(ext); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 315 | }); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 316 | |
| 317 | handlerCrossConnects(handlerInstances); |
| 318 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 319 | log.debug("#handlers = {}, #overlays = {}", |
| 320 | handlers.size(), overlayCache.size()); |
| 321 | } |
| 322 | |
| 323 | private Map<String, LionBundle> generateLionMap(UiExtensionService service) { |
| 324 | Map<String, LionBundle> bundles = new HashMap<>(); |
| 325 | service.getExtensions().forEach(ext -> { |
| 326 | ext.lionBundles().forEach(lb -> bundles.put(lb.id(), lb)); |
| 327 | }); |
| 328 | return bundles; |
| 329 | } |
| 330 | |
| 331 | private void injectLionBundles(UiMessageHandler handler, |
| 332 | Map<String, LionBundle> lionBundleMap) { |
| 333 | handler.requiredLionBundles().forEach(lbid -> { |
| 334 | LionBundle lb = lionBundleMap.get(lbid); |
| 335 | if (lb != null) { |
| 336 | handler.cacheLionBundle(lb); |
| 337 | } else { |
| 338 | log.warn("handler {}: Lion bundle {} non existent!", |
| 339 | handler.getClass().getName(), lbid); |
| 340 | } |
| 341 | }); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 344 | private void authenticate(String type, ObjectNode message) { |
| 345 | if (!AUTHENTICATION.equals(type)) { |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 346 | log.warn("WebSocket not authenticated: {}", message); |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 347 | sendMessage(ERROR, notAuthorized(null)); |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 348 | close(); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 349 | return; |
| 350 | } |
| 351 | |
| 352 | String tokstr = message.path(PAYLOAD).path(TOKEN).asText(UNKNOWN); |
| 353 | UiSessionToken token = new UiSessionToken(tokstr); |
| 354 | |
| 355 | if (tokenService().isTokenValid(token)) { |
| 356 | sessionToken = token; |
| 357 | log.info("Session token authenticated"); |
| 358 | log.debug("WebSocket authenticated: {}", message); |
| 359 | } else { |
| 360 | log.warn("Invalid Authentication Token: {}", message); |
| 361 | sendMessage(ERROR, notAuthorized(token)); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | private ObjectNode notAuthorized(UiSessionToken token) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 366 | return objectNode() |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 367 | .put("message", "invalid authentication token") |
Thomas Vachuska | d358574 | 2018-08-14 10:06:39 -0700 | [diff] [blame] | 368 | .put("badToken", token != null ? token.toString() : "null"); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 371 | private void registerOverlays(UiExtension ext) { |
| 372 | UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory(); |
| 373 | if (overlayFactory != null) { |
| 374 | overlayFactory.newOverlays().forEach(overlayCache::add); |
| 375 | } |
| 376 | |
| 377 | UiTopo2OverlayFactory overlay2Factory = ext.topo2OverlayFactory(); |
| 378 | if (overlay2Factory != null) { |
| 379 | overlay2Factory.newOverlays().forEach(overlay2Cache::add); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | private void handlerCrossConnects(Map<Class<?>, UiMessageHandler> handlers) { |
| 384 | TopologyViewMessageHandler topomh = (TopologyViewMessageHandler) |
| 385 | handlers.get(TopologyViewMessageHandler.class); |
| 386 | if (topomh != null) { |
| 387 | topomh.setOverlayCache(overlayCache); |
| 388 | } |
| 389 | |
| 390 | Topo2ViewMessageHandler topo2mh = (Topo2ViewMessageHandler) |
| 391 | handlers.get(Topo2ViewMessageHandler.class); |
| 392 | if (topo2mh != null) { |
| 393 | topo2mh.setOverlayCache(overlay2Cache); |
| 394 | |
| 395 | // We also need a link to Topo2Traffic |
| 396 | Topo2TrafficMessageHandler topo2traffic = (Topo2TrafficMessageHandler) |
| 397 | handlers.get(Topo2TrafficMessageHandler.class); |
| 398 | if (topo2traffic != null) { |
| 399 | topo2mh.setTrafficHandler(topo2traffic); |
| 400 | } else { |
| 401 | log.error("No topo2 traffic handler found"); |
| 402 | } |
| 403 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // Destroys message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 407 | private synchronized void destroyHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 408 | log.debug("Destroying handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 409 | handlers.forEach((type, handler) -> handler.destroy()); |
| 410 | handlers.clear(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 411 | |
| 412 | if (overlayCache != null) { |
| 413 | overlayCache.destroy(); |
| 414 | overlayCache = null; |
| 415 | } |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 416 | if (overlay2Cache != null) { |
| 417 | overlay2Cache.destroy(); |
| 418 | overlay2Cache = null; |
| 419 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 420 | } |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 421 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 422 | // Sends initial information (username and cluster member information) |
| 423 | // to allow GUI to display logged-in user, and to be able to |
| 424 | // fail-over to an alternate cluster member if necessary. |
| 425 | private void sendBootstrapData() { |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 426 | ClusterService service = directory.get(ClusterService.class); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 427 | ArrayNode instances = arrayNode(); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 428 | |
| 429 | for (ControllerNode node : service.getNodes()) { |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 430 | ObjectNode instance = objectNode() |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 431 | .put(ID, node.id().toString()) |
| 432 | .put(IP, node.ip().toString()) |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 433 | .put(GlyphConstants.UI_ATTACHED, |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 434 | node.equals(service.getLocalNode())); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 435 | instances.add(instance); |
| 436 | } |
| 437 | |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 438 | ArrayNode glyphInstances = arrayNode(); |
| 439 | UiExtensionService uiExtensionService = directory.get(UiExtensionService.class); |
| 440 | for (UiGlyph glyph : uiExtensionService.getGlyphs()) { |
| 441 | ObjectNode glyphInstance = objectNode() |
| 442 | .put(GlyphConstants.ID, glyph.id()) |
| 443 | .put(GlyphConstants.VIEWBOX, glyph.viewbox()) |
| 444 | .put(GlyphConstants.PATH, glyph.path()); |
| 445 | glyphInstances.add(glyphInstance); |
| 446 | } |
| 447 | |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 448 | ObjectNode payload = objectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 449 | payload.set(CLUSTER_NODES, instances); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 450 | payload.set(GLYPHS, glyphInstances); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 451 | payload.put(USER, userName); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame] | 452 | |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 453 | sendMessage(BOOTSTRAP, payload); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 456 | private UiTokenService tokenService() { |
dvaddire | b09fdf3 | 2017-07-04 11:42:53 +0530 | [diff] [blame] | 457 | return directory.get(UiTokenService.class); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 458 | } |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 459 | |
| 460 | // sends the collated localization bundle data up to the client. |
| 461 | private void sendUberLionBundle() { |
| 462 | UiExtensionService service = directory.get(UiExtensionService.class); |
| 463 | ObjectNode lion = objectNode(); |
| 464 | |
| 465 | service.getExtensions().forEach(ext -> { |
| 466 | ext.lionBundles().forEach(lb -> { |
| 467 | ObjectNode lionMap = objectNode(); |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 468 | lb.getItems().forEach(item -> lionMap.put(item.key(), item.value())); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 469 | lion.set(lb.id(), lionMap); |
| 470 | }); |
| 471 | }); |
| 472 | |
| 473 | ObjectNode payload = objectNode(); |
| 474 | payload.set(LION, lion); |
Simon Hunt | 1f4365d | 2017-06-21 17:25:09 -0700 | [diff] [blame] | 475 | payload.put(LOCALE, Locale.getDefault().toString()); |
Simon Hunt | d6d3ad3 | 2017-06-21 15:27:06 -0700 | [diff] [blame] | 476 | sendMessage(UBERLION, payload); |
| 477 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 478 | } |