fb11711c03ea6cbe455b62c3d46b1bc97e01e89f
[mdsal.git] / dom / mdsal-dom-api / src / main / java / org / opendaylight / mdsal / dom / api / DOMDataTreeWriteTransaction.java
1 /*
2  * Copyright (c) 2014 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.mdsal.dom.api;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import edu.umd.cs.findbugs.annotations.CheckReturnValue;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.common.api.CommitInfo;
14 import org.opendaylight.mdsal.common.api.DataValidationFailedException;
15 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
16 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
17
18 /**
19  * Write transaction provides mutation capabilities for a data tree.
20  *
21  * <p>
22  * Initial state of write transaction is a stable snapshot of the current data tree.
23  * The state is captured when the transaction is created and its state and underlying
24  * data tree are not affected by other concurrently running transactions.
25  *
26  * <p>
27  * Write transactions are isolated from other concurrent write transactions. All
28  * writes are local to the transaction and represent only a proposal of state
29  * change for the data tree and it is not visible to any other concurrently running
30  * transaction.
31  *
32  * <p>
33  * Applications make changes to the local data tree in the transaction by via the
34  * <b>put</b>, <b>merge</b>, and <b>delete</b> operations.
35  *
36  * <h2>Put operation</h2>
37  * Stores a piece of data at a specified path. This acts as an add / replace
38  * operation, which is to say that whole subtree will be replaced by the
39  * specified data.
40  *
41  * <p>
42  * Performing the following put operations:
43  *
44  * <pre>
45  * 1) container { list [ a ] }
46  * 2) container { list [ b ] }
47  * </pre>
48  * will result in the following data being present:
49  *
50  * <pre>
51  * container { list [ b ] }
52  * </pre>
53  * <h2>Merge operation</h2>
54  * Merges a piece of data with the existing data at a specified path. Any pre-existing data
55  * which is not explicitly overwritten will be preserved. This means that if you store a container,
56  * its child lists will be merged.
57  *
58  * <p>
59  * Performing the following merge operations:
60  *
61  * <pre>
62  * 1) container { list [ a ] }
63  * 2) container { list [ b ] }
64  * </pre>
65  * will result in the following data being present:
66  *
67  * <pre>
68  * container { list [ a, b ] }
69  * </pre>
70  * This also means that storing the container will preserve any
71  * augmentations which have been attached to it.
72  *
73  * <h2>Delete operation</h2>
74  * Removes a piece of data from a specified path.
75  *
76  * <p>
77  * After applying changes to the local data tree, applications publish the changes proposed in the
78  * transaction by calling {@link #commit} on the transaction. This seals the transaction
79  * (preventing any further writes using this transaction) and commits it to be
80  * processed and applied to global conceptual data tree.
81  *
82  * <p>
83  * The transaction commit may fail due to a concurrent transaction modifying and committing data in
84  * an incompatible way. See {@link #commit} for more concrete commit failure examples.
85  *
86  * <p>
87  * <b>Implementation Note:</b> This interface is not intended to be implemented
88  * by users of MD-SAL, but only to be consumed by them.
89  */
90 public interface DOMDataTreeWriteTransaction extends DOMDataTreeTransaction, DOMDataTreeWriteOperations {
91     /**
92      * Commits this transaction to be asynchronously applied to update the logical data tree. The returned
93      * {@link FluentFuture} conveys the result of applying the data changes.
94      *
95      * <p>
96      * This call logically seals the transaction, which prevents the client from further changing the data tree using
97      * this transaction. Any subsequent calls to <code>put(LogicalDatastoreType, Path, Object)</code>,
98      * <code>merge(LogicalDatastoreType, Path, Object)</code>, <code>delete(LogicalDatastoreType, Path)</code> will fail
99      * with {@link IllegalStateException}. The transaction is marked as committed and enqueued into the data store
100      * back-end for processing.
101      *
102      * <p>
103      * Whether or not the commit is successful is determined by versioning of the data tree and validation of registered
104      * commit participants if the transaction changes the data tree.
105      *
106      * <p>
107      * The effects of a successful commit of data depends on listeners and commit participants that are registered with
108      * the data broker.
109      *
110      * <h3>Example usage:</h3>
111      * <pre>
112      *  private void doWrite(final int tries) {
113      *      WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction();
114      *      MyDataObject data = ...;
115      *      InstanceIdentifier&lt;MyDataObject&gt; path = ...;
116      *      writeTx.put(LogicalDatastoreType.OPERATIONAL, path, data);
117      *      Futures.addCallback(writeTx.commit(), new FutureCallback&lt;CommitInfo&gt;() {
118      *          public void onSuccess(CommitInfo result) {
119      *              // succeeded
120      *          }
121      *          public void onFailure(Throwable t) {
122      *              if (t instanceof OptimisticLockFailedException) {
123      *                  if(( tries - 1) &gt; 0 ) {
124      *                      // do retry
125      *                      doWrite(tries - 1);
126      *                  } else {
127      *                      // out of retries
128      *                  }
129      *              } else {
130      *                  // failed due to another type of TransactionCommitFailedException.
131      *              }
132      *          });
133      * }
134      * ...
135      * doWrite(2);
136      * </pre>
137      *
138      * <h2>Failure scenarios</h2>
139      *
140      * <p>
141      * Transaction may fail because of multiple reasons, such as
142      * <ul>
143      *   <li>
144      *     Another transaction finished earlier and modified the same node in a non-compatible way (see below). In this
145      *     case the returned future will fail with an {@link OptimisticLockFailedException}. It is the responsibility
146      *     of the caller to create a new transaction and commit the same modification again in order to update data
147      *     tree.
148      *     <i>
149      *       <b>Warning</b>: In most cases, retrying after an OptimisticLockFailedException will result in a high
150      *       probability of success. However, there are scenarios, albeit unusual, where any number of retries will
151      *       not succeed. Therefore it is strongly recommended to limit the number of retries (2 or 3) to avoid
152      *       an endless loop.
153      *     </i>
154      *   </li>
155      *   <li>Data change introduced by this transaction did not pass validation by commit handlers or data was
156      *       incorrectly structured. Returned future will fail with a {@link DataValidationFailedException}. User
157      *       should not retry to create new transaction with same data, since it probably will fail again.
158      *   </li>
159      * </ul>
160      *
161      * <h3>Change compatibility</h3>
162      * There are several sets of changes which could be considered incompatible between two transactions which are
163      * derived from same initial state. Rules for conflict detection applies recursively for each subtree level.
164      *
165      * <h4>Change compatibility of leafs, leaf-list items</h4>
166      * Following table shows state changes and failures between two concurrent transactions, which are based on same
167      * initial state, Tx 1 completes successfully before Tx 2 is committed.
168      *
169      * <table>
170      * <caption>Change compatibility of leaf values</caption>
171      * <tr>
172      * <th>Initial state</th>
173      * <th>Tx 1</th>
174      * <th>Tx 2</th>
175      * <th>Result</th>
176      * </tr>
177      * <tr>
178      * <td>Empty</td>
179      * <td>put(A,1)</td>
180      * <td>put(A,2)</td>
181      * <td>Tx 2 will fail, state is A=1</td>
182      * </tr>
183      * <tr>
184      * <td>Empty</td>
185      * <td>put(A,1)</td>
186      * <td>merge(A,2)</td>
187      * <td>A=2</td>
188      * </tr>
189      *
190      * <tr>
191      * <td>Empty</td>
192      * <td>merge(A,1)</td>
193      * <td>put(A,2)</td>
194      * <td>Tx 2 will fail, state is A=1</td>
195      * </tr>
196      * <tr>
197      * <td>Empty</td>
198      * <td>merge(A,1)</td>
199      * <td>merge(A,2)</td>
200      * <td>A=2</td>
201      * </tr>
202      *
203      *
204      * <tr>
205      * <td>A=0</td>
206      * <td>put(A,1)</td>
207      * <td>put(A,2)</td>
208      * <td>Tx 2 will fail, A=1</td>
209      * </tr>
210      * <tr>
211      * <td>A=0</td>
212      * <td>put(A,1)</td>
213      * <td>merge(A,2)</td>
214      * <td>A=2</td>
215      * </tr>
216      * <tr>
217      * <td>A=0</td>
218      * <td>merge(A,1)</td>
219      * <td>put(A,2)</td>
220      * <td>Tx 2 will fail, A=1</td>
221      * </tr>
222      * <tr>
223      * <td>A=0</td>
224      * <td>merge(A,1)</td>
225      * <td>merge(A,2)</td>
226      * <td>A=2</td>
227      * </tr>
228      *
229      * <tr>
230      * <td>A=0</td>
231      * <td>delete(A)</td>
232      * <td>put(A,2)</td>
233      * <td>Tx 2 will fail, A does not exists</td>
234      * </tr>
235      * <tr>
236      * <td>A=0</td>
237      * <td>delete(A)</td>
238      * <td>merge(A,2)</td>
239      * <td>A=2</td>
240      * </tr>
241      * </table>
242      *
243      * <h4>Change compatibility of subtrees</h4>
244      * Following table shows state changes and failures between two concurrent transactions, which are based on same
245      * initial state, Tx 1 completes successfully before Tx 2 is committed.
246      *
247      * <table>
248      * <caption>Change compatibility of containers</caption>
249      * <tr>
250      * <th>Initial state</th>
251      * <th>Tx 1</th>
252      * <th>Tx 2</th>
253      * <th>Result</th>
254      * </tr>
255      *
256      * <tr>
257      * <td>Empty</td>
258      * <td>put(TOP,[])</td>
259      * <td>put(TOP,[])</td>
260      * <td>Tx 2 will fail, state is TOP=[]</td>
261      * </tr>
262      * <tr>
263      * <td>Empty</td>
264      * <td>put(TOP,[])</td>
265      * <td>merge(TOP,[])</td>
266      * <td>TOP=[]</td>
267      * </tr>
268      *
269      * <tr>
270      * <td>Empty</td>
271      * <td>put(TOP,[FOO=1])</td>
272      * <td>put(TOP,[BAR=1])</td>
273      * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
274      * </tr>
275      * <tr>
276      * <td>Empty</td>
277      * <td>put(TOP,[FOO=1])</td>
278      * <td>merge(TOP,[BAR=1])</td>
279      * <td>TOP=[FOO=1,BAR=1]</td>
280      * </tr>
281      *
282      * <tr>
283      * <td>Empty</td>
284      * <td>merge(TOP,[FOO=1])</td>
285      * <td>put(TOP,[BAR=1])</td>
286      * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
287      * </tr>
288      * <tr>
289      * <td>Empty</td>
290      * <td>merge(TOP,[FOO=1])</td>
291      * <td>merge(TOP,[BAR=1])</td>
292      * <td>TOP=[FOO=1,BAR=1]</td>
293      * </tr>
294      *
295      * <tr>
296      * <td>TOP=[]</td>
297      * <td>put(TOP,[FOO=1])</td>
298      * <td>put(TOP,[BAR=1])</td>
299      * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
300      * </tr>
301      * <tr>
302      * <td>TOP=[]</td>
303      * <td>put(TOP,[FOO=1])</td>
304      * <td>merge(TOP,[BAR=1])</td>
305      * <td>state is TOP=[FOO=1,BAR=1]</td>
306      * </tr>
307      * <tr>
308      * <td>TOP=[]</td>
309      * <td>merge(TOP,[FOO=1])</td>
310      * <td>put(TOP,[BAR=1])</td>
311      * <td>Tx 2 will fail, state is TOP=[FOO=1]</td>
312      * </tr>
313      * <tr>
314      * <td>TOP=[]</td>
315      * <td>merge(TOP,[FOO=1])</td>
316      * <td>merge(TOP,[BAR=1])</td>
317      * <td>state is TOP=[FOO=1,BAR=1]</td>
318      * </tr>
319      * <tr>
320      * <td>TOP=[]</td>
321      * <td>delete(TOP)</td>
322      * <td>put(TOP,[BAR=1])</td>
323      * <td>Tx 2 will fail, state is empty store</td>
324      * </tr>
325      * <tr>
326      * <td>TOP=[]</td>
327      * <td>delete(TOP)</td>
328      * <td>merge(TOP,[BAR=1])</td>
329      * <td>state is TOP=[BAR=1]</td>
330      * </tr>
331      *
332      * <tr>
333      * <td>TOP=[]</td>
334      * <td>put(TOP/FOO,1)</td>
335      * <td>put(TOP/BAR,1])</td>
336      * <td>state is TOP=[FOO=1,BAR=1]</td>
337      * </tr>
338      * <tr>
339      * <td>TOP=[]</td>
340      * <td>put(TOP/FOO,1)</td>
341      * <td>merge(TOP/BAR,1)</td>
342      * <td>state is TOP=[FOO=1,BAR=1]</td>
343      * </tr>
344      * <tr>
345      * <td>TOP=[]</td>
346      * <td>merge(TOP/FOO,1)</td>
347      * <td>put(TOP/BAR,1)</td>
348      * <td>state is TOP=[FOO=1,BAR=1]</td>
349      * </tr>
350      * <tr>
351      * <td>TOP=[]</td>
352      * <td>merge(TOP/FOO,1)</td>
353      * <td>merge(TOP/BAR,1)</td>
354      * <td>state is TOP=[FOO=1,BAR=1]</td>
355      * </tr>
356      * <tr>
357      * <td>TOP=[]</td>
358      * <td>delete(TOP)</td>
359      * <td>put(TOP/BAR,1)</td>
360      * <td>Tx 2 will fail, state is empty store</td>
361      * </tr>
362      * <tr>
363      * <td>TOP=[]</td>
364      * <td>delete(TOP)</td>
365      * <td>merge(TOP/BAR,1]</td>
366      * <td>Tx 2 will fail, state is empty store</td>
367      * </tr>
368      *
369      * <tr>
370      * <td>TOP=[FOO=1]</td>
371      * <td>put(TOP/FOO,2)</td>
372      * <td>put(TOP/BAR,1)</td>
373      * <td>state is TOP=[FOO=2,BAR=1]</td>
374      * </tr>
375      * <tr>
376      * <td>TOP=[FOO=1]</td>
377      * <td>put(TOP/FOO,2)</td>
378      * <td>merge(TOP/BAR,1)</td>
379      * <td>state is TOP=[FOO=2,BAR=1]</td>
380      * </tr>
381      * <tr>
382      * <td>TOP=[FOO=1]</td>
383      * <td>merge(TOP/FOO,2)</td>
384      * <td>put(TOP/BAR,1)</td>
385      * <td>state is TOP=[FOO=2,BAR=1]</td>
386      * </tr>
387      * <tr>
388      * <td>TOP=[FOO=1]</td>
389      * <td>merge(TOP/FOO,2)</td>
390      * <td>merge(TOP/BAR,1)</td>
391      * <td>state is TOP=[FOO=2,BAR=1]</td>
392      * </tr>
393      * <tr>
394      * <td>TOP=[FOO=1]</td>
395      * <td>delete(TOP/FOO)</td>
396      * <td>put(TOP/BAR,1)</td>
397      * <td>state is TOP=[BAR=1]</td>
398      * </tr>
399      * <tr>
400      * <td>TOP=[FOO=1]</td>
401      * <td>delete(TOP/FOO)</td>
402      * <td>merge(TOP/BAR,1]</td>
403      * <td>state is TOP=[BAR=1]</td>
404      * </tr>
405      * </table>
406      *
407      *
408      * <h3>Examples of failure scenarios</h3>
409      *
410      * <h4>Conflict of two transactions</h4>
411      * This example illustrates two concurrent transactions, which derived from same initial state
412      * of data tree and proposes conflicting modifications.
413      *
414      * <pre>
415      * txA = broker.newWriteTransaction(); // allocates new transaction, data tree is empty
416      * txB = broker.newWriteTransaction(); // allocates new transaction, data tree is empty
417      * txA.put(CONFIGURATION, PATH, A);    // writes to PATH value A
418      * txB.put(CONFIGURATION, PATH, B)     // writes to PATH value B
419      * ListenableFuture futureA = txA.commit(); // transaction A is sealed and committed
420      * ListenebleFuture futureB = txB.commit(); // transaction B is sealed and committed
421      * </pre>
422      * Commit of transaction A will be processed asynchronously and data tree will be updated to
423      * contain value <code>A</code> for <code>PATH</code>. Returned {@link FluentFuture} will
424      * successfully complete once state is applied to data tree.
425      * Commit of Transaction B will fail, because previous transaction also modified path in a
426      * concurrent way. The state introduced by transaction B will not be applied. Returned
427      * {@link FluentFuture} object will fail with {@link OptimisticLockFailedException}
428      * exception, which indicates to client that concurrent transaction prevented the committed
429      * transaction from being applied. <br>
430      *
431      * <p>
432      * A successful commit produces implementation-specific {@link CommitInfo} structure, which is used to communicate
433      * post-condition information to the caller. Such information can contain commit-id, timing information or any
434      * other information the implementation wishes to share.
435      *
436      * @return a FluentFuture containing the result of the commit information. The Future blocks until the commit
437      *         operation is complete. A successful commit returns nothing. On failure, the Future will fail with a
438      *         {@link TransactionCommitFailedException} or an exception derived from TransactionCommitFailedException.
439      * @throws IllegalStateException if the transaction is already committed or was canceled.
440      */
441     @CheckReturnValue
442     @NonNull FluentFuture<? extends @NonNull CommitInfo> commit();
443
444     /**
445      * Cancels the transaction. Transactions can only be cancelled if it was not yet committed.
446      * Invoking cancel() on failed or already canceled will have no effect, and transaction is considered cancelled.
447      * Invoking cancel() on finished transaction (future returned by {@link #commit()} already successfully completed)
448      * will always fail (return false).
449      *
450      * @return {@code false} if the task could not be cancelled, typically because it has already completed normally;
451      *         {@code true} otherwise
452      */
453     boolean cancel();
454 }