Victor Silva

[ONOS-5170] GroupStore: add purgeGroupEntries

The GroupStore exposes purgeGroupEntry, which purges
from the store by a specific device.

Add purgeGroupEntries, to purge entries from all devices
from the GroupStore, and expose purgeGroupEntries to allow
applications to purge all group entries from the GroupStore
without specifying a device.

Change-Id: I735f011a1fbbfa3ce8f1dd57a591a81c4377b012
......@@ -113,6 +113,11 @@ public interface GroupService
void purgeGroupEntries(DeviceId deviceId);
/**
* Purges all group entries.
*/
default void purgeGroupEntries() {};
/**
* Deletes a group associated to an application cookie.
* GROUP_DELETED or GROUP_DELETE_FAILED notifications would be
* provided along with cookie depending on the result of the
......
......@@ -125,6 +125,11 @@ public interface GroupStore extends Store<GroupEvent, GroupStoreDelegate> {
void purgeGroupEntry(DeviceId deviceId);
/**
* Removes all group entries from store.
*/
default void purgeGroupEntries() {};
/**
* A group entry that is present in switch but not in the store.
*
* @param group group entry
......
......@@ -482,6 +482,18 @@ public class SimpleGroupStore
}
@Override
public void purgeGroupEntries() {
groupEntriesById.values().forEach(groupEntries -> {
groupEntries.entrySet().forEach(entry -> {
notifyDelegate(new GroupEvent(Type.GROUP_REMOVED, entry.getValue()));
});
});
groupEntriesById.clear();
groupEntriesByKey.clear();
}
@Override
public void deviceInitialAuditCompleted(DeviceId deviceId,
boolean completed) {
synchronized (deviceAuditStatus) {
......
......@@ -214,10 +214,15 @@ public class SimpleGroupStoreTest {
// Testing removeGroupEntry operation from southbound
testRemoveGroupFromSB(currKey);
// Testing removing all groups on the given device
// Testing removing all groups on the given device by deviceid
newKey = new DefaultGroupKey("group1".getBytes());
testStoreAndGetGroup(newKey);
testDeleteGroupOnDevice(newKey);
// Testing removing all groups on the given device
newKey = new DefaultGroupKey("group1".getBytes());
testStoreAndGetGroup(newKey);
testPurgeGroupEntries();
}
// Testing storeGroup operation
......@@ -432,6 +437,13 @@ public class SimpleGroupStoreTest {
assertThat(simpleGroupStore.getGroupCount(D1), is(0));
}
// Testing purgeGroupEntries
private void testPurgeGroupEntries() {
assertThat(simpleGroupStore.getGroupCount(D1), is(1));
simpleGroupStore.purgeGroupEntries();
assertThat(simpleGroupStore.getGroupCount(D1), is(0));
}
// Testing removeGroupEntry operation from southbound
private void testRemoveGroupFromSB(GroupKey currKey) {
Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
......
......@@ -230,6 +230,11 @@ public class GroupManager
store.purgeGroupEntry(deviceId);
}
@Override
public void purgeGroupEntries() {
checkPermission(GROUP_WRITE);
store.purgeGroupEntries();
}
/**
* Delete a group associated to an application cookie.
......
......@@ -931,19 +931,27 @@ public class DistributedGroupStore
}
}
private void purgeGroupEntries(Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entries) {
entries.forEach(entry -> {
groupStoreEntriesByKey.remove(entry.getKey());
});
}
@Override
public void purgeGroupEntry(DeviceId deviceId) {
Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entryPendingRemove =
Set<Entry<GroupStoreKeyMapKey, StoredGroupEntry>> entriesPendingRemove =
new HashSet<>();
getGroupStoreKeyMap().entrySet().stream()
.filter(entry -> entry.getKey().deviceId().equals(deviceId))
.forEach(entryPendingRemove::add);
.forEach(entriesPendingRemove::add);
entryPendingRemove.forEach(entry -> {
groupStoreEntriesByKey.remove(entry.getKey());
notifyDelegate(new GroupEvent(Type.GROUP_REMOVED, entry.getValue()));
});
purgeGroupEntries(entriesPendingRemove);
}
@Override
public void purgeGroupEntries() {
purgeGroupEntries(getGroupStoreKeyMap().entrySet());
}
@Override
......
......@@ -236,6 +236,10 @@ public class DistributedGroupStoreTest {
groupStore.purgeGroupEntry(deviceId2);
assertThat(groupStore.getGroupCount(deviceId1), is(1));
assertThat(groupStore.getGroupCount(deviceId2), is(0));
groupStore.purgeGroupEntries();
assertThat(groupStore.getGroupCount(deviceId1), is(0));
assertThat(groupStore.getGroupCount(deviceId2), is(0));
}
/**
......