Migrate nullness annotations
[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 java.util.concurrent.TimeUnit;
16 import org.eclipse.jdt.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.ActorUtils;
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 ActorUtils actorUtils;
31
32     AbstractDataStoreClientActor(final FrontendIdentifier frontendId, final ActorUtils actorUtils) {
33         super(frontendId);
34         this.actorUtils = requireNonNull(actorUtils);
35     }
36
37     @Override
38     protected ClientActorConfig getClientActorConfig() {
39         return actorUtils.getDatastoreContext();
40     }
41
42     @Override
43     protected final AbstractDataStoreClientBehavior initialBehavior(final ClientActorContext context) {
44         return verifyNotNull(initialBehavior(context, actorUtils));
45     }
46
47     @SuppressWarnings("checkstyle:hiddenField")
48     abstract AbstractDataStoreClientBehavior initialBehavior(ClientActorContext context, ActorUtils actorUtils);
49
50     @SuppressWarnings("checkstyle:IllegalCatch")
51     public static DataStoreClient getDistributedDataStoreClient(final @NonNull ActorRef actor,
52             final long timeout, final TimeUnit unit) {
53         try {
54             return (DataStoreClient) Await.result(ExplicitAsk.ask(actor, GET_CLIENT_FACTORY,
55                 Timeout.apply(timeout, unit)), Duration.Inf());
56         } catch (RuntimeException e) {
57             throw e;
58         } catch (Exception e) {
59             throw new RuntimeException(e);
60         }
61     }
62 }