Migrate nullness annotations
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / LookupTask.java
1 /*
2  * Copyright (c) 2017 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.sharding;
9
10 import static akka.actor.ActorRef.noSender;
11
12 import akka.actor.ActorRef;
13 import akka.actor.Status;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.eclipse.jdt.annotation.Nullable;
16
17 /**
18  * Base class for lookup tasks. Lookup tasks are supposed to run repeatedly
19  * until successful lookup or maximum retries are hit.
20  */
21 @NotThreadSafe
22 abstract class LookupTask implements Runnable {
23     private final int maxRetries;
24     private final ActorRef replyTo;
25     private int retried = 0;
26
27     LookupTask(final ActorRef replyTo, final int maxRetries) {
28         this.replyTo = replyTo;
29         this.maxRetries = maxRetries;
30     }
31
32     abstract void reschedule(int retries);
33
34     void tryReschedule(final @Nullable Throwable throwable) {
35         if (retried <= maxRetries) {
36             retried++;
37             reschedule(retried);
38         } else {
39             fail(throwable);
40         }
41     }
42
43     void fail(final @Nullable Throwable throwable) {
44         if (throwable == null) {
45             replyTo.tell(new Status.Failure(
46                     new DOMDataTreeShardCreationFailedException("Unable to find the backend shard."
47                             + "Failing..")), noSender());
48         } else {
49             replyTo.tell(new Status.Failure(
50                     new DOMDataTreeShardCreationFailedException("Unable to find the backend shard."
51                             + "Failing..", throwable)), noSender());
52         }
53     }
54 }