b91ff70b62fab26bb93ca5e1f89411be41c83148
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractDataStoreClientActor.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.util.Timeout;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Throwables;
14 import com.google.common.base.Verify;
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.ClientActorConfig;
19 import org.opendaylight.controller.cluster.access.client.ClientActorContext;
20 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
21 import org.opendaylight.controller.cluster.common.actor.ExplicitAsk;
22 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
23 import scala.Function1;
24 import scala.concurrent.Await;
25 import scala.concurrent.duration.Duration;
26
27 public abstract class AbstractDataStoreClientActor extends AbstractClientActor {
28     private static final Function1<ActorRef, ?> GET_CLIENT_FACTORY = ExplicitAsk.toScala(GetClientRequest::new);
29
30     private final ActorContext actorContext;
31
32     AbstractDataStoreClientActor(final FrontendIdentifier frontendId, final ActorContext actorContext) {
33         super(frontendId);
34         this.actorContext = Preconditions.checkNotNull(actorContext);
35     }
36
37     @Override
38     protected ClientActorConfig getClientActorConfig() {
39         return actorContext.getDatastoreContext();
40     }
41
42     @Override
43     protected final AbstractDataStoreClientBehavior initialBehavior(final ClientActorContext context) {
44         return Verify.verifyNotNull(initialBehavior(context, actorContext));
45     }
46
47     abstract AbstractDataStoreClientBehavior initialBehavior(ClientActorContext context, ActorContext actorContext);
48
49     @SuppressWarnings("checkstyle:IllegalCatch")
50     public static DataStoreClient getDistributedDataStoreClient(@Nonnull final ActorRef actor,
51             final long timeout, final TimeUnit unit) {
52         try {
53             return (DataStoreClient) Await.result(ExplicitAsk.ask(actor, GET_CLIENT_FACTORY,
54                 Timeout.apply(timeout, unit)), Duration.Inf());
55         } catch (Exception e) {
56             throw Throwables.propagate(e);
57         }
58     }
59 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.