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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
15 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
29 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
30 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
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 import com.google.common.base.Optional;
38 import com.google.common.collect.ImmutableMap;
39 import com.google.common.util.concurrent.ListenableFuture;
40 import com.google.common.util.concurrent.ListeningExecutorService;
41 import com.google.common.util.concurrent.MoreExecutors;
43 public class DOMTransactionChainTest {
45 private SchemaContext schemaContext;
46 private DOMDataBrokerImpl domBroker;
49 public void setupStore() {
50 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
51 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
52 schemaContext = TestModel.createTestContext();
54 operStore.onGlobalContextUpdated(schemaContext);
55 configStore.onGlobalContextUpdated(schemaContext);
57 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
58 .put(CONFIGURATION, configStore) //
59 .put(OPERATIONAL, operStore) //
62 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
63 domBroker = new DOMDataBrokerImpl(stores, executor);
67 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
68 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
69 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
70 assertNotNull(txChain);
73 * We alocate new read-write transaction and write /test
77 DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
80 * First transaction is marked as ready, we are able to allocate chained
83 ListenableFuture<RpcResult<TransactionStatus>> firstWriteTxFuture = firstTx.commit();
86 * We alocate chained transaction - read transaction.
88 DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
92 * We test if we are able to read data from tx, read should not fail
93 * since we are using chained transaction.
97 assertTestContainerExists(secondReadTx);
101 * We alocate next transaction, which is still based on first one, but
105 DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
108 * We commit first transaction
111 assertCommitSuccessful(firstWriteTxFuture);
115 * Allocates transaction from data store.
118 DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
121 * We verify transaction is commited to store, container should exists
124 assertTestContainerExists(storeReadTx);
127 * third transaction is sealed and commited
129 ListenableFuture<RpcResult<TransactionStatus>> thirdDeleteTxFuture = thirdDeleteTx.commit();
130 assertCommitSuccessful(thirdDeleteTxFuture);
133 * We close transaction chain.
137 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
141 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
142 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
143 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
144 assertNotNull(txChain);
147 * We alocate new read-write transaction and write /test
151 allocateAndWrite(txChain);
154 * We alocate chained transaction - read transaction, note first one is
155 * still not commited to datastore, so this allocation should fail with
156 * IllegalStateException.
159 txChain.newReadOnlyTransaction();
160 fail("Allocation of secondReadTx should fail with IllegalStateException");
161 } catch (Exception e) {
162 assertTrue(e instanceof IllegalStateException);
166 private static DOMDataReadWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
167 throws InterruptedException, ExecutionException {
168 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
171 * We test existence of /test in third transaction container should
172 * still be visible from first one (which is still uncommmited).
175 assertTestContainerExists(tx);
178 * We delete node in third transaction
180 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
184 private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
185 throws InterruptedException, ExecutionException {
186 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
187 assertTestContainerWrite(tx);
191 private static void assertCommitSuccessful(final ListenableFuture<RpcResult<TransactionStatus>> future)
192 throws InterruptedException, ExecutionException {
193 RpcResult<TransactionStatus> rpcResult = future.get();
194 assertTrue(rpcResult.isSuccessful());
195 assertEquals(TransactionStatus.COMMITED, rpcResult.getResult());
198 private static void assertTestContainerExists(final DOMDataReadTransaction readTx) throws InterruptedException,
200 ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
201 Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
202 assertTrue(readedData.isPresent());
205 private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx) throws InterruptedException,
207 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
208 assertTestContainerExists(tx);