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 org.opendaylight.mdsal.dom.api.DOMDataReadTransaction;
17 import org.opendaylight.mdsal.dom.api.DOMDataReadWriteTransaction;
18 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
20 import com.google.common.base.Optional;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.ListeningExecutorService;
24 import com.google.common.util.concurrent.MoreExecutors;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.TimeoutException;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
33 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
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 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
73 DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
76 * First transaction is marked as ready, we are able to allocate chained
79 ListenableFuture<Void> firstWriteTxFuture = firstTx.submit();
82 * We alocate chained transaction - read transaction.
84 DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
88 * We test if we are able to read data from tx, read should not fail
89 * since we are using chained transaction.
93 assertTestContainerExists(secondReadTx);
97 * We alocate next transaction, which is still based on first one, but
101 DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
104 * We commit first transaction
107 assertCommitSuccessful(firstWriteTxFuture);
111 * Allocates transaction from data store.
114 DOMDataReadTransaction 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 ListenableFuture<Void> thirdDeleteTxFuture = thirdDeleteTx.submit();
126 assertCommitSuccessful(thirdDeleteTxFuture);
129 * We close transaction chain.
133 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
137 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
138 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
139 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
140 assertNotNull(txChain);
143 * We alocate new read-write transaction and write /test
147 allocateAndWrite(txChain);
150 * We alocate chained transaction - read transaction, note first one is
151 * still not commited to datastore, so this allocation should fail with
152 * IllegalStateException.
155 txChain.newReadOnlyTransaction();
156 fail("Allocation of secondReadTx should fail with IllegalStateException");
157 } catch (Exception e) {
158 assertTrue(e instanceof IllegalStateException);
162 private static DOMDataReadWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
163 throws InterruptedException, ExecutionException {
164 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
167 * We test existence of /test in third transaction container should
168 * still be visible from first one (which is still uncommmited).
171 assertTestContainerExists(tx);
174 * We delete node in third transaction
176 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
180 private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
181 throws InterruptedException, ExecutionException {
182 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
183 assertTestContainerWrite(tx);
187 private static void assertCommitSuccessful(final ListenableFuture<Void> future)
188 throws InterruptedException, ExecutionException {
192 private static void assertTestContainerExists(final DOMDataReadTransaction readTx) throws InterruptedException,
194 ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
195 Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
196 assertTrue(readedData.isPresent());
199 private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx) throws InterruptedException,
201 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
202 assertTestContainerExists(tx);