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 com.google.common.base.Optional;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.ListeningExecutorService;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
31 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
32 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 public class DOMTransactionChainTest {
40 private SchemaContext schemaContext;
41 private AbstractDOMDataBroker domBroker;
44 public void setupStore() {
45 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
46 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
47 schemaContext = TestModel.createTestContext();
49 operStore.onGlobalContextUpdated(schemaContext);
50 configStore.onGlobalContextUpdated(schemaContext);
52 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder() //
53 .put(CONFIGURATION, configStore) //
54 .put(OPERATIONAL, operStore) //
57 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
58 domBroker = new SerializedDOMDataBroker(stores, executor);
62 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
63 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
64 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
65 assertNotNull(txChain);
68 * We alocate new read-write transaction and write /test.
70 DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
73 * First transaction is marked as ready, we are able to allocate chained
76 ListenableFuture<?> firstWriteTxFuture = firstTx.commit();
79 * We alocate chained transaction - read transaction.
81 DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
85 * We test if we are able to read data from tx, read should not fail
86 * since we are using chained transaction.
88 assertTestContainerExists(secondReadTx);
91 * We alocate next transaction, which is still based on first one, but
95 DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
98 * We commit first transaction.
101 assertCommitSuccessful(firstWriteTxFuture);
104 * Allocates transaction from data store.
106 DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
109 * We verify transaction is commited to store, container should exists
112 assertTestContainerExists(storeReadTx);
115 * third transaction is sealed and commited.
117 ListenableFuture<?> thirdDeleteTxFuture = thirdDeleteTx.commit();
118 assertCommitSuccessful(thirdDeleteTxFuture);
121 * We close transaction chain.
125 listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
129 @SuppressWarnings("checkstyle:IllegalCatch")
130 public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException {
131 BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
132 DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
133 assertNotNull(txChain);
136 * We alocate new read-write transaction and write /test
138 allocateAndWrite(txChain);
141 * We alocate chained transaction - read transaction, note first one is
142 * still not commited to datastore, so this allocation should fail with
143 * IllegalStateException.
146 txChain.newReadOnlyTransaction();
147 fail("Allocation of secondReadTx should fail with IllegalStateException");
148 } catch (Exception e) {
149 assertTrue(e instanceof IllegalStateException);
153 private static DOMDataReadWriteTransaction allocateAndDelete(
154 final DOMTransactionChain txChain) throws InterruptedException, ExecutionException {
155 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
158 * We test existence of /test in third transaction container should
159 * still be visible from first one (which is still uncommmited).
162 assertTestContainerExists(tx);
165 * We delete node in third transaction
167 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
171 private static DOMDataReadWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
172 throws InterruptedException, ExecutionException {
173 DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
174 assertTestContainerWrite(tx);
178 private static void assertCommitSuccessful(final ListenableFuture<?> future)
179 throws InterruptedException, ExecutionException {
183 private static void assertTestContainerExists(final DOMDataReadTransaction readTx)
184 throws InterruptedException, ExecutionException {
185 ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
186 Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
187 assertTrue(readedData.isPresent());
190 private static void assertTestContainerWrite(final DOMDataReadWriteTransaction tx)
191 throws InterruptedException, ExecutionException {
192 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
193 assertTestContainerExists(tx);