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;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
15 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
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.Optional;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.common.api.CommitInfo;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
32 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
33 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
34 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
38 public class DOMTransactionChainTest extends AbstractDatastoreTest {
40 private AbstractDOMDataBroker domBroker;
43 public void setupStore() {
44 final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
45 MoreExecutors.newDirectExecutorService());
46 final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
47 MoreExecutors.newDirectExecutorService());
49 operStore.onModelContextUpdated(SCHEMA_CONTEXT);
50 configStore.onModelContextUpdated(SCHEMA_CONTEXT);
52 final ImmutableMap<LogicalDatastoreType, DOMStore> stores =
53 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
54 .put(CONFIGURATION, configStore)
55 .put(OPERATIONAL, operStore)
58 final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
59 Executors.newSingleThreadExecutor());
60 domBroker = new SerializedDOMDataBroker(stores, executor);
64 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
65 final DOMTransactionChain txChain = domBroker.createTransactionChain();
66 assertNotNull(txChain);
69 * We allocate new read-write transaction and write /test.
71 final DOMDataTreeWriteTransaction firstTx = allocateAndWrite(txChain);
74 * First transaction is marked as ready, we are able to allocate chained
77 final ListenableFuture<? extends CommitInfo> firstWriteTxFuture = firstTx.commit();
80 * We allocate chained transaction - read transaction.
82 final DOMDataTreeReadTransaction 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);
94 * We allocate next transaction, which is still based on first one, but
98 final DOMDataTreeWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
101 * We commit first transaction
104 assertCommitSuccessful(firstWriteTxFuture);
107 * Allocates transaction from data store.
110 final DOMDataTreeReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
113 * We verify transaction is committed to store, container should exist
116 assertTestContainerExists(storeReadTx);
119 * third transaction is sealed and committed.
121 assertCommitSuccessful(thirdDeleteTx.commit());
124 * We close transaction chain.
128 txChain.future().get(1000, TimeUnit.MILLISECONDS);
132 public void testTransactionChainNotSealed() {
133 final var txChain = domBroker.createTransactionChain();
134 assertNotNull(txChain);
137 * We allocate new read-write transaction and write /test
139 allocateAndWrite(txChain);
142 * We allocate chained transaction - read transaction, note first one is
143 * still not committed to datastore, so this allocation should fail with
144 * IllegalStateException.
146 // actual backing tx allocation happens on put
147 final var ex = assertThrows(IllegalStateException.class, () -> allocateAndWrite(txChain));
148 assertEquals("Previous transaction OPER-0 is not ready yet", ex.getMessage());
151 private static DOMDataTreeWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
152 throws InterruptedException, ExecutionException {
153 final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
155 * We delete node in third transaction
157 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
161 private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain) {
162 final var tx = txChain.newWriteOnlyTransaction();
163 writeTestContainer(tx);
167 private static void assertCommitSuccessful(final ListenableFuture<? extends CommitInfo> firstWriteTxFuture)
168 throws InterruptedException, ExecutionException {
169 firstWriteTxFuture.get();
172 private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx)
173 throws InterruptedException, ExecutionException {
174 final ListenableFuture<Optional<NormalizedNode>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
175 final Optional<NormalizedNode> readedData = readFuture.get();
176 assertTrue(readedData.isPresent());
179 private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) {
180 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));