Merge "Remove remoterpc dead code."
[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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
15 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
16
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
29 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
30 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
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 import com.google.common.base.Optional;
38 import com.google.common.collect.ImmutableMap;
39 import com.google.common.util.concurrent.ListenableFuture;
40 import com.google.common.util.concurrent.ListeningExecutorService;
41 import com.google.common.util.concurrent.MoreExecutors;
42
43 public class DOMTransactionChainTest {
44
45     private SchemaContext schemaContext;
46     private DOMDataBrokerImpl domBroker;
47
48     @Before
49     public void setupStore() {
50         InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
51         InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
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 DOMDataBrokerImpl(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         DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
78
79         /**
80          * First transaction is marked as ready, we are able to allocate chained
81          * transactions
82          */
83         ListenableFuture<RpcResult<TransactionStatus>> firstWriteTxFuture = firstTx.commit();
84
85         /**
86          * We alocate chained transaction - read transaction.
87          */
88         DOMDataReadTransaction 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         DOMDataReadWriteTransaction 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         DOMDataReadTransaction 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<RpcResult<TransactionStatus>> thirdDeleteTxFuture = thirdDeleteTx.commit();
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 DOMDataReadWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
167             throws InterruptedException, ExecutionException {
168         DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
169
170         /**
171          * We test existence of /test in third transaction container should
172          * still be visible from first one (which is still uncommmited).
173          *
174          */
175         assertTestContainerExists(tx);
176
177         /**
178          * We delete node in third transaction
179          */
180         tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
181         return tx;
182     }
183
184     private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
185             throws InterruptedException, ExecutionException {
186         DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
187         assertTestContainerWrite(tx);
188         return tx;
189     }
190
191     private static void assertCommitSuccessful(final ListenableFuture<RpcResult<TransactionStatus>> future)
192             throws InterruptedException, ExecutionException {
193         RpcResult<TransactionStatus> rpcResult = future.get();
194         assertTrue(rpcResult.isSuccessful());
195         assertEquals(TransactionStatus.COMMITED, rpcResult.getResult());
196     }
197
198     private static void assertTestContainerExists(final DOMDataReadTransaction readTx) throws InterruptedException,
199             ExecutionException {
200         ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
201         Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
202         assertTrue(readedData.isPresent());
203     }
204
205     private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx) throws InterruptedException,
206             ExecutionException {
207         tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
208         assertTestContainerExists(tx);
209     }
210 }