Distribute failover event with topic

Change-Id: I8629e7e19ebd4a18f95b32ad3ce1eba7ddf4ecc6
diff --git a/core/api/src/test/java/org/onosproject/store/service/TestStorageService.java b/core/api/src/test/java/org/onosproject/store/service/TestStorageService.java
index d8dff29..4305fbe 100644
--- a/core/api/src/test/java/org/onosproject/store/service/TestStorageService.java
+++ b/core/api/src/test/java/org/onosproject/store/service/TestStorageService.java
@@ -47,4 +47,9 @@
     public TransactionContextBuilder transactionContextBuilder() {
         throw new UnsupportedOperationException("transactionContextBuilder");
     }
+
+    @Override
+    public <T> Topic<T> getTopic(String name, Serializer serializer) {
+        return new TestTopic(name);
+    }
 }
diff --git a/core/api/src/test/java/org/onosproject/store/service/TestTopic.java b/core/api/src/test/java/org/onosproject/store/service/TestTopic.java
new file mode 100644
index 0000000..2155255
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/store/service/TestTopic.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.store.service;
+
+import com.google.common.collect.Sets;
+
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Consumer;
+
+/**
+ * Test implementation of topic.
+ */
+public class TestTopic<T> implements Topic<T> {
+    private final String name;
+    private final Set<Consumer<T>> callbacks = Sets.newConcurrentHashSet();
+
+    public TestTopic(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public CompletableFuture<Void> publish(T message) {
+        callbacks.forEach(c -> c.accept(message));
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public CompletableFuture<Void> subscribe(Consumer<T> callback) {
+        callbacks.add(callback);
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public CompletableFuture<Void> unsubscribe(Consumer<T> callback) {
+        callbacks.remove(callback);
+        return CompletableFuture.completedFuture(null);
+    }
+
+    @Override
+    public String name() {
+        return name;
+    }
+
+    @Override
+    public Type primitiveType() {
+        return Type.TOPIC;
+    }
+}
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 1f9387e..1b4faa8 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
@@ -62,6 +62,7 @@
 import org.onosproject.store.service.MultiValuedTimestamp;
 import org.onosproject.store.service.Serializer;
 import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Topic;
 import org.onosproject.store.service.Versioned;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
@@ -145,6 +146,8 @@
 
     private KryoNamespace clusterMsgSerializer;
 
+    private static Topic<GroupStoreMessage> groupTopic;
+
     @Property(name = "garbageCollect", boolValue = GARBAGE_COLLECT,
             label = "Enable group garbage collection")
     private boolean garbageCollect = GARBAGE_COLLECT;
@@ -210,6 +213,9 @@
         log.debug("Current size of pendinggroupkeymap:{}",
                   auditPendingReqQueue.size());
 
+        groupTopic = getOrCreateGroupTopic(serializer);
+        groupTopic.subscribe(this::processGroupMessage);
+
         log.info("Started");
     }
 
@@ -237,6 +243,14 @@
         }
     }
 
+    private Topic<GroupStoreMessage> getOrCreateGroupTopic(Serializer serializer) {
+        if (groupTopic == null) {
+            return storageService.getTopic("group-failover-notif", serializer);
+        } else {
+            return groupTopic;
+        }
+    };
+
     /**
      * Returns the group store eventual consistent key map.
      *
@@ -1109,6 +1123,16 @@
         }
     }
 
+    private void processGroupMessage(GroupStoreMessage message) {
+        if (message.type() == GroupStoreMessage.Type.FAILOVER) {
+            // FIXME: groupStoreEntriesByKey inaccessible here
+            getGroupIdTable(message.deviceId()).values()
+                    .stream()
+                    .filter((storedGroup) -> (storedGroup.appCookie().equals(message.appCookie())))
+                    .findFirst().ifPresent(group -> notifyDelegate(new GroupEvent(Type.GROUP_BUCKET_FAILOVER, group)));
+        }
+    }
+
     private void process(GroupStoreMessage groupOp) {
         log.debug("Received remote group operation {} request for device {}",
                   groupOp.type(),
@@ -1314,13 +1338,12 @@
 
     @Override
     public void notifyOfFailovers(Collection<Group> failoverGroups) {
-        List<GroupEvent> failoverEvents = new ArrayList<>();
         failoverGroups.forEach(group -> {
             if (group.type() == Group.Type.FAILOVER) {
-                failoverEvents.add(new GroupEvent(GroupEvent.Type.GROUP_BUCKET_FAILOVER, group));
+                groupTopic.publish(GroupStoreMessage.createGroupFailoverMsg(
+                        group.deviceId(), group));
             }
         });
-        notifyDelegate(failoverEvents);
     }
 
     private void garbageCollect(DeviceId deviceId,
diff --git a/core/store/dist/src/main/java/org/onosproject/store/group/impl/GroupStoreMessage.java b/core/store/dist/src/main/java/org/onosproject/store/group/impl/GroupStoreMessage.java
index f3071d5..6551f36 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/group/impl/GroupStoreMessage.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/group/impl/GroupStoreMessage.java
@@ -40,7 +40,8 @@
     public enum Type {
         ADD,
         UPDATE,
-        DELETE
+        DELETE,
+        FAILOVER
     }
 
     private GroupStoreMessage(Type type,
@@ -119,6 +120,18 @@
                                      null);
     }
 
+    public static GroupStoreMessage createGroupFailoverMsg(DeviceId deviceId,
+                                                           GroupDescription desc) {
+        return new GroupStoreMessage(Type.FAILOVER,
+                                     deviceId,
+                                     desc.appCookie(),
+                                     desc,
+                                     null,
+                                     null,
+                                     desc.appCookie());
+    }
+
+
     /**
      * Returns the device identifier of this group request.
      *