2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker;
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;
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 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 public class DOMTransactionChainTest {
41 private SchemaContext schemaContext;
42 private AbstractDOMDataBroker domBroker;
45 public void setupStore() {
46 final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
47 MoreExecutors.newDirectExecutorService());
48 final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
49 MoreExecutors.newDirectExecutorService());
50 schemaContext = TestModel.createTestContext();
52 operStore.onGlobalContextUpdated(schemaContext);
53 configStore.onGlobalContextUpdated(schemaContext);
55 final ImmutableMap<LogicalDatastoreType, DOMStore> stores =
56 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
57 .put(CONFIGURATION, configStore)
58 .put(OPERATIONAL, operStore)
61 final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
62 Executors.newSingleThreadExecutor());
63 domBroker = new SerializedDOMDataBroker(stores, executor);
67 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
68 final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
69 final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
70 assertNotNull(txChain);
73 * We alocate new read-write transaction and write /test.
75 final DOMDataTreeWriteTransaction firstTx = allocateAndWrite(txChain);
78 * First transaction is marked as ready, we are able to allocate chained
81 final ListenableFuture<? extends CommitInfo> firstWriteTxFuture = firstTx.commit();
84 * We alocate chained transaction - read transaction.
86 final DOMDataTreeReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
90 * We test if we are able to read data from tx, read should not fail
91 * since we are using chained transaction.
95 assertTestContainerExists(secondReadTx);
98 * We alocate next transaction, which is still based on first one, but
102 final DOMDataTreeWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
105 * We commit first transaction
108 assertCommitSuccessful(firstWriteTxFuture);
111 * Allocates transaction from data store.
114 final DOMDataTreeReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
117 * We verify transaction is commited to store, container should exists
120 assertTestContainerExists(storeReadTx);
123 * third transaction is sealed and commited.
125 assertCommitSuccessful(thirdDeleteTx.commit());
128 * We close transaction chain.
132 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
135 @SuppressWarnings("checkstyle:IllegalCatch")
137 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
138 final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
139 final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
140 assertNotNull(txChain);
143 * We alocate new read-write transaction and write /test
145 allocateAndWrite(txChain);
148 * We alocate chained transaction - read transaction, note first one is
149 * still not commited to datastore, so this allocation should fail with
150 * IllegalStateException.
153 txChain.newReadOnlyTransaction();
154 fail("Allocation of secondReadTx should fail with IllegalStateException");
155 } catch (final Exception e) {
156 assertTrue(e instanceof IllegalStateException);
160 private static DOMDataTreeWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
161 throws InterruptedException, ExecutionException {
162 final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
164 * We delete node in third transaction
166 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
170 private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
171 throws InterruptedException, ExecutionException {
172 final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
173 writeTestContainer(tx);
177 private static void assertCommitSuccessful(final ListenableFuture<? extends CommitInfo> firstWriteTxFuture)
178 throws InterruptedException, ExecutionException {
179 firstWriteTxFuture.get();
182 private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx)
183 throws InterruptedException, ExecutionException {
184 final ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture =
185 readTx.read(OPERATIONAL, TestModel.TEST_PATH);
186 final Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
187 assertTrue(readedData.isPresent());
190 private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) throws InterruptedException,
192 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));