import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+
+import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.FutureCallback;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.SynchronousQueue;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
-import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
+import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
import org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeCommitCohortRegistry;
import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
private final DOMStoreThreePhaseCommitCohort mockCohort1 = mock(DOMStoreThreePhaseCommitCohort.class);
private final DOMStoreThreePhaseCommitCohort mockCohort2 = mock(DOMStoreThreePhaseCommitCohort.class);
private final ThreadPoolExecutor futureExecutor =
- new ThreadPoolExecutor(0, 1, 5, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
+ new ThreadPoolExecutor(0, 1, 5, TimeUnit.SECONDS, new SynchronousQueue<>());
private ConcurrentDOMDataBroker coordinator;
@Before
DOMStore store = new InMemoryDOMDataStore("OPER",
MoreExecutors.newDirectExecutorService());
- coordinator = new ConcurrentDOMDataBroker(ImmutableMap.of(LogicalDatastoreType.OPERATIONAL, store), futureExecutor);
+ coordinator = new ConcurrentDOMDataBroker(ImmutableMap.of(LogicalDatastoreType.OPERATIONAL, store),
+ futureExecutor);
}
@After
}
@Test
- public void testSuccessfulSubmitAsync() throws Throwable {
+ public void testSuccessfulSubmitAsync() throws Exception {
testSuccessfulSubmit(true);
}
@Test
- public void testSuccessfulSubmitSync() throws Throwable {
+ public void testSuccessfulSubmitSync() throws Exception {
testSuccessfulSubmit(false);
}
- private void testSuccessfulSubmit(final boolean doAsync) throws Throwable {
+ private void testSuccessfulSubmit(final boolean doAsync) throws InterruptedException {
final CountDownLatch asyncCanCommitContinue = new CountDownLatch(1);
- Answer<ListenableFuture<Boolean>> asyncCanCommit = new Answer<ListenableFuture<Boolean>>() {
- @Override
- public ListenableFuture<Boolean> answer(final InvocationOnMock invocation) {
- final SettableFuture<Boolean> future = SettableFuture.create();
- if(doAsync) {
- new Thread() {
- @Override
- public void run() {
- Uninterruptibles.awaitUninterruptibly(asyncCanCommitContinue,
- 10, TimeUnit.SECONDS);
- future.set(true);
- }
- }.start();
- } else {
+ Answer<ListenableFuture<Boolean>> asyncCanCommit = invocation -> {
+ final SettableFuture<Boolean> future = SettableFuture.create();
+ if (doAsync) {
+ new Thread(() -> {
+ Uninterruptibles.awaitUninterruptibly(asyncCanCommitContinue,
+ 10, TimeUnit.SECONDS);
future.set(true);
- }
-
- return future;
+ }).start();
+ } else {
+ future.set(true);
}
+
+ return future;
};
doAnswer(asyncCanCommit).when(mockCohort1).canCommit();
}
@Override
- public void onFailure(final Throwable t) {
- caughtEx.set(t);
+ public void onFailure(final Throwable failure) {
+ caughtEx.set(failure);
doneLatch.countDown();
}
});
assertEquals("Submit complete", true, doneLatch.await(5, TimeUnit.SECONDS));
- if(caughtEx.get() != null) {
- throw caughtEx.get();
+ if (caughtEx.get() != null) {
+ Throwables.propagate(caughtEx.get());
}
assertEquals("Task count", doAsync ? 1 : 0, futureExecutor.getTaskCount());
future.checkedGet(5, TimeUnit.SECONDS);
fail("Expected TransactionCommitFailedException");
} catch (TransactionCommitFailedException e) {
- if(expCause != null) {
+ if (expCause != null) {
assertSame("Expected cause", expCause.getClass(), e.getCause().getClass());
}
InOrder inOrder = inOrder((Object[])mockCohorts);
- for(DOMStoreThreePhaseCommitCohort c: mockCohorts) {
+ for (DOMStoreThreePhaseCommitCohort c: mockCohorts) {
inOrder.verify(c).abort();
}
} catch (TimeoutException e) {
DOMStoreThreePhaseCommitCohort mockCohort3 = mock(DOMStoreThreePhaseCommitCohort.class);
doReturn(Futures.immediateFuture(true)).when(mockCohort3).canCommit();
- doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock2"))).
- when(mockCohort3).preCommit();
+ doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock2")))
+ .when(mockCohort3).preCommit();
doReturn(Futures.immediateFuture(null)).when(mockCohort3).abort();
CheckedFuture<Void, TransactionCommitFailedException> future = coordinator.submit(
DOMStoreThreePhaseCommitCohort mockCohort3 = mock(DOMStoreThreePhaseCommitCohort.class);
doReturn(Futures.immediateFuture(true)).when(mockCohort3).canCommit();
doReturn(Futures.immediateFuture(null)).when(mockCohort3).preCommit();
- doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock2"))).
- when(mockCohort3).commit();
+ doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock2")))
+ .when(mockCohort3).commit();
doReturn(Futures.immediateFuture(null)).when(mockCohort3).abort();
CheckedFuture<Void, TransactionCommitFailedException> future = coordinator.submit(
@Test
public void testSubmitWithAbortException() throws Exception {
doReturn(Futures.immediateFuture(true)).when(mockCohort1).canCommit();
- doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock abort error"))).
- when(mockCohort1).abort();
+ doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock abort error")))
+ .when(mockCohort1).abort();
IllegalStateException cause = new IllegalStateException("mock canCommit error");
doReturn(Futures.immediateFailedFuture(cause)).when(mockCohort2).canCommit();
}
@Test
- public void testCreateReadWriteTransaction(){
+ public void testCreateReadWriteTransaction() {
DOMStore domStore = mock(DOMStore.class);
try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(
LogicalDatastoreType.OPERATIONAL, domStore, LogicalDatastoreType.CONFIGURATION, domStore),
}
@Test
- public void testCreateWriteOnlyTransaction(){
+ public void testCreateWriteOnlyTransaction() {
DOMStore domStore = mock(DOMStore.class);
try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(
LogicalDatastoreType.OPERATIONAL, domStore, LogicalDatastoreType.CONFIGURATION, domStore),
}
@Test
- public void testCreateReadOnlyTransaction(){
+ public void testCreateReadOnlyTransaction() {
DOMStore domStore = mock(DOMStore.class);
try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(
LogicalDatastoreType.OPERATIONAL, domStore, LogicalDatastoreType.CONFIGURATION, domStore),
}
@Test
- public void testLazySubTransactionCreationForReadWriteTransactions(){
+ public void testLazySubTransactionCreationForReadWriteTransactions() {
DOMStore configDomStore = mock(DOMStore.class);
DOMStore operationalDomStore = mock(DOMStore.class);
DOMStoreReadWriteTransaction storeTxn = mock(DOMStoreReadWriteTransaction.class);
}
@Test
- public void testLazySubTransactionCreationForWriteOnlyTransactions(){
+ public void testLazySubTransactionCreationForWriteOnlyTransactions() {
DOMStore configDomStore = mock(DOMStore.class);
DOMStore operationalDomStore = mock(DOMStore.class);
DOMStoreWriteTransaction storeTxn = mock(DOMStoreWriteTransaction.class);
}
@Test
- public void testLazySubTransactionCreationForReadOnlyTransactions(){
+ public void testLazySubTransactionCreationForReadOnlyTransactions() {
DOMStore configDomStore = mock(DOMStore.class);
DOMStore operationalDomStore = mock(DOMStore.class);
DOMStoreReadTransaction storeTxn = mock(DOMStoreReadTransaction.class);
LogicalDatastoreType.OPERATIONAL, operationalDomStore, LogicalDatastoreType.CONFIGURATION,
configDomStore), futureExecutor) {
@Override
- public CheckedFuture<Void, TransactionCommitFailedException> submit(DOMDataWriteTransaction transaction, Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
+ public CheckedFuture<Void, TransactionCommitFailedException> submit(DOMDataWriteTransaction writeTx,
+ Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
commitCohorts.addAll(cohorts);
latch.countDown();
- return super.submit(transaction, cohorts);
+ return super.submit(writeTx, cohorts);
}
}) {
DOMDataReadWriteTransaction domDataReadWriteTransaction = dataBroker.newReadWriteTransaction();
domDataReadWriteTransaction.submit();
- latch.await(10, TimeUnit.SECONDS);
+ assertTrue(latch.await(10, TimeUnit.SECONDS));
assertTrue(commitCohorts.size() == 1);
}
LogicalDatastoreType.OPERATIONAL, operationalDomStore, LogicalDatastoreType.CONFIGURATION,
configDomStore), futureExecutor) {
@Override
- public CheckedFuture<Void, TransactionCommitFailedException> submit(DOMDataWriteTransaction transaction, Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
+ public CheckedFuture<Void, TransactionCommitFailedException> submit(DOMDataWriteTransaction transaction,
+ Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
commitCohorts.addAll(cohorts);
latch.countDown();
return super.submit(transaction, cohorts);
}) {
DOMDataReadWriteTransaction domDataReadWriteTransaction = dataBroker.newReadWriteTransaction();
- domDataReadWriteTransaction.put(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY, mock(NormalizedNode.class));
- domDataReadWriteTransaction.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY, mock(NormalizedNode.class));
+ domDataReadWriteTransaction.put(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY,
+ mock(NormalizedNode.class));
+ domDataReadWriteTransaction.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY,
+ mock(NormalizedNode.class));
domDataReadWriteTransaction.submit();
- latch.await(10, TimeUnit.SECONDS);
+ assertTrue(latch.await(10, TimeUnit.SECONDS));
assertTrue(commitCohorts.size() == 2);
}
}
@Test
- public void testCreateTransactionChain(){
+ public void testCreateTransactionChain() {
DOMStore domStore = mock(DOMStore.class);
try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(
LogicalDatastoreType.OPERATIONAL, domStore, LogicalDatastoreType.CONFIGURATION, domStore),
}
@Test
- public void testCreateTransactionOnChain(){
+ public void testCreateTransactionOnChain() {
DOMStore domStore = mock(DOMStore.class);
try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(
LogicalDatastoreType.OPERATIONAL, domStore, LogicalDatastoreType.CONFIGURATION, domStore),
doReturn(mockChain).when(domStore).createTransactionChain();
doReturn(operationalTransaction).when(mockChain).newWriteOnlyTransaction();
- DOMTransactionChain transactionChain = dataBroker.createTransactionChain(mock(TransactionChainListener.class));
+ DOMTransactionChain transactionChain = dataBroker.createTransactionChain(
+ mock(TransactionChainListener.class));
DOMDataWriteTransaction domDataWriteTransaction = transactionChain.newWriteOnlyTransaction();
verify(mockChain, never()).newWriteOnlyTransaction();
- domDataWriteTransaction.put(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY, mock(NormalizedNode.class));
+ domDataWriteTransaction.put(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY,
+ mock(NormalizedNode.class));
}
}
LogicalDatastoreType.OPERATIONAL, domStore, LogicalDatastoreType.CONFIGURATION, domStore),
futureExecutor)) {
- CheckedFuture<Void, TransactionCommitFailedException> submit1 = dataBroker.newWriteOnlyTransaction().submit();
+ CheckedFuture<Void, TransactionCommitFailedException> submit1 =
+ dataBroker.newWriteOnlyTransaction().submit();
assertNotNull(submit1);
submit1.get();
- CheckedFuture<Void, TransactionCommitFailedException> submit2 = dataBroker.newReadWriteTransaction().submit();
+ CheckedFuture<Void, TransactionCommitFailedException> submit2 =
+ dataBroker.newReadWriteTransaction().submit();
assertNotNull(submit2);
}
}
+ @Test
+ public void testExtensions() {
+ DistributedDataStore mockConfigStore = mock(DistributedDataStore.class);
+ DistributedDataStore mockOperStore = mock(DistributedDataStore.class);
+ try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(
+ LogicalDatastoreType.OPERATIONAL, mockOperStore,
+ LogicalDatastoreType.CONFIGURATION, mockConfigStore), futureExecutor)) {
+
+ Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> supportedExtensions =
+ dataBroker.getSupportedExtensions();
+ assertNotNull(supportedExtensions.get(DOMDataTreeChangeService.class));
+
+ DOMDataTreeCommitCohortRegistry cohortRegistry =
+ (DOMDataTreeCommitCohortRegistry) supportedExtensions.get(DOMDataTreeCommitCohortRegistry.class);
+ assertNotNull(cohortRegistry);
+
+ DOMDataTreeCommitCohort mockCohort = mock(DOMDataTreeCommitCohort.class);
+ DOMDataTreeIdentifier path = new DOMDataTreeIdentifier(
+ org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
+ YangInstanceIdentifier.EMPTY);
+ cohortRegistry.registerCommitCohort(path, mockCohort);
+
+ verify(mockConfigStore).registerCommitCohort(path, mockCohort);
+ }
+ }
}