Bug 1073: Added support to DOMBrokerImpl for Transaction Chaining
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMTransactionChainTest.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.dom.broker.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
15 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
16
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
29 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
30 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36
37 import com.google.common.base.Optional;
38 import com.google.common.collect.ImmutableMap;
39 import com.google.common.util.concurrent.ListenableFuture;
40 import com.google.common.util.concurrent.ListeningExecutorService;
41 import com.google.common.util.concurrent.MoreExecutors;
42
43 public class DOMTransactionChainTest {
44
45     private SchemaContext schemaContext;
46     private DOMDataBrokerImpl domBroker;
47
48     @Before
49     public void setupStore() {
50         InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
51         InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
52         schemaContext = TestModel.createTestContext();
53
54         operStore.onGlobalContextUpdated(schemaContext);
55         configStore.onGlobalContextUpdated(schemaContext);
56
57         ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
58                 .put(CONFIGURATION, configStore) //
59                 .put(OPERATIONAL, operStore) //
60                 .build();
61
62         ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
63         domBroker = new DOMDataBrokerImpl(stores, executor);
64     }
65
66     @Test
67     public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
68         BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
69         DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
70         assertNotNull(txChain);
71
72         /**
73          * We alocate new read-write transaction and write /test
74          * 
75          * 
76          */
77         DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
78
79         /**
80          * First transaction is marked as ready, we are able to allocate chained
81          * transactions
82          */
83         ListenableFuture<RpcResult<TransactionStatus>> firstWriteTxFuture = firstTx.commit();
84
85         /**
86          * We alocate chained transaction - read transaction.
87          */
88         DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
89
90         /**
91          * 
92          * We test if we are able to read data from tx, read should not fail
93          * since we are using chained transaction.
94          * 
95          * 
96          */
97         assertTestContainerExists(secondReadTx);
98
99         /**
100          * 
101          * We alocate next transaction, which is still based on first one, but
102          * is read-write.
103          * 
104          */
105         DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
106
107         /**
108          * third transaction is sealed.
109          */
110         ListenableFuture<RpcResult<TransactionStatus>> thirdDeleteTxFuture = thirdDeleteTx.commit();
111
112         /**
113          * We commit first transaction
114          * 
115          */
116         assertCommitSuccessful(firstWriteTxFuture);
117
118         // Alocates store transaction
119         DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
120         /**
121          * We verify transaction is commited to store, container should exists
122          * in datastore.
123          */
124         assertTestContainerExists(storeReadTx);
125         /**
126          * We commit third transaction
127          * 
128          */
129         assertCommitSuccessful(thirdDeleteTxFuture);
130
131         /**
132          * We close transaction chain.
133          */
134         txChain.close();
135
136         listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
137     }
138
139     @Test
140     public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
141         BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
142         DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
143         assertNotNull(txChain);
144
145         /**
146          * We alocate new read-write transaction and write /test
147          * 
148          * 
149          */
150         allocateAndWrite(txChain);
151
152         /**
153          * We alocate chained transaction - read transaction, note first one is
154          * still not commited to datastore, so this allocation should fail with
155          * IllegalStateException.
156          */
157         try {
158             DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
159             fail("Allocation of secondReadTx should fail with IllegalStateException");
160         } catch (Exception e) {
161             assertTrue(e instanceof IllegalStateException);
162         }
163     }
164
165     private static DOMDataReadWriteTransaction allocateAndDelete(DOMTransactionChain txChain)
166             throws InterruptedException, ExecutionException {
167         DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
168
169         /**
170          * We test existence of /test in third transaction container should
171          * still be visible from first one (which is still uncommmited).
172          * 
173          */
174         assertTestContainerExists(tx);
175
176         /**
177          * We delete node in third transaction
178          */
179         tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
180         return tx;
181     }
182
183     private static DOMDataReadWriteTransaction allocateAndWrite(DOMTransactionChain txChain)
184             throws InterruptedException, ExecutionException {
185         DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
186         assertTestContainerWrite(tx);
187         return tx;
188     }
189
190     private static void assertCommitSuccessful(ListenableFuture<RpcResult<TransactionStatus>> future)
191             throws InterruptedException, ExecutionException {
192         RpcResult<TransactionStatus> rpcResult = future.get();
193         assertTrue(rpcResult.isSuccessful());
194         assertEquals(TransactionStatus.COMMITED, rpcResult.getResult());
195     }
196
197     private static void assertTestContainerExists(DOMDataReadTransaction readTx) throws InterruptedException,
198             ExecutionException {
199         ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
200         Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
201         assertTrue(readedData.isPresent());
202     }
203
204     private static void assertTestContainerWrite(DOMDataReadWriteTransaction tx) throws InterruptedException,
205             ExecutionException {
206         tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
207         assertTestContainerExists(tx);
208     }
209 }