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 com.google.common.base.Optional;
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.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.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
31 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
32 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 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 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
47 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
48 schemaContext = TestModel.createTestContext();
50 operStore.onGlobalContextUpdated(schemaContext);
51 configStore.onGlobalContextUpdated(schemaContext);
53 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder() //
54 .put(CONFIGURATION, configStore) //
55 .put(OPERATIONAL, operStore) //
58 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
59 domBroker = new SerializedDOMDataBroker(stores, executor);
63 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
64 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
65 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
66 assertNotNull(txChain);
69 * We alocate new read-write transaction and write /test.
71 DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
74 * First transaction is marked as ready, we are able to allocate chained
77 ListenableFuture<?> firstWriteTxFuture = firstTx.commit();
80 * We alocate chained transaction - read transaction.
82 DOMDataReadTransaction 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.
89 assertTestContainerExists(secondReadTx);
92 * We alocate next transaction, which is still based on first one, but
96 DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
99 * We commit first transaction.
102 assertCommitSuccessful(firstWriteTxFuture);
105 * Allocates transaction from data store.
107 DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
110 * We verify transaction is commited to store, container should exists
113 assertTestContainerExists(storeReadTx);
116 * third transaction is sealed and commited.
118 ListenableFuture<?> thirdDeleteTxFuture = thirdDeleteTx.commit();
119 assertCommitSuccessful(thirdDeleteTxFuture);
122 * We close transaction chain.
126 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
130 @SuppressWarnings("checkstyle:IllegalCatch")
131 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException {
132 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
133 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
134 assertNotNull(txChain);
137 * We alocate new read-write transaction and write /test
139 allocateAndWrite(txChain);
142 * We alocate chained transaction - read transaction, note first one is
143 * still not commited to datastore, so this allocation should fail with
144 * IllegalStateException.
147 txChain.newReadOnlyTransaction();
148 fail("Allocation of secondReadTx should fail with IllegalStateException");
149 } catch (Exception e) {
150 assertTrue(e instanceof IllegalStateException);
154 private static DOMDataReadWriteTransaction allocateAndDelete(
155 final DOMTransactionChain txChain) throws InterruptedException, ExecutionException {
156 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
159 * We test existence of /test in third transaction container should
160 * still be visible from first one (which is still uncommmited).
163 assertTestContainerExists(tx);
166 * We delete node in third transaction
168 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
172 private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
173 throws InterruptedException, ExecutionException {
174 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
175 assertTestContainerWrite(tx);
179 private static void assertCommitSuccessful(final ListenableFuture<?> future)
180 throws InterruptedException, ExecutionException {
184 private static void assertTestContainerExists(final DOMDataReadTransaction readTx)
185 throws InterruptedException, ExecutionException {
186 ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
187 Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
188 assertTrue(readedData.isPresent());
191 private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx)
192 throws InterruptedException, ExecutionException {
193 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
194 assertTestContainerExists(tx);