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