2 * Copyright (c) 2014, 2015 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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
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.ForwardingExecutorService;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.ListeningExecutorService;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import java.util.Collections;
23 import java.util.Optional;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.RejectedExecutionException;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.atomic.AtomicReference;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.mdsal.common.api.ReadFailedException;
36 import org.opendaylight.mdsal.common.api.TransactionCommitDeadlockException;
37 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
39 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
40 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
41 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
42 import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService;
43 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
48 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
50 public class DOMBrokerTest extends AbstractDatastoreTest {
52 private AbstractDOMDataBroker domBroker;
53 private ListeningExecutorService executor;
54 private ExecutorService futureExecutor;
55 private CommitExecutorService commitExecutor;
58 public void setupStore() {
59 final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
60 MoreExecutors.newDirectExecutorService());
61 final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
62 MoreExecutors.newDirectExecutorService());
64 operStore.onModelContextUpdated(SCHEMA_CONTEXT);
65 configStore.onModelContextUpdated(SCHEMA_CONTEXT);
67 final ImmutableMap<LogicalDatastoreType, DOMStore> stores =
68 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
69 .put(CONFIGURATION, configStore)
70 .put(OPERATIONAL, operStore)
73 commitExecutor = new CommitExecutorService(Executors.newSingleThreadExecutor());
74 futureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(1, 5, "FCB", DOMBrokerTest.class);
75 executor = new DeadlockDetectingListeningExecutorService(commitExecutor,
76 TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER, futureExecutor);
77 domBroker = new SerializedDOMDataBroker(stores, executor);
81 public void tearDown() {
82 if (executor != null) {
83 executor.shutdownNow();
86 if (futureExecutor != null) {
87 futureExecutor.shutdownNow();
91 @Test(timeout = 10000)
92 public void testTransactionIsolation() throws InterruptedException, ExecutionException {
93 assertNotNull(domBroker);
95 final DOMDataTreeReadTransaction readTx = domBroker.newReadOnlyTransaction();
96 assertNotNull(readTx);
98 final DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
99 assertNotNull(writeTx);
102 * Writes /test in writeTx.
105 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
108 * Reads /test from readTx Read should return Absent.
111 final ListenableFuture<Optional<NormalizedNode>> readTxContainer = readTx.read(OPERATIONAL,
112 TestModel.TEST_PATH);
113 assertFalse(readTxContainer.get().isPresent());
116 @Test(timeout = 10000)
117 public void testTransactionCommit() throws InterruptedException, ExecutionException {
118 final DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
119 assertNotNull(writeTx);
121 * Writes /test in writeTx
124 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
126 writeTx.commit().get();
128 final Optional<NormalizedNode> afterCommitRead = domBroker.newReadOnlyTransaction()
129 .read(OPERATIONAL, TestModel.TEST_PATH).get();
130 assertTrue(afterCommitRead.isPresent());
133 @Test(expected = TransactionCommitFailedException.class)
134 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
135 public void testRejectedCommit() throws Throwable {
136 commitExecutor.delegate = Mockito.mock(ExecutorService.class);
137 Mockito.doThrow(new RejectedExecutionException("mock"))
138 .when(commitExecutor.delegate).execute(Mockito.any(Runnable.class));
139 Mockito.doNothing().when(commitExecutor.delegate).shutdown();
140 Mockito.doReturn(Collections.emptyList()).when(commitExecutor.delegate).shutdownNow();
141 Mockito.doReturn("").when(commitExecutor.delegate).toString();
142 Mockito.doReturn(Boolean.TRUE).when(commitExecutor.delegate)
143 .awaitTermination(Mockito.anyLong(), Mockito.any(TimeUnit.class));
145 final DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
146 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
149 writeTx.commit().get(5, TimeUnit.SECONDS);
150 } catch (ExecutionException e) {
155 @SuppressWarnings("checkstyle:IllegalCatch")
156 AtomicReference<Throwable> submitTxAsync(final DOMDataTreeWriteTransaction writeTx) {
157 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
161 } catch (final Throwable e) {
169 @Test(expected = ReadFailedException.class)
170 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:AvoidHidingCauseException"})
171 public void basicTests() throws Throwable {
172 final DataContainerChild outerList = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
173 .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1))
175 final NormalizedNode testContainer = Builders.containerBuilder()
176 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
177 .withChild(outerList)
180 DOMDataTreeWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
181 final DOMDataTreeReadTransaction readRx = domBroker.newReadOnlyTransaction();
182 assertNotNull(writeTx);
183 assertNotNull(readRx);
184 assertNotNull(((SerializedDOMDataBroker) domBroker).getCommitStatsTracker());
186 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
187 writeTx.commit().get();
188 assertFalse(writeTx.cancel());
190 assertEquals(false, domBroker.newReadOnlyTransaction().exists(CONFIGURATION, TestModel.TEST_PATH).get());
191 assertEquals(true, domBroker.newReadOnlyTransaction().exists(OPERATIONAL, TestModel.TEST_PATH).get());
192 assertEquals(false, domBroker.newReadOnlyTransaction().exists(OPERATIONAL, TestModel.TEST2_PATH).get());
194 writeTx = domBroker.newWriteOnlyTransaction();
195 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
196 writeTx.delete(OPERATIONAL, TestModel.TEST_PATH);
197 writeTx.commit().get();
198 assertEquals(false, domBroker.newReadOnlyTransaction().exists(OPERATIONAL, TestModel.TEST_PATH).get());
199 assertTrue(domBroker.newWriteOnlyTransaction().cancel());
201 writeTx = domBroker.newWriteOnlyTransaction();
202 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
203 writeTx.merge(OPERATIONAL, TestModel.TEST_PATH, testContainer);
204 writeTx.commit().get();
205 assertEquals(Boolean.TRUE, domBroker.newReadOnlyTransaction().exists(OPERATIONAL, TestModel.TEST_PATH).get());
206 assertEquals(Boolean.TRUE, domBroker.newReadOnlyTransaction().read(OPERATIONAL, TestModel.TEST_PATH).get()
207 .get().toString().contains(testContainer.toString()));
211 //Expected exception after close call
214 readRx.read(OPERATIONAL, TestModel.TEST_PATH).get();
215 } catch (ExecutionException e) {
220 @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
222 public void closeTest() throws Exception {
223 final String testException = "TestException";
225 domBroker.setCloseable(() -> {
226 throw new Exception(testException);
231 } catch (final Exception e) {
232 assertTrue(e.getMessage().contains(testException));
236 static class CommitExecutorService extends ForwardingExecutorService {
238 ExecutorService delegate;
240 CommitExecutorService(final ExecutorService delegate) {
241 this.delegate = delegate;
245 protected ExecutorService delegate() {