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