45875baa150682e01a7bc3bc6e5a6d6404c46ada
[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.util.Timeout;
13 import com.google.common.base.Preconditions;
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.client.AbstractClientActor;
18 import org.opendaylight.controller.cluster.access.client.ClientActorContext;
19 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
21 import org.opendaylight.controller.cluster.access.concepts.MemberName;
22 import org.opendaylight.controller.cluster.common.actor.ExplicitAsk;
23 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
24 import scala.Function1;
25 import scala.concurrent.Await;
26 import scala.concurrent.duration.Duration;
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     private static final Function1<ActorRef, ?> GET_CLIENT_FACTORY = ExplicitAsk.toScala(t -> new GetClientRequest(t));
35
36     private final ActorContext actorContext;
37
38     private DistributedDataStoreClientActor(final FrontendIdentifier frontendId, final ActorContext actorContext) {
39         super(frontendId);
40         this.actorContext = Preconditions.checkNotNull(actorContext);
41     }
42
43     @Override
44     protected DistributedDataStoreClientBehavior initialBehavior(final ClientActorContext context) {
45         return new DistributedDataStoreClientBehavior(context, actorContext);
46     }
47
48     public static Props props(@Nonnull final MemberName memberName, @Nonnull final String storeName,
49             final ActorContext ctx) {
50         final String name = "datastore-" + storeName;
51         final FrontendIdentifier frontendId = FrontendIdentifier.create(memberName, FrontendType.forName(name));
52         return Props.create(DistributedDataStoreClientActor.class,
53             () -> new DistributedDataStoreClientActor(frontendId, ctx));
54     }
55
56     @SuppressWarnings("checkstyle:IllegalCatch")
57     public static DistributedDataStoreClient getDistributedDataStoreClient(@Nonnull final ActorRef actor,
58             final long timeout, final TimeUnit unit) {
59         try {
60             return (DistributedDataStoreClient) Await.result(ExplicitAsk.ask(actor, GET_CLIENT_FACTORY,
61                 Timeout.apply(timeout, unit)), Duration.Inf());
62         } catch (Exception e) {
63             throw Throwables.propagate(e);
64         }
65     }
66 }