GUI -- Initial cut at bootstrap message from server to client.
- Note.. Thomas to layer changes on top of this... WIP

Change-Id: I6e2a624e2cfd6fc0ece761be46b71b23242dd2b2
diff --git a/core/api/src/main/java/org/onosproject/ui/UiMessageHandler.java b/core/api/src/main/java/org/onosproject/ui/UiMessageHandler.java
index 14d4f3d..5bd9647 100644
--- a/core/api/src/main/java/org/onosproject/ui/UiMessageHandler.java
+++ b/core/api/src/main/java/org/onosproject/ui/UiMessageHandler.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.ui;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.onlab.osgi.ServiceDirectory;
 
@@ -44,6 +45,9 @@
     private UiConnection connection;
     private ServiceDirectory directory;
 
+    /** Mapper for creating ObjectNodes and ArrayNodes etc. */
+    protected final ObjectMapper mapper = new ObjectMapper();
+
     /**
      * Creates a new message handler for the specified set of message types.
      *
@@ -121,4 +125,23 @@
         return directory.get(serviceClass);
     }
 
+    /**
+     * Wraps a message payload into an event structure for the given event
+     * type and sequence ID. Generally the
+     *
+     * @param type event type
+     * @param sid sequence ID
+     * @param payload event payload
+     * @return the object node representation
+     */
+    protected ObjectNode envelope(String type, long sid, ObjectNode payload) {
+        ObjectNode event = mapper.createObjectNode();
+        event.put("event", type);
+        if (sid > 0) {
+            event.put("sid", sid);
+        }
+        event.set("payload", payload);
+        return event;
+    }
+
 }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/BootstrapMessageHandler.java b/web/gui/src/main/java/org/onosproject/ui/impl/BootstrapMessageHandler.java
new file mode 100644
index 0000000..1ec23d7
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/BootstrapMessageHandler.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.ui.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import org.onlab.osgi.ServiceDirectory;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.ControllerNode;
+import org.onosproject.ui.UiConnection;
+import org.onosproject.ui.UiMessageHandler;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+
+/**
+ * Facility for creating and sending initial bootstrap message to the GUI.
+ */
+public class BootstrapMessageHandler extends UiMessageHandler {
+
+    private static final String BOOTSTRAP = "bootstrap";
+
+    private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
+            new Comparator<ControllerNode>() {
+                @Override
+                public int compare(ControllerNode o1, ControllerNode o2) {
+                    return o1.id().toString().compareTo(o2.id().toString());
+                }
+            };
+
+    private final ObjectMapper mapper = new ObjectMapper();
+
+    private ClusterService clusterService;
+
+    // TODO: ClusterEventListener - update GUI when instances come or go...
+
+    /**
+     * Creates a new bootstrap message handler for bootstrapping the GUI.
+     */
+    protected BootstrapMessageHandler() {
+        super(ImmutableSet.of(BOOTSTRAP));
+    }
+
+    @Override
+    public void init(UiConnection connection, ServiceDirectory directory) {
+        super.init(connection, directory);
+        clusterService = directory.get(ClusterService.class);
+
+        // Obtain list of cluster nodes and send bootstrap message to GUI
+        List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
+        Collections.sort(nodes, NODE_COMPARATOR);
+
+        ObjectNode payload = mapper.createObjectNode();
+
+        ArrayNode anode = mapper.createArrayNode();
+        for (ControllerNode node : nodes) {
+            anode.add(clusterNodeData(node));
+        }
+        payload.set("instances", anode);
+        connection.sendMessage(envelope(BOOTSTRAP, 0, payload));
+    }
+
+    private ObjectNode clusterNodeData(ControllerNode node) {
+        return mapper.createObjectNode()
+                .put("id", node.id().toString())
+                .put("ip", node.ip().toString())
+                .put("uiAttached", node.equals(clusterService.getLocalNode()));
+    }
+
+
+    @Override
+    public void process(ObjectNode message) {
+        // We registered for "bootstrap" events, but we don't expect
+        // the GUI to send us any; it was just that we had to define a
+        // non-empty set of events that we handle, in the constructor.
+    }
+
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java
index 9e8fcc7..2137ffa 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java
@@ -127,7 +127,6 @@
     protected StatisticService statService;
     protected TopologyService topologyService;
 
-    protected final ObjectMapper mapper = new ObjectMapper();
     private String version;
 
     // TODO: extract into an external & durable state; good enough for now and demo
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
index fa8df2e..803ce94 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
@@ -59,7 +59,11 @@
         List<UiView> coreViews = of(new UiView("sample", "Sample"),
                                     new UiView("topo", "Topology View"),
                                     new UiView("device", "Devices"));
-        UiMessageHandlerFactory messageHandlerFactory = () -> ImmutableList.of(new TopologyViewMessageHandler());
+        UiMessageHandlerFactory messageHandlerFactory =
+                () -> ImmutableList.of(
+                        new BootstrapMessageHandler(),
+                        new TopologyViewMessageHandler()
+                );
         return new UiExtension(coreViews, messageHandlerFactory, "core",
                                UiExtensionManager.class.getClassLoader());
     }