blob: 69850ffe732ff3a9cc33e67523939d00bfdf42ee [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;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070033import org.onosproject.ui.impl.topo.UiTopoSession;
34import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
35import org.onosproject.ui.model.topo.UiTopoLayout;
Thomas Vachuska3553b302015-03-07 14:49:43 -080036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39import java.io.IOException;
40import java.util.HashMap;
41import java.util.Map;
42
43/**
Simon Hunt7092cc42016-04-06 18:40:17 -070044 * Web socket capable of interacting with the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -080045 */
46public class UiWebSocket
47 implements UiConnection, WebSocket.OnTextMessage, WebSocket.OnControl {
48
49 private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class);
50
Simon Hunt7715e892016-04-12 19:55:32 -070051 private static final String EVENT = "event";
52 private static final String SID = "sid";
53 private static final String PAYLOAD = "payload";
54 private static final String UNKNOWN = "unknown";
55
56 private static final String ID = "id";
57 private static final String IP = "ip";
58 private static final String CLUSTER_NODES = "clusterNodes";
59 private static final String USER = "user";
60 private static final String BOOTSTRAP = "bootstrap";
61
Thomas Vachuska92b016b2016-05-20 11:37:57 -070062 public static final String TOPO = "topo";
63
Thomas Vachuska1a989c12015-06-09 18:29:22 -070064 private static final long MAX_AGE_MS = 30_000;
Thomas Vachuska3553b302015-03-07 14:49:43 -080065
66 private static final byte PING = 0x9;
67 private static final byte PONG = 0xA;
68 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
69
Simon Hunt7092cc42016-04-06 18:40:17 -070070 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuska3553b302015-03-07 14:49:43 -080071 private final ServiceDirectory directory;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070072 private final UiTopoSession topoSession;
Thomas Vachuska3553b302015-03-07 14:49:43 -080073
74 private Connection connection;
75 private FrameConnection control;
Thomas Vachuska0af26912016-03-21 21:37:30 -070076 private String userName;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070077 private String currentView;
Thomas Vachuska3553b302015-03-07 14:49:43 -080078
Thomas Vachuska3553b302015-03-07 14:49:43 -080079 private long lastActive = System.currentTimeMillis();
80
Simon Hunta0ddb022015-05-01 09:53:01 -070081 private Map<String, UiMessageHandler> handlers;
Simon Hunte05cae42015-07-23 17:35:24 -070082 private TopoOverlayCache overlayCache;
Thomas Vachuska3553b302015-03-07 14:49:43 -080083
84 /**
Simon Huntcda9c032016-04-11 10:32:54 -070085 * Creates a new web-socket for serving data to the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -080086 *
87 * @param directory service directory
Simon Hunt7715e892016-04-12 19:55:32 -070088 * @param userName user name of the logged-in user
Thomas Vachuska3553b302015-03-07 14:49:43 -080089 */
Thomas Vachuska0af26912016-03-21 21:37:30 -070090 public UiWebSocket(ServiceDirectory directory, String userName) {
Thomas Vachuska3553b302015-03-07 14:49:43 -080091 this.directory = directory;
Thomas Vachuska0af26912016-03-21 21:37:30 -070092 this.userName = userName;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070093 this.topoSession =
94 new UiTopoSession(this, directory.get(UiSharedTopologyModel.class),
Simon Huntd5b96732016-07-08 13:22:27 -070095 directory.get(UiTopoLayoutService.class));
Thomas Vachuska0af26912016-03-21 21:37:30 -070096 }
97
98 @Override
99 public String userName() {
100 return userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800101 }
102
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700103 @Override
104 public UiTopoLayout currentLayout() {
105 return topoSession.currentLayout();
106 }
107
108 @Override
109 public void setCurrentLayout(UiTopoLayout topoLayout) {
110 topoSession.setCurrentLayout(topoLayout);
111 }
112
113 @Override
114 public String currentView() {
115 return currentView;
116 }
117
118 @Override
119 public void setCurrentView(String viewId) {
120 currentView = viewId;
121 topoSession.enableEvent(viewId.equals(TOPO));
122 }
123
Thomas Vachuska3553b302015-03-07 14:49:43 -0800124 /**
Simon Huntd5b96732016-07-08 13:22:27 -0700125 * Provides a reference to the topology session.
126 *
127 * @return topo session reference
128 */
129 public UiTopoSession topoSession() {
130 return topoSession;
131 }
132
133 /**
Thomas Vachuska3553b302015-03-07 14:49:43 -0800134 * Issues a close on the connection.
135 */
136 synchronized void close() {
Simon Hunte05cae42015-07-23 17:35:24 -0700137 destroyHandlersAndOverlays();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800138 if (connection.isOpen()) {
139 connection.close();
140 }
141 }
142
143 /**
144 * Indicates if this connection is idle.
145 *
146 * @return true if idle or closed
147 */
148 synchronized boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -0700149 long quietFor = System.currentTimeMillis() - lastActive;
150 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800151 if (idle || (connection != null && !connection.isOpen())) {
Simon Huntda580882015-05-12 20:58:18 -0700152 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800153 return true;
154 } else if (connection != null) {
155 try {
156 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
157 } catch (IOException e) {
158 log.warn("Unable to send ping message due to: ", e);
159 }
160 }
161 return false;
162 }
163
164 @Override
Satish K598c28d2015-11-24 17:20:40 +0530165 public synchronized void onOpen(Connection connection) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800166 this.connection = connection;
167 this.control = (FrameConnection) connection;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700168 try {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700169 topoSession.init();
Simon Hunte05cae42015-07-23 17:35:24 -0700170 createHandlersAndOverlays();
Simon Hunt7715e892016-04-12 19:55:32 -0700171 sendBootstrapData();
172 log.info("GUI client connected -- user <{}>", userName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700173
174 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400175 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700176 this.connection.close();
177 this.connection = null;
178 this.control = null;
179 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800180 }
181
182 @Override
183 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700184 topoSession.destroy();
Simon Hunte05cae42015-07-23 17:35:24 -0700185 destroyHandlersAndOverlays();
Simon Huntda580882015-05-12 20:58:18 -0700186 log.info("GUI client disconnected [close-code={}, message={}]",
Simon Huntd5b96732016-07-08 13:22:27 -0700187 closeCode, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800188 }
189
190 @Override
191 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
192 lastActive = System.currentTimeMillis();
193 return true;
194 }
195
196 @Override
197 public void onMessage(String data) {
198 lastActive = System.currentTimeMillis();
199 try {
200 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Simon Hunt7715e892016-04-12 19:55:32 -0700201 String type = message.path(EVENT).asText(UNKNOWN);
Simon Hunta0ddb022015-05-01 09:53:01 -0700202 UiMessageHandler handler = handlers.get(type);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800203 if (handler != null) {
Simon Huntd5b96732016-07-08 13:22:27 -0700204 log.debug("RX message: {}", message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800205 handler.process(message);
206 } else {
207 log.warn("No GUI message handler for type {}", type);
208 }
209 } catch (Exception e) {
210 log.warn("Unable to parse GUI message {} due to {}", data, e);
211 log.debug("Boom!!!", e);
212 }
213 }
214
215 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700216 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800217 try {
218 if (connection.isOpen()) {
219 connection.sendMessage(message.toString());
Simon Huntd5b96732016-07-08 13:22:27 -0700220 log.debug("TX message: {}", message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800221 }
222 } catch (IOException e) {
223 log.warn("Unable to send message {} to GUI due to {}", message, e);
224 log.debug("Boom!!!", e);
225 }
226 }
227
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700228 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700229 public synchronized void sendMessage(String type, long sid, ObjectNode payload) {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700230 ObjectNode message = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700231 message.put(EVENT, type);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700232 if (sid > 0) {
Simon Hunt7715e892016-04-12 19:55:32 -0700233 message.put(SID, sid);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700234 }
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
255 if (handler instanceof TopologyViewMessageHandler) {
256 ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
257 }
258 } catch (Exception e) {
259 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700260 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700261 });
262 }
Simon Hunte05cae42015-07-23 17:35:24 -0700263
264 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
265 if (overlayFactory != null) {
266 overlayFactory.newOverlays().forEach(overlayCache::add);
267 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700268 });
Simon Hunte05cae42015-07-23 17:35:24 -0700269 log.debug("#handlers = {}, #overlays = {}", handlers.size(),
Simon Huntd5b96732016-07-08 13:22:27 -0700270 overlayCache.size());
Thomas Vachuska3553b302015-03-07 14:49:43 -0800271 }
272
273 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700274 private synchronized void destroyHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700275 log.debug("Destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800276 handlers.forEach((type, handler) -> handler.destroy());
277 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700278
279 if (overlayCache != null) {
280 overlayCache.destroy();
281 overlayCache = null;
282 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800283 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700284
Simon Hunt7715e892016-04-12 19:55:32 -0700285 // Sends initial information (username and cluster member information)
286 // to allow GUI to display logged-in user, and to be able to
287 // fail-over to an alternate cluster member if necessary.
288 private void sendBootstrapData() {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700289 ClusterService service = directory.get(ClusterService.class);
290 ArrayNode instances = mapper.createArrayNode();
291
292 for (ControllerNode node : service.getNodes()) {
293 ObjectNode instance = mapper.createObjectNode()
Simon Hunt7715e892016-04-12 19:55:32 -0700294 .put(ID, node.id().toString())
295 .put(IP, node.ip().toString())
Simon Hunt8add9ee2016-09-20 17:05:07 -0700296 .put(GlyphConstants.UI_ATTACHED,
Simon Huntd5b96732016-07-08 13:22:27 -0700297 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700298 instances.add(instance);
299 }
300
301 ObjectNode payload = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700302 payload.set(CLUSTER_NODES, instances);
303 payload.put(USER, userName);
304 sendMessage(BOOTSTRAP, 0, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700305 }
306
Thomas Vachuska3553b302015-03-07 14:49:43 -0800307}
308