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