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