Adding protection against UI extension failures crashing the whole UI.

Fixed NPE for null devices when displaying details.

Change-Id: I0053939807ea2493e125c7a1fd58606e4c4d3e02
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/DeviceViewMessageHandler.java b/web/gui/src/main/java/org/onosproject/ui/impl/DeviceViewMessageHandler.java
index 53b16a6..a740a73 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/DeviceViewMessageHandler.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/DeviceViewMessageHandler.java
@@ -18,6 +18,7 @@
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.ImmutableSet;
+import org.onosproject.cluster.NodeId;
 import org.onosproject.mastership.MastershipService;
 import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.ConnectPoint;
@@ -86,6 +87,7 @@
     private static final String NAME = "name";
     private static final String WARN = "warn";
 
+    private static final String NONE = "none";
 
     private static final String[] COL_IDS = {
             AVAILABLE, AVAILABLE_IID, TYPE_IID,
@@ -178,6 +180,7 @@
             MastershipService ms = get(MastershipService.class);
             Device device = service.getDevice(deviceId);
             ObjectNode data = objectNode();
+            NodeId masterFor = ms.getMasterFor(deviceId);
 
             data.put(ID, deviceId.toString());
             data.put(NAME, deviceName(device));
@@ -188,7 +191,7 @@
             data.put(SW, device.swVersion());
             data.put(SERIAL, device.serialNumber());
             data.put(CHASSIS_ID, device.chassisId().toString());
-            data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
+            data.put(MASTER_ID, masterFor != null ? masterFor.toString() : NONE);
             data.put(PROTOCOL, deviceProtocol(device));
 
             ArrayNode ports = arrayNode();
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocket.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocket.java
index 1d49d02..449cdec 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocket.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocket.java
@@ -186,12 +186,16 @@
             UiMessageHandlerFactory factory = ext.messageHandlerFactory();
             if (factory != null) {
                 factory.newHandlers().forEach(handler -> {
-                    handler.init(this, directory);
-                    handler.messageTypes().forEach(type -> handlers.put(type, handler));
+                    try {
+                        handler.init(this, directory);
+                        handler.messageTypes().forEach(type -> handlers.put(type, handler));
 
-                    // need to inject the overlay cache into topology message handler
-                    if (handler instanceof TopologyViewMessageHandler) {
-                        ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
+                        // need to inject the overlay cache into topology message handler
+                        if (handler instanceof TopologyViewMessageHandler) {
+                            ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache);
+                        }
+                    } catch (Exception e) {
+                        log.warn("Unable to setup handler {} due to", handler, e);
                     }
                 });
             }