Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.eclipse.jetty.websocket.WebSocket; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 23 | import org.onlab.osgi.ServiceNotFoundException; |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.ControllerNode; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.GlyphConstants; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 27 | import org.onosproject.ui.UiConnection; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.UiExtension; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 29 | import org.onosproject.ui.UiExtensionService; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.UiMessageHandlerFactory; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.UiTopo2OverlayFactory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.UiTopoOverlayFactory; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 35 | import org.onosproject.ui.impl.topo.Topo2Jsonifier; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.impl.topo.Topo2OverlayCache; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.impl.topo.Topo2TrafficMessageHandler; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 38 | import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 39 | import org.onosproject.ui.impl.topo.UiTopoSession; |
| 40 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
| 41 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | import java.io.IOException; |
| 46 | import java.util.HashMap; |
| 47 | import java.util.Map; |
| 48 | |
| 49 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 50 | * Web socket capable of interacting with the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 51 | */ |
| 52 | public class UiWebSocket |
| 53 | implements UiConnection, WebSocket.OnTextMessage, WebSocket.OnControl { |
| 54 | |
| 55 | private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class); |
| 56 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 57 | private static final String EVENT = "event"; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 58 | private static final String PAYLOAD = "payload"; |
| 59 | private static final String UNKNOWN = "unknown"; |
| 60 | |
| 61 | private static final String ID = "id"; |
| 62 | private static final String IP = "ip"; |
| 63 | private static final String CLUSTER_NODES = "clusterNodes"; |
| 64 | private static final String USER = "user"; |
| 65 | private static final String BOOTSTRAP = "bootstrap"; |
| 66 | |
Simon Hunt | d7395c8 | 2016-10-20 17:54:01 -0700 | [diff] [blame] | 67 | private static final String TOPO = "topo"; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 68 | |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 69 | private static final long MAX_AGE_MS = 30_000; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 70 | |
| 71 | private static final byte PING = 0x9; |
| 72 | private static final byte PONG = 0xA; |
| 73 | private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad}; |
| 74 | |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 75 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 76 | private final ServiceDirectory directory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 77 | private final UiTopoSession topoSession; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 78 | |
| 79 | private Connection connection; |
| 80 | private FrameConnection control; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 81 | private String userName; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 82 | private String currentView; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 83 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 84 | private long lastActive = System.currentTimeMillis(); |
| 85 | |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 86 | private Map<String, UiMessageHandler> handlers; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 87 | private TopoOverlayCache overlayCache; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 88 | private Topo2OverlayCache overlay2Cache; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 91 | * Creates a new web-socket for serving data to the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 92 | * |
| 93 | * @param directory service directory |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 94 | * @param userName user name of the logged-in user |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 95 | */ |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 96 | public UiWebSocket(ServiceDirectory directory, String userName) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 97 | this.directory = directory; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 98 | this.userName = userName; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 99 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 100 | Topo2Jsonifier t2json = new Topo2Jsonifier(directory, userName); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 101 | UiSharedTopologyModel sharedModel = directory.get(UiSharedTopologyModel.class); |
| 102 | UiTopoLayoutService layoutService = directory.get(UiTopoLayoutService.class); |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 103 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 104 | sharedModel.injectJsonifier(t2json); |
| 105 | |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 106 | topoSession = new UiTopoSession(this, t2json, sharedModel, layoutService); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public String userName() { |
| 111 | return userName; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public UiTopoLayout currentLayout() { |
| 116 | return topoSession.currentLayout(); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void setCurrentLayout(UiTopoLayout topoLayout) { |
| 121 | topoSession.setCurrentLayout(topoLayout); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public String currentView() { |
| 126 | return currentView; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void setCurrentView(String viewId) { |
| 131 | currentView = viewId; |
| 132 | topoSession.enableEvent(viewId.equals(TOPO)); |
| 133 | } |
| 134 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 135 | /** |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 136 | * Provides a reference to the topology session. |
| 137 | * |
| 138 | * @return topo session reference |
| 139 | */ |
| 140 | public UiTopoSession topoSession() { |
| 141 | return topoSession; |
| 142 | } |
| 143 | |
| 144 | /** |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 145 | * Issues a close on the connection. |
| 146 | */ |
| 147 | synchronized void close() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 148 | destroyHandlersAndOverlays(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 149 | if (connection.isOpen()) { |
| 150 | connection.close(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Indicates if this connection is idle. |
| 156 | * |
| 157 | * @return true if idle or closed |
| 158 | */ |
| 159 | synchronized boolean isIdle() { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 160 | long quietFor = System.currentTimeMillis() - lastActive; |
| 161 | boolean idle = quietFor > MAX_AGE_MS; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 162 | if (idle || (connection != null && !connection.isOpen())) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 163 | log.debug("IDLE (or closed) websocket [{} ms]", quietFor); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 164 | return true; |
| 165 | } else if (connection != null) { |
| 166 | try { |
| 167 | control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 168 | } catch (IOException e) { |
| 169 | log.warn("Unable to send ping message due to: ", e); |
| 170 | } |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | @Override |
Satish K | 598c28d | 2015-11-24 17:20:40 +0530 | [diff] [blame] | 176 | public synchronized void onOpen(Connection connection) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 177 | this.connection = connection; |
| 178 | this.control = (FrameConnection) connection; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 179 | try { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 180 | topoSession.init(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 181 | createHandlersAndOverlays(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 182 | sendBootstrapData(); |
| 183 | log.info("GUI client connected -- user <{}>", userName); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 184 | |
| 185 | } catch (ServiceNotFoundException e) { |
Brian O'Connor | 75deea6 | 2015-06-24 16:09:17 -0400 | [diff] [blame] | 186 | log.warn("Unable to open GUI connection; services have been shut-down", e); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 187 | this.connection.close(); |
| 188 | this.connection = null; |
| 189 | this.control = null; |
| 190 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public synchronized void onClose(int closeCode, String message) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 195 | topoSession.destroy(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 196 | destroyHandlersAndOverlays(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 197 | log.info("GUI client disconnected [close-code={}, message={}]", |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 198 | closeCode, message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public boolean onControl(byte controlCode, byte[] data, int offset, int length) { |
| 203 | lastActive = System.currentTimeMillis(); |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void onMessage(String data) { |
| 209 | lastActive = System.currentTimeMillis(); |
| 210 | try { |
| 211 | ObjectNode message = (ObjectNode) mapper.reader().readTree(data); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 212 | String type = message.path(EVENT).asText(UNKNOWN); |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 213 | UiMessageHandler handler = handlers.get(type); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 214 | if (handler != null) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 215 | log.debug("RX message: {}", message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 216 | handler.process(message); |
| 217 | } else { |
| 218 | log.warn("No GUI message handler for type {}", type); |
| 219 | } |
| 220 | } catch (Exception e) { |
| 221 | log.warn("Unable to parse GUI message {} due to {}", data, e); |
| 222 | log.debug("Boom!!!", e); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | @Override |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 227 | public synchronized void sendMessage(ObjectNode message) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 228 | try { |
| 229 | if (connection.isOpen()) { |
| 230 | connection.sendMessage(message.toString()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 231 | log.debug("TX message: {}", message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 232 | } |
| 233 | } catch (IOException e) { |
| 234 | log.warn("Unable to send message {} to GUI due to {}", message, e); |
| 235 | log.debug("Boom!!!", e); |
| 236 | } |
| 237 | } |
| 238 | |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 239 | @Override |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 240 | public synchronized void sendMessage(String type, ObjectNode payload) { |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 241 | ObjectNode message = mapper.createObjectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 242 | message.put(EVENT, type); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 243 | message.set(PAYLOAD, payload != null ? payload : mapper.createObjectNode()); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 244 | sendMessage(message); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 247 | // Creates new message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 248 | private synchronized void createHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 249 | log.debug("Creating handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 250 | handlers = new HashMap<>(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 251 | overlayCache = new TopoOverlayCache(); |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 252 | overlay2Cache = new Topo2OverlayCache(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 253 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 254 | Map<Class<?>, UiMessageHandler> handlerInstances = new HashMap<>(); |
| 255 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 256 | UiExtensionService service = directory.get(UiExtensionService.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 257 | service.getExtensions().forEach(ext -> { |
| 258 | UiMessageHandlerFactory factory = ext.messageHandlerFactory(); |
| 259 | if (factory != null) { |
| 260 | factory.newHandlers().forEach(handler -> { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 261 | try { |
| 262 | handler.init(this, directory); |
| 263 | handler.messageTypes().forEach(type -> handlers.put(type, handler)); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 264 | handlerInstances.put(handler.getClass(), handler); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 265 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 266 | } catch (Exception e) { |
| 267 | log.warn("Unable to setup handler {} due to", handler, e); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 268 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 269 | }); |
| 270 | } |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 271 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 272 | registerOverlays(ext); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 273 | }); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 274 | |
| 275 | handlerCrossConnects(handlerInstances); |
| 276 | |
| 277 | log.debug("#handlers = {}, #overlays = {}", handlers.size(), overlayCache.size()); |
| 278 | } |
| 279 | |
| 280 | private void registerOverlays(UiExtension ext) { |
| 281 | UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory(); |
| 282 | if (overlayFactory != null) { |
| 283 | overlayFactory.newOverlays().forEach(overlayCache::add); |
| 284 | } |
| 285 | |
| 286 | UiTopo2OverlayFactory overlay2Factory = ext.topo2OverlayFactory(); |
| 287 | if (overlay2Factory != null) { |
| 288 | overlay2Factory.newOverlays().forEach(overlay2Cache::add); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | private void handlerCrossConnects(Map<Class<?>, UiMessageHandler> handlers) { |
| 293 | TopologyViewMessageHandler topomh = (TopologyViewMessageHandler) |
| 294 | handlers.get(TopologyViewMessageHandler.class); |
| 295 | if (topomh != null) { |
| 296 | topomh.setOverlayCache(overlayCache); |
| 297 | } |
| 298 | |
| 299 | Topo2ViewMessageHandler topo2mh = (Topo2ViewMessageHandler) |
| 300 | handlers.get(Topo2ViewMessageHandler.class); |
| 301 | if (topo2mh != null) { |
| 302 | topo2mh.setOverlayCache(overlay2Cache); |
| 303 | |
| 304 | // We also need a link to Topo2Traffic |
| 305 | Topo2TrafficMessageHandler topo2traffic = (Topo2TrafficMessageHandler) |
| 306 | handlers.get(Topo2TrafficMessageHandler.class); |
| 307 | if (topo2traffic != null) { |
| 308 | topo2mh.setTrafficHandler(topo2traffic); |
| 309 | } else { |
| 310 | log.error("No topo2 traffic handler found"); |
| 311 | } |
| 312 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Destroys message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 316 | private synchronized void destroyHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 317 | log.debug("Destroying handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 318 | handlers.forEach((type, handler) -> handler.destroy()); |
| 319 | handlers.clear(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 320 | |
| 321 | if (overlayCache != null) { |
| 322 | overlayCache.destroy(); |
| 323 | overlayCache = null; |
| 324 | } |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 325 | if (overlay2Cache != null) { |
| 326 | overlay2Cache.destroy(); |
| 327 | overlay2Cache = null; |
| 328 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 329 | } |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 330 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 331 | // Sends initial information (username and cluster member information) |
| 332 | // to allow GUI to display logged-in user, and to be able to |
| 333 | // fail-over to an alternate cluster member if necessary. |
| 334 | private void sendBootstrapData() { |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 335 | ClusterService service = directory.get(ClusterService.class); |
| 336 | ArrayNode instances = mapper.createArrayNode(); |
| 337 | |
| 338 | for (ControllerNode node : service.getNodes()) { |
| 339 | ObjectNode instance = mapper.createObjectNode() |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 340 | .put(ID, node.id().toString()) |
| 341 | .put(IP, node.ip().toString()) |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 342 | .put(GlyphConstants.UI_ATTACHED, |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 343 | node.equals(service.getLocalNode())); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 344 | instances.add(instance); |
| 345 | } |
| 346 | |
| 347 | ObjectNode payload = mapper.createObjectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 348 | payload.set(CLUSTER_NODES, instances); |
| 349 | payload.put(USER, userName); |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 350 | sendMessage(BOOTSTRAP, payload); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 353 | } |