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.test;
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 org.opendaylight.mdsal.dom.broker.test.util.TestModel;
18 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.broker.AbstractDOMDataBroker;
22 import org.opendaylight.mdsal.dom.broker.SerializedDOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMDataReadTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMDataReadWriteTransaction;
25 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
26 import com.google.common.base.Optional;
27 import com.google.common.collect.ImmutableMap;
28 import com.google.common.util.concurrent.ListenableFuture;
29 import com.google.common.util.concurrent.ListeningExecutorService;
30 import com.google.common.util.concurrent.MoreExecutors;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.TimeoutException;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 public class DOMTransactionChainTest {
43 private SchemaContext schemaContext;
44 private AbstractDOMDataBroker domBroker;
47 public void setupStore() {
48 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
49 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
50 schemaContext = TestModel.createTestContext();
52 operStore.onGlobalContextUpdated(schemaContext);
53 configStore.onGlobalContextUpdated(schemaContext);
55 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
56 .put(CONFIGURATION, configStore) //
57 .put(OPERATIONAL, operStore) //
60 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
61 domBroker = new SerializedDOMDataBroker(stores, executor);
65 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
66 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
67 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
68 assertNotNull(txChain);
71 * We alocate new read-write transaction and write /test
75 DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
78 * First transaction is marked as ready, we are able to allocate chained
81 ListenableFuture<Void> firstWriteTxFuture = firstTx.submit();
84 * We alocate chained transaction - read transaction.
86 DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
90 * We test if we are able to read data from tx, read should not fail
91 * since we are using chained transaction.
95 assertTestContainerExists(secondReadTx);
99 * We alocate next transaction, which is still based on first one, but
103 DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
106 * We commit first transaction
109 assertCommitSuccessful(firstWriteTxFuture);
113 * Allocates transaction from data store.
116 DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
119 * We verify transaction is commited to store, container should exists
122 assertTestContainerExists(storeReadTx);
125 * third transaction is sealed and commited
127 ListenableFuture<Void> thirdDeleteTxFuture = thirdDeleteTx.submit();
128 assertCommitSuccessful(thirdDeleteTxFuture);
131 * We close transaction chain.
135 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
139 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
140 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
141 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
142 assertNotNull(txChain);
145 * We alocate new read-write transaction and write /test
149 allocateAndWrite(txChain);
152 * We alocate chained transaction - read transaction, note first one is
153 * still not commited to datastore, so this allocation should fail with
154 * IllegalStateException.
157 txChain.newReadOnlyTransaction();
158 fail("Allocation of secondReadTx should fail with IllegalStateException");
159 } catch (Exception e) {
160 assertTrue(e instanceof IllegalStateException);
164 private static DOMDataReadWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
165 throws InterruptedException, ExecutionException {
166 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
169 * We test existence of /test in third transaction container should
170 * still be visible from first one (which is still uncommmited).
173 assertTestContainerExists(tx);
176 * We delete node in third transaction
178 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
182 private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
183 throws InterruptedException, ExecutionException {
184 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
185 assertTestContainerWrite(tx);
189 private static void assertCommitSuccessful(final ListenableFuture<Void> future)
190 throws InterruptedException, ExecutionException {
194 private static void assertTestContainerExists(final DOMDataReadTransaction readTx) throws InterruptedException,
196 ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
197 Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
198 assertTrue(readedData.isPresent());
201 private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx) throws InterruptedException,
203 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
204 assertTestContainerExists(tx);