2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.remote.rpc.registry.gossip;
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.getBucketsByMembersMessage;
12 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.getLocalDataMessage;
13 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.getRemoteBucketsMessage;
14 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.removeBucketMessage;
15 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.updateRemoteBucketsMessage;
17 import akka.actor.ActorRef;
18 import akka.actor.Address;
19 import akka.dispatch.OnComplete;
20 import akka.pattern.Patterns;
21 import akka.util.Timeout;
22 import com.google.common.annotations.VisibleForTesting;
23 import java.util.Collection;
25 import java.util.function.Consumer;
26 import scala.concurrent.ExecutionContext;
27 import scala.concurrent.Future;
30 * Convenience access to {@link BucketStoreActor}. Used mostly by {@link Gossiper}.
33 public final class BucketStoreAccess {
34 private final ActorRef actorRef;
35 private final ExecutionContext dispatcher;
36 private final Timeout timeout;
38 public BucketStoreAccess(final ActorRef actorRef, final ExecutionContext dispatcher, final Timeout timeout) {
39 this.actorRef = requireNonNull(actorRef);
40 this.dispatcher = requireNonNull(dispatcher);
41 this.timeout = requireNonNull(timeout);
44 <T extends BucketData<T>> void getBucketsByMembers(final Collection<Address> members,
45 final Consumer<Map<Address, Bucket<T>>> callback) {
46 Patterns.ask(actorRef, getBucketsByMembersMessage(members), timeout)
47 .onComplete(new OnComplete<>() {
48 @SuppressWarnings("unchecked")
50 public void onComplete(final Throwable failure, final Object success) {
51 if (failure == null) {
52 callback.accept((Map<Address, Bucket<T>>) success);
58 void getBucketVersions(final Consumer<Map<Address, Long>> callback) {
59 Patterns.ask(actorRef, Singletons.GET_BUCKET_VERSIONS, timeout).onComplete(new OnComplete<>() {
60 @SuppressWarnings("unchecked")
62 public void onComplete(final Throwable failure, final Object success) {
63 if (failure == null) {
64 callback.accept((Map<Address, Long>) success);
70 @SuppressWarnings({ "unchecked", "rawtypes" })
71 public Future<Map<Address, Long>> getBucketVersions() {
72 return (Future) Patterns.ask(actorRef, Singletons.GET_BUCKET_VERSIONS, timeout);
75 @SuppressWarnings("unchecked")
76 void updateRemoteBuckets(final Map<Address, ? extends Bucket<?>> buckets) {
77 actorRef.tell(updateRemoteBucketsMessage((Map<Address, Bucket<?>>) buckets), ActorRef.noSender());
80 void removeRemoteBucket(final Address addr) {
81 actorRef.tell(removeBucketMessage(addr), ActorRef.noSender());
84 @SuppressWarnings({ "unchecked", "rawtypes" })
85 public <T extends BucketData<T>> Future<T> getLocalData() {
86 return (Future) Patterns.ask(actorRef, getLocalDataMessage(), timeout);
89 @SuppressWarnings({ "unchecked", "rawtypes" })
90 public <T extends BucketData<T>> Future<Map<Address, Bucket<T>>> getRemoteBuckets() {
91 return (Future) Patterns.ask(actorRef, getRemoteBucketsMessage(), timeout);
94 public enum Singletons {
96 * Sent from Gossiper to BucketStore, response is an immutable {@code Map<Address, Bucket<?>>}.
100 * Sent from Gossiper to BucketStore, response is an immutable {@code Map<Address, Long>}.