Fix followerDistributedDataStore tear down
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / gossip / BucketStoreAccess.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.remote.rpc.registry.gossip;
9
10 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.getBucketsByMembersMessage;
11 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.getLocalDataMessage;
12 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.getRemoteBucketsMessage;
13 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.removeBucketMessage;
14 import static org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreActor.updateRemoteBucketsMessage;
15
16 import akka.actor.ActorRef;
17 import akka.actor.Address;
18 import akka.dispatch.OnComplete;
19 import akka.pattern.Patterns;
20 import akka.util.Timeout;
21 import com.google.common.annotations.Beta;
22 import com.google.common.annotations.VisibleForTesting;
23 import java.util.Collection;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.function.Consumer;
27 import scala.concurrent.ExecutionContext;
28 import scala.concurrent.Future;
29
30 /**
31  * Convenience access to {@link BucketStoreActor}. Used mostly by {@link Gossiper}.
32  *
33  * @author Robert Varga
34  */
35 @Beta
36 @VisibleForTesting
37 public final class BucketStoreAccess {
38     private final ActorRef actorRef;
39     private final ExecutionContext dispatcher;
40     private final Timeout timeout;
41
42     public BucketStoreAccess(final ActorRef actorRef, final ExecutionContext dispatcher, final Timeout timeout) {
43         this.actorRef = Objects.requireNonNull(actorRef);
44         this.dispatcher = Objects.requireNonNull(dispatcher);
45         this.timeout = Objects.requireNonNull(timeout);
46     }
47
48     <T extends BucketData<T>> void getBucketsByMembers(final Collection<Address> members,
49             final Consumer<Map<Address, Bucket<T>>> callback) {
50         Patterns.ask(actorRef, getBucketsByMembersMessage(members), timeout)
51             .onComplete(new OnComplete<Object>() {
52                 @SuppressWarnings("unchecked")
53                 @Override
54                 public void onComplete(final Throwable failure, final Object success) {
55                     if (failure == null) {
56                         callback.accept((Map<Address, Bucket<T>>) success);
57                     }
58                 }
59             }, dispatcher);
60     }
61
62     void getBucketVersions(final Consumer<Map<Address, Long>> callback) {
63         Patterns.ask(actorRef, Singletons.GET_BUCKET_VERSIONS, timeout).onComplete(new OnComplete<Object>() {
64             @SuppressWarnings("unchecked")
65             @Override
66             public void onComplete(final Throwable failure, final Object success) {
67                 if (failure == null) {
68                     callback.accept((Map<Address, Long>) success);
69                 }
70             }
71         }, dispatcher);
72     }
73
74     @SuppressWarnings({ "unchecked", "rawtypes" })
75     public Future<Map<Address, Long>> getBucketVersions() {
76         return (Future) Patterns.ask(actorRef, Singletons.GET_BUCKET_VERSIONS, timeout);
77     }
78
79     @SuppressWarnings("unchecked")
80     void updateRemoteBuckets(final Map<Address, ? extends Bucket<?>> buckets) {
81         actorRef.tell(updateRemoteBucketsMessage((Map<Address, Bucket<?>>) buckets), ActorRef.noSender());
82     }
83
84     void removeRemoteBucket(final Address addr) {
85         actorRef.tell(removeBucketMessage(addr), ActorRef.noSender());
86     }
87
88     @SuppressWarnings({ "unchecked", "rawtypes" })
89     public <T extends BucketData<T>> Future<T> getLocalData() {
90         return (Future) Patterns.ask(actorRef, getLocalDataMessage(), timeout);
91     }
92
93     @SuppressWarnings({ "unchecked", "rawtypes" })
94     public <T extends BucketData<T>> Future<Map<Address, Bucket<T>>> getRemoteBuckets() {
95         return (Future) Patterns.ask(actorRef, getRemoteBucketsMessage(), timeout);
96     }
97
98     public enum Singletons {
99         // Sent from Gossiper to BucketStore, response is an immutable Map<Address, Bucket<?>>
100         GET_ALL_BUCKETS,
101         // Sent from Gossiper to BucketStore, response is an immutable Map<Address, Long>
102         GET_BUCKET_VERSIONS,
103     }
104 }