GUI -- Beginnings of structure for topology Overlay API.
- Re-implemented RequestSummary / ShowSummary in Alt implementation.

Change-Id: Idb86c7bf3ede8f8815abcb488bbf9b0a7041ef79
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/OverlayService.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/OverlayService.java
new file mode 100644
index 0000000..08743ce
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/OverlayService.java
@@ -0,0 +1,41 @@
+/*
+ * 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.topo;
+
+import org.onosproject.ui.impl.topo.overlay.SummaryGenerator;
+
+/**
+ * Provides the API for external agents to inject topology overlay behavior.
+ */
+public interface OverlayService {
+
+    /**
+     * Registers a custom summary generator for the specified overlay.
+     *
+     * @param overlayId overlay identifier
+     * @param generator generator to register
+     */
+    void addSummaryGenerator(String overlayId, SummaryGenerator generator);
+
+    /**
+     * Unregisters the generator associated with the specified overlay.
+     *
+     * @param overlayId overlay identifier
+     */
+    void removeSummaryGenerator(String overlayId);
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/SummaryData.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/SummaryData.java
new file mode 100644
index 0000000..6dd93ee
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/SummaryData.java
@@ -0,0 +1,67 @@
+/*
+ * 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.topo;
+
+/**
+ * Provides basic summary data for the topology.
+ */
+public interface SummaryData {
+
+    /**
+     * Returns the number of devices.
+     *
+     * @return number of devices
+     */
+    int deviceCount();
+
+    /**
+     * Returns the number of links.
+     *
+     * @return number of links
+     */
+    int linkCount();
+
+    /**
+     * Returns the number of hosts.
+     *
+     * @return number of hosts
+     */
+    int hostCount();
+
+    /**
+     * Returns the number of clusters (topology SCCs).
+     *
+     * @return number of clusters
+     */
+    int clusterCount();
+
+    /**
+     * Returns the number of intents in the system.
+     *
+     * @return number of intents
+     */
+    long intentCount();
+
+    /**
+     * Returns the number of flow rules in the system.
+     *
+     * @return number of flow rules
+     */
+    int flowRuleCount();
+
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiEvent.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiEvent.java
index 32ff62b..957476c 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiEvent.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiEvent.java
@@ -29,6 +29,10 @@
      * Type of Topology UI Model events.
      */
     public enum Type {
+        // notification events
+        SUMMARY_UPDATE,
+
+        // unsolicited topology events
         INSTANCE_ADDED,
         INSTANCE_REMOVED,
         DEVICE_ADDED,
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelManager.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelManager.java
index 95e8430..7cb0e40 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelManager.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelManager.java
@@ -35,10 +35,14 @@
 import org.onosproject.net.Link;
 import org.onosproject.net.device.DeviceEvent;
 import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.FlowRuleService;
 import org.onosproject.net.host.HostEvent;
 import org.onosproject.net.host.HostService;
+import org.onosproject.net.intent.IntentService;
 import org.onosproject.net.link.LinkEvent;
 import org.onosproject.net.link.LinkService;
+import org.onosproject.net.topology.Topology;
+import org.onosproject.net.topology.TopologyService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -51,6 +55,7 @@
 import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
 import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
 import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
+import static org.onosproject.ui.impl.topo.TopoUiEvent.Type.SUMMARY_UPDATE;
 
 
 /**
@@ -79,6 +84,14 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected MastershipService mastershipService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected IntentService intentService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected FlowRuleService flowRuleService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected TopologyService topologyService;
 
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -103,6 +116,7 @@
                 linkService,
                 hostService,
                 mastershipService
+                // TODO: others??
         );
         log.info("Started");
     }
@@ -113,6 +127,17 @@
         log.info("Stopped");
     }
 
+
+    // TODO: figure out how to cull zombie listeners
+    // The problem is when one refreshes the GUI (topology view)
+    //  a new instance of AltTopoViewMessageHandler is created and added
+    //  as a listener, but we never got a TopoStop event, which is what
+    //  causes the listener (for an AltTopoViewMessageHandler instance) to
+    //  be removed.
+    // ==== Somehow need to tie this in to the GUI-disconnected event.
+
+
+
     @Override
     public void addListener(TopoUiListener listener) {
         listenerRegistry.addListener(listener);
@@ -133,6 +158,60 @@
         return results;
     }
 
+    @Override
+    public void startSummaryMonitoring() {
+        // TODO: set up periodic monitoring task
+        // send a summary now, and periodically...
+        post(new TopoUiEvent(SUMMARY_UPDATE, null));
+    }
+
+    @Override
+    public void stopSummaryMonitoring() {
+        // TODO: cancel monitoring task
+
+    }
+
+    @Override
+    public SummaryData getSummaryData() {
+        return new SummaryDataImpl();
+    }
+
+    // =====================================================================
+
+    private final class SummaryDataImpl implements SummaryData {
+        private final Topology topology = topologyService.currentTopology();
+
+        @Override
+        public int deviceCount() {
+            return topology.deviceCount();
+        }
+
+        @Override
+        public int linkCount() {
+            return topology.linkCount();
+        }
+
+        @Override
+        public int hostCount() {
+            return hostService.getHostCount();
+        }
+
+        @Override
+        public int clusterCount() {
+            return topology.clusterCount();
+        }
+
+        @Override
+        public long intentCount() {
+            return intentService.getIntentCount();
+        }
+
+        @Override
+        public int flowRuleCount() {
+            return flowRuleService.getFlowRuleCount();
+        }
+    }
+
     // =====================================================================
 
     private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelService.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelService.java
index 7bbbbe8..83d0dbf 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelService.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelService.java
@@ -47,8 +47,27 @@
      * These will be in the form of "addInstance", "addDevice", "addLink",
      * and "addHost" events, as appropriate.
      *
-     * @return initial state events
+     * @return initial state messages
      */
     List<ObjectNode> getInitialState();
 
+    /**
+     * Starts the summary monitoring process.
+     * <p>
+     * Sends a "showSummary" message now, and schedules a task to send
+     * updates whenever the data changes.
+     */
+    void startSummaryMonitoring();
+
+    /**
+     * Cancels the task that sends summary updates.
+     */
+    void stopSummaryMonitoring();
+
+    /**
+     * Returns base data about the topology.
+     *
+     * @return summary data
+     */
+    SummaryData getSummaryData();
 }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/overlay/AbstractSummaryGenerator.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/overlay/AbstractSummaryGenerator.java
new file mode 100644
index 0000000..ce840bd
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/overlay/AbstractSummaryGenerator.java
@@ -0,0 +1,163 @@
+/*
+ * 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.topo.overlay;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.text.DecimalFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Base implementation of a {@link SummaryGenerator}. Provides convenience
+ * methods for compiling a list of properties to be displayed in the summary
+ * panel on the UI.
+ */
+public abstract class AbstractSummaryGenerator implements SummaryGenerator {
+    private static final String NUMBER_FORMAT = "#,###";
+    private static final DecimalFormat DF = new DecimalFormat(NUMBER_FORMAT);
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    private final List<Prop> props = new ArrayList<>();
+    private String iconId;
+    private String title;
+
+    /**
+     * Constructs a summary generator without specifying the icon ID or title.
+     * It is expected that the title (and optionally the icon ID) will be set
+     * later via {@link #title(String)} (and {@link #iconId(String)}), before
+     * {@link #buildObjectNode()} is invoked to generate the message payload.
+     */
+    public AbstractSummaryGenerator() {
+    }
+
+    /**
+     * Constructs a summary generator that uses the specified iconId ID and
+     * title in its generated output.
+     *
+     * @param iconId iconId ID
+     * @param title title
+     */
+    public AbstractSummaryGenerator(String iconId, String title) {
+        this.iconId = iconId;
+        this.title = title;
+    }
+
+    /**
+     * Subclasses need to provide an implementation.
+     *
+     * @return the summary payload
+     */
+    @Override
+    public abstract ObjectNode generateSummary();
+
+    /**
+     * Formats the given number into a string, using comma separator.
+     *
+     * @param number the number
+     * @return formatted as a string
+     */
+    protected String format(Number number) {
+        return DF.format(number);
+    }
+
+    /**
+     * Sets the iconId ID to use.
+     *
+     * @param iconId iconId ID
+     */
+    protected void iconId(String iconId) {
+        this.iconId = iconId;
+    }
+
+    /**
+     * Sets the summary panel title.
+     *
+     * @param title the title
+     */
+    protected void title(String title) {
+        this.title = title;
+    }
+
+    /**
+     * Clears out the cache of properties.
+     */
+    protected void clearProps() {
+        props.clear();
+    }
+
+    /**
+     * Adds a property to the summary. Note that the value is converted to
+     * a string by invoking the <code>toString()</code> method on it.
+     *
+     * @param label the label
+     * @param value the value
+     */
+    protected void prop(String label, Object value) {
+        props.add(new Prop(label, value));
+    }
+
+    /**
+     * Adds a separator to the summary; when rendered on the client, a visible
+     * break between properties.
+     */
+    protected void separator() {
+        props.add(new Prop("-", ""));
+    }
+
+    /**
+     * Builds an object node from the current state of the summary generator.
+     *
+     * @return summary payload as JSON object node
+     */
+    protected ObjectNode buildObjectNode() {
+        ObjectNode result = MAPPER.createObjectNode();
+        // NOTE: "id" and "type" are currently used for title and iconID
+        //  so that this structure can be "re-used" with detail panel payloads
+        result.put("id", title).put("type", iconId);
+
+        ObjectNode pnode = MAPPER.createObjectNode();
+        ArrayNode porder = MAPPER.createArrayNode();
+
+        for (Prop p : props) {
+            porder.add(p.label);
+            pnode.put(p.label, p.value);
+        }
+        result.set("propOrder", porder);
+        result.set("props", pnode);
+
+        return result;
+    }
+
+    // ===================================================================
+
+    /**
+     * Abstraction of a property, that is, a label-value pair.
+     */
+    private static class Prop {
+        private final String label;
+        private final String value;
+
+        public Prop(String label, Object value) {
+            this.label = label;
+            this.value = value.toString();
+        }
+    }
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/overlay/SummaryGenerator.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/overlay/SummaryGenerator.java
new file mode 100644
index 0000000..aa1be9c
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/overlay/SummaryGenerator.java
@@ -0,0 +1,37 @@
+/*
+ * 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.topo.overlay;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * May be called upon to generate the summary messages for the topology view
+ * in the GUI.
+ * <p>
+ * It is assumed that if a custom summary generator is installed on the server
+ * (as part of a topology overlay), a peer custom summary message handler will
+ * be installed on the client side to handle the messages thus generated.
+ */
+public interface SummaryGenerator {
+    /**
+     * Generates the payload for the "showSummary" message.
+     *
+     * @return the message payload
+     */
+    ObjectNode generateSummary();
+}