1e4ccad6e11d5f1ba5c04280e97b501dd4e813a9
[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.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
38
39 public class DOMTransactionChainTest extends AbstractDatastoreTest {
40
41     private AbstractDOMDataBroker domBroker;
42
43     @Before
44     public void setupStore() {
45         final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
46                 MoreExecutors.newDirectExecutorService());
47         final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
48                 MoreExecutors.newDirectExecutorService());
49
50         operStore.onModelContextUpdated(SCHEMA_CONTEXT);
51         configStore.onModelContextUpdated(SCHEMA_CONTEXT);
52
53         final ImmutableMap<LogicalDatastoreType, DOMStore> stores =
54                 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
55                 .put(CONFIGURATION, configStore)
56                 .put(OPERATIONAL, operStore)
57                 .build();
58
59         final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
60                 Executors.newSingleThreadExecutor());
61         domBroker = new SerializedDOMDataBroker(stores, executor);
62     }
63
64     @Test
65     public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
66         final DOMTransactionChain txChain = domBroker.createTransactionChain();
67         assertNotNull(txChain);
68
69         /**
70          * We allocate 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 allocate 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 allocate 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 committed to store, container should exist
115          * in datastore.
116          */
117         assertTestContainerExists(storeReadTx);
118
119         /**
120          * third transaction is sealed and committed.
121          */
122         assertCommitSuccessful(thirdDeleteTx.commit());
123
124         /**
125          * We close transaction chain.
126          */
127         txChain.close();
128
129         txChain.future().get(1000, TimeUnit.MILLISECONDS);
130     }
131
132     @Test
133     public void testTransactionChainNotSealed() {
134         final var txChain = domBroker.createTransactionChain();
135         assertNotNull(txChain);
136
137         /**
138          * We allocate new read-write transaction and write /test
139          */
140         allocateAndWrite(txChain);
141
142         /**
143          * We allocate chained transaction - read transaction, note first one is
144          * still not committed to datastore, so this allocation should fail with
145          * IllegalStateException.
146          */
147         // actual backing tx allocation happens on put
148         final var ex = assertThrows(IllegalStateException.class, () -> allocateAndWrite(txChain));
149         assertEquals("Previous transaction OPER-0 is not ready yet", ex.getMessage());
150     }
151
152     private static DOMDataTreeWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
153             throws InterruptedException, ExecutionException {
154         final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
155         /**
156          * We delete node in third transaction
157          */
158         tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
159         return tx;
160     }
161
162     private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain) {
163         final var tx = txChain.newWriteOnlyTransaction();
164         writeTestContainer(tx);
165         return tx;
166     }
167
168     private static void assertCommitSuccessful(final ListenableFuture<? extends CommitInfo> firstWriteTxFuture)
169             throws InterruptedException, ExecutionException {
170         firstWriteTxFuture.get();
171     }
172
173     private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx)
174             throws InterruptedException, ExecutionException {
175         final ListenableFuture<Optional<NormalizedNode>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
176         final Optional<NormalizedNode> readedData = readFuture.get();
177         assertTrue(readedData.isPresent());
178     }
179
180     private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) {
181         tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
182             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
183             .build());
184     }
185 }