1 package org.opendaylight.controller.md.sal.dom.broker.impl;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
7 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
9 import java.util.concurrent.ExecutionException;
10 import java.util.concurrent.Executors;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
18 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import com.google.common.base.Optional;
25 import com.google.common.collect.ImmutableMap;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.ListeningExecutorService;
28 import com.google.common.util.concurrent.MoreExecutors;
30 public class DOMBrokerTest {
32 private SchemaContext schemaContext;
33 private DOMDataBrokerImpl domBroker;
36 public void setupStore() {
37 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
38 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
39 schemaContext = TestModel.createTestContext();
41 operStore.onGlobalContextUpdated(schemaContext);
42 configStore.onGlobalContextUpdated(schemaContext);
44 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
45 .put(CONFIGURATION, configStore) //
46 .put(OPERATIONAL, operStore) //
49 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
50 domBroker = new DOMDataBrokerImpl(stores, executor);
54 public void testTransactionIsolation() throws InterruptedException, ExecutionException {
56 assertNotNull(domBroker);
58 DOMDataReadTransaction readTx = domBroker.newReadOnlyTransaction();
59 assertNotNull(readTx);
61 DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
62 assertNotNull(writeTx);
65 * Writes /test in writeTx
68 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
72 * Reads /test from writeTx Read should return container.
75 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
77 assertTrue(writeTxContainer.get().isPresent());
81 * Reads /test from readTx Read should return Absent.
84 ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx
85 .read(OPERATIONAL, TestModel.TEST_PATH);
86 assertFalse(readTxContainer.get().isPresent());
90 public void testTransactionCommit() throws InterruptedException, ExecutionException {
92 DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
93 assertNotNull(writeTx);
96 * Writes /test in writeTx
99 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
103 * Reads /test from writeTx Read should return container.
106 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
107 TestModel.TEST_PATH);
108 assertTrue(writeTxContainer.get().isPresent());
110 writeTx.commit().get();
112 Optional<NormalizedNode<?, ?>> afterCommitRead = domBroker.newReadOnlyTransaction()
113 .read(OPERATIONAL, TestModel.TEST_PATH).get();
114 assertTrue(afterCommitRead.isPresent());