Charles Chan
Committed by Gerrit Code Review

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

Change-Id: I84753bf95c47bbebb3156474c03c2860c51ecb4e
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.store.group.impl; 16 package org.onosproject.store.group.impl;
17 17
18 import com.google.common.collect.FluentIterable; 18 import com.google.common.collect.FluentIterable;
19 +import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Iterables; 20 import com.google.common.collect.Iterables;
20 import com.google.common.collect.Sets; 21 import com.google.common.collect.Sets;
21 22
...@@ -28,6 +29,7 @@ import org.apache.felix.scr.annotations.Service; ...@@ -28,6 +29,7 @@ import org.apache.felix.scr.annotations.Service;
28 import org.onlab.util.KryoNamespace; 29 import org.onlab.util.KryoNamespace;
29 import org.onlab.util.NewConcurrentHashMap; 30 import org.onlab.util.NewConcurrentHashMap;
30 import org.onosproject.cluster.ClusterService; 31 import org.onosproject.cluster.ClusterService;
32 +import org.onosproject.cluster.NodeId;
31 import org.onosproject.core.DefaultGroupId; 33 import org.onosproject.core.DefaultGroupId;
32 import org.onosproject.core.GroupId; 34 import org.onosproject.core.GroupId;
33 import org.onosproject.mastership.MastershipService; 35 import org.onosproject.mastership.MastershipService;
...@@ -63,6 +65,7 @@ import org.slf4j.Logger; ...@@ -63,6 +65,7 @@ import org.slf4j.Logger;
63 65
64 import java.util.ArrayList; 66 import java.util.ArrayList;
65 import java.util.Collection; 67 import java.util.Collection;
68 +import java.util.Collections;
66 import java.util.HashMap; 69 import java.util.HashMap;
67 import java.util.Iterator; 70 import java.util.Iterator;
68 import java.util.List; 71 import java.util.List;
...@@ -276,16 +279,22 @@ public class DistributedGroupStore ...@@ -276,16 +279,22 @@ public class DistributedGroupStore
276 */ 279 */
277 @Override 280 @Override
278 public Iterable<Group> getGroups(DeviceId deviceId) { 281 public Iterable<Group> getGroups(DeviceId deviceId) {
279 - // flatten and make iterator unmodifiable 282 + // Let ImmutableSet.copyOf do the type conversion
280 - return FluentIterable.from(getGroupStoreKeyMap().values()) 283 + return ImmutableSet.copyOf(getStoredGroups(deviceId));
281 - .filter(input -> input.deviceId().equals(deviceId))
282 - .transform(input -> input);
283 } 284 }
284 285
285 private Iterable<StoredGroupEntry> getStoredGroups(DeviceId deviceId) { 286 private Iterable<StoredGroupEntry> getStoredGroups(DeviceId deviceId) {
286 - // flatten and make iterator unmodifiable 287 + NodeId master = mastershipService.getMasterFor(deviceId);
287 - return FluentIterable.from(getGroupStoreKeyMap().values()) 288 + if (master == null) {
288 - .filter(input -> input.deviceId().equals(deviceId)); 289 + log.debug("Failed to getGroups: No master for {}", deviceId);
290 + return Collections.emptySet();
291 + }
292 +
293 + Set<StoredGroupEntry> storedGroups = getGroupStoreKeyMap().values()
294 + .stream()
295 + .filter(input -> input.deviceId().equals(deviceId))
296 + .collect(Collectors.toSet());
297 + return ImmutableSet.copyOf(storedGroups);
289 } 298 }
290 299
291 /** 300 /**
......
...@@ -23,6 +23,7 @@ import org.junit.After; ...@@ -23,6 +23,7 @@ import org.junit.After;
23 import org.junit.Before; 23 import org.junit.Before;
24 import org.junit.Test; 24 import org.junit.Test;
25 import org.onlab.junit.TestUtils; 25 import org.onlab.junit.TestUtils;
26 +import org.onosproject.cluster.NodeId;
26 import org.onosproject.core.DefaultGroupId; 27 import org.onosproject.core.DefaultGroupId;
27 import org.onosproject.core.GroupId; 28 import org.onosproject.core.GroupId;
28 import org.onosproject.mastership.MastershipServiceAdapter; 29 import org.onosproject.mastership.MastershipServiceAdapter;
...@@ -106,6 +107,11 @@ public class DistributedGroupStoreTest { ...@@ -106,6 +107,11 @@ public class DistributedGroupStoreTest {
106 public MastershipRole getLocalRole(DeviceId deviceId) { 107 public MastershipRole getLocalRole(DeviceId deviceId) {
107 return MastershipRole.MASTER; 108 return MastershipRole.MASTER;
108 } 109 }
110 +
111 + @Override
112 + public NodeId getMasterFor(DeviceId deviceId) {
113 + return new NodeId("foo");
114 + }
109 } 115 }
110 116
111 @Before 117 @Before
......