ONOS-4971: Synthetic Link Data -- WIP

- adding CLI commands for dumping Model Cache contents.

Change-Id: I28dfe179835de6cd0c5356faf87af46a239eb772
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java
index b4eabe9..585e658 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java
@@ -261,6 +261,15 @@
     }
 
     /**
+     * Returns all links in the model.
+     *
+     * @return all links
+     */
+    public Set<UiLink> allLinks() {
+        return new HashSet<>(linkLookup.values());
+    }
+
+    /**
      * Returns the link with the specified identifier, or null if no such
      * link exists.
      *
@@ -302,6 +311,15 @@
     }
 
     /**
+     * Returns all hosts in the model.
+     *
+     * @return all hosts
+     */
+    public Set<UiHost> allHosts() {
+        return new HashSet<>(hostLookup.values());
+    }
+
+    /**
      * Returns the host with the specified identifier, or null if no such
      * host exists.
      *
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/AbstractElementCommand.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/AbstractElementCommand.java
new file mode 100644
index 0000000..820ddf0
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/AbstractElementCommand.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2016-present 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.cli;
+
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.ui.model.topo.UiElement;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Base class for model cache CLI commands.
+ */
+abstract class AbstractElementCommand extends AbstractShellCommand {
+
+    /**
+     * Built in comparator for elements.
+     */
+    private static final Comparator<UiElement> ELEMENT_COMPARATOR =
+            (o1, o2) -> o1.idAsString().compareTo(o2.idAsString());
+
+    /**
+     * Returns the given elements in a list, sorted by string representation
+     * of the identifiers.
+     *
+     * @param elements the elements to sort
+     * @return the sorted elements
+     */
+    protected List<UiElement> sorted(Set<? extends UiElement> elements) {
+        List<UiElement> list = new ArrayList<>(elements);
+        Collections.sort(list, ELEMENT_COMPARATOR);
+        return list;
+    }
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListDevices.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListDevices.java
new file mode 100644
index 0000000..9544bcb
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListDevices.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016-present 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.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
+
+/**
+ * CLI command to list the UiDevices stored in the ModelCache.
+ */
+@Command(scope = "onos", name = "ui-cache-devices",
+        description = "Lists UiDevices in the Model Cache")
+public class ListDevices extends AbstractElementCommand {
+
+    @Override
+    protected void execute() {
+        UiSharedTopologyModel model = get(UiSharedTopologyModel.class);
+        sorted(model.getDevices()).forEach(d -> print("%s", d));
+    }
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListHosts.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListHosts.java
new file mode 100644
index 0000000..9de0159
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListHosts.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016-present 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.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
+
+/**
+ * CLI command to list the UiH0osts stored in the ModelCache.
+ */
+@Command(scope = "onos", name = "ui-cache-hosts",
+        description = "Lists UiHosts in the Model Cache")
+public class ListHosts extends AbstractElementCommand {
+
+    @Override
+    protected void execute() {
+        UiSharedTopologyModel model = get(UiSharedTopologyModel.class);
+        sorted(model.getHosts()).forEach(h -> print("%s", h));
+    }
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListLinks.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListLinks.java
new file mode 100644
index 0000000..640b37d
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListLinks.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016-present 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.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
+
+/**
+ * CLI command to list the UiLinks stored in the ModelCache.
+ */
+@Command(scope = "onos", name = "ui-cache-links",
+        description = "Lists UiLinks in the Model Cache")
+public class ListLinks extends AbstractElementCommand {
+
+    @Override
+    protected void execute() {
+        UiSharedTopologyModel model = get(UiSharedTopologyModel.class);
+        sorted(model.getLinks()).forEach(l -> print("%s", l));
+    }
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListMembers.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListMembers.java
new file mode 100644
index 0000000..a6f5617
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListMembers.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2016-present 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.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
+
+/**
+ * CLI command to list the UiClusterMembers stored in the ModelCache.
+ */
+@Command(scope = "onos", name = "ui-cache-members",
+        description = "Lists UiClusterMembers in the Model Cache")
+public class ListMembers extends AbstractElementCommand {
+
+    @Override
+    protected void execute() {
+        UiSharedTopologyModel model = get(UiSharedTopologyModel.class);
+        // note: getClusterMembers() returns an already sorted list...
+        model.getClusterMembers().forEach(m -> print("%s", m));
+    }
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListRegions.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListRegions.java
index f7c7972..cdb8bd5 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListRegions.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/cli/ListRegions.java
@@ -17,7 +17,6 @@
 package org.onosproject.ui.impl.topo.cli;
 
 import org.apache.karaf.shell.commands.Command;
-import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
 
 /**
@@ -25,11 +24,11 @@
  */
 @Command(scope = "onos", name = "ui-cache-regions",
         description = "Lists UiRegions in the Model Cache")
-public class ListRegions extends AbstractShellCommand {
+public class ListRegions extends AbstractElementCommand {
 
     @Override
     protected void execute() {
         UiSharedTopologyModel model = get(UiSharedTopologyModel.class);
-        model.getRegions().forEach(r -> print("%s", r));
+        sorted(model.getRegions()).forEach(r -> print("%s", r));
     }
 }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/ModelCache.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/ModelCache.java
index e3fedd1..4ca6b42 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/ModelCache.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/ModelCache.java
@@ -298,6 +298,10 @@
         }
     }
 
+    Set<UiDevice> getAllDevices() {
+        return uiTopology.allDevices();
+    }
+
 
     // === LINKS
 
@@ -357,6 +361,9 @@
         }
     }
 
+    Set<UiLink> getAllLinks() {
+        return uiTopology.allLinks();
+    }
 
     // === HOSTS
 
@@ -460,6 +467,10 @@
         }
     }
 
+    Set<UiHost> getAllHosts() {
+        return uiTopology.allHosts();
+    }
+
 
     /**
      * Refreshes the internal state.
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiSharedTopologyModel.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiSharedTopologyModel.java
index 52eea69..4a5cc91 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiSharedTopologyModel.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiSharedTopologyModel.java
@@ -63,6 +63,9 @@
 import org.onosproject.ui.impl.topo.UiTopoSession;
 import org.onosproject.ui.model.ServiceBundle;
 import org.onosproject.ui.model.topo.UiClusterMember;
+import org.onosproject.ui.model.topo.UiDevice;
+import org.onosproject.ui.model.topo.UiHost;
+import org.onosproject.ui.model.topo.UiLink;
 import org.onosproject.ui.model.topo.UiRegion;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -200,7 +203,14 @@
     //  Methods for topo session (or CLI) to use to get information from us
 
     /**
-     * Returns the list of cluster members stored in our model cache.
+     * Refreshes the cache's internal state.
+     */
+    public void refresh() {
+        cache.refresh();
+    }
+
+    /**
+     * Returns the list of cluster members stored in the model cache.
      *
      * @return list of cluster members
      */
@@ -209,7 +219,7 @@
     }
 
     /**
-     * Returns the set of regions stored in our model cache.
+     * Returns the set of regions stored in the model cache.
      *
      * @return set of regions
      */
@@ -237,10 +247,30 @@
     }
 
     /**
-     * Refreshes the cache's internal state.
+     * Returns the set of devices stored in the model cache.
+     *
+     * @return set of devices
      */
-    public void refresh() {
-        cache.refresh();
+    public Set<UiDevice> getDevices() {
+        return cache.getAllDevices();
+    }
+
+    /**
+     * Returns the set of hosts stored in the model cache.
+     *
+     * @return set of hosts
+     */
+    public Set<UiHost> getHosts() {
+        return cache.getAllHosts();
+    }
+
+    /**
+     * Returns the set of links stored in the model cache.
+     *
+     * @return set of links
+     */
+    public Set<UiLink> getLinks() {
+        return cache.getAllLinks();
     }
 
     // =====================================================================
diff --git a/web/gui/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/web/gui/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 4d1abe3..6b8633c 100644
--- a/web/gui/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/web/gui/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -18,8 +18,20 @@
 
     <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
         <command>
+            <action class="org.onosproject.ui.impl.topo.cli.ListMembers"/>
+        </command>
+        <command>
             <action class="org.onosproject.ui.impl.topo.cli.ListRegions"/>
         </command>
+        <command>
+            <action class="org.onosproject.ui.impl.topo.cli.ListDevices"/>
+        </command>
+        <command>
+            <action class="org.onosproject.ui.impl.topo.cli.ListHosts"/>
+        </command>
+        <command>
+            <action class="org.onosproject.ui.impl.topo.cli.ListLinks"/>
+        </command>
     </command-bundle>
 
     <!--<bean id="macIDCompleter" class="org.onosproject.dhcp.cli.MacIdCompleter"/>-->