blob: e1d403d7978060aaef62f5ab48ce57d70ae7cd51 [file] [log] [blame]
Thomas Vachuska3553b302015-03-07 14:49:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska3553b302015-03-07 14:49:43 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ui.impl;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuska3553b302015-03-07 14:49:43 -080020import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.eclipse.jetty.websocket.WebSocket;
22import org.onlab.osgi.ServiceDirectory;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -070023import org.onlab.osgi.ServiceNotFoundException;
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -070024import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.ControllerNode;
Simon Hunt8add9ee2016-09-20 17:05:07 -070026import org.onosproject.ui.GlyphConstants;
Thomas Vachuska3553b302015-03-07 14:49:43 -080027import org.onosproject.ui.UiConnection;
28import org.onosproject.ui.UiExtensionService;
Simon Hunta0ddb022015-05-01 09:53:01 -070029import org.onosproject.ui.UiMessageHandler;
Simon Hunt7715e892016-04-12 19:55:32 -070030import org.onosproject.ui.UiMessageHandlerFactory;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070031import org.onosproject.ui.UiTopoLayoutService;
Simon Hunte05cae42015-07-23 17:35:24 -070032import org.onosproject.ui.UiTopoOverlayFactory;
Simon Hunt537bc762016-12-20 12:15:13 -080033import org.onosproject.ui.impl.topo.Topo2Jsonifier;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070034import org.onosproject.ui.impl.topo.UiTopoSession;
35import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
36import org.onosproject.ui.model.topo.UiTopoLayout;
Thomas Vachuska3553b302015-03-07 14:49:43 -080037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40import java.io.IOException;
41import java.util.HashMap;
42import java.util.Map;
43
44/**
Simon Hunt7092cc42016-04-06 18:40:17 -070045 * Web socket capable of interacting with the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -080046 */
47public class UiWebSocket
48 implements UiConnection, WebSocket.OnTextMessage, WebSocket.OnControl {
49
50 private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class);
51
Simon Hunt7715e892016-04-12 19:55:32 -070052 private static final String EVENT = "event";
53 private static final String SID = "sid";
54 private static final String PAYLOAD = "payload";
55 private static final String UNKNOWN = "unknown";
56
57 private static final String ID = "id";
58 private static final String IP = "ip";
59 private static final String CLUSTER_NODES = "clusterNodes";
60 private static final String USER = "user";
61 private static final String BOOTSTRAP = "bootstrap";
62
Simon Huntd7395c82016-10-20 17:54:01 -070063 private static final String TOPO = "topo";
Thomas Vachuska92b016b2016-05-20 11:37:57 -070064
Thomas Vachuska1a989c12015-06-09 18:29:22 -070065 private static final long MAX_AGE_MS = 30_000;
Thomas Vachuska3553b302015-03-07 14:49:43 -080066
67 private static final byte PING = 0x9;
68 private static final byte PONG = 0xA;
69 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
70
Simon Hunt7092cc42016-04-06 18:40:17 -070071 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuska3553b302015-03-07 14:49:43 -080072 private final ServiceDirectory directory;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070073 private final UiTopoSession topoSession;
Thomas Vachuska3553b302015-03-07 14:49:43 -080074
75 private Connection connection;
76 private FrameConnection control;
Thomas Vachuska0af26912016-03-21 21:37:30 -070077 private String userName;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070078 private String currentView;
Thomas Vachuska3553b302015-03-07 14:49:43 -080079
Thomas Vachuska3553b302015-03-07 14:49:43 -080080 private long lastActive = System.currentTimeMillis();
81
Simon Hunta0ddb022015-05-01 09:53:01 -070082 private Map<String, UiMessageHandler> handlers;
Simon Hunte05cae42015-07-23 17:35:24 -070083 private TopoOverlayCache overlayCache;
Thomas Vachuska3553b302015-03-07 14:49:43 -080084
85 /**
Simon Huntcda9c032016-04-11 10:32:54 -070086 * Creates a new web-socket for serving data to the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -080087 *
88 * @param directory service directory
Simon Hunt7715e892016-04-12 19:55:32 -070089 * @param userName user name of the logged-in user
Thomas Vachuska3553b302015-03-07 14:49:43 -080090 */
Thomas Vachuska0af26912016-03-21 21:37:30 -070091 public UiWebSocket(ServiceDirectory directory, String userName) {
Thomas Vachuska3553b302015-03-07 14:49:43 -080092 this.directory = directory;
Thomas Vachuska0af26912016-03-21 21:37:30 -070093 this.userName = userName;
Simon Hunt537bc762016-12-20 12:15:13 -080094
95 Topo2Jsonifier t2json = new Topo2Jsonifier(directory);
96 UiSharedTopologyModel sharedModel = directory.get(UiSharedTopologyModel.class);
97 UiTopoLayoutService layoutService = directory.get(UiTopoLayoutService.class);
Simon Huntbbd0f462017-01-10 14:50:22 -080098
Simon Hunt8eac4ae2017-01-20 12:56:45 -080099 sharedModel.injectJsonifier(t2json);
100
Simon Huntbbd0f462017-01-10 14:50:22 -0800101 topoSession = new UiTopoSession(this, t2json, sharedModel, layoutService);
102
103 // FIXME: this is temporary to prevent unhandled events being set to GUI...
104 // while Topo2 is still under development
105 topoSession.enableEvent(false);
Thomas Vachuska0af26912016-03-21 21:37:30 -0700106 }
107
108 @Override
109 public String userName() {
110 return userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800111 }
112
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700113 @Override
114 public UiTopoLayout currentLayout() {
115 return topoSession.currentLayout();
116 }
117
118 @Override
119 public void setCurrentLayout(UiTopoLayout topoLayout) {
120 topoSession.setCurrentLayout(topoLayout);
121 }
122
123 @Override
124 public String currentView() {
125 return currentView;
126 }
127
128 @Override
129 public void setCurrentView(String viewId) {
130 currentView = viewId;
131 topoSession.enableEvent(viewId.equals(TOPO));
132 }
133
Thomas Vachuska3553b302015-03-07 14:49:43 -0800134 /**
Simon Huntd5b96732016-07-08 13:22:27 -0700135 * Provides a reference to the topology session.
136 *
137 * @return topo session reference
138 */
139 public UiTopoSession topoSession() {
140 return topoSession;
141 }
142
143 /**
Thomas Vachuska3553b302015-03-07 14:49:43 -0800144 * Issues a close on the connection.
145 */
146 synchronized void close() {
Simon Hunte05cae42015-07-23 17:35:24 -0700147 destroyHandlersAndOverlays();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800148 if (connection.isOpen()) {
149 connection.close();
150 }
151 }
152
153 /**
154 * Indicates if this connection is idle.
155 *
156 * @return true if idle or closed
157 */
158 synchronized boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -0700159 long quietFor = System.currentTimeMillis() - lastActive;
160 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800161 if (idle || (connection != null && !connection.isOpen())) {
Simon Huntda580882015-05-12 20:58:18 -0700162 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800163 return true;
164 } else if (connection != null) {
165 try {
166 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
167 } catch (IOException e) {
168 log.warn("Unable to send ping message due to: ", e);
169 }
170 }
171 return false;
172 }
173
174 @Override
Satish K598c28d2015-11-24 17:20:40 +0530175 public synchronized void onOpen(Connection connection) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800176 this.connection = connection;
177 this.control = (FrameConnection) connection;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700178 try {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700179 topoSession.init();
Simon Hunte05cae42015-07-23 17:35:24 -0700180 createHandlersAndOverlays();
Simon Hunt7715e892016-04-12 19:55:32 -0700181 sendBootstrapData();
182 log.info("GUI client connected -- user <{}>", userName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700183
184 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400185 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700186 this.connection.close();
187 this.connection = null;
188 this.control = null;
189 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800190 }
191
192 @Override
193 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700194 topoSession.destroy();
Simon Hunte05cae42015-07-23 17:35:24 -0700195 destroyHandlersAndOverlays();
Simon Huntda580882015-05-12 20:58:18 -0700196 log.info("GUI client disconnected [close-code={}, message={}]",
Simon Huntd5b96732016-07-08 13:22:27 -0700197 closeCode, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800198 }
199
200 @Override
201 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
202 lastActive = System.currentTimeMillis();
203 return true;
204 }
205
206 @Override
207 public void onMessage(String data) {
208 lastActive = System.currentTimeMillis();
209 try {
210 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Simon Hunt7715e892016-04-12 19:55:32 -0700211 String type = message.path(EVENT).asText(UNKNOWN);
Simon Hunta0ddb022015-05-01 09:53:01 -0700212 UiMessageHandler handler = handlers.get(type);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800213 if (handler != null) {
Simon Huntd5b96732016-07-08 13:22:27 -0700214 log.debug("RX message: {}", message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800215 handler.process(message);
216 } else {
217 log.warn("No GUI message handler for type {}", type);
218 }
219 } catch (Exception e) {
220 log.warn("Unable to parse GUI message {} due to {}", data, e);
221 log.debug("Boom!!!", e);
222 }
223 }
224
225 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700226 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800227 try {
228 if (connection.isOpen()) {
229 connection.sendMessage(message.toString());
Simon Huntd5b96732016-07-08 13:22:27 -0700230 log.debug("TX message: {}", message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800231 }
232 } catch (IOException e) {
233 log.warn("Unable to send message {} to GUI due to {}", message, e);
234 log.debug("Boom!!!", e);
235 }
236 }
237
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700238 @Override
Simon Huntc5368182017-01-10 13:32:04 -0800239 public synchronized void sendMessage(String type, ObjectNode payload) {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700240 ObjectNode message = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700241 message.put(EVENT, type);
Simon Hunt7715e892016-04-12 19:55:32 -0700242 message.set(PAYLOAD, payload != null ? payload : mapper.createObjectNode());
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700243 sendMessage(message);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700244 }
245
Thomas Vachuska3553b302015-03-07 14:49:43 -0800246 // Creates new message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700247 private synchronized void createHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700248 log.debug("Creating handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800249 handlers = new HashMap<>();
Simon Hunte05cae42015-07-23 17:35:24 -0700250 overlayCache = new TopoOverlayCache();
251
Thomas Vachuska3553b302015-03-07 14:49:43 -0800252 UiExtensionService service = directory.get(UiExtensionService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700253 service.getExtensions().forEach(ext -> {
254 UiMessageHandlerFactory factory = ext.messageHandlerFactory();
255 if (factory != null) {
256 factory.newHandlers().forEach(handler -> {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800257 try {
258 handler.init(this, directory);
259 handler.messageTypes().forEach(type -> handlers.put(type, handler));
Simon Hunte05cae42015-07-23 17:35:24 -0700260
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800261 // need to inject the overlay cache into topology message handler
Simon Hunt537bc762016-12-20 12:15:13 -0800262 // TODO: code for Topo2ViewMessageHandler required here
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800263 if (handler instanceof TopologyViewMessageHandler) {
264 ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
265 }
266 } catch (Exception e) {
267 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700268 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700269 });
270 }
Simon Hunte05cae42015-07-23 17:35:24 -0700271
272 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
273 if (overlayFactory != null) {
274 overlayFactory.newOverlays().forEach(overlayCache::add);
275 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700276 });
Simon Hunte05cae42015-07-23 17:35:24 -0700277 log.debug("#handlers = {}, #overlays = {}", handlers.size(),
Simon Huntd5b96732016-07-08 13:22:27 -0700278 overlayCache.size());
Thomas Vachuska3553b302015-03-07 14:49:43 -0800279 }
280
281 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700282 private synchronized void destroyHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700283 log.debug("Destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800284 handlers.forEach((type, handler) -> handler.destroy());
285 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700286
287 if (overlayCache != null) {
288 overlayCache.destroy();
289 overlayCache = null;
290 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800291 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700292
Simon Hunt7715e892016-04-12 19:55:32 -0700293 // Sends initial information (username and cluster member information)
294 // to allow GUI to display logged-in user, and to be able to
295 // fail-over to an alternate cluster member if necessary.
296 private void sendBootstrapData() {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700297 ClusterService service = directory.get(ClusterService.class);
298 ArrayNode instances = mapper.createArrayNode();
299
300 for (ControllerNode node : service.getNodes()) {
301 ObjectNode instance = mapper.createObjectNode()
Simon Hunt7715e892016-04-12 19:55:32 -0700302 .put(ID, node.id().toString())
303 .put(IP, node.ip().toString())
Simon Hunt8add9ee2016-09-20 17:05:07 -0700304 .put(GlyphConstants.UI_ATTACHED,
Simon Huntd5b96732016-07-08 13:22:27 -0700305 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700306 instances.add(instance);
307 }
308
309 ObjectNode payload = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700310 payload.set(CLUSTER_NODES, instances);
311 payload.put(USER, userName);
Simon Huntc5368182017-01-10 13:32:04 -0800312 sendMessage(BOOTSTRAP, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700313 }
314
Thomas Vachuska3553b302015-03-07 14:49:43 -0800315}
316