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.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
39 public class DOMTransactionChainTest extends AbstractDatastoreTest {
41 private AbstractDOMDataBroker domBroker;
44 public void setupStore() {
45 final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
46 MoreExecutors.newDirectExecutorService());
47 final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
48 MoreExecutors.newDirectExecutorService());
50 operStore.onModelContextUpdated(SCHEMA_CONTEXT);
51 configStore.onModelContextUpdated(SCHEMA_CONTEXT);
53 final ImmutableMap<LogicalDatastoreType, DOMStore> stores =
54 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
55 .put(CONFIGURATION, configStore)
56 .put(OPERATIONAL, operStore)
59 final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
60 Executors.newSingleThreadExecutor());
61 domBroker = new SerializedDOMDataBroker(stores, executor);
65 public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
66 final DOMTransactionChain txChain = domBroker.createTransactionChain();
67 assertNotNull(txChain);
70 * We allocate new read-write transaction and write /test.
72 final DOMDataTreeWriteTransaction firstTx = allocateAndWrite(txChain);
75 * First transaction is marked as ready, we are able to allocate chained
78 final ListenableFuture<? extends CommitInfo> firstWriteTxFuture = firstTx.commit();
81 * We allocate chained transaction - read transaction.
83 final DOMDataTreeReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
87 * We test if we are able to read data from tx, read should not fail
88 * since we are using chained transaction.
92 assertTestContainerExists(secondReadTx);
95 * We allocate next transaction, which is still based on first one, but
99 final DOMDataTreeWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
102 * We commit first transaction
105 assertCommitSuccessful(firstWriteTxFuture);
108 * Allocates transaction from data store.
111 final DOMDataTreeReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
114 * We verify transaction is committed to store, container should exist
117 assertTestContainerExists(storeReadTx);
120 * third transaction is sealed and committed.
122 assertCommitSuccessful(thirdDeleteTx.commit());
125 * We close transaction chain.
129 txChain.future().get(1000, TimeUnit.MILLISECONDS);
133 public void testTransactionChainNotSealed() {
134 final var txChain = domBroker.createTransactionChain();
135 assertNotNull(txChain);
138 * We allocate new read-write transaction and write /test
140 allocateAndWrite(txChain);
143 * We allocate chained transaction - read transaction, note first one is
144 * still not committed to datastore, so this allocation should fail with
145 * IllegalStateException.
147 // actual backing tx allocation happens on put
148 final var ex = assertThrows(IllegalStateException.class, () -> allocateAndWrite(txChain));
149 assertEquals("Previous transaction OPER-0 is not ready yet", ex.getMessage());
152 private static DOMDataTreeWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
153 throws InterruptedException, ExecutionException {
154 final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
156 * We delete node in third transaction
158 tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
162 private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain) {
163 final var tx = txChain.newWriteOnlyTransaction();
164 writeTestContainer(tx);
168 private static void assertCommitSuccessful(final ListenableFuture<? extends CommitInfo> firstWriteTxFuture)
169 throws InterruptedException, ExecutionException {
170 firstWriteTxFuture.get();
173 private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx)
174 throws InterruptedException, ExecutionException {
175 final ListenableFuture<Optional<NormalizedNode>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
176 final Optional<NormalizedNode> readedData = readFuture.get();
177 assertTrue(readedData.isPresent());
180 private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) {
181 tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
182 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))