Change RTE to an ISE
[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 static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import akka.util.Timeout;
15 import com.google.common.base.Throwables;
16 import java.util.concurrent.TimeUnit;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.access.client.AbstractClientActor;
19 import org.opendaylight.controller.cluster.access.client.ClientActorConfig;
20 import org.opendaylight.controller.cluster.access.client.ClientActorContext;
21 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
22 import org.opendaylight.controller.cluster.common.actor.ExplicitAsk;
23 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
24 import scala.Function1;
25 import scala.concurrent.Await;
26 import scala.concurrent.duration.Duration;
27
28 public abstract class AbstractDataStoreClientActor extends AbstractClientActor {
29     private static final Function1<ActorRef, ?> GET_CLIENT_FACTORY = ExplicitAsk.toScala(GetClientRequest::new);
30
31     private final ActorUtils actorUtils;
32
33     AbstractDataStoreClientActor(final FrontendIdentifier frontendId, final ActorUtils actorUtils) {
34         super(frontendId);
35         this.actorUtils = requireNonNull(actorUtils);
36     }
37
38     @Override
39     protected ClientActorConfig getClientActorConfig() {
40         return actorUtils.getDatastoreContext();
41     }
42
43     @Override
44     protected final AbstractDataStoreClientBehavior initialBehavior(final ClientActorContext context) {
45         return verifyNotNull(initialBehavior(context, actorUtils));
46     }
47
48     @SuppressWarnings("checkstyle:hiddenField")
49     abstract AbstractDataStoreClientBehavior initialBehavior(ClientActorContext context, ActorUtils actorUtils);
50
51     @SuppressWarnings("checkstyle:IllegalCatch")
52     public static DataStoreClient getDistributedDataStoreClient(final @NonNull ActorRef actor,
53             final long timeout, final TimeUnit unit) {
54         try {
55             return (DataStoreClient) Await.result(ExplicitAsk.ask(actor, GET_CLIENT_FACTORY,
56                 Timeout.apply(timeout, unit)), Duration.Inf());
57         } catch (Exception e) {
58             Throwables.throwIfUnchecked(e);
59             throw new IllegalStateException(e);
60         }
61     }
62 }