Starting implementation of UI topo  modeling. WIP.
If reviewing this, please refer to http://tinyurl.com/onos-ui-topo-model

Change-Id: Iab75f213ca0a7d36619f9c498387b402ac9477af
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 6a76eb5..6d5de44 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
@@ -24,6 +24,7 @@
 import org.onosproject.net.Host;
 import org.onosproject.net.Link;
 import org.onosproject.net.region.Region;
+import org.onosproject.ui.model.topo.UiClusterMember;
 import org.onosproject.ui.model.topo.UiDevice;
 import org.onosproject.ui.model.topo.UiTopology;
 
@@ -42,6 +43,11 @@
         this.dispatcher = eventDispatcher;
     }
 
+    @Override
+    public String toString() {
+        return "ModelCache{" + uiTopology + "}";
+    }
+
     /**
      * Clear our model.
      */
@@ -61,6 +67,44 @@
     }
 
 
+    /**
+     * Updates the model (adds a new instance if necessary) with the given
+     * controller node information.
+     *
+     * @param cnode controller node to be added/updated
+     */
+    void addOrUpdateClusterMember(ControllerNode cnode) {
+        UiClusterMember member = uiTopology.findClusterMember(cnode.id());
+        if (member != null) {
+            member.update(cnode);
+        } else {
+            member = new UiClusterMember(cnode);
+            uiTopology.add(member);
+        }
+
+        // TODO: post event
+    }
+
+    void removeClusterMember(ControllerNode cnode) {
+        // TODO: find cluster member assoc. with parameter; remove from model
+        // TODO: post event
+    }
+
+    void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) {
+        // TODO: store the updated mastership information
+        // TODO: post event
+    }
+
+    void addOrUpdateRegion(Region region) {
+        // TODO: find or create region assoc. with parameter
+        // TODO: post event
+    }
+
+    void removeRegion(Region region) {
+        // TODO: find region assoc. with parameter; remove from model
+        // TODO: post event
+    }
+
     void addOrUpdateDevice(Device device) {
         // TODO: find or create device assoc. with parameter
         // FIXME
@@ -80,32 +124,6 @@
 
     }
 
-    void addOrUpdateClusterMember(ControllerNode cnode) {
-        // TODO: find or create cluster member assoc. with parameter
-        // TODO: post event
-    }
-
-    void removeClusterMember(ControllerNode cnode) {
-        // TODO: find cluster member assoc. with parameter; remove from model
-        // TODO: post event
-    }
-
-
-    void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) {
-        // TODO: store the updated mastership information
-        // TODO: post event
-    }
-
-    void addOrUpdateRegion(Region region) {
-        // TODO: find or create region assoc. with parameter
-        // TODO: post event
-    }
-
-    void removeRegion(Region region) {
-        // TODO: find region assoc. with parameter; remove from model
-        // TODO: post event
-    }
-
     void addOrUpdateLink(Link link) {
         // TODO: find ui-link assoc. with parameter; create or update.
         // TODO: post event
@@ -129,4 +147,22 @@
     void removeHost(Host host) {
         // TODO: find host assoc. with parameter; remove from model
     }
+
+    /**
+     * Returns the number of members in the cluster.
+     *
+     * @return number of cluster members
+     */
+    public int clusterMemberCount() {
+        return uiTopology.clusterMemberCount();
+    }
+
+    /**
+     * Returns the number of regions configured in the topology.
+     *
+     * @return number of regions
+     */
+    public int regionCount() {
+        return uiTopology.regionCount();
+    }
 }
diff --git a/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/AbstractModelTest.java b/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/AbstractModelTest.java
new file mode 100644
index 0000000..8cc047d
--- /dev/null
+++ b/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/AbstractModelTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.model;
+
+/**
+ * Base class for model test classes.
+ */
+public abstract class AbstractModelTest {
+
+    /**
+     * System agnostic end-of-line character.
+     */
+    protected static final String EOL = String.format("%n");
+
+    /**
+     * Prints the given string to stdout.
+     *
+     * @param s string to print
+     */
+    protected void print(String s) {
+        System.out.println(s);
+    }
+
+    /**
+     * Prints the toString() of the given object to stdout.
+     *
+     * @param o object to print
+     */
+    protected void print(Object o) {
+        print(o.toString());
+    }
+
+    /**
+     * Prints the formatted string to stdout.
+     *
+     * @param fmt    format string
+     * @param params parameters
+     * @see String#format(String, Object...)
+     */
+    protected void print(String fmt, Object... params) {
+        print(String.format(fmt, params));
+    }
+
+}
diff --git a/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/ModelCacheTest.java b/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/ModelCacheTest.java
new file mode 100644
index 0000000..23a2b1b
--- /dev/null
+++ b/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/ModelCacheTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.model;
+
+import org.junit.Test;
+import org.onosproject.event.EventDispatcher;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link ModelCache}.
+ */
+public class ModelCacheTest extends AbstractModelTest {
+
+    private static final EventDispatcher DISPATCHER = event -> {
+        // Do we care?
+    };
+
+    private ModelCache cache;
+
+    @Test
+    public void basic() {
+        cache = new ModelCache(DISPATCHER);
+        print(cache);
+        assertEquals("unex # members", 0, cache.clusterMemberCount());
+        assertEquals("unex # regions", 0, cache.regionCount());
+    }
+
+}