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