blob: efe30843d90824f56b3958cd718dcaf628e10c38 [file] [log] [blame]
Thomas Vachuska3553b302015-03-07 14:49:43 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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;
Thomas Vachuska329af532015-03-10 02:08:33 -070028import org.onosproject.ui.UiMessageHandlerFactory;
Simon Hunta0ddb022015-05-01 09:53:01 -070029import org.onosproject.ui.UiMessageHandler;
Simon Hunte05cae42015-07-23 17:35:24 -070030import org.onosproject.ui.UiTopoOverlayFactory;
Simon Hunt629b99e2015-07-27 17:38:33 -070031import org.onosproject.ui.topo.TopoConstants;
Thomas Vachuska3553b302015-03-07 14:49:43 -080032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.io.IOException;
36import java.util.HashMap;
37import java.util.Map;
38
39/**
40 * Web socket capable of interacting with the GUI.
41 */
42public class UiWebSocket
43 implements UiConnection, WebSocket.OnTextMessage, WebSocket.OnControl {
44
45 private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class);
46
Thomas Vachuska1a989c12015-06-09 18:29:22 -070047 private static final long MAX_AGE_MS = 30_000;
Thomas Vachuska3553b302015-03-07 14:49:43 -080048
49 private static final byte PING = 0x9;
50 private static final byte PONG = 0xA;
51 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
52
53 private final ServiceDirectory directory;
54
55 private Connection connection;
56 private FrameConnection control;
Thomas Vachuska0af26912016-03-21 21:37:30 -070057 private String userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -080058
59 private final ObjectMapper mapper = new ObjectMapper();
60
61 private long lastActive = System.currentTimeMillis();
62
Simon Hunta0ddb022015-05-01 09:53:01 -070063 private Map<String, UiMessageHandler> handlers;
Simon Hunte05cae42015-07-23 17:35:24 -070064 private TopoOverlayCache overlayCache;
Thomas Vachuska3553b302015-03-07 14:49:43 -080065
66 /**
67 * Creates a new web-socket for serving data to GUI.
68 *
69 * @param directory service directory
Thomas Vachuska0af26912016-03-21 21:37:30 -070070 * @param userName user name of the logged-in user
Thomas Vachuska3553b302015-03-07 14:49:43 -080071 */
Thomas Vachuska0af26912016-03-21 21:37:30 -070072 public UiWebSocket(ServiceDirectory directory, String userName) {
Thomas Vachuska3553b302015-03-07 14:49:43 -080073 this.directory = directory;
Thomas Vachuska0af26912016-03-21 21:37:30 -070074 this.userName = userName;
75 }
76
77 @Override
78 public String userName() {
79 return userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -080080 }
81
82 /**
83 * Issues a close on the connection.
84 */
85 synchronized void close() {
Simon Hunte05cae42015-07-23 17:35:24 -070086 destroyHandlersAndOverlays();
Thomas Vachuska3553b302015-03-07 14:49:43 -080087 if (connection.isOpen()) {
88 connection.close();
89 }
90 }
91
92 /**
93 * Indicates if this connection is idle.
94 *
95 * @return true if idle or closed
96 */
97 synchronized boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -070098 long quietFor = System.currentTimeMillis() - lastActive;
99 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800100 if (idle || (connection != null && !connection.isOpen())) {
Simon Huntda580882015-05-12 20:58:18 -0700101 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800102 return true;
103 } else if (connection != null) {
104 try {
105 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
106 } catch (IOException e) {
107 log.warn("Unable to send ping message due to: ", e);
108 }
109 }
110 return false;
111 }
112
113 @Override
Satish K598c28d2015-11-24 17:20:40 +0530114 public synchronized void onOpen(Connection connection) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800115 this.connection = connection;
116 this.control = (FrameConnection) connection;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700117 try {
Simon Hunte05cae42015-07-23 17:35:24 -0700118 createHandlersAndOverlays();
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700119 sendInstanceData();
120 log.info("GUI client connected");
121
122 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400123 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700124 this.connection.close();
125 this.connection = null;
126 this.control = null;
127 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800128 }
129
130 @Override
131 public synchronized void onClose(int closeCode, String message) {
Simon Hunte05cae42015-07-23 17:35:24 -0700132 destroyHandlersAndOverlays();
Simon Huntda580882015-05-12 20:58:18 -0700133 log.info("GUI client disconnected [close-code={}, message={}]",
134 closeCode, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800135 }
136
137 @Override
138 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
139 lastActive = System.currentTimeMillis();
140 return true;
141 }
142
143 @Override
144 public void onMessage(String data) {
Simon Hunte05cae42015-07-23 17:35:24 -0700145 log.debug("onMessage: {}", data);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800146 lastActive = System.currentTimeMillis();
147 try {
148 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Thomas Vachuska329af532015-03-10 02:08:33 -0700149 String type = message.path("event").asText("unknown");
Simon Hunta0ddb022015-05-01 09:53:01 -0700150 UiMessageHandler handler = handlers.get(type);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800151 if (handler != null) {
152 handler.process(message);
153 } else {
154 log.warn("No GUI message handler for type {}", type);
155 }
156 } catch (Exception e) {
157 log.warn("Unable to parse GUI message {} due to {}", data, e);
158 log.debug("Boom!!!", e);
159 }
160 }
161
162 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700163 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800164 try {
165 if (connection.isOpen()) {
166 connection.sendMessage(message.toString());
167 }
168 } catch (IOException e) {
169 log.warn("Unable to send message {} to GUI due to {}", message, e);
170 log.debug("Boom!!!", e);
171 }
172 }
173
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700174 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700175 public synchronized void sendMessage(String type, long sid, ObjectNode payload) {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700176 ObjectNode message = mapper.createObjectNode();
177 message.put("event", type);
178 if (sid > 0) {
179 message.put("sid", sid);
180 }
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700181 message.set("payload", payload != null ? payload : mapper.createObjectNode());
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700182 sendMessage(message);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700183 }
184
Thomas Vachuska3553b302015-03-07 14:49:43 -0800185 // Creates new message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700186 private synchronized void createHandlersAndOverlays() {
187 log.debug("creating handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800188 handlers = new HashMap<>();
Simon Hunte05cae42015-07-23 17:35:24 -0700189 overlayCache = new TopoOverlayCache();
190
Thomas Vachuska3553b302015-03-07 14:49:43 -0800191 UiExtensionService service = directory.get(UiExtensionService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700192 service.getExtensions().forEach(ext -> {
193 UiMessageHandlerFactory factory = ext.messageHandlerFactory();
194 if (factory != null) {
195 factory.newHandlers().forEach(handler -> {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800196 try {
197 handler.init(this, directory);
198 handler.messageTypes().forEach(type -> handlers.put(type, handler));
Simon Hunte05cae42015-07-23 17:35:24 -0700199
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800200 // need to inject the overlay cache into topology message handler
201 if (handler instanceof TopologyViewMessageHandler) {
202 ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
203 }
204 } catch (Exception e) {
205 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700206 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700207 });
208 }
Simon Hunte05cae42015-07-23 17:35:24 -0700209
210 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
211 if (overlayFactory != null) {
212 overlayFactory.newOverlays().forEach(overlayCache::add);
213 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700214 });
Simon Hunte05cae42015-07-23 17:35:24 -0700215 log.debug("#handlers = {}, #overlays = {}", handlers.size(),
216 overlayCache.size());
Thomas Vachuska3553b302015-03-07 14:49:43 -0800217 }
218
219 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700220 private synchronized void destroyHandlersAndOverlays() {
221 log.debug("destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800222 handlers.forEach((type, handler) -> handler.destroy());
223 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700224
225 if (overlayCache != null) {
226 overlayCache.destroy();
227 overlayCache = null;
228 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800229 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700230
231 // Sends cluster node/instance information to allow GUI to fail-over.
232 private void sendInstanceData() {
233 ClusterService service = directory.get(ClusterService.class);
234 ArrayNode instances = mapper.createArrayNode();
235
236 for (ControllerNode node : service.getNodes()) {
237 ObjectNode instance = mapper.createObjectNode()
238 .put("id", node.id().toString())
239 .put("ip", node.ip().toString())
Simon Hunt629b99e2015-07-27 17:38:33 -0700240 .put(TopoConstants.Glyphs.UI_ATTACHED,
241 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700242 instances.add(instance);
243 }
244
245 ObjectNode payload = mapper.createObjectNode();
Thomas Vachuska20084b72015-03-11 13:46:50 -0700246 payload.set("clusterNodes", instances);
247 sendMessage("bootstrap", 0, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700248 }
249
Thomas Vachuska3553b302015-03-07 14:49:43 -0800250}
251