Fixing Hosts filtered by RegionId... for topo-2 view.

Change-Id: I7779edc5e25957e9fcd0203b05942e9122461b13
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 a21a1f9..49cc378 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,7 +17,6 @@
 
 import org.onosproject.cluster.NodeId;
 import org.onosproject.net.DeviceId;
-import org.onosproject.net.HostId;
 import org.onosproject.store.Store;
 
 import java.util.Collection;
@@ -111,12 +110,4 @@
      */
     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/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 75d96ed..b988021 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,7 +25,9 @@
 import org.onosproject.cluster.NodeId;
 import org.onosproject.event.AbstractListenerManager;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.Host;
 import org.onosproject.net.HostId;
+import org.onosproject.net.host.HostService;
 import org.onosproject.net.region.Region;
 import org.onosproject.net.region.RegionAdminService;
 import org.onosproject.net.region.RegionEvent;
@@ -37,15 +39,17 @@
 import org.slf4j.Logger;
 
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.collect.ImmutableList.of;
-import static org.slf4j.LoggerFactory.getLogger;
 import static org.onosproject.security.AppGuard.checkPermission;
 import static org.onosproject.security.AppPermission.Type.REGION_READ;
+import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Provides implementation of the region service APIs.
@@ -69,6 +73,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected RegionStore store;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected HostService hostService;
+
     @Activate
     public void activate() {
         store.setDelegate(delegate);
@@ -154,7 +161,14 @@
     public Set<HostId> getRegionHosts(RegionId regionId) {
         checkPermission(REGION_READ);
         checkNotNull(regionId, REGION_ID_NULL);
-        return store.getRegionHosts(regionId);
+        Set<DeviceId> devs = getRegionDevices(regionId);
+        Set<HostId> hostIds = new HashSet<>();
+        for (DeviceId d : devs) {
+            Set<HostId> ids = hostService.getConnectedHosts(d).stream()
+                    .map(Host::id)
+                    .collect(Collectors.toSet());
+            hostIds.addAll(ids);
+        }
+        return hostIds;
     }
-
 }
diff --git a/core/net/src/test/java/org/onosproject/net/region/impl/RegionManagerTest.java b/core/net/src/test/java/org/onosproject/net/region/impl/RegionManagerTest.java
index fd15104..73fba4d 100644
--- a/core/net/src/test/java/org/onosproject/net/region/impl/RegionManagerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/region/impl/RegionManagerTest.java
@@ -38,9 +38,16 @@
 import java.util.List;
 import java.util.Set;
 
-import static org.junit.Assert.*;
-import static org.onosproject.net.region.Region.Type.*;
-import static org.onosproject.net.region.RegionEvent.Type.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.onosproject.net.region.Region.Type.CAMPUS;
+import static org.onosproject.net.region.Region.Type.COUNTRY;
+import static org.onosproject.net.region.Region.Type.METRO;
+import static org.onosproject.net.region.RegionEvent.Type.REGION_ADDED;
+import static org.onosproject.net.region.RegionEvent.Type.REGION_MEMBERSHIP_CHANGED;
+import static org.onosproject.net.region.RegionEvent.Type.REGION_REMOVED;
+import static org.onosproject.net.region.RegionEvent.Type.REGION_UPDATED;
 
 /**
  * Tests of the region service implementation.
@@ -68,6 +75,8 @@
         TestUtils.setField(store, "storageService", new TestStorageService());
         store.activate();
 
+        // possibly manager.hostService = new MockHostService();
+
         manager.store = store;
         manager.addListener(listener);
         NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
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 e101514..7e9a1a9b 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,7 +27,6 @@
 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;
@@ -79,7 +78,6 @@
 
     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<>();
 
@@ -92,7 +90,7 @@
     protected void activate() {
         Serializer serializer =
                 Serializer.using(Arrays.asList(KryoNamespaces.API),
-                                 Identifier.class);
+                        Identifier.class);
 
         regionsRepo = storageService.<RegionId, Region>consistentMapBuilder()
                 .withSerializer(serializer)
@@ -142,12 +140,6 @@
     }
 
     @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) -> {
@@ -173,8 +165,9 @@
 
     @Override
     public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
-        // Devices can only be a member in one region.  Remove the device if it belongs to
-        // a different region than the region for which we are attempting to add it.
+        // Devices can only be a member in one region.
+        // Remove the device if it belongs to a different region than
+        // the region for which we are attempting to add it.
         for (DeviceId deviceId : deviceIds) {
             Region region = getRegionForDevice(deviceId);
             if ((region != null) && (!regionId.id().equals(region.id().id()))) {
@@ -208,7 +201,7 @@
             } else {
                 return ImmutableSet.<DeviceId>builder()
                         .addAll(Sets.difference(existingDevices,
-                                                ImmutableSet.copyOf(deviceIds)))
+                                ImmutableSet.copyOf(deviceIds)))
                         .build();
             }
         });
@@ -219,7 +212,8 @@
     /**
      * Listener class to map listener events to the region inventory events.
      */
-    private class InternalRegionListener implements MapEventListener<RegionId, Region> {
+    private class InternalRegionListener
+            implements MapEventListener<RegionId, Region> {
         @Override
         public void event(MapEvent<RegionId, Region> event) {
             Region region = null;
@@ -247,13 +241,14 @@
     /**
      * Listener class to map listener events to the region membership events.
      */
-    private class InternalMembershipListener implements MapEventListener<RegionId, Set<DeviceId>> {
+    private class InternalMembershipListener
+            implements MapEventListener<RegionId, Set<DeviceId>> {
         @Override
         public void event(MapEvent<RegionId, Set<DeviceId>> event) {
             if (event.type() != MapEvent.Type.REMOVE) {
                 notifyDelegate(new RegionEvent(REGION_MEMBERSHIP_CHANGED,
-                                               regionsById.get(event.key()),
-                                               event.newValue().value()));
+                        regionsById.get(event.key()),
+                        event.newValue().value()));
             }
         }
     }