ONOS-3552 Do not return group information when a device is unavailable

Change-Id: I84753bf95c47bbebb3156474c03c2860c51ecb4e
diff --git a/core/store/dist/src/main/java/org/onosproject/store/group/impl/DistributedGroupStore.java b/core/store/dist/src/main/java/org/onosproject/store/group/impl/DistributedGroupStore.java
index cc32a73..4f4c06f 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/group/impl/DistributedGroupStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/group/impl/DistributedGroupStore.java
@@ -16,6 +16,7 @@
 package org.onosproject.store.group.impl;
 
 import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
 
@@ -28,6 +29,7 @@
 import org.onlab.util.KryoNamespace;
 import org.onlab.util.NewConcurrentHashMap;
 import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.NodeId;
 import org.onosproject.core.DefaultGroupId;
 import org.onosproject.core.GroupId;
 import org.onosproject.mastership.MastershipService;
@@ -63,6 +65,7 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -276,16 +279,22 @@
      */
     @Override
     public Iterable<Group> getGroups(DeviceId deviceId) {
-        // flatten and make iterator unmodifiable
-        return FluentIterable.from(getGroupStoreKeyMap().values())
-                .filter(input -> input.deviceId().equals(deviceId))
-                .transform(input -> input);
+        // Let ImmutableSet.copyOf do the type conversion
+        return ImmutableSet.copyOf(getStoredGroups(deviceId));
     }
 
     private Iterable<StoredGroupEntry> getStoredGroups(DeviceId deviceId) {
-        // flatten and make iterator unmodifiable
-        return FluentIterable.from(getGroupStoreKeyMap().values())
-                .filter(input -> input.deviceId().equals(deviceId));
+        NodeId master = mastershipService.getMasterFor(deviceId);
+        if (master == null) {
+            log.debug("Failed to getGroups: No master for {}", deviceId);
+            return Collections.emptySet();
+        }
+
+        Set<StoredGroupEntry> storedGroups = getGroupStoreKeyMap().values()
+                .stream()
+                .filter(input -> input.deviceId().equals(deviceId))
+                .collect(Collectors.toSet());
+        return ImmutableSet.copyOf(storedGroups);
     }
 
     /**
diff --git a/core/store/dist/src/test/java/org/onosproject/store/group/impl/DistributedGroupStoreTest.java b/core/store/dist/src/test/java/org/onosproject/store/group/impl/DistributedGroupStoreTest.java
index 560fdb3..dbb63b2 100644
--- a/core/store/dist/src/test/java/org/onosproject/store/group/impl/DistributedGroupStoreTest.java
+++ b/core/store/dist/src/test/java/org/onosproject/store/group/impl/DistributedGroupStoreTest.java
@@ -23,6 +23,7 @@
 import org.junit.Before;
 import org.junit.Test;
 import org.onlab.junit.TestUtils;
+import org.onosproject.cluster.NodeId;
 import org.onosproject.core.DefaultGroupId;
 import org.onosproject.core.GroupId;
 import org.onosproject.mastership.MastershipServiceAdapter;
@@ -106,6 +107,11 @@
         public MastershipRole getLocalRole(DeviceId deviceId) {
             return MastershipRole.MASTER;
         }
+
+        @Override
+        public NodeId getMasterFor(DeviceId deviceId) {
+            return new NodeId("foo");
+        }
     }
 
     @Before