Migrate OSGI compendium reference
[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 com.google.common.base.Optional;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.ListeningExecutorService;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
31 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
32 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37
38 @Deprecated
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         DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
72
73         /**
74          * First transaction is marked as ready, we are able to allocate chained
75          * transactions.
76          */
77         ListenableFuture<?> firstWriteTxFuture = firstTx.commit();
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         assertTestContainerExists(secondReadTx);
90
91         /**
92          * We alocate next transaction, which is still based on first one, but
93          * is read-write.
94          *
95          */
96         DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
97
98         /**
99          * We commit first transaction.
100          *
101          */
102         assertCommitSuccessful(firstWriteTxFuture);
103
104         /**
105          * Allocates transaction from data store.
106          */
107         DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
108
109         /**
110          * We verify transaction is commited to store, container should exists
111          * in datastore.
112          */
113         assertTestContainerExists(storeReadTx);
114
115         /**
116          * third transaction is sealed and commited.
117          */
118         ListenableFuture<?> thirdDeleteTxFuture = thirdDeleteTx.commit();
119         assertCommitSuccessful(thirdDeleteTxFuture);
120
121         /**
122          * We close transaction chain.
123          */
124         txChain.close();
125
126         listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
127     }
128
129     @Test
130     @SuppressWarnings("checkstyle:IllegalCatch")
131     public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException {
132         BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
133         DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
134         assertNotNull(txChain);
135
136         /**
137          * We alocate new read-write transaction and write /test
138          */
139         allocateAndWrite(txChain);
140
141         /**
142          * We alocate chained transaction - read transaction, note first one is
143          * still not commited to datastore, so this allocation should fail with
144          * IllegalStateException.
145          */
146         try {
147             txChain.newReadOnlyTransaction();
148             fail("Allocation of secondReadTx should fail with IllegalStateException");
149         } catch (Exception e) {
150             assertTrue(e instanceof IllegalStateException);
151         }
152     }
153
154     private static DOMDataReadWriteTransaction allocateAndDelete(
155             final DOMTransactionChain txChain) throws InterruptedException, ExecutionException {
156         DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
157
158         /**
159          * We test existence of /test in third transaction container should
160          * still be visible from first one (which is still uncommmited).
161          *
162          */
163         assertTestContainerExists(tx);
164
165         /**
166          * We delete node in third transaction
167          */
168         tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
169         return tx;
170     }
171
172     private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
173             throws InterruptedException, ExecutionException {
174         DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
175         assertTestContainerWrite(tx);
176         return tx;
177     }
178
179     private static void assertCommitSuccessful(final ListenableFuture<?> future)
180             throws InterruptedException, ExecutionException {
181         future.get();
182     }
183
184     private static void assertTestContainerExists(final DOMDataReadTransaction readTx)
185             throws InterruptedException, ExecutionException {
186         ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
187         Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
188         assertTrue(readedData.isPresent());
189     }
190
191     private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx)
192             throws InterruptedException, ExecutionException {
193         tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
194         assertTestContainerExists(tx);
195     }
196 }