Move TestModel
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / 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.mdsal.dom.broker;
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.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
14 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
15
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.Optional;
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.mdsal.common.api.CommitInfo;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
31 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
32 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
33 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36
37 public class DOMTransactionChainTest extends AbstractDatastoreTest {
38
39     private AbstractDOMDataBroker domBroker;
40
41     @Before
42     public void setupStore() {
43         final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
44                 MoreExecutors.newDirectExecutorService());
45         final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
46                 MoreExecutors.newDirectExecutorService());
47
48         operStore.onModelContextUpdated(SCHEMA_CONTEXT);
49         configStore.onModelContextUpdated(SCHEMA_CONTEXT);
50
51         final ImmutableMap<LogicalDatastoreType, DOMStore> stores =
52                 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
53                 .put(CONFIGURATION, configStore)
54                 .put(OPERATIONAL, operStore)
55                 .build();
56
57         final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
58                 Executors.newSingleThreadExecutor());
59         domBroker = new SerializedDOMDataBroker(stores, executor);
60     }
61
62     @Test
63     public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
64         final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
65         final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
66         assertNotNull(txChain);
67
68         /**
69          * We alocate new read-write transaction and write /test.
70          */
71         final DOMDataTreeWriteTransaction firstTx = allocateAndWrite(txChain);
72
73         /**
74          * First transaction is marked as ready, we are able to allocate chained
75          * transactions.
76          */
77         final ListenableFuture<? extends CommitInfo> firstWriteTxFuture = firstTx.commit();
78
79         /**
80          * We alocate chained transaction - read transaction.
81          */
82         final DOMDataTreeReadTransaction 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          * We alocate next transaction, which is still based on first one, but
95          * is read-write.
96          *
97          */
98         final DOMDataTreeWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
99
100         /**
101          * We commit first transaction
102          *
103          */
104         assertCommitSuccessful(firstWriteTxFuture);
105
106         /**
107          * Allocates transaction from data store.
108          *
109          */
110         final DOMDataTreeReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
111
112         /**
113          * We verify transaction is commited to store, container should exists
114          * in datastore.
115          */
116         assertTestContainerExists(storeReadTx);
117
118         /**
119          * third transaction is sealed and commited.
120          */
121         assertCommitSuccessful(thirdDeleteTx.commit());
122
123         /**
124          * We close transaction chain.
125          */
126         txChain.close();
127
128         listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
129     }
130
131     @SuppressWarnings("checkstyle:IllegalCatch")
132     @Test
133     public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
134         final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
135         final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
136         assertNotNull(txChain);
137
138         /**
139          * We alocate new read-write transaction and write /test
140          */
141         allocateAndWrite(txChain);
142
143         /**
144          * We alocate chained transaction - read transaction, note first one is
145          * still not commited to datastore, so this allocation should fail with
146          * IllegalStateException.
147          */
148         try {
149             txChain.newReadOnlyTransaction();
150             fail("Allocation of secondReadTx should fail with IllegalStateException");
151         } catch (final Exception e) {
152             assertTrue(e instanceof IllegalStateException);
153         }
154     }
155
156     private static DOMDataTreeWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
157             throws InterruptedException, ExecutionException {
158         final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
159         /**
160          * We delete node in third transaction
161          */
162         tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
163         return tx;
164     }
165
166     private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
167             throws InterruptedException, ExecutionException {
168         final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
169         writeTestContainer(tx);
170         return tx;
171     }
172
173     private static void assertCommitSuccessful(final ListenableFuture<? extends CommitInfo> firstWriteTxFuture)
174             throws InterruptedException, ExecutionException {
175         firstWriteTxFuture.get();
176     }
177
178     private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx)
179             throws InterruptedException, ExecutionException {
180         final ListenableFuture<Optional<NormalizedNode>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
181         final Optional<NormalizedNode> readedData = readFuture.get();
182         assertTrue(readedData.isPresent());
183     }
184
185     private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) throws InterruptedException,
186             ExecutionException {
187         tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
188     }
189 }