blob: d1d79508bff531a713e551abf00725b9a8f86ad3 [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;
Thomas Vachuska3553b302015-03-07 14:49:43 -080026import org.onosproject.ui.UiConnection;
27import org.onosproject.ui.UiExtensionService;
Simon Hunta0ddb022015-05-01 09:53:01 -070028import org.onosproject.ui.UiMessageHandler;
Simon Hunt7715e892016-04-12 19:55:32 -070029import org.onosproject.ui.UiMessageHandlerFactory;
Simon Hunte05cae42015-07-23 17:35:24 -070030import org.onosproject.ui.UiTopoOverlayFactory;
Simon Hunt7092cc42016-04-06 18:40:17 -070031import org.onosproject.ui.impl.topo.UiTopoSession;
Simon Huntcda9c032016-04-11 10:32:54 -070032import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
Simon Hunt629b99e2015-07-27 17:38:33 -070033import org.onosproject.ui.topo.TopoConstants;
Thomas Vachuska3553b302015-03-07 14:49:43 -080034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37import java.io.IOException;
38import java.util.HashMap;
39import java.util.Map;
40
41/**
Simon Hunt7092cc42016-04-06 18:40:17 -070042 * Web socket capable of interacting with the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -080043 */
44public class UiWebSocket
45 implements UiConnection, WebSocket.OnTextMessage, WebSocket.OnControl {
46
47 private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class);
48
Simon Hunt7715e892016-04-12 19:55:32 -070049 private static final String EVENT = "event";
50 private static final String SID = "sid";
51 private static final String PAYLOAD = "payload";
52 private static final String UNKNOWN = "unknown";
53
54 private static final String ID = "id";
55 private static final String IP = "ip";
56 private static final String CLUSTER_NODES = "clusterNodes";
57 private static final String USER = "user";
58 private static final String BOOTSTRAP = "bootstrap";
59
Thomas Vachuska1a989c12015-06-09 18:29:22 -070060 private static final long MAX_AGE_MS = 30_000;
Thomas Vachuska3553b302015-03-07 14:49:43 -080061
62 private static final byte PING = 0x9;
63 private static final byte PONG = 0xA;
64 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
65
Simon Hunt7092cc42016-04-06 18:40:17 -070066 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuska3553b302015-03-07 14:49:43 -080067 private final ServiceDirectory directory;
Simon Hunt7092cc42016-04-06 18:40:17 -070068 private final UiTopoSession topoSession;
Thomas Vachuska3553b302015-03-07 14:49:43 -080069
70 private Connection connection;
71 private FrameConnection control;
Thomas Vachuska0af26912016-03-21 21:37:30 -070072 private String userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -080073
Thomas Vachuska3553b302015-03-07 14:49:43 -080074 private long lastActive = System.currentTimeMillis();
75
Simon Hunta0ddb022015-05-01 09:53:01 -070076 private Map<String, UiMessageHandler> handlers;
Simon Hunte05cae42015-07-23 17:35:24 -070077 private TopoOverlayCache overlayCache;
Thomas Vachuska3553b302015-03-07 14:49:43 -080078
79 /**
Simon Huntcda9c032016-04-11 10:32:54 -070080 * Creates a new web-socket for serving data to the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -080081 *
82 * @param directory service directory
Simon Hunt7715e892016-04-12 19:55:32 -070083 * @param userName user name of the logged-in user
Thomas Vachuska3553b302015-03-07 14:49:43 -080084 */
Thomas Vachuska0af26912016-03-21 21:37:30 -070085 public UiWebSocket(ServiceDirectory directory, String userName) {
Thomas Vachuska3553b302015-03-07 14:49:43 -080086 this.directory = directory;
Thomas Vachuska0af26912016-03-21 21:37:30 -070087 this.userName = userName;
Simon Huntcda9c032016-04-11 10:32:54 -070088 this.topoSession =
89 new UiTopoSession(this, directory.get(UiSharedTopologyModel.class));
Thomas Vachuska0af26912016-03-21 21:37:30 -070090 }
91
92 @Override
93 public String userName() {
94 return userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -080095 }
96
97 /**
98 * Issues a close on the connection.
99 */
100 synchronized void close() {
Simon Hunte05cae42015-07-23 17:35:24 -0700101 destroyHandlersAndOverlays();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800102 if (connection.isOpen()) {
103 connection.close();
104 }
105 }
106
107 /**
108 * Indicates if this connection is idle.
109 *
110 * @return true if idle or closed
111 */
112 synchronized boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -0700113 long quietFor = System.currentTimeMillis() - lastActive;
114 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800115 if (idle || (connection != null && !connection.isOpen())) {
Simon Huntda580882015-05-12 20:58:18 -0700116 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800117 return true;
118 } else if (connection != null) {
119 try {
120 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
121 } catch (IOException e) {
122 log.warn("Unable to send ping message due to: ", e);
123 }
124 }
125 return false;
126 }
127
128 @Override
Satish K598c28d2015-11-24 17:20:40 +0530129 public synchronized void onOpen(Connection connection) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800130 this.connection = connection;
131 this.control = (FrameConnection) connection;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700132 try {
Simon Hunt7092cc42016-04-06 18:40:17 -0700133 topoSession.init();
Simon Hunte05cae42015-07-23 17:35:24 -0700134 createHandlersAndOverlays();
Simon Hunt7715e892016-04-12 19:55:32 -0700135 sendBootstrapData();
136 log.info("GUI client connected -- user <{}>", userName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700137
138 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400139 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700140 this.connection.close();
141 this.connection = null;
142 this.control = null;
143 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800144 }
145
146 @Override
147 public synchronized void onClose(int closeCode, String message) {
Simon Hunt7092cc42016-04-06 18:40:17 -0700148 topoSession.destroy();
Simon Hunte05cae42015-07-23 17:35:24 -0700149 destroyHandlersAndOverlays();
Simon Huntda580882015-05-12 20:58:18 -0700150 log.info("GUI client disconnected [close-code={}, message={}]",
Simon Hunt7715e892016-04-12 19:55:32 -0700151 closeCode, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800152 }
153
154 @Override
155 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
156 lastActive = System.currentTimeMillis();
157 return true;
158 }
159
160 @Override
161 public void onMessage(String data) {
Simon Hunte05cae42015-07-23 17:35:24 -0700162 log.debug("onMessage: {}", data);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800163 lastActive = System.currentTimeMillis();
164 try {
165 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Simon Hunt7715e892016-04-12 19:55:32 -0700166 String type = message.path(EVENT).asText(UNKNOWN);
Simon Hunta0ddb022015-05-01 09:53:01 -0700167 UiMessageHandler handler = handlers.get(type);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800168 if (handler != null) {
169 handler.process(message);
170 } else {
171 log.warn("No GUI message handler for type {}", type);
172 }
173 } catch (Exception e) {
174 log.warn("Unable to parse GUI message {} due to {}", data, e);
175 log.debug("Boom!!!", e);
176 }
177 }
178
179 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700180 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800181 try {
182 if (connection.isOpen()) {
183 connection.sendMessage(message.toString());
184 }
185 } catch (IOException e) {
186 log.warn("Unable to send message {} to GUI due to {}", message, e);
187 log.debug("Boom!!!", e);
188 }
189 }
190
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700191 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700192 public synchronized void sendMessage(String type, long sid, ObjectNode payload) {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700193 ObjectNode message = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700194 message.put(EVENT, type);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700195 if (sid > 0) {
Simon Hunt7715e892016-04-12 19:55:32 -0700196 message.put(SID, sid);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700197 }
Simon Hunt7715e892016-04-12 19:55:32 -0700198 message.set(PAYLOAD, payload != null ? payload : mapper.createObjectNode());
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700199 sendMessage(message);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700200 }
201
Thomas Vachuska3553b302015-03-07 14:49:43 -0800202 // Creates new message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700203 private synchronized void createHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700204 log.debug("Creating handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800205 handlers = new HashMap<>();
Simon Hunte05cae42015-07-23 17:35:24 -0700206 overlayCache = new TopoOverlayCache();
207
Thomas Vachuska3553b302015-03-07 14:49:43 -0800208 UiExtensionService service = directory.get(UiExtensionService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700209 service.getExtensions().forEach(ext -> {
210 UiMessageHandlerFactory factory = ext.messageHandlerFactory();
211 if (factory != null) {
212 factory.newHandlers().forEach(handler -> {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800213 try {
214 handler.init(this, directory);
215 handler.messageTypes().forEach(type -> handlers.put(type, handler));
Simon Hunte05cae42015-07-23 17:35:24 -0700216
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800217 // need to inject the overlay cache into topology message handler
218 if (handler instanceof TopologyViewMessageHandler) {
219 ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
220 }
221 } catch (Exception e) {
222 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700223 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700224 });
225 }
Simon Hunte05cae42015-07-23 17:35:24 -0700226
227 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
228 if (overlayFactory != null) {
229 overlayFactory.newOverlays().forEach(overlayCache::add);
230 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700231 });
Simon Hunte05cae42015-07-23 17:35:24 -0700232 log.debug("#handlers = {}, #overlays = {}", handlers.size(),
Simon Hunt7715e892016-04-12 19:55:32 -0700233 overlayCache.size());
Thomas Vachuska3553b302015-03-07 14:49:43 -0800234 }
235
236 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700237 private synchronized void destroyHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700238 log.debug("Destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800239 handlers.forEach((type, handler) -> handler.destroy());
240 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700241
242 if (overlayCache != null) {
243 overlayCache.destroy();
244 overlayCache = null;
245 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800246 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700247
Simon Hunt7715e892016-04-12 19:55:32 -0700248 // Sends initial information (username and cluster member information)
249 // to allow GUI to display logged-in user, and to be able to
250 // fail-over to an alternate cluster member if necessary.
251 private void sendBootstrapData() {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700252 ClusterService service = directory.get(ClusterService.class);
253 ArrayNode instances = mapper.createArrayNode();
254
255 for (ControllerNode node : service.getNodes()) {
256 ObjectNode instance = mapper.createObjectNode()
Simon Hunt7715e892016-04-12 19:55:32 -0700257 .put(ID, node.id().toString())
258 .put(IP, node.ip().toString())
Simon Hunt629b99e2015-07-27 17:38:33 -0700259 .put(TopoConstants.Glyphs.UI_ATTACHED,
Simon Hunt7715e892016-04-12 19:55:32 -0700260 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700261 instances.add(instance);
262 }
263
264 ObjectNode payload = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700265 payload.set(CLUSTER_NODES, instances);
266 payload.put(USER, userName);
267 sendMessage(BOOTSTRAP, 0, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700268 }
269
Thomas Vachuska3553b302015-03-07 14:49:43 -0800270}
271