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;
17 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
18 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.dom.broker.AbstractDOMDataBroker;
21 import org.opendaylight.mdsal.dom.broker.SerializedDOMDataBroker;
22 import org.opendaylight.mdsal.dom.api.DOMDataReadTransaction;
23 import org.opendaylight.mdsal.dom.api.DOMDataWriteTransaction;
24 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;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.TimeoutException;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 public class DOMTransactionChainTest {
45 private SchemaContext schemaContext;
46 private AbstractDOMDataBroker domBroker;
49 public void setupStore() {
50 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
51 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
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 SerializedDOMDataBroker(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 DOMDataWriteTransaction firstTx = allocateAndWrite(txChain);
80 * First transaction is marked as ready, we are able to allocate chained
83 ListenableFuture<Void> firstWriteTxFuture = firstTx.submit();
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 DOMDataWriteTransaction 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<Void> thirdDeleteTxFuture = thirdDeleteTx.submit();
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 DOMDataWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
167 throws InterruptedException, ExecutionException {
168 DOMDataWriteTransaction tx = txChain.newWriteOnlyTransaction();
170 * We delete node in third transaction
172 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
176 private static DOMDataWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
177 throws InterruptedException, ExecutionException {
178 DOMDataWriteTransaction tx = txChain.newWriteOnlyTransaction();
179 writeTestContainer(tx);
183 private static void assertCommitSuccessful(final ListenableFuture<Void> future)
184 throws InterruptedException, ExecutionException {
188 private static void assertTestContainerExists(final DOMDataReadTransaction readTx) throws InterruptedException,
190 ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
191 Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
192 assertTrue(readedData.isPresent());
195 private static void writeTestContainer(final DOMDataWriteTransaction tx) throws InterruptedException,
197 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));