blob: 731840e161867639042ef3e86634bab3e5596476 [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;
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 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
Thomas Vachuska1a989c12015-06-09 18:29:22 -070049 private static final long MAX_AGE_MS = 30_000;
Thomas Vachuska3553b302015-03-07 14:49:43 -080050
51 private static final byte PING = 0x9;
52 private static final byte PONG = 0xA;
53 private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad};
54
Simon Hunt7092cc42016-04-06 18:40:17 -070055 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuska3553b302015-03-07 14:49:43 -080056 private final ServiceDirectory directory;
Simon Hunt7092cc42016-04-06 18:40:17 -070057 private final UiTopoSession topoSession;
Thomas Vachuska3553b302015-03-07 14:49:43 -080058
59 private Connection connection;
60 private FrameConnection control;
Thomas Vachuska0af26912016-03-21 21:37:30 -070061 private String userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -080062
Thomas Vachuska3553b302015-03-07 14:49:43 -080063 private long lastActive = System.currentTimeMillis();
64
Simon Hunta0ddb022015-05-01 09:53:01 -070065 private Map<String, UiMessageHandler> handlers;
Simon Hunte05cae42015-07-23 17:35:24 -070066 private TopoOverlayCache overlayCache;
Thomas Vachuska3553b302015-03-07 14:49:43 -080067
68 /**
Simon Huntcda9c032016-04-11 10:32:54 -070069 * Creates a new web-socket for serving data to the Web UI.
Thomas Vachuska3553b302015-03-07 14:49:43 -080070 *
71 * @param directory service directory
Thomas Vachuska0af26912016-03-21 21:37:30 -070072 * @param userName user name of the logged-in user
Thomas Vachuska3553b302015-03-07 14:49:43 -080073 */
Thomas Vachuska0af26912016-03-21 21:37:30 -070074 public UiWebSocket(ServiceDirectory directory, String userName) {
Thomas Vachuska3553b302015-03-07 14:49:43 -080075 this.directory = directory;
Thomas Vachuska0af26912016-03-21 21:37:30 -070076 this.userName = userName;
Simon Huntcda9c032016-04-11 10:32:54 -070077 this.topoSession =
78 new UiTopoSession(this, directory.get(UiSharedTopologyModel.class));
Thomas Vachuska0af26912016-03-21 21:37:30 -070079 }
80
81 @Override
82 public String userName() {
83 return userName;
Thomas Vachuska3553b302015-03-07 14:49:43 -080084 }
85
86 /**
87 * Issues a close on the connection.
88 */
89 synchronized void close() {
Simon Hunte05cae42015-07-23 17:35:24 -070090 destroyHandlersAndOverlays();
Thomas Vachuska3553b302015-03-07 14:49:43 -080091 if (connection.isOpen()) {
92 connection.close();
93 }
94 }
95
96 /**
97 * Indicates if this connection is idle.
98 *
99 * @return true if idle or closed
100 */
101 synchronized boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -0700102 long quietFor = System.currentTimeMillis() - lastActive;
103 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuska3553b302015-03-07 14:49:43 -0800104 if (idle || (connection != null && !connection.isOpen())) {
Simon Huntda580882015-05-12 20:58:18 -0700105 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800106 return true;
107 } else if (connection != null) {
108 try {
109 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
110 } catch (IOException e) {
111 log.warn("Unable to send ping message due to: ", e);
112 }
113 }
114 return false;
115 }
116
117 @Override
Satish K598c28d2015-11-24 17:20:40 +0530118 public synchronized void onOpen(Connection connection) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800119 this.connection = connection;
120 this.control = (FrameConnection) connection;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700121 try {
Simon Hunt7092cc42016-04-06 18:40:17 -0700122 topoSession.init();
Simon Hunte05cae42015-07-23 17:35:24 -0700123 createHandlersAndOverlays();
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700124 sendInstanceData();
125 log.info("GUI client connected");
126
127 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400128 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700129 this.connection.close();
130 this.connection = null;
131 this.control = null;
132 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800133 }
134
135 @Override
136 public synchronized void onClose(int closeCode, String message) {
Simon Hunt7092cc42016-04-06 18:40:17 -0700137 topoSession.destroy();
Simon Hunte05cae42015-07-23 17:35:24 -0700138 destroyHandlersAndOverlays();
Simon Huntda580882015-05-12 20:58:18 -0700139 log.info("GUI client disconnected [close-code={}, message={}]",
140 closeCode, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800141 }
142
143 @Override
144 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
145 lastActive = System.currentTimeMillis();
146 return true;
147 }
148
149 @Override
150 public void onMessage(String data) {
Simon Hunte05cae42015-07-23 17:35:24 -0700151 log.debug("onMessage: {}", data);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800152 lastActive = System.currentTimeMillis();
153 try {
154 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Thomas Vachuska329af532015-03-10 02:08:33 -0700155 String type = message.path("event").asText("unknown");
Simon Hunta0ddb022015-05-01 09:53:01 -0700156 UiMessageHandler handler = handlers.get(type);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800157 if (handler != null) {
158 handler.process(message);
159 } else {
160 log.warn("No GUI message handler for type {}", type);
161 }
162 } catch (Exception e) {
163 log.warn("Unable to parse GUI message {} due to {}", data, e);
164 log.debug("Boom!!!", e);
165 }
166 }
167
168 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700169 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800170 try {
171 if (connection.isOpen()) {
172 connection.sendMessage(message.toString());
173 }
174 } catch (IOException e) {
175 log.warn("Unable to send message {} to GUI due to {}", message, e);
176 log.debug("Boom!!!", e);
177 }
178 }
179
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700180 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700181 public synchronized void sendMessage(String type, long sid, ObjectNode payload) {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700182 ObjectNode message = mapper.createObjectNode();
183 message.put("event", type);
184 if (sid > 0) {
185 message.put("sid", sid);
186 }
Thomas Vachuskafa74dd72016-03-20 19:11:12 -0700187 message.set("payload", payload != null ? payload : mapper.createObjectNode());
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700188 sendMessage(message);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700189 }
190
Thomas Vachuska3553b302015-03-07 14:49:43 -0800191 // Creates new message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700192 private synchronized void createHandlersAndOverlays() {
193 log.debug("creating handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800194 handlers = new HashMap<>();
Simon Hunte05cae42015-07-23 17:35:24 -0700195 overlayCache = new TopoOverlayCache();
196
Thomas Vachuska3553b302015-03-07 14:49:43 -0800197 UiExtensionService service = directory.get(UiExtensionService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700198 service.getExtensions().forEach(ext -> {
199 UiMessageHandlerFactory factory = ext.messageHandlerFactory();
200 if (factory != null) {
201 factory.newHandlers().forEach(handler -> {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800202 try {
203 handler.init(this, directory);
204 handler.messageTypes().forEach(type -> handlers.put(type, handler));
Simon Hunte05cae42015-07-23 17:35:24 -0700205
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800206 // need to inject the overlay cache into topology message handler
207 if (handler instanceof TopologyViewMessageHandler) {
208 ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
209 }
210 } catch (Exception e) {
211 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700212 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700213 });
214 }
Simon Hunte05cae42015-07-23 17:35:24 -0700215
216 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
217 if (overlayFactory != null) {
218 overlayFactory.newOverlays().forEach(overlayCache::add);
219 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700220 });
Simon Hunte05cae42015-07-23 17:35:24 -0700221 log.debug("#handlers = {}, #overlays = {}", handlers.size(),
222 overlayCache.size());
Thomas Vachuska3553b302015-03-07 14:49:43 -0800223 }
224
225 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700226 private synchronized void destroyHandlersAndOverlays() {
227 log.debug("destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800228 handlers.forEach((type, handler) -> handler.destroy());
229 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700230
231 if (overlayCache != null) {
232 overlayCache.destroy();
233 overlayCache = null;
234 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800235 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700236
237 // Sends cluster node/instance information to allow GUI to fail-over.
238 private void sendInstanceData() {
239 ClusterService service = directory.get(ClusterService.class);
240 ArrayNode instances = mapper.createArrayNode();
241
242 for (ControllerNode node : service.getNodes()) {
243 ObjectNode instance = mapper.createObjectNode()
244 .put("id", node.id().toString())
245 .put("ip", node.ip().toString())
Simon Hunt629b99e2015-07-27 17:38:33 -0700246 .put(TopoConstants.Glyphs.UI_ATTACHED,
247 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700248 instances.add(instance);
249 }
250
251 ObjectNode payload = mapper.createObjectNode();
Thomas Vachuska20084b72015-03-11 13:46:50 -0700252 payload.set("clusterNodes", instances);
253 sendMessage("bootstrap", 0, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700254 }
255
Thomas Vachuska3553b302015-03-07 14:49:43 -0800256}
257