Added PartitionedAsyncLeaderElector that federates leader election contents across a collection of AsyncLeaderElectors

Change-Id: I6ae220d4e4d2ed8ae1cd9060482f66f418ae0551
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/FederatedDistributedPrimitiveCreator.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/FederatedDistributedPrimitiveCreator.java
index 99d0897..7be143f 100644
--- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/FederatedDistributedPrimitiveCreator.java
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/FederatedDistributedPrimitiveCreator.java
@@ -34,6 +34,7 @@
 import org.onosproject.store.service.DistributedQueue;
 import org.onosproject.store.service.Serializer;
 
+import com.google.common.base.Charsets;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -93,7 +94,15 @@
 
     @Override
     public AsyncLeaderElector newAsyncLeaderElector(String name) {
-        return getCreator(name).newAsyncLeaderElector(name);
+        checkNotNull(name);
+        Map<PartitionId, AsyncLeaderElector> leaderElectors =
+                Maps.transformValues(members,
+                                     partition -> partition.newAsyncLeaderElector(name));
+        Hasher<String> hasher = topic -> {
+            long hashCode = HashCode.fromBytes(topic.getBytes(Charsets.UTF_8)).asLong();
+            return sortedMemberPartitionIds.get(Hashing.consistentHash(hashCode, members.size()));
+        };
+        return new PartitionedAsyncLeaderElector(name, leaderElectors, hasher);
     }
 
     @Override
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/PartitionedAsyncLeaderElector.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/PartitionedAsyncLeaderElector.java
new file mode 100644
index 0000000..e364a7e
--- /dev/null
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/PartitionedAsyncLeaderElector.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2016 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.primitives.impl;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Consumer;
+
+import org.onosproject.cluster.Leadership;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.cluster.PartitionId;
+import org.onosproject.event.Change;
+import org.onosproject.store.service.AsyncLeaderElector;
+
+import com.google.common.collect.Maps;
+
+/**
+ * {@link AsyncLeaderElector} that has its topics partitioned horizontally across
+ * several {@link AsyncLeaderElector leader electors}.
+ */
+public class PartitionedAsyncLeaderElector implements AsyncLeaderElector {
+
+    private final String name;
+    private final TreeMap<PartitionId, AsyncLeaderElector> partitions = Maps.newTreeMap();
+    private final Hasher<String> topicHasher;
+
+    public PartitionedAsyncLeaderElector(String name,
+            Map<PartitionId, AsyncLeaderElector> partitions,
+            Hasher<String> topicHasher) {
+        this.name = name;
+        this.partitions.putAll(checkNotNull(partitions));
+        this.topicHasher = checkNotNull(topicHasher);
+    }
+
+    @Override
+    public String name() {
+        return name;
+    }
+
+    @Override
+    public CompletableFuture<Leadership> run(String topic, NodeId nodeId) {
+        return getLeaderElector(topic).run(topic, nodeId);
+    }
+
+    @Override
+    public CompletableFuture<Void> withdraw(String topic) {
+        return getLeaderElector(topic).withdraw(topic);
+    }
+
+    @Override
+    public CompletableFuture<Boolean> anoint(String topic, NodeId nodeId) {
+        return getLeaderElector(topic).anoint(topic, nodeId);
+    }
+
+    @Override
+    public CompletableFuture<Leadership> getLeadership(String topic) {
+        return getLeaderElector(topic).getLeadership(topic);
+    }
+
+    @Override
+    public CompletableFuture<Map<String, Leadership>> getLeaderships() {
+        Map<String, Leadership> leaderships = Maps.newConcurrentMap();
+        return CompletableFuture.allOf(getLeaderElectors().stream()
+                                                          .map(le -> le.getLeaderships()
+                                                                       .thenAccept(m -> leaderships.putAll(m)))
+                                                          .toArray(CompletableFuture[]::new))
+                                .thenApply(v -> leaderships);
+    }
+
+    @Override
+    public CompletableFuture<Void> addChangeListener(Consumer<Change<Leadership>> listener) {
+        return CompletableFuture.allOf(getLeaderElectors().stream()
+                                                          .map(map -> map.addChangeListener(listener))
+                                                          .toArray(CompletableFuture[]::new));
+    }
+
+    @Override
+    public CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> listener) {
+        return CompletableFuture.allOf(getLeaderElectors().stream()
+                                                          .map(map -> map.removeChangeListener(listener))
+                                                          .toArray(CompletableFuture[]::new));
+    }
+
+    /**
+     * Returns the leaderElector (partition) to which the specified topic maps.
+     * @param topic topic name
+     * @return AsyncLeaderElector to which topic maps
+     */
+    private AsyncLeaderElector getLeaderElector(String topic) {
+        return partitions.get(topicHasher.hash(topic));
+    }
+
+    /**
+     * Returns all the constituent leader electors.
+     * @return collection of leader electors.
+     */
+    private Collection<AsyncLeaderElector> getLeaderElectors() {
+        return partitions.values();
+    }
+}
\ No newline at end of file