Migrate to Objects.requireNonNull()
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / reconciliation / ReconciliationTask.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.ovsdb.hwvtepsouthbound.reconciliation;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionManager;
13 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.connection.ConnectionReconciliationTask;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
16
17 /**
18  * Copied from org.opendaylight.ovsdb.southbound.reconciliation.ReconciliationTask
19  *
20  * <p>
21  * Abstract implementation of a reconciliation task. Each new type of
22  * resource configuration reconciliation task should extend this class
23  * and implement the abstract methods.
24  * Created by Anil Vishnoi (avishnoi@Brocade.com) on 3/9/16.
25  */
26 public abstract class ReconciliationTask implements Runnable {
27     protected final ReconciliationManager reconciliationManager;
28     protected final HwvtepConnectionManager connectionManager;
29     protected final InstanceIdentifier<?> nodeIid;
30     protected final DataObject configData;
31
32     protected ReconciliationTask(ReconciliationManager reconciliationManager, HwvtepConnectionManager connectionManager,
33                                  InstanceIdentifier<?> nodeIid,
34                                  DataObject configData) {
35         this.reconciliationManager = requireNonNull(reconciliationManager, "Reconciliation manager must not be null");
36         this.connectionManager = requireNonNull(connectionManager, "Connection manager must not be null");
37         this.nodeIid = requireNonNull(nodeIid, "Node Iid must not be null");
38         this.configData = configData;
39     }
40
41     /**
42      * Method contains task reconciliation logic. Please refer to
43      * {@link ConnectionReconciliationTask#reconcileConfiguration(HwvtepConnectionManager)}
44      * for example.
45      * @param connectionManager Connection manager to get connection instance of the device
46      * @return True if reconciliation was successful, else false
47      */
48     public abstract boolean reconcileConfiguration(HwvtepConnectionManager connectionManager);
49
50     /**
51      * Extended task should implement the logic that decides whether retry for the task
52      * is required or not. If retry is required but it does not requires any delay, submit
53      * the task immediately using {@link ReconciliationManager#enqueue(ReconciliationTask)}.
54      * If retry requires delay, use {@link ReconciliationManager#enqueueForRetry(ReconciliationTask)}
55      * and specify the delay using {@link #retryDelayInMills()}.
56      * If data store operation is required to decide if the task need retry, please implement
57      * it as an async operation and submit the task on the callback of the future.
58      * <p>
59      * Note:Please do not write blocking data store operations
60      * {@link ConnectionReconciliationTask#doRetry(boolean)}
61      * </p>
62      * @param wasPreviousAttemptSuccessful Status of the previous attempt
63      */
64     public abstract void doRetry(boolean wasPreviousAttemptSuccessful);
65
66     /**
67      * Extended task should implement the logic that check the readiness of the task
68      * for execution. If task is ready for the execution, submit it for immediate
69      * execution using {@link ReconciliationManager#enqueue(ReconciliationTask)}.
70      * If task is not ready for execution yet, enqueue it again for delayed execution
71      * using {@link ReconciliationManager#enqueueForRetry(ReconciliationTask)}.
72      * To check the readiness of the task, if the data store operation is required, please
73      * implement it as an async operation and submit the task on the callback of the future.
74      * <p>
75      * Note:Please do not write blocking data store operations
76      * {@link ConnectionReconciliationTask#doRetry(boolean)}
77      * </p>
78      */
79     public abstract void checkReadinessAndProcess();
80
81     /**
82      * Method returns the time interval for retrying the failed task.
83      * {@link ReconciliationTask#doRetry(boolean)}
84      * @return time
85      */
86     public abstract long retryDelayInMills();
87
88     @Override
89     public void run() {
90         boolean status = this.reconcileConfiguration(connectionManager);
91         doRetry(status);
92     }
93
94     @Override
95     public boolean equals(Object obj) {
96         if (this == obj) {
97             return true;
98         }
99         if (obj == null || getClass() != obj.getClass()) {
100             return false;
101         }
102
103         ReconciliationTask that = (ReconciliationTask) obj;
104
105         return nodeIid.equals(that.nodeIid);
106
107     }
108
109     @Override
110     public int hashCode() {
111         return getClass().hashCode() + nodeIid.hashCode();
112     }
113
114     @Override
115     public String toString() {
116         return "ReconciliationTask{ type=" + getClass().toString() + ", nodeIid=" + nodeIid + '}';
117     }
118 }