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