blob: 2585bc8c99594007ce00a7699c02fdedcc1137f5 [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;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070030import org.onosproject.ui.UiTopoLayoutService;
Simon Hunte05cae42015-07-23 17:35:24 -070031import org.onosproject.ui.UiTopoOverlayFactory;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070032import org.onosproject.ui.impl.topo.UiTopoSession;
33import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
34import org.onosproject.ui.model.topo.UiTopoLayout;
Simon Hunt629b99e2015-07-27 17:38:33 -070035import org.onosproject.ui.topo.TopoConstants;
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),
95 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 /**
125 * Issues a close on the connection.
126 */
127 synchronized void close() {
Simon Hunte05cae42015-07-23 17:35:24 -0700128 destroyHandlersAndOverlays();
Thomas Vachuska3553b302015-03-07 14:49:43 -0800129 if (connection.isOpen()) {
130 connection.close();
131 }
132 }
133
134 /**
135 * Indicates if this connection is idle.
136 *
137 * @return true if idle or closed
138 */
139 synchronized boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -0700140 long quietFor = System.currentTimeMillis() - lastActive;
141 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800142 if (idle || (connection != null && !connection.isOpen())) {
Simon Huntda580882015-05-12 20:58:18 -0700143 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800144 return true;
145 } else if (connection != null) {
146 try {
147 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
148 } catch (IOException e) {
149 log.warn("Unable to send ping message due to: ", e);
150 }
151 }
152 return false;
153 }
154
155 @Override
Satish K598c28d2015-11-24 17:20:40 +0530156 public synchronized void onOpen(Connection connection) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800157 this.connection = connection;
158 this.control = (FrameConnection) connection;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700159 try {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700160 topoSession.init();
Simon Hunte05cae42015-07-23 17:35:24 -0700161 createHandlersAndOverlays();
Simon Hunt7715e892016-04-12 19:55:32 -0700162 sendBootstrapData();
163 log.info("GUI client connected -- user <{}>", userName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700164
165 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400166 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700167 this.connection.close();
168 this.connection = null;
169 this.control = null;
170 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800171 }
172
173 @Override
174 public synchronized void onClose(int closeCode, String message) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700175 topoSession.destroy();
Simon Hunte05cae42015-07-23 17:35:24 -0700176 destroyHandlersAndOverlays();
Simon Huntda580882015-05-12 20:58:18 -0700177 log.info("GUI client disconnected [close-code={}, message={}]",
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700178 closeCode, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800179 }
180
181 @Override
182 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
183 lastActive = System.currentTimeMillis();
184 return true;
185 }
186
187 @Override
188 public void onMessage(String data) {
Simon Hunte05cae42015-07-23 17:35:24 -0700189 log.debug("onMessage: {}", data);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800190 lastActive = System.currentTimeMillis();
191 try {
192 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Simon Hunt7715e892016-04-12 19:55:32 -0700193 String type = message.path(EVENT).asText(UNKNOWN);
Simon Hunta0ddb022015-05-01 09:53:01 -0700194 UiMessageHandler handler = handlers.get(type);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800195 if (handler != null) {
196 handler.process(message);
197 } else {
198 log.warn("No GUI message handler for type {}", type);
199 }
200 } catch (Exception e) {
201 log.warn("Unable to parse GUI message {} due to {}", data, e);
202 log.debug("Boom!!!", e);
203 }
204 }
205
206 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700207 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800208 try {
209 if (connection.isOpen()) {
210 connection.sendMessage(message.toString());
211 }
212 } catch (IOException e) {
213 log.warn("Unable to send message {} to GUI due to {}", message, e);
214 log.debug("Boom!!!", e);
215 }
216 }
217
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700218 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700219 public synchronized void sendMessage(String type, long sid, ObjectNode payload) {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700220 ObjectNode message = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700221 message.put(EVENT, type);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700222 if (sid > 0) {
Simon Hunt7715e892016-04-12 19:55:32 -0700223 message.put(SID, sid);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700224 }
Simon Hunt7715e892016-04-12 19:55:32 -0700225 message.set(PAYLOAD, payload != null ? payload : mapper.createObjectNode());
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700226 sendMessage(message);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700227 }
228
Thomas Vachuska3553b302015-03-07 14:49:43 -0800229 // Creates new message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700230 private synchronized void createHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700231 log.debug("Creating handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800232 handlers = new HashMap<>();
Simon Hunte05cae42015-07-23 17:35:24 -0700233 overlayCache = new TopoOverlayCache();
234
Thomas Vachuska3553b302015-03-07 14:49:43 -0800235 UiExtensionService service = directory.get(UiExtensionService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700236 service.getExtensions().forEach(ext -> {
237 UiMessageHandlerFactory factory = ext.messageHandlerFactory();
238 if (factory != null) {
239 factory.newHandlers().forEach(handler -> {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800240 try {
241 handler.init(this, directory);
242 handler.messageTypes().forEach(type -> handlers.put(type, handler));
Simon Hunte05cae42015-07-23 17:35:24 -0700243
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800244 // need to inject the overlay cache into topology message handler
245 if (handler instanceof TopologyViewMessageHandler) {
246 ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
247 }
248 } catch (Exception e) {
249 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700250 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700251 });
252 }
Simon Hunte05cae42015-07-23 17:35:24 -0700253
254 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
255 if (overlayFactory != null) {
256 overlayFactory.newOverlays().forEach(overlayCache::add);
257 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700258 });
Simon Hunte05cae42015-07-23 17:35:24 -0700259 log.debug("#handlers = {}, #overlays = {}", handlers.size(),
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700260 overlayCache.size());
Thomas Vachuska3553b302015-03-07 14:49:43 -0800261 }
262
263 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700264 private synchronized void destroyHandlersAndOverlays() {
Simon Hunt7715e892016-04-12 19:55:32 -0700265 log.debug("Destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800266 handlers.forEach((type, handler) -> handler.destroy());
267 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700268
269 if (overlayCache != null) {
270 overlayCache.destroy();
271 overlayCache = null;
272 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800273 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700274
Simon Hunt7715e892016-04-12 19:55:32 -0700275 // Sends initial information (username and cluster member information)
276 // to allow GUI to display logged-in user, and to be able to
277 // fail-over to an alternate cluster member if necessary.
278 private void sendBootstrapData() {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700279 ClusterService service = directory.get(ClusterService.class);
280 ArrayNode instances = mapper.createArrayNode();
281
282 for (ControllerNode node : service.getNodes()) {
283 ObjectNode instance = mapper.createObjectNode()
Simon Hunt7715e892016-04-12 19:55:32 -0700284 .put(ID, node.id().toString())
285 .put(IP, node.ip().toString())
Simon Hunt629b99e2015-07-27 17:38:33 -0700286 .put(TopoConstants.Glyphs.UI_ATTACHED,
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700287 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700288 instances.add(instance);
289 }
290
291 ObjectNode payload = mapper.createObjectNode();
Simon Hunt7715e892016-04-12 19:55:32 -0700292 payload.set(CLUSTER_NODES, instances);
293 payload.put(USER, userName);
294 sendMessage(BOOTSTRAP, 0, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700295 }
296
Thomas Vachuska3553b302015-03-07 14:49:43 -0800297}
298