2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.common.api;
10 import org.opendaylight.yangtools.concepts.Path;
13 * Transaction enabling a client to have a combined read/write capabilities.
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.
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.
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.
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.
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.
39 * <h3>Transaction local state</h3>
41 * Let assume initial state of data tree for <code>PATH</code> is <code>A</code> .
44 * txWrite = broker.newReadWriteTransaction(); // concurrent write transaction
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
50 * txWrite.submit().get(); // data tree is updated, PATH contains B
52 * tx1afterCommit = broker.newReadOnlyTransaction(); // read Transaction is snapshot of new state
53 * tx1afterCommit.read(OPERATIONAL,PATH).get(); // returns Optional containing B
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.
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>.
63 * txRead = broker.newReadOnlyTransaction(); // read Transaction is snapshot of data
64 * txWrite = broker.newReadWriteTransaction(); // concurrent write transaction
66 * txRead.read(OPERATIONAL,PATH).get(); // will return Optional containing A
67 * txWrite.read(OPERATIONAL,PATH).get() // will return Optional containing A
69 * txWrite.put(OPERATIONAL,PATH,B); // writes B to PATH
70 * txWrite.read(OPERATIONAL,PATH).get() // will return Optional Containing B
72 * txRead.read(OPERATIONAL,PATH).get(); // concurrent read transaction still returns
73 * // Optional containing A
75 * txWrite.submit().get(); // data tree is updated, PATH contains B
76 * txRead.read(OPERATIONAL,PATH).get(); // still returns Optional containing A
78 * tx1afterCommit = broker.newReadOnlyTransaction(); // read Transaction is snapshot of new state
79 * tx1afterCommit.read(OPERATIONAL,PATH).get(); // returns Optional containing B
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>.
86 * tx1 = broker.newReadWriteTransaction(); // read Transaction is snapshot of data
87 * tx2 = broker.newReadWriteTransaction(); // concurrent write transaction
89 * tx1.read(OPERATIONAL,PATH).get(); // will return Optional containing A
90 * tx2.read(OPERATIONAL,PATH).get() // will return Optional containing A
92 * tx2.put(OPERATIONAL,PATH,B); // writes B to PATH
93 * tx2.read(OPERATIONAL,PATH).get() // will return Optional Containing B
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
100 * tx2.read(OPERATIONAL,PATH).get() // tx2 read-write transaction still sees Optional
101 * // containing B since is isolated from tx1
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
106 * tx1afterCommit = broker.newReadOnlyTransaction(); // read Transaction is snapshot of new state
107 * tx1afterCommit.read(OPERATIONAL,PATH).get(); // returns Optional containing B
109 * tx1.submit() // Will fail with OptimisticLockFailedException
110 * // which means concurrent transaction changed the same PATH
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
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.
123 * @see AsyncReadTransaction
124 * @see AsyncWriteTransaction
126 * @param <P> Type of path (subtree identifier), which represents location in tree
127 * @param <D> Type of data (payload), which represents data payload
129 public interface AsyncReadWriteTransaction<P extends Path<P>, D> extends AsyncReadTransaction<P, D>,
130 AsyncWriteTransaction<P, D> {