Deprecate DOMDataTreeProducer-related classes
[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 org.eclipse.jdt.annotation.Nullable;
15
16 /**
17  * Base class for lookup tasks. Lookup tasks are supposed to run repeatedly until successful lookup or maximum retries
18  * are hit. This class is NOT thread-safe.
19  */
20 @Deprecated(forRemoval = true)
21 abstract class LookupTask implements Runnable {
22     private final int maxRetries;
23     private final ActorRef replyTo;
24     private int retried = 0;
25
26     LookupTask(final ActorRef replyTo, final int maxRetries) {
27         this.replyTo = replyTo;
28         this.maxRetries = maxRetries;
29     }
30
31     abstract void reschedule(int retries);
32
33     void tryReschedule(final @Nullable Throwable throwable) {
34         if (retried <= maxRetries) {
35             retried++;
36             reschedule(retried);
37         } else {
38             fail(throwable);
39         }
40     }
41
42     void fail(final @Nullable Throwable throwable) {
43         if (throwable == null) {
44             replyTo.tell(new Status.Failure(
45                     new DOMDataTreeShardCreationFailedException("Unable to find the backend shard."
46                             + "Failing..")), noSender());
47         } else {
48             replyTo.tell(new Status.Failure(
49                     new DOMDataTreeShardCreationFailedException("Unable to find the backend shard."
50                             + "Failing..", throwable)), noSender());
51         }
52     }
53 }