Simon Hunt
Committed by Gerrit Code Review

Web UI - reflect logged-in-username back to UI in the bootstrap event.

Change-Id: I771b0d1d426cc2435d8c8e5953d3e52d410f6293
...@@ -25,8 +25,8 @@ import org.onosproject.cluster.ClusterService; ...@@ -25,8 +25,8 @@ import org.onosproject.cluster.ClusterService;
25 import org.onosproject.cluster.ControllerNode; 25 import org.onosproject.cluster.ControllerNode;
26 import org.onosproject.ui.UiConnection; 26 import org.onosproject.ui.UiConnection;
27 import org.onosproject.ui.UiExtensionService; 27 import org.onosproject.ui.UiExtensionService;
28 -import org.onosproject.ui.UiMessageHandlerFactory;
29 import org.onosproject.ui.UiMessageHandler; 28 import org.onosproject.ui.UiMessageHandler;
29 +import org.onosproject.ui.UiMessageHandlerFactory;
30 import org.onosproject.ui.UiTopoOverlayFactory; 30 import org.onosproject.ui.UiTopoOverlayFactory;
31 import org.onosproject.ui.impl.topo.UiTopoSession; 31 import org.onosproject.ui.impl.topo.UiTopoSession;
32 import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; 32 import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
...@@ -46,6 +46,17 @@ public class UiWebSocket ...@@ -46,6 +46,17 @@ public class UiWebSocket
46 46
47 private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class); 47 private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class);
48 48
49 + private static final String EVENT = "event";
50 + private static final String SID = "sid";
51 + private static final String PAYLOAD = "payload";
52 + private static final String UNKNOWN = "unknown";
53 +
54 + private static final String ID = "id";
55 + private static final String IP = "ip";
56 + private static final String CLUSTER_NODES = "clusterNodes";
57 + private static final String USER = "user";
58 + private static final String BOOTSTRAP = "bootstrap";
59 +
49 private static final long MAX_AGE_MS = 30_000; 60 private static final long MAX_AGE_MS = 30_000;
50 61
51 private static final byte PING = 0x9; 62 private static final byte PING = 0x9;
...@@ -69,7 +80,7 @@ public class UiWebSocket ...@@ -69,7 +80,7 @@ public class UiWebSocket
69 * Creates a new web-socket for serving data to the Web UI. 80 * Creates a new web-socket for serving data to the Web UI.
70 * 81 *
71 * @param directory service directory 82 * @param directory service directory
72 - * @param userName user name of the logged-in user 83 + * @param userName user name of the logged-in user
73 */ 84 */
74 public UiWebSocket(ServiceDirectory directory, String userName) { 85 public UiWebSocket(ServiceDirectory directory, String userName) {
75 this.directory = directory; 86 this.directory = directory;
...@@ -121,8 +132,8 @@ public class UiWebSocket ...@@ -121,8 +132,8 @@ public class UiWebSocket
121 try { 132 try {
122 topoSession.init(); 133 topoSession.init();
123 createHandlersAndOverlays(); 134 createHandlersAndOverlays();
124 - sendInstanceData(); 135 + sendBootstrapData();
125 - log.info("GUI client connected"); 136 + log.info("GUI client connected -- user <{}>", userName);
126 137
127 } catch (ServiceNotFoundException e) { 138 } catch (ServiceNotFoundException e) {
128 log.warn("Unable to open GUI connection; services have been shut-down", e); 139 log.warn("Unable to open GUI connection; services have been shut-down", e);
...@@ -137,7 +148,7 @@ public class UiWebSocket ...@@ -137,7 +148,7 @@ public class UiWebSocket
137 topoSession.destroy(); 148 topoSession.destroy();
138 destroyHandlersAndOverlays(); 149 destroyHandlersAndOverlays();
139 log.info("GUI client disconnected [close-code={}, message={}]", 150 log.info("GUI client disconnected [close-code={}, message={}]",
140 - closeCode, message); 151 + closeCode, message);
141 } 152 }
142 153
143 @Override 154 @Override
...@@ -152,7 +163,7 @@ public class UiWebSocket ...@@ -152,7 +163,7 @@ public class UiWebSocket
152 lastActive = System.currentTimeMillis(); 163 lastActive = System.currentTimeMillis();
153 try { 164 try {
154 ObjectNode message = (ObjectNode) mapper.reader().readTree(data); 165 ObjectNode message = (ObjectNode) mapper.reader().readTree(data);
155 - String type = message.path("event").asText("unknown"); 166 + String type = message.path(EVENT).asText(UNKNOWN);
156 UiMessageHandler handler = handlers.get(type); 167 UiMessageHandler handler = handlers.get(type);
157 if (handler != null) { 168 if (handler != null) {
158 handler.process(message); 169 handler.process(message);
...@@ -180,17 +191,17 @@ public class UiWebSocket ...@@ -180,17 +191,17 @@ public class UiWebSocket
180 @Override 191 @Override
181 public synchronized void sendMessage(String type, long sid, ObjectNode payload) { 192 public synchronized void sendMessage(String type, long sid, ObjectNode payload) {
182 ObjectNode message = mapper.createObjectNode(); 193 ObjectNode message = mapper.createObjectNode();
183 - message.put("event", type); 194 + message.put(EVENT, type);
184 if (sid > 0) { 195 if (sid > 0) {
185 - message.put("sid", sid); 196 + message.put(SID, sid);
186 } 197 }
187 - message.set("payload", payload != null ? payload : mapper.createObjectNode()); 198 + message.set(PAYLOAD, payload != null ? payload : mapper.createObjectNode());
188 sendMessage(message); 199 sendMessage(message);
189 } 200 }
190 201
191 // Creates new message handlers. 202 // Creates new message handlers.
192 private synchronized void createHandlersAndOverlays() { 203 private synchronized void createHandlersAndOverlays() {
193 - log.debug("creating handlers and overlays..."); 204 + log.debug("Creating handlers and overlays...");
194 handlers = new HashMap<>(); 205 handlers = new HashMap<>();
195 overlayCache = new TopoOverlayCache(); 206 overlayCache = new TopoOverlayCache();
196 207
...@@ -219,12 +230,12 @@ public class UiWebSocket ...@@ -219,12 +230,12 @@ public class UiWebSocket
219 } 230 }
220 }); 231 });
221 log.debug("#handlers = {}, #overlays = {}", handlers.size(), 232 log.debug("#handlers = {}, #overlays = {}", handlers.size(),
222 - overlayCache.size()); 233 + overlayCache.size());
223 } 234 }
224 235
225 // Destroys message handlers. 236 // Destroys message handlers.
226 private synchronized void destroyHandlersAndOverlays() { 237 private synchronized void destroyHandlersAndOverlays() {
227 - log.debug("destroying handlers and overlays..."); 238 + log.debug("Destroying handlers and overlays...");
228 handlers.forEach((type, handler) -> handler.destroy()); 239 handlers.forEach((type, handler) -> handler.destroy());
229 handlers.clear(); 240 handlers.clear();
230 241
...@@ -234,23 +245,26 @@ public class UiWebSocket ...@@ -234,23 +245,26 @@ public class UiWebSocket
234 } 245 }
235 } 246 }
236 247
237 - // Sends cluster node/instance information to allow GUI to fail-over. 248 + // Sends initial information (username and cluster member information)
238 - private void sendInstanceData() { 249 + // to allow GUI to display logged-in user, and to be able to
250 + // fail-over to an alternate cluster member if necessary.
251 + private void sendBootstrapData() {
239 ClusterService service = directory.get(ClusterService.class); 252 ClusterService service = directory.get(ClusterService.class);
240 ArrayNode instances = mapper.createArrayNode(); 253 ArrayNode instances = mapper.createArrayNode();
241 254
242 for (ControllerNode node : service.getNodes()) { 255 for (ControllerNode node : service.getNodes()) {
243 ObjectNode instance = mapper.createObjectNode() 256 ObjectNode instance = mapper.createObjectNode()
244 - .put("id", node.id().toString()) 257 + .put(ID, node.id().toString())
245 - .put("ip", node.ip().toString()) 258 + .put(IP, node.ip().toString())
246 .put(TopoConstants.Glyphs.UI_ATTACHED, 259 .put(TopoConstants.Glyphs.UI_ATTACHED,
247 - node.equals(service.getLocalNode())); 260 + node.equals(service.getLocalNode()));
248 instances.add(instance); 261 instances.add(instance);
249 } 262 }
250 263
251 ObjectNode payload = mapper.createObjectNode(); 264 ObjectNode payload = mapper.createObjectNode();
252 - payload.set("clusterNodes", instances); 265 + payload.set(CLUSTER_NODES, instances);
253 - sendMessage("bootstrap", 0, payload); 266 + payload.put(USER, userName);
267 + sendMessage(BOOTSTRAP, 0, payload);
254 } 268 }
255 269
256 } 270 }
......
...@@ -36,13 +36,16 @@ ...@@ -36,13 +36,16 @@
36 clusterIndex = -1, // the instance to which we are connected 36 clusterIndex = -1, // the instance to which we are connected
37 connectRetries = 0, // limit our attempts at reconnecting 37 connectRetries = 0, // limit our attempts at reconnecting
38 openListeners = {}, // registered listeners for websocket open() 38 openListeners = {}, // registered listeners for websocket open()
39 - nextListenerId = 1; // internal ID for open listeners 39 + nextListenerId = 1, // internal ID for open listeners
40 + loggedInUser = null; // name of logged-in user
40 41
41 // ======================= 42 // =======================
42 // === Bootstrap Handler 43 // === Bootstrap Handler
43 44
44 var builtinHandlers = { 45 var builtinHandlers = {
45 bootstrap: function (data) { 46 bootstrap: function (data) {
47 + $log.debug('bootstrap data', data);
48 + loggedInUser = data.user;
46 clusterNodes = data.clusterNodes; 49 clusterNodes = data.clusterNodes;
47 clusterNodes.forEach(function (d, i) { 50 clusterNodes.forEach(function (d, i) {
48 if (d.uiAttached) { 51 if (d.uiAttached) {
...@@ -332,6 +335,7 @@ ...@@ -332,6 +335,7 @@
332 removeOpenListener: removeOpenListener, 335 removeOpenListener: removeOpenListener,
333 sendEvent: sendEvent, 336 sendEvent: sendEvent,
334 isConnected: function () { return wsUp; }, 337 isConnected: function () { return wsUp; },
338 + loggedInUser: function () { return loggedInUser || '(no-one)'; },
335 339
336 _setVeilDelegate: setVeilDelegate, 340 _setVeilDelegate: setVeilDelegate,
337 _setLoadingDelegate: setLoadingDelegate 341 _setLoadingDelegate: setLoadingDelegate
......