190c46811cc1c5d6ba475be4e6d6c1745f03de76
[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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
15 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
16
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.Optional;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.common.api.CommitInfo;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
32 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
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 DOMTransactionChain txChain = domBroker.createTransactionChain();
66         assertNotNull(txChain);
67
68         /**
69          * We allocate 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 allocate 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 allocate 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 committed to store, container should exist
114          * in datastore.
115          */
116         assertTestContainerExists(storeReadTx);
117
118         /**
119          * third transaction is sealed and committed.
120          */
121         assertCommitSuccessful(thirdDeleteTx.commit());
122
123         /**
124          * We close transaction chain.
125          */
126         txChain.close();
127
128         txChain.future().get(1000, TimeUnit.MILLISECONDS);
129     }
130
131     @Test
132     public void testTransactionChainNotSealed() {
133         final var txChain = domBroker.createTransactionChain();
134         assertNotNull(txChain);
135
136         /**
137          * We allocate new read-write transaction and write /test
138          */
139         allocateAndWrite(txChain);
140
141         /**
142          * We allocate chained transaction - read transaction, note first one is
143          * still not committed to datastore, so this allocation should fail with
144          * IllegalStateException.
145          */
146         // actual backing tx allocation happens on put
147         final var ex = assertThrows(IllegalStateException.class, () -> allocateAndWrite(txChain));
148         assertEquals("Previous transaction OPER-0 is not ready yet", ex.getMessage());
149     }
150
151     private static DOMDataTreeWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
152             throws InterruptedException, ExecutionException {
153         final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
154         /**
155          * We delete node in third transaction
156          */
157         tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
158         return tx;
159     }
160
161     private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain) {
162         final var tx = txChain.newWriteOnlyTransaction();
163         writeTestContainer(tx);
164         return tx;
165     }
166
167     private static void assertCommitSuccessful(final ListenableFuture<? extends CommitInfo> firstWriteTxFuture)
168             throws InterruptedException, ExecutionException {
169         firstWriteTxFuture.get();
170     }
171
172     private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx)
173             throws InterruptedException, ExecutionException {
174         final ListenableFuture<Optional<NormalizedNode>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
175         final Optional<NormalizedNode> readedData = readFuture.get();
176         assertTrue(readedData.isPresent());
177     }
178
179     private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) {
180         tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
181     }
182 }