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.spi.store.DOMStore;
33 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
37 public class DOMTransactionChainTest extends AbstractDatastoreTest {
39 private AbstractDOMDataBroker domBroker;
42 public void setupStore() {
43 final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
44 MoreExecutors.newDirectExecutorService());
45 final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
46 MoreExecutors.newDirectExecutorService());
48 operStore.onModelContextUpdated(SCHEMA_CONTEXT);
49 configStore.onModelContextUpdated(SCHEMA_CONTEXT);
51 final ImmutableMap<LogicalDatastoreType, DOMStore> stores =
52 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
53 .put(CONFIGURATION, configStore)
54 .put(OPERATIONAL, operStore)
57 final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
58 Executors.newSingleThreadExecutor());
59 domBroker = new SerializedDOMDataBroker(stores, executor);
63 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
64 final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
65 final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
66 assertNotNull(txChain);
69 * We alocate new read-write transaction and write /test.
71 final DOMDataTreeWriteTransaction firstTx = allocateAndWrite(txChain);
74 * First transaction is marked as ready, we are able to allocate chained
77 final ListenableFuture<? extends CommitInfo> firstWriteTxFuture = firstTx.commit();
80 * We alocate chained transaction - read transaction.
82 final DOMDataTreeReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
86 * We test if we are able to read data from tx, read should not fail
87 * since we are using chained transaction.
91 assertTestContainerExists(secondReadTx);
94 * We alocate next transaction, which is still based on first one, but
98 final DOMDataTreeWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
101 * We commit first transaction
104 assertCommitSuccessful(firstWriteTxFuture);
107 * Allocates transaction from data store.
110 final DOMDataTreeReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
113 * We verify transaction is commited to store, container should exists
116 assertTestContainerExists(storeReadTx);
119 * third transaction is sealed and commited.
121 assertCommitSuccessful(thirdDeleteTx.commit());
124 * We close transaction chain.
128 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
131 @SuppressWarnings("checkstyle:IllegalCatch")
133 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
134 final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
135 final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
136 assertNotNull(txChain);
139 * We alocate new read-write transaction and write /test
141 allocateAndWrite(txChain);
144 * We alocate chained transaction - read transaction, note first one is
145 * still not commited to datastore, so this allocation should fail with
146 * IllegalStateException.
149 txChain.newReadOnlyTransaction();
150 fail("Allocation of secondReadTx should fail with IllegalStateException");
151 } catch (final Exception e) {
152 assertTrue(e instanceof IllegalStateException);
156 private static DOMDataTreeWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
157 throws InterruptedException, ExecutionException {
158 final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
160 * We delete node in third transaction
162 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
166 private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
167 throws InterruptedException, ExecutionException {
168 final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
169 writeTestContainer(tx);
173 private static void assertCommitSuccessful(final ListenableFuture<? extends CommitInfo> firstWriteTxFuture)
174 throws InterruptedException, ExecutionException {
175 firstWriteTxFuture.get();
178 private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx)
179 throws InterruptedException, ExecutionException {
180 final ListenableFuture<Optional<NormalizedNode>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
181 final Optional<NormalizedNode> readedData = readFuture.get();
182 assertTrue(readedData.isPresent());
185 private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) throws InterruptedException,
187 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));