blob: 0290857dcb4666125d39e06994d3f17fae89cb53 [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);
98 this.topoSession = new UiTopoSession(this, t2json, sharedModel, layoutService);
Thomas Vachuska0af26912016-03-21 21:37:30 -070099 }
100
101 @Override
102 public String userName() {
103 return userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800104 }
105
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700106 @Override
107 public UiTopoLayout currentLayout() {
108 return topoSession.currentLayout();
109 }
110
111 @Override
112 public void setCurrentLayout(UiTopoLayout topoLayout) {
113 topoSession.setCurrentLayout(topoLayout);
114 }
115
116 @Override
117 public String currentView() {
118 return currentView;
119 }
120
121 @Override
122 public void setCurrentView(String viewId) {
123 currentView = viewId;
124 topoSession.enableEvent(viewId.equals(TOPO));
125 }
126
Thomas Vachuska3553b302015-03-07 14:49:43 -0800127 /**
Simon Huntd5b96732016-07-08 13:22:27 -0700128 * Provides a reference to the topology session.
129 *
130 * @return topo session reference
131 */
132 public UiTopoSession topoSession() {
133 return topoSession;
134 }
135
136 /**
Thomas Vachuska3553b302015-03-07 14:49:43 -0800137 * Issues a close on the connection.
138 */
139 synchronized void close() {
Simon Hunte05cae42015-07-23 17:35:24 -0700140 destroyHandlersAndOverlays();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800141 if (connection.isOpen()) {
142 connection.close();
143 }
144 }
145
146 /**
147 * Indicates if this connection is idle.
148 *
149 * @return true if idle or closed
150 */
151 synchronized boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -0700152 long quietFor = System.currentTimeMillis() - lastActive;
153 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800154 if (idle || (connection != null && !connection.isOpen())) {
Simon Huntda580882015-05-12 20:58:18 -0700155 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800156 return true;
157 } else if (connection != null) {
158 try {
159 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
160 } catch (IOException e) {
161 log.warn("Unable to send ping message due to: ", e);
162 }
163 }
164 return false;
165 }
166
167 @Override
Satish K598c28d2015-11-24 17:20:40 +0530168 public synchronized void onOpen(Connection connection) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800169 this.connection = connection;
170 this.control = (FrameConnection) connection;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700171 try {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700172 topoSession.init();
Simon Hunte05cae42015-07-23 17:35:24 -0700173 createHandlersAndOverlays();
Simon Hunt7715e892016-04-12 19:55:32 -0700174 sendBootstrapData();
175 log.info("GUI client connected -- user <{}>", userName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700176
177 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400178 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700179 this.connection.close();
180 this.connection = null;
181 this.control = null;
182 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800183 }
184
185 @Override
186 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700187 topoSession.destroy();
Simon Hunte05cae42015-07-23 17:35:24 -0700188 destroyHandlersAndOverlays();
Simon Huntda580882015-05-12 20:58:18 -0700189 log.info("GUI client disconnected [close-code={}, message={}]",
Simon Huntd5b96732016-07-08 13:22:27 -0700190 closeCode, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800191 }
192
193 @Override
194 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
195 lastActive = System.currentTimeMillis();
196 return true;
197 }
198
199 @Override
200 public void onMessage(String data) {
201 lastActive = System.currentTimeMillis();
202 try {
203 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Simon Hunt7715e892016-04-12 19:55:32 -0700204 String type = message.path(EVENT).asText(UNKNOWN);
Simon Hunta0ddb022015-05-01 09:53:01 -0700205 UiMessageHandler handler = handlers.get(type);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800206 if (handler != null) {
Simon Huntd5b96732016-07-08 13:22:27 -0700207 log.debug("RX message: {}", message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800208 handler.process(message);
209 } else {
210 log.warn("No GUI message handler for type {}", type);
211 }
212 } catch (Exception e) {
213 log.warn("Unable to parse GUI message {} due to {}", data, e);
214 log.debug("Boom!!!", e);
215 }
216 }
217
218 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700219 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800220 try {
221 if (connection.isOpen()) {
222 connection.sendMessage(message.toString());
Simon Huntd5b96732016-07-08 13:22:27 -0700223 log.debug("TX message: {}", message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800224 }
225 } catch (IOException e) {
226 log.warn("Unable to send message {} to GUI due to {}", message, e);
227 log.debug("Boom!!!", e);
228 }
229 }
230
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700231 @Override
Simon Huntc5368182017-01-10 13:32:04 -0800232 public synchronized void sendMessage(String type, ObjectNode payload) {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700233 ObjectNode message = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700234 message.put(EVENT, type);
Simon Hunt7715e892016-04-12 19:55:32 -0700235 message.set(PAYLOAD, payload != null ? payload : mapper.createObjectNode());
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700236 sendMessage(message);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700237 }
238
Thomas Vachuska3553b302015-03-07 14:49:43 -0800239 // Creates new message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700240 private synchronized void createHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700241 log.debug("Creating handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800242 handlers = new HashMap<>();
Simon Hunte05cae42015-07-23 17:35:24 -0700243 overlayCache = new TopoOverlayCache();
244
Thomas Vachuska3553b302015-03-07 14:49:43 -0800245 UiExtensionService service = directory.get(UiExtensionService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700246 service.getExtensions().forEach(ext -> {
247 UiMessageHandlerFactory factory = ext.messageHandlerFactory();
248 if (factory != null) {
249 factory.newHandlers().forEach(handler -> {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800250 try {
251 handler.init(this, directory);
252 handler.messageTypes().forEach(type -> handlers.put(type, handler));
Simon Hunte05cae42015-07-23 17:35:24 -0700253
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800254 // need to inject the overlay cache into topology message handler
Simon Hunt537bc762016-12-20 12:15:13 -0800255 // TODO: code for Topo2ViewMessageHandler required here
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800256 if (handler instanceof TopologyViewMessageHandler) {
257 ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
258 }
259 } catch (Exception e) {
260 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700261 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700262 });
263 }
Simon Hunte05cae42015-07-23 17:35:24 -0700264
265 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
266 if (overlayFactory != null) {
267 overlayFactory.newOverlays().forEach(overlayCache::add);
268 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700269 });
Simon Hunte05cae42015-07-23 17:35:24 -0700270 log.debug("#handlers = {}, #overlays = {}", handlers.size(),
Simon Huntd5b96732016-07-08 13:22:27 -0700271 overlayCache.size());
Thomas Vachuska3553b302015-03-07 14:49:43 -0800272 }
273
274 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700275 private synchronized void destroyHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700276 log.debug("Destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800277 handlers.forEach((type, handler) -> handler.destroy());
278 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700279
280 if (overlayCache != null) {
281 overlayCache.destroy();
282 overlayCache = null;
283 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800284 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700285
Simon Hunt7715e892016-04-12 19:55:32 -0700286 // Sends initial information (username and cluster member information)
287 // to allow GUI to display logged-in user, and to be able to
288 // fail-over to an alternate cluster member if necessary.
289 private void sendBootstrapData() {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700290 ClusterService service = directory.get(ClusterService.class);
291 ArrayNode instances = mapper.createArrayNode();
292
293 for (ControllerNode node : service.getNodes()) {
294 ObjectNode instance = mapper.createObjectNode()
Simon Hunt7715e892016-04-12 19:55:32 -0700295 .put(ID, node.id().toString())
296 .put(IP, node.ip().toString())
Simon Hunt8add9ee2016-09-20 17:05:07 -0700297 .put(GlyphConstants.UI_ATTACHED,
Simon Huntd5b96732016-07-08 13:22:27 -0700298 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700299 instances.add(instance);
300 }
301
302 ObjectNode payload = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700303 payload.set(CLUSTER_NODES, instances);
304 payload.put(USER, userName);
Simon Huntc5368182017-01-10 13:32:04 -0800305 sendMessage(BOOTSTRAP, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700306 }
307
Thomas Vachuska3553b302015-03-07 14:49:43 -0800308}
309