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;
38 public class DOMTransactionChainTest extends AbstractDatastoreTest {
40 private AbstractDOMDataBroker domBroker;
43 public void setupStore() {
44 final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
45 MoreExecutors.newDirectExecutorService());
46 final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
47 MoreExecutors.newDirectExecutorService());
49 operStore.onGlobalContextUpdated(SCHEMA_CONTEXT);
50 configStore.onGlobalContextUpdated(SCHEMA_CONTEXT);
52 final ImmutableMap<LogicalDatastoreType, DOMStore> stores =
53 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
54 .put(CONFIGURATION, configStore)
55 .put(OPERATIONAL, operStore)
58 final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
59 Executors.newSingleThreadExecutor());
60 domBroker = new SerializedDOMDataBroker(stores, executor);
64 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
65 final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
66 final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
67 assertNotNull(txChain);
70 * We alocate new read-write transaction and write /test.
72 final DOMDataTreeWriteTransaction firstTx = allocateAndWrite(txChain);
75 * First transaction is marked as ready, we are able to allocate chained
78 final ListenableFuture<? extends CommitInfo> firstWriteTxFuture = firstTx.commit();
81 * We alocate chained transaction - read transaction.
83 final DOMDataTreeReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
87 * We test if we are able to read data from tx, read should not fail
88 * since we are using chained transaction.
92 assertTestContainerExists(secondReadTx);
95 * We alocate next transaction, which is still based on first one, but
99 final DOMDataTreeWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
102 * We commit first transaction
105 assertCommitSuccessful(firstWriteTxFuture);
108 * Allocates transaction from data store.
111 final DOMDataTreeReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
114 * We verify transaction is commited to store, container should exists
117 assertTestContainerExists(storeReadTx);
120 * third transaction is sealed and commited.
122 assertCommitSuccessful(thirdDeleteTx.commit());
125 * We close transaction chain.
129 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
132 @SuppressWarnings("checkstyle:IllegalCatch")
134 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
135 final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
136 final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
137 assertNotNull(txChain);
140 * We alocate new read-write transaction and write /test
142 allocateAndWrite(txChain);
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.
150 txChain.newReadOnlyTransaction();
151 fail("Allocation of secondReadTx should fail with IllegalStateException");
152 } catch (final Exception e) {
153 assertTrue(e instanceof IllegalStateException);
157 private static DOMDataTreeWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
158 throws InterruptedException, ExecutionException {
159 final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
161 * We delete node in third transaction
163 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
167 private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
168 throws InterruptedException, ExecutionException {
169 final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
170 writeTestContainer(tx);
174 private static void assertCommitSuccessful(final ListenableFuture<? extends CommitInfo> firstWriteTxFuture)
175 throws InterruptedException, ExecutionException {
176 firstWriteTxFuture.get();
179 private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx)
180 throws InterruptedException, ExecutionException {
181 final ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture =
182 readTx.read(OPERATIONAL, TestModel.TEST_PATH);
183 final Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
184 assertTrue(readedData.isPresent());
187 private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) throws InterruptedException,
189 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));