API Clarity: Documented Async Data Broker APIs.
[controller.git] / opendaylight / md-sal / sal-common-api / src / main / java / org / opendaylight / controller / md / sal / common / api / data / AsyncReadWriteTransaction.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.controller.md.sal.common.api.data;
9
10 import org.opendaylight.yangtools.concepts.Path;
11
12 /**
13  * Transaction enabling a client to have a combined read/write capabilities.
14  *
15  * <p>
16  * The initial state of the write transaction is stable snapshot of current data tree
17  * state captured when transaction was created and it's state and underlying
18  * data tree are not affected by other concurrently running transactions.
19  *
20  * <p>
21  * Write transactions are isolated from other concurrent write transactions. All
22  * writes are local to the transaction and represents only a proposal of state
23  * change for data tree and it is not visible to any other concurrently running
24  * transactions.
25  *
26  * <p>
27  * Applications publish the changes proposed in the transaction by calling {@link #commit}
28  * on the transaction. This seals the transaction
29  * (preventing any further writes using this transaction) and submits it to be
30  * processed and applied to global conceptual data tree.
31  *
32  * <p>
33  * The transaction commit may fail due to a concurrent transaction modifying and committing data in
34  * an incompatible way. See {@link #commit()} for more concrete commit failure examples.
35  *
36  * <b>Implementation Note:</b> This interface is not intended to be implemented
37  * by users of MD-SAL, but only to be consumed by them.
38  *
39  * <h2>Examples</h2>
40  *
41  * <h3>Transaction local state</h3>
42  *
43  * Let assume initial state of data tree for <code>PATH</code> is <code>A</code>
44  * .
45  *
46  * <pre>
47  * txWrite = broker.newReadWriteTransaction(); // concurrent write transaction
48  *
49  * txWrite.read(OPERATIONAL,PATH).get()        // will return Optional containing A
50  * txWrite.put(OPERATIONAL,PATH,B);            // writes B to PATH
51  * txWrite.read(OPERATIONAL,PATH).get()        // will return Optional Containing B
52  *
53  * txWrite.commit().get();                     // data tree is updated, PATH contains B
54  *
55  * tx1afterCommit = broker.newReadOnlyTransaction(); // read Transaction is snapshot of new state
56  * tx1afterCommit.read(OPERATIONAL,PATH).get(); // returns Optional containing B
57  * </pre>
58  *
59  * As you could see read-write transaction provides capabilities as
60  * {@link AsyncWriteTransaction} but also allows for reading proposed changes as
61  * if they already happened.
62  *
63  * <h3>Transaction isolation (read transaction, read-write transaction)</h3> Let
64  * assume initial state of data tree for <code>PATH</code> is <code>A</code>.
65  *
66  * <pre>
67  * txRead = broker.newReadOnlyTransaction();   // read Transaction is snapshot of data
68  * txWrite = broker.newReadWriteTransaction(); // concurrent write transaction
69  *
70  * txRead.read(OPERATIONAL,PATH).get();        // will return Optional containing A
71  * txWrite.read(OPERATIONAL,PATH).get()        // will return Optional containing A
72  *
73  * txWrite.put(OPERATIONAL,PATH,B);            // writes B to PATH
74  * txWrite.read(OPERATIONAL,PATH).get()        // will return Optional Containing B
75  *
76  * txRead.read(OPERATIONAL,PATH).get();        // concurrent read transaction still returns
77  *                                             // Optional containing A
78  *
79  * txWrite.commit().get();                     // data tree is updated, PATH contains B
80  * txRead.read(OPERATIONAL,PATH).get();        // still returns Optional containing A
81  *
82  * tx1afterCommit = broker.newReadOnlyTransaction(); // read Transaction is snapshot of new state
83  * tx1afterCommit.read(OPERATIONAL,PATH).get(); // returns Optional containing B
84  * </pre>
85  *
86  * <h3>Transaction isolation (2 concurrent read-write transactions)</h3> Let
87  * assume initial state of data tree for <code>PATH</code> is <code>A</code>.
88  *
89  * <pre>
90  * tx1 = broker.newReadWriteTransaction(); // read Transaction is snapshot of data
91  * tx2 = broker.newReadWriteTransaction(); // concurrent write transaction
92  *
93  * tx1.read(OPERATIONAL,PATH).get();       // will return Optional containing A
94  * tx2.read(OPERATIONAL,PATH).get()        // will return Optional containing A
95  *
96  * tx2.put(OPERATIONAL,PATH,B);            // writes B to PATH
97  * tx2.read(OPERATIONAL,PATH).get()        // will return Optional Containing B
98  *
99  * tx1.read(OPERATIONAL,PATH).get();       // tx1 read-write transaction still sees Optional
100  *                                         // containing A since is isolated from tx2
101  * tx1.put(OPERATIONAL,PATH,C);            // writes C to PATH
102  * tx1.read(OPERATIONAL,PATH).get()        // will return Optional Containing C
103  *
104  * tx2.read(OPERATIONAL,PATH).get()        // tx2 read-write transaction still sees Optional
105  *                                         // containing B since is isolated from tx1
106  *
107  * tx2.commit().get();                     // data tree is updated, PATH contains B
108  * tx1.read(OPERATIONAL,PATH).get();       // still returns Optional containing C since is isolated from tx2
109  *
110  * tx1afterCommit = broker.newReadOnlyTransaction(); // read Transaction is snapshot of new state
111  * tx1afterCommit.read(OPERATIONAL,PATH).get(); // returns Optional containing B
112  *
113  * tx1.commit()                            // Will fail with OptimisticLockFailedException
114  *                                         // which means concurrent transaction changed the same PATH
115  *
116  * </pre>
117  *
118  * <p>
119  * <b>Note:</b> examples contains blocking calls on future only to illustrate
120  * that action happened after other asynchronous action. Use of blocking call
121  * {@link com.google.common.util.concurrent.ListenableFuture#get()} is discouraged for most uses and you should
122  * use
123  * {@link com.google.common.util.concurrent.Futures#addCallback(com.google.common.util.concurrent.ListenableFuture, com.google.common.util.concurrent.FutureCallback)}
124  * or other functions from {@link com.google.common.util.concurrent.Futures} to
125  * register more specific listeners.
126  *
127  *
128  * @param <P>
129  *            Type of path (subtree identifier), which represents location in
130  *            tree
131  * @param <D>
132  *            Type of data (payload), which represents data payload
133  */
134 public interface AsyncReadWriteTransaction<P extends Path<P>, D> extends AsyncReadTransaction<P, D>,
135         AsyncWriteTransaction<P, D> {
136
137 }