Implementing region hosts for topology 2

Change-Id: I6d1e45b1152b2387d4ff981dc0666868235eb1c3
diff --git a/core/api/src/main/java/org/onosproject/net/region/RegionService.java b/core/api/src/main/java/org/onosproject/net/region/RegionService.java
index 259c8b0..b2b9b14 100644
--- a/core/api/src/main/java/org/onosproject/net/region/RegionService.java
+++ b/core/api/src/main/java/org/onosproject/net/region/RegionService.java
@@ -18,6 +18,7 @@
 
 import org.onosproject.event.ListenerService;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
 
 import java.util.Set;
 
@@ -59,4 +60,12 @@
      */
     Set<DeviceId> getRegionDevices(RegionId regionId);
 
+    /**
+     * Returns the set of hosts that belong to the specified region.
+     *
+     * @param regionId region identifier
+     * @return set of identifiers for hosts in the given region
+     */
+    Set<HostId> getRegionHosts(RegionId regionId);
+
 }
diff --git a/core/api/src/main/java/org/onosproject/net/region/RegionStore.java b/core/api/src/main/java/org/onosproject/net/region/RegionStore.java
index 49cc378..a21a1f9 100644
--- a/core/api/src/main/java/org/onosproject/net/region/RegionStore.java
+++ b/core/api/src/main/java/org/onosproject/net/region/RegionStore.java
@@ -17,6 +17,7 @@
 
 import org.onosproject.cluster.NodeId;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
 import org.onosproject.store.Store;
 
 import java.util.Collection;
@@ -110,4 +111,12 @@
      */
     void removeDevices(RegionId regionId, Collection<DeviceId> deviceIds);
 
+    /**
+     * Returns the set of hosts that belong to the specified region.
+     *
+     * @param regionId region identifier
+     * @return set of identifiers for hosts in the given region
+     */
+    Set<HostId> getRegionHosts(RegionId regionId);
+
 }
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiHost.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiHost.java
index 8c5107e..ff22116 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiHost.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiHost.java
@@ -20,6 +20,7 @@
 import org.onosproject.net.Host;
 import org.onosproject.net.HostId;
 import org.onosproject.net.PortNumber;
+import org.onosproject.net.region.RegionId;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
 
@@ -36,6 +37,7 @@
     private PortNumber locPort;
 
     private UiLinkId edgeLinkId;
+    private RegionId regionId;
 
     /**
      * Creates a new UI host.
@@ -70,6 +72,15 @@
         return host.id();
     }
 
+    /**
+     * Sets the ID of the region to which this device belongs.
+     *
+     * @param regionId region identifier
+     */
+    public void setRegionId(RegionId regionId) {
+        this.regionId = regionId;
+    }
+
     @Override
     public String idAsString() {
         return id().toString();
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiRegion.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiRegion.java
index 40dec55..7cd0da9 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiRegion.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiRegion.java
@@ -241,6 +241,17 @@
         return topology.deviceSet(deviceIds);
     }
 
+
+    /**
+     * Make sure we have only these hosts in the region.
+     *
+     * @param hosts hosts in the region
+     */
+    public void reconcileHosts(Set<HostId> hosts) {
+        hostIds.clear();
+        hostIds.addAll(hosts);
+    }
+
     /**
      * Returns the set of host identifiers for this region.
      *
@@ -260,6 +271,15 @@
     }
 
     /**
+     * Returns the count of devices in this region.
+     *
+     * @return the device count
+     */
+    public int hostCount() {
+        return hostIds.size();
+    }
+
+    /**
      * Returns the order in which layers should be rendered. Lower layers
      * come earlier in the list. For example, to indicate that nodes in the
      * optical layer should be rendered "below" nodes in the packet layer,
diff --git a/core/net/src/main/java/org/onosproject/net/region/impl/RegionManager.java b/core/net/src/main/java/org/onosproject/net/region/impl/RegionManager.java
index 81a9dfa..75d96ed 100644
--- a/core/net/src/main/java/org/onosproject/net/region/impl/RegionManager.java
+++ b/core/net/src/main/java/org/onosproject/net/region/impl/RegionManager.java
@@ -25,6 +25,7 @@
 import org.onosproject.cluster.NodeId;
 import org.onosproject.event.AbstractListenerManager;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
 import org.onosproject.net.region.Region;
 import org.onosproject.net.region.RegionAdminService;
 import org.onosproject.net.region.RegionEvent;
@@ -149,4 +150,11 @@
         return store.getRegionDevices(regionId);
     }
 
+    @Override
+    public Set<HostId> getRegionHosts(RegionId regionId) {
+        checkPermission(REGION_READ);
+        checkNotNull(regionId, REGION_ID_NULL);
+        return store.getRegionHosts(regionId);
+    }
+
 }
diff --git a/core/store/dist/src/main/java/org/onosproject/store/region/impl/DistributedRegionStore.java b/core/store/dist/src/main/java/org/onosproject/store/region/impl/DistributedRegionStore.java
index 6d70376..e101514 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/region/impl/DistributedRegionStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/region/impl/DistributedRegionStore.java
@@ -27,6 +27,7 @@
 import org.onlab.util.Identifier;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
 import org.onosproject.net.region.DefaultRegion;
 import org.onosproject.net.region.Region;
 import org.onosproject.net.region.RegionEvent;
@@ -78,6 +79,7 @@
 
     private ConsistentMap<RegionId, Set<DeviceId>> membershipRepo;
     private Map<RegionId, Set<DeviceId>> regionDevices;
+    private Map<RegionId, Set<HostId>> regionHosts;
 
     private Map<DeviceId, Region> regionsByDevice = new HashMap<>();
 
@@ -140,6 +142,12 @@
     }
 
     @Override
+    public Set<HostId> getRegionHosts(RegionId regionId) {
+        Set<HostId> hostIds = regionHosts.get(regionId);
+        return hostIds != null ? ImmutableSet.copyOf(hostIds) : ImmutableSet.of();
+    }
+
+    @Override
     public Region createRegion(RegionId regionId, String name, Region.Type type,
                                List<Set<NodeId>> masterNodeIds) {
         return regionsRepo.compute(regionId, (id, region) -> {