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;
15 import com.google.common.base.Optional;
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.concurrent.ExecutionException;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
30 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
31 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 public class DOMTransactionChainTest {
39 private SchemaContext schemaContext;
40 private AbstractDOMDataBroker domBroker;
43 public void setupStore() {
44 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
45 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
46 schemaContext = TestModel.createTestContext();
48 operStore.onGlobalContextUpdated(schemaContext);
49 configStore.onGlobalContextUpdated(schemaContext);
51 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
52 .put(CONFIGURATION, configStore) //
53 .put(OPERATIONAL, operStore) //
56 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
57 domBroker = new SerializedDOMDataBroker(stores, executor);
61 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
62 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
63 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
64 assertNotNull(txChain);
67 * 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<Void> firstWriteTxFuture = firstTx.submit();
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.
91 assertTestContainerExists(secondReadTx);
95 * We alocate next transaction, which is still based on first one, but
99 DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
102 * We commit first transaction
105 assertCommitSuccessful(firstWriteTxFuture);
109 * Allocates transaction from data store.
112 DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
115 * We verify transaction is commited to store, container should exists
118 assertTestContainerExists(storeReadTx);
121 * third transaction is sealed and commited
123 ListenableFuture<Void> thirdDeleteTxFuture = thirdDeleteTx.submit();
124 assertCommitSuccessful(thirdDeleteTxFuture);
127 * We close transaction chain.
131 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
135 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
136 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
137 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
138 assertNotNull(txChain);
141 * We alocate new read-write transaction and write /test
145 allocateAndWrite(txChain);
148 * We alocate chained transaction - read transaction, note first one is
149 * still not commited to datastore, so this allocation should fail with
150 * IllegalStateException.
153 txChain.newReadOnlyTransaction();
154 fail("Allocation of secondReadTx should fail with IllegalStateException");
155 } catch (Exception e) {
156 assertTrue(e instanceof IllegalStateException);
160 private static DOMDataReadWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
161 throws InterruptedException, ExecutionException {
162 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
165 * We test existence of /test in third transaction container should
166 * still be visible from first one (which is still uncommmited).
169 assertTestContainerExists(tx);
172 * We delete node in third transaction
174 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
178 private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
179 throws InterruptedException, ExecutionException {
180 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
181 assertTestContainerWrite(tx);
185 private static void assertCommitSuccessful(final ListenableFuture<Void> future)
186 throws InterruptedException, ExecutionException {
190 private static void assertTestContainerExists(final DOMDataReadTransaction readTx) throws InterruptedException,
192 ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
193 Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
194 assertTrue(readedData.isPresent());
197 private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx) throws InterruptedException,
199 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
200 assertTestContainerExists(tx);