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