blob: 449cdecbc12d2bb23799fa16e36c15bfcfc26366 [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;
57
58 private final ObjectMapper mapper = new ObjectMapper();
59
60 private long lastActive = System.currentTimeMillis();
61
Simon Hunta0ddb022015-05-01 09:53:01 -070062 private Map<String, UiMessageHandler> handlers;
Simon Hunte05cae42015-07-23 17:35:24 -070063 private TopoOverlayCache overlayCache;
Thomas Vachuska3553b302015-03-07 14:49:43 -080064
65 /**
66 * Creates a new web-socket for serving data to GUI.
67 *
68 * @param directory service directory
69 */
70 public UiWebSocket(ServiceDirectory directory) {
71 this.directory = directory;
72 }
73
74 /**
75 * Issues a close on the connection.
76 */
77 synchronized void close() {
Simon Hunte05cae42015-07-23 17:35:24 -070078 destroyHandlersAndOverlays();
Thomas Vachuska3553b302015-03-07 14:49:43 -080079 if (connection.isOpen()) {
80 connection.close();
81 }
82 }
83
84 /**
85 * Indicates if this connection is idle.
86 *
87 * @return true if idle or closed
88 */
89 synchronized boolean isIdle() {
Simon Huntda580882015-05-12 20:58:18 -070090 long quietFor = System.currentTimeMillis() - lastActive;
91 boolean idle = quietFor > MAX_AGE_MS;
Thomas Vachuska3553b302015-03-07 14:49:43 -080092 if (idle || (connection != null && !connection.isOpen())) {
Simon Huntda580882015-05-12 20:58:18 -070093 log.debug("IDLE (or closed) websocket [{} ms]", quietFor);
Thomas Vachuska3553b302015-03-07 14:49:43 -080094 return true;
95 } else if (connection != null) {
96 try {
97 control.sendControl(PING, PING_DATA, 0, PING_DATA.length);
98 } catch (IOException e) {
99 log.warn("Unable to send ping message due to: ", e);
100 }
101 }
102 return false;
103 }
104
105 @Override
Satish K598c28d2015-11-24 17:20:40 +0530106 public synchronized void onOpen(Connection connection) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800107 this.connection = connection;
108 this.control = (FrameConnection) connection;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700109 try {
Simon Hunte05cae42015-07-23 17:35:24 -0700110 createHandlersAndOverlays();
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700111 sendInstanceData();
112 log.info("GUI client connected");
113
114 } catch (ServiceNotFoundException e) {
Brian O'Connor75deea62015-06-24 16:09:17 -0400115 log.warn("Unable to open GUI connection; services have been shut-down", e);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700116 this.connection.close();
117 this.connection = null;
118 this.control = null;
119 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800120 }
121
122 @Override
123 public synchronized void onClose(int closeCode, String message) {
Simon Hunte05cae42015-07-23 17:35:24 -0700124 destroyHandlersAndOverlays();
Simon Huntda580882015-05-12 20:58:18 -0700125 log.info("GUI client disconnected [close-code={}, message={}]",
126 closeCode, message);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800127 }
128
129 @Override
130 public boolean onControl(byte controlCode, byte[] data, int offset, int length) {
131 lastActive = System.currentTimeMillis();
132 return true;
133 }
134
135 @Override
136 public void onMessage(String data) {
Simon Hunte05cae42015-07-23 17:35:24 -0700137 log.debug("onMessage: {}", data);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800138 lastActive = System.currentTimeMillis();
139 try {
140 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
Thomas Vachuska329af532015-03-10 02:08:33 -0700141 String type = message.path("event").asText("unknown");
Simon Hunta0ddb022015-05-01 09:53:01 -0700142 UiMessageHandler handler = handlers.get(type);
Thomas Vachuska3553b302015-03-07 14:49:43 -0800143 if (handler != null) {
144 handler.process(message);
145 } else {
146 log.warn("No GUI message handler for type {}", type);
147 }
148 } catch (Exception e) {
149 log.warn("Unable to parse GUI message {} due to {}", data, e);
150 log.debug("Boom!!!", e);
151 }
152 }
153
154 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700155 public synchronized void sendMessage(ObjectNode message) {
Thomas Vachuska3553b302015-03-07 14:49:43 -0800156 try {
157 if (connection.isOpen()) {
158 connection.sendMessage(message.toString());
159 }
160 } catch (IOException e) {
161 log.warn("Unable to send message {} to GUI due to {}", message, e);
162 log.debug("Boom!!!", e);
163 }
164 }
165
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700166 @Override
Thomas Vachuska35fa3d42015-04-30 10:11:47 -0700167 public synchronized void sendMessage(String type, long sid, ObjectNode payload) {
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700168 ObjectNode message = mapper.createObjectNode();
169 message.put("event", type);
170 if (sid > 0) {
171 message.put("sid", sid);
172 }
173 message.set("payload", payload);
174 sendMessage(message);
175
176 }
177
Thomas Vachuska3553b302015-03-07 14:49:43 -0800178 // Creates new message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700179 private synchronized void createHandlersAndOverlays() {
180 log.debug("creating handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800181 handlers = new HashMap<>();
Simon Hunte05cae42015-07-23 17:35:24 -0700182 overlayCache = new TopoOverlayCache();
183
Thomas Vachuska3553b302015-03-07 14:49:43 -0800184 UiExtensionService service = directory.get(UiExtensionService.class);
Thomas Vachuska329af532015-03-10 02:08:33 -0700185 service.getExtensions().forEach(ext -> {
186 UiMessageHandlerFactory factory = ext.messageHandlerFactory();
187 if (factory != null) {
188 factory.newHandlers().forEach(handler -> {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800189 try {
190 handler.init(this, directory);
191 handler.messageTypes().forEach(type -> handlers.put(type, handler));
Simon Hunte05cae42015-07-23 17:35:24 -0700192
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800193 // need to inject the overlay cache into topology message handler
194 if (handler instanceof TopologyViewMessageHandler) {
195 ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
196 }
197 } catch (Exception e) {
198 log.warn("Unable to setup handler {} due to", handler, e);
Simon Hunte05cae42015-07-23 17:35:24 -0700199 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700200 });
201 }
Simon Hunte05cae42015-07-23 17:35:24 -0700202
203 UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory();
204 if (overlayFactory != null) {
205 overlayFactory.newOverlays().forEach(overlayCache::add);
206 }
Thomas Vachuska329af532015-03-10 02:08:33 -0700207 });
Simon Hunte05cae42015-07-23 17:35:24 -0700208 log.debug("#handlers = {}, #overlays = {}", handlers.size(),
209 overlayCache.size());
Thomas Vachuska3553b302015-03-07 14:49:43 -0800210 }
211
212 // Destroys message handlers.
Simon Hunte05cae42015-07-23 17:35:24 -0700213 private synchronized void destroyHandlersAndOverlays() {
214 log.debug("destroying handlers and overlays...");
Thomas Vachuska3553b302015-03-07 14:49:43 -0800215 handlers.forEach((type, handler) -> handler.destroy());
216 handlers.clear();
Simon Hunte05cae42015-07-23 17:35:24 -0700217
218 if (overlayCache != null) {
219 overlayCache.destroy();
220 overlayCache = null;
221 }
Thomas Vachuska3553b302015-03-07 14:49:43 -0800222 }
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700223
224 // Sends cluster node/instance information to allow GUI to fail-over.
225 private void sendInstanceData() {
226 ClusterService service = directory.get(ClusterService.class);
227 ArrayNode instances = mapper.createArrayNode();
228
229 for (ControllerNode node : service.getNodes()) {
230 ObjectNode instance = mapper.createObjectNode()
231 .put("id", node.id().toString())
232 .put("ip", node.ip().toString())
Simon Hunt629b99e2015-07-27 17:38:33 -0700233 .put(TopoConstants.Glyphs.UI_ATTACHED,
234 node.equals(service.getLocalNode()));
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700235 instances.add(instance);
236 }
237
238 ObjectNode payload = mapper.createObjectNode();
Thomas Vachuska20084b72015-03-11 13:46:50 -0700239 payload.set("clusterNodes", instances);
240 sendMessage("bootstrap", 0, payload);
Thomas Vachuskab6acc7b2015-03-11 09:11:21 -0700241 }
242
Thomas Vachuska3553b302015-03-07 14:49:43 -0800243}
244