Topo2: Fixed instance device count and mastership

Change-Id: Ic6790df426765f929a2a0b42fe416501643659ba
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiClusterMember.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiClusterMember.java
index f0aae01..934559f 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiClusterMember.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiClusterMember.java
@@ -19,11 +19,6 @@
 import org.onlab.packet.IpAddress;
 import org.onosproject.cluster.ControllerNode;
 import org.onosproject.cluster.NodeId;
-import org.onosproject.net.DeviceId;
-
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
 
 import static org.onosproject.cluster.ControllerNode.State.INACTIVE;
 
@@ -36,7 +31,6 @@
     private final ControllerNode cnode;
 
     private ControllerNode.State state = INACTIVE;
-    private final Set<DeviceId> mastership = new HashSet<>();
 
     /**
      * Constructs a UI cluster member, with a reference to the parent
@@ -55,7 +49,6 @@
         return "UiClusterMember{" + cnode +
                 ", online=" + isOnline() +
                 ", ready=" + isReady() +
-                ", #devices=" + deviceCount() +
                 "}";
     }
 
@@ -83,17 +76,6 @@
     }
 
     /**
-     * Sets the collection of identities of devices for which this
-     * controller node is master.
-     *
-     * @param mastership device IDs
-     */
-    public void setMastership(Set<DeviceId> mastership) {
-        this.mastership.clear();
-        this.mastership.addAll(mastership);
-    }
-
-    /**
      * Returns the identity of the cluster member.
      *
      * @return member identifier
@@ -128,33 +110,4 @@
     public boolean isReady() {
         return state.isReady();
     }
-
-    /**
-     * Returns the number of devices for which this cluster member is master.
-     *
-     * @return number of devices for which this member is master
-     */
-    public int deviceCount() {
-        return mastership.size();
-    }
-
-    /**
-     * Returns the list of devices for which this cluster member is master.
-     *
-     * @return list of devices for which this member is master
-     */
-    public Set<DeviceId> masterOf() {
-        return Collections.unmodifiableSet(mastership);
-    }
-
-    /**
-     * Returns true if the specified device is one for which this cluster
-     * member is master.
-     *
-     * @param deviceId device ID
-     * @return true if this cluster member is master for the given device
-     */
-    public boolean masterOf(DeviceId deviceId) {
-        return mastership.contains(deviceId);
-    }
 }
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiDevice.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiDevice.java
index 04eaab0..84522de 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiDevice.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiDevice.java
@@ -17,7 +17,6 @@
 package org.onosproject.ui.model.topo;
 
 import com.google.common.base.MoreObjects;
-import org.onosproject.cluster.NodeId;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.region.RegionId;
@@ -31,7 +30,6 @@
     private final Device device;
 
     private RegionId regionId;
-    private NodeId masterId;
 
     /**
      * Creates a new UI device.
@@ -53,15 +51,6 @@
         this.regionId = regionId;
     }
 
-    /**
-     * Sets the ID of the controller node that holds mastership for this device.
-     *
-     * @param masterId master identifier
-     */
-    public void setMasterId(NodeId masterId) {
-        this.masterId = masterId;
-    }
-
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this)
@@ -70,10 +59,6 @@
                 .toString();
     }
 
-    //    @Override
-//    protected void destroy() {
-//    }
-
     /**
      * Returns the identity of the device.
      *
@@ -124,14 +109,4 @@
     public String type() {
         return device.type().toString().toLowerCase();
     }
-
-    /**
-     * Returns the identifier for the cluster member that has
-     * mastership over this device.
-     *
-     * @return master cluster member identifier
-     */
-    public NodeId master() {
-        return masterId;
-    }
 }
diff --git a/core/api/src/test/java/org/onosproject/ui/model/topo/UiClusterMemberTest.java b/core/api/src/test/java/org/onosproject/ui/model/topo/UiClusterMemberTest.java
index e7c75a8..f29e8dd 100644
--- a/core/api/src/test/java/org/onosproject/ui/model/topo/UiClusterMemberTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/model/topo/UiClusterMemberTest.java
@@ -55,6 +55,5 @@
         assertEquals("wrong IP", NODE_IP, member.ip());
         assertEquals("unex. online", false, member.isOnline());
         assertEquals("unex. ready", false, member.isReady());
-        assertEquals("unex. device count", 0, member.deviceCount());
     }
 }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java
index 323ccee..15332d4 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java
@@ -30,6 +30,7 @@
 import org.onosproject.net.Annotated;
 import org.onosproject.net.Annotations;
 import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.Host;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.flow.FlowRuleService;
@@ -162,13 +163,14 @@
     }
 
     private ObjectNode json(UiClusterMember member, boolean isUiAttached) {
+        int switchCount = mastershipService.getDevicesOf(member.id()).size();
         return objectNode()
                 .put("id", member.id().toString())
                 .put("ip", member.ip().toString())
                 .put("online", member.isOnline())
                 .put("ready", member.isReady())
                 .put("uiAttached", isUiAttached)
-                .put("switches", member.deviceCount());
+                .put("switches", switchCount);
     }
 
     /**
@@ -261,6 +263,11 @@
         return result;
     }
 
+    // Returns the name of the master node for the specified device id.
+    private String master(DeviceId deviceId) {
+        NodeId master = mastershipService.getMasterFor(deviceId);
+        return master != null ? master.toString() : "";
+    }
 
     private ObjectNode json(UiNode node) {
         if (node instanceof UiRegion) {
@@ -281,7 +288,7 @@
                 .put("nodeType", DEVICE)
                 .put("type", device.type())
                 .put("online", deviceService.isAvailable(device.id()))
-                .put("master", nullIsEmpty(device.master()))
+                .put("master", master(device.id()))
                 .put("layer", device.layer());
 
         Device d = device.backingDevice();
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 cbb82b8..f98ab7b 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
@@ -119,9 +119,6 @@
     private void updateClusterMember(UiClusterMember member) {
         ControllerNode.State state = services.cluster().getState(member.id());
         member.setState(state);
-        member.setMastership(services.mastership().getDevicesOf(member.id()));
-        // NOTE: 'UI-attached' is session-based data, not global, so will
-        //       be set elsewhere
     }
 
     private void loadClusterMembers() {
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
index d7ec3b6..e8c2d3b 100644
--- 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
@@ -39,7 +39,6 @@
 import java.util.Iterator;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -157,24 +156,8 @@
         // see AbstractUiImplTest Mock Environment for expected values...
         assertEquals("wrong id str", C1, member.idAsString());
         assertEquals("wrong id", nodeId(C1), member.id());
-        assertEquals("wrong dev count", 3, member.deviceCount());
         assertEquals("not online", true, member.isOnline());
         assertEquals("not ready", true, member.isReady());
-
-        assertMasterOf(member, DEVID_1, DEVID_2, DEVID_3);
-        assertNotMasterOf(member, DEVID_4, DEVID_6, DEVID_9);
-    }
-
-    private void assertMasterOf(UiClusterMember member, DeviceId... ids) {
-        for (DeviceId id : ids) {
-            assertTrue("not master of " + id, member.masterOf(id));
-        }
-    }
-
-    private void assertNotMasterOf(UiClusterMember member, DeviceId... ids) {
-        for (DeviceId id : ids) {
-            assertFalse("? master of " + id, member.masterOf(id));
-        }
     }