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.controller.md.sal.dom.broker.impl;
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.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
14 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
26 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
27 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
28 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import com.google.common.base.Optional;
35 import com.google.common.collect.ImmutableMap;
36 import com.google.common.util.concurrent.ListenableFuture;
37 import com.google.common.util.concurrent.ListeningExecutorService;
38 import com.google.common.util.concurrent.MoreExecutors;
40 public class DOMTransactionChainTest {
42 private SchemaContext schemaContext;
43 private DOMDataBrokerImpl domBroker;
46 public void setupStore() {
47 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
48 MoreExecutors.sameThreadExecutor(), MoreExecutors.sameThreadExecutor());
49 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
50 MoreExecutors.sameThreadExecutor(), MoreExecutors.sameThreadExecutor());
51 schemaContext = TestModel.createTestContext();
53 operStore.onGlobalContextUpdated(schemaContext);
54 configStore.onGlobalContextUpdated(schemaContext);
56 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
57 .put(CONFIGURATION, configStore) //
58 .put(OPERATIONAL, operStore) //
61 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
62 domBroker = new DOMDataBrokerImpl(stores, executor);
66 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
67 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
68 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
69 assertNotNull(txChain);
72 * We alocate new read-write transaction and write /test
76 DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
79 * First transaction is marked as ready, we are able to allocate chained
82 ListenableFuture<Void> firstWriteTxFuture = firstTx.submit();
85 * We alocate chained transaction - read transaction.
87 DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
91 * We test if we are able to read data from tx, read should not fail
92 * since we are using chained transaction.
96 assertTestContainerExists(secondReadTx);
100 * We alocate next transaction, which is still based on first one, but
104 DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
107 * We commit first transaction
110 assertCommitSuccessful(firstWriteTxFuture);
114 * Allocates transaction from data store.
117 DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
120 * We verify transaction is commited to store, container should exists
123 assertTestContainerExists(storeReadTx);
126 * third transaction is sealed and commited
128 ListenableFuture<Void> thirdDeleteTxFuture = thirdDeleteTx.submit();
129 assertCommitSuccessful(thirdDeleteTxFuture);
132 * We close transaction chain.
136 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
140 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
141 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
142 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
143 assertNotNull(txChain);
146 * We alocate new read-write transaction and write /test
150 allocateAndWrite(txChain);
153 * We alocate chained transaction - read transaction, note first one is
154 * still not commited to datastore, so this allocation should fail with
155 * IllegalStateException.
158 txChain.newReadOnlyTransaction();
159 fail("Allocation of secondReadTx should fail with IllegalStateException");
160 } catch (Exception e) {
161 assertTrue(e instanceof IllegalStateException);
165 private static DOMDataReadWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
166 throws InterruptedException, ExecutionException {
167 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
170 * We test existence of /test in third transaction container should
171 * still be visible from first one (which is still uncommmited).
174 assertTestContainerExists(tx);
177 * We delete node in third transaction
179 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
183 private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
184 throws InterruptedException, ExecutionException {
185 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
186 assertTestContainerWrite(tx);
190 private static void assertCommitSuccessful(final ListenableFuture<Void> future)
191 throws InterruptedException, ExecutionException {
195 private static void assertTestContainerExists(final DOMDataReadTransaction readTx) throws InterruptedException,
197 ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
198 Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
199 assertTrue(readedData.isPresent());
202 private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx) throws InterruptedException,
204 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
205 assertTestContainerExists(tx);