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", MoreExecutors.sameThreadExecutor());
48 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
49 schemaContext = TestModel.createTestContext();
51 operStore.onGlobalContextUpdated(schemaContext);
52 configStore.onGlobalContextUpdated(schemaContext);
54 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
55 .put(CONFIGURATION, configStore) //
56 .put(OPERATIONAL, operStore) //
59 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
60 domBroker = new DOMDataBrokerImpl(stores, executor);
64 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
65 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
66 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
67 assertNotNull(txChain);
70 * We alocate new read-write transaction and write /test
74 DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
77 * First transaction is marked as ready, we are able to allocate chained
80 ListenableFuture<Void> firstWriteTxFuture = firstTx.submit();
83 * We alocate chained transaction - read transaction.
85 DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
89 * We test if we are able to read data from tx, read should not fail
90 * since we are using chained transaction.
94 assertTestContainerExists(secondReadTx);
98 * We alocate next transaction, which is still based on first one, but
102 DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
105 * We commit first transaction
108 assertCommitSuccessful(firstWriteTxFuture);
112 * Allocates transaction from data store.
115 DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
118 * We verify transaction is commited to store, container should exists
121 assertTestContainerExists(storeReadTx);
124 * third transaction is sealed and commited
126 ListenableFuture<Void> thirdDeleteTxFuture = thirdDeleteTx.submit();
127 assertCommitSuccessful(thirdDeleteTxFuture);
130 * We close transaction chain.
134 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
138 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
139 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
140 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
141 assertNotNull(txChain);
144 * We alocate new read-write transaction and write /test
148 allocateAndWrite(txChain);
151 * We alocate chained transaction - read transaction, note first one is
152 * still not commited to datastore, so this allocation should fail with
153 * IllegalStateException.
156 txChain.newReadOnlyTransaction();
157 fail("Allocation of secondReadTx should fail with IllegalStateException");
158 } catch (Exception e) {
159 assertTrue(e instanceof IllegalStateException);
163 private static DOMDataReadWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
164 throws InterruptedException, ExecutionException {
165 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
168 * We test existence of /test in third transaction container should
169 * still be visible from first one (which is still uncommmited).
172 assertTestContainerExists(tx);
175 * We delete node in third transaction
177 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
181 private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
182 throws InterruptedException, ExecutionException {
183 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
184 assertTestContainerWrite(tx);
188 private static void assertCommitSuccessful(final ListenableFuture<Void> future)
189 throws InterruptedException, ExecutionException {
193 private static void assertTestContainerExists(final DOMDataReadTransaction readTx) throws InterruptedException,
195 ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
196 Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
197 assertTrue(readedData.isPresent());
200 private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx) throws InterruptedException,
202 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
203 assertTestContainerExists(tx);