1e15fefd6f05c8189a84decaaeb3c015efab2029
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / DistributedDataStoreClientActor.java
1 /*
2  * Copyright (c) 2016 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.cluster.databroker.actors.dds;
9
10 import akka.actor.ActorRef;
11 import akka.actor.Props;
12 import akka.pattern.ExplicitAskSupport;
13 import akka.util.Timeout;
14 import com.google.common.base.Throwables;
15 import java.util.concurrent.TimeUnit;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
19 import org.opendaylight.controller.cluster.access.concepts.MemberName;
20 import org.opendaylight.controller.cluster.datastore.actors.client.AbstractClientActor;
21 import org.opendaylight.controller.cluster.datastore.actors.client.ClientActorBehavior;
22 import org.opendaylight.controller.cluster.datastore.actors.client.ClientActorContext;
23 import scala.Function1;
24 import scala.concurrent.Await;
25 import scala.concurrent.duration.Duration;
26 import scala.runtime.AbstractFunction1;
27
28 /**
29  * A {@link AbstractClientActor} which acts as the point of contact for DistributedDataStore.
30  *
31  * @author Robert Varga
32  */
33 public final class DistributedDataStoreClientActor extends AbstractClientActor {
34     // Unfortunately Akka's explicit ask pattern does not work with its Java API, as it fails to invoke passed message.
35     // In order to make this work for now, we tap directly into ExplicitAskSupport and use a Scala function instead
36     // of akka.japi.Function.
37     private static final ExplicitAskSupport ASK_SUPPORT = akka.pattern.extended.package$.MODULE$;
38     private static final Function1<ActorRef, Object> GET_CLIENT_FACTORY = new AbstractFunction1<ActorRef, Object>() {
39         @Override
40         public Object apply(final ActorRef askSender) {
41             return new GetClientRequest(askSender);
42         }
43     };
44
45     private DistributedDataStoreClientActor(final FrontendIdentifier frontendId) {
46         super(frontendId);
47     }
48
49     @Override
50     protected ClientActorBehavior initialBehavior(final ClientActorContext context) {
51         return new DistributedDataStoreClientBehavior(context);
52     }
53
54     public static Props props(final @Nonnull MemberName memberName, @Nonnull final String storeName) {
55         final String name = "DistributedDataStore:storeName='" + storeName + "'";
56         final FrontendIdentifier frontendId = FrontendIdentifier.create(memberName, FrontendType.forName(name));
57         return Props.create(DistributedDataStoreClientActor.class, () -> new DistributedDataStoreClientActor(frontendId));
58     }
59
60     public static DistributedDataStoreClient getDistributedDataStoreClient(final @Nonnull ActorRef actor,
61             final long timeout, final TimeUnit unit) {
62         try {
63             return (DistributedDataStoreClient) Await.result(ASK_SUPPORT.ask(actor, GET_CLIENT_FACTORY,
64                 Timeout.apply(timeout, unit)), Duration.Inf());
65         } catch (Exception e) {
66             throw Throwables.propagate(e);
67         }
68     }
69 }