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.controller.md.sal.common.api.data;
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
17 * state captured when transaction was created and it's state and underlying
18 * data tree are not affected by other concurrently running transactions.
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
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.
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.
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.
41 * <h3>Transaction local state</h3>
43 * Let assume initial state of data tree for <code>PATH</code> is <code>A</code>
47 * txWrite = broker.newReadWriteTransaction(); // concurrent write transaction
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
53 * txWrite.commit().get(); // data tree is updated, PATH contains B
55 * tx1afterCommit = broker.newReadOnlyTransaction(); // read Transaction is snapshot of new state
56 * tx1afterCommit.read(OPERATIONAL,PATH).get(); // returns Optional containing B
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.
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>.
67 * txRead = broker.newReadOnlyTransaction(); // read Transaction is snapshot of data
68 * txWrite = broker.newReadWriteTransaction(); // concurrent write transaction
70 * txRead.read(OPERATIONAL,PATH).get(); // will return Optional containing A
71 * txWrite.read(OPERATIONAL,PATH).get() // will return Optional containing A
73 * txWrite.put(OPERATIONAL,PATH,B); // writes B to PATH
74 * txWrite.read(OPERATIONAL,PATH).get() // will return Optional Containing B
76 * txRead.read(OPERATIONAL,PATH).get(); // concurrent read transaction still returns
77 * // Optional containing A
79 * txWrite.commit().get(); // data tree is updated, PATH contains B
80 * txRead.read(OPERATIONAL,PATH).get(); // still returns Optional containing A
82 * tx1afterCommit = broker.newReadOnlyTransaction(); // read Transaction is snapshot of new state
83 * tx1afterCommit.read(OPERATIONAL,PATH).get(); // returns Optional containing B
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>.
90 * tx1 = broker.newReadWriteTransaction(); // read Transaction is snapshot of data
91 * tx2 = broker.newReadWriteTransaction(); // concurrent write transaction
93 * tx1.read(OPERATIONAL,PATH).get(); // will return Optional containing A
94 * tx2.read(OPERATIONAL,PATH).get() // will return Optional containing A
96 * tx2.put(OPERATIONAL,PATH,B); // writes B to PATH
97 * tx2.read(OPERATIONAL,PATH).get() // will return Optional Containing B
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
104 * tx2.read(OPERATIONAL,PATH).get() // tx2 read-write transaction still sees Optional
105 * // containing B since is isolated from tx1
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
110 * tx1afterCommit = broker.newReadOnlyTransaction(); // read Transaction is snapshot of new state
111 * tx1afterCommit.read(OPERATIONAL,PATH).get(); // returns Optional containing B
113 * tx1.commit() // Will fail with OptimisticLockFailedException
114 * // which means concurrent transaction changed the same PATH
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
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.
127 * @see AsyncReadTransaction
128 * @see AsyncWriteTransaction
131 * Type of path (subtree identifier), which represents location in
134 * Type of data (payload), which represents data payload
136 public interface AsyncReadWriteTransaction<P extends Path<P>, D> extends AsyncReadTransaction<P, D>,
137 AsyncWriteTransaction<P, D> {