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