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