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