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