Cleanup ShardedDOMTransactionChainAdapter
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / ShardedDOMTransactionChainAdapter.java
index 99dece31fe77947ccd9d5d72392118080ed03df9..8ffa21b232ff256f5fae4a06f6c75ca920195cb8 100644 (file)
@@ -5,23 +5,22 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.mdsal.dom.broker;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.ClassToInstanceMap;
+import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.EnumMap;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicLong;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-import org.opendaylight.mdsal.common.api.AsyncTransaction;
+import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.mdsal.common.api.TransactionChainListener;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
@@ -29,9 +28,13 @@ import org.opendaylight.mdsal.dom.api.DOMDataTreeLoopException;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeServiceExtension;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 
 public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
@@ -39,20 +42,17 @@ public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
     private final DOMDataTreeService dataTreeService;
     private final Object txChainIdentifier;
     private final AtomicLong txNum = new AtomicLong();
-    private final TransactionChainListener txChainListener;
+    private final DOMTransactionChainListener txChainListener;
     private final CachedDataTreeService cachedDataTreeService;
     private TransactionChainWriteTransaction writeTx;
     private TransactionChainReadTransaction readTx;
-    private ListenableFuture<Void> writeTxSubmitFuture;
+    private FluentFuture<? extends CommitInfo> writeTxCommitFuture;
     private boolean finished = false;
 
     public ShardedDOMTransactionChainAdapter(final Object txChainIdentifier,
-                                             final DOMDataTreeService dataTreeService,
-                                             final TransactionChainListener txChainListener) {
-        Preconditions.checkNotNull(dataTreeService);
-        Preconditions.checkNotNull(txChainIdentifier);
-        this.dataTreeService = dataTreeService;
-        this.txChainIdentifier = txChainIdentifier;
+            final DOMDataTreeService dataTreeService, final DOMTransactionChainListener txChainListener) {
+        this.dataTreeService = requireNonNull(dataTreeService);
+        this.txChainIdentifier = requireNonNull(txChainIdentifier);
         this.txChainListener = txChainListener;
         this.cachedDataTreeService = new CachedDataTreeService(dataTreeService);
     }
@@ -64,7 +64,7 @@ public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
         checkWriteTxClosed();
         readTx = new TransactionChainReadTransaction(newTransactionIdentifier(),
                 new ShardedDOMReadTransactionAdapter(newTransactionIdentifier(), dataTreeService),
-                writeTxSubmitFuture, this);
+                writeTxCommitFuture, this);
 
         return readTx;
     }
@@ -81,6 +81,20 @@ public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
         return writeTx;
     }
 
+    @Override
+    public DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
+        checkRunning();
+        checkWriteTxClosed();
+        checkReadTxClosed();
+        ShardedDOMReadWriteTransactionAdapter adapter = new ShardedDOMReadWriteTransactionAdapter(
+                newTransactionIdentifier(), cachedDataTreeService);
+        TransactionChainReadWriteTransaction readWriteTx = new TransactionChainReadWriteTransaction(
+                newTransactionIdentifier(), adapter, adapter.getReadAdapter(), writeTxCommitFuture, this);
+
+        writeTx = readWriteTx;
+        return readWriteTx;
+    }
+
     @Override
     public void close() {
         if (finished) {
@@ -90,9 +104,9 @@ public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
 
         checkReadTxClosed();
         checkWriteTxClosed();
-        Futures.addCallback(writeTxSubmitFuture, new FutureCallback<Void>() {
+        writeTxCommitFuture.addCallback(new FutureCallback<CommitInfo>() {
             @Override
-            public void onSuccess(@Nullable final Void result) {
+            public void onSuccess(final CommitInfo result) {
                 txChainListener.onTransactionChainSuccessful(ShardedDOMTransactionChainAdapter.this);
             }
 
@@ -101,7 +115,7 @@ public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
                 // We don't have to do nothing here,
                 // tx should take car of it
             }
-        });
+        }, MoreExecutors.directExecutor());
 
         cachedDataTreeService.closeProducers();
         finished = true;
@@ -111,8 +125,8 @@ public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
         readTx = null;
     }
 
-    public void closeWriteTransaction(final ListenableFuture<Void> submitFuture) {
-        writeTxSubmitFuture = submitFuture;
+    public void closeWriteTransaction(final FluentFuture<? extends CommitInfo> commitFuture) {
+        writeTxCommitFuture = commitFuture;
         writeTx = null;
     }
 
@@ -121,18 +135,18 @@ public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
     }
 
     private void checkWriteTxClosed() {
-        Preconditions.checkState(writeTx == null);
+        checkState(writeTx == null);
     }
 
     private void checkReadTxClosed() {
-        Preconditions.checkState(readTx == null);
+        checkState(readTx == null);
     }
 
     private void checkRunning() {
-        Preconditions.checkState(!finished);
+        checkState(!finished);
     }
 
-    public void transactionFailed(final AsyncTransaction<?, ?> tx, final Throwable cause) {
+    public void transactionFailed(final DOMDataTreeTransaction tx, final Throwable cause) {
         txChainListener.onTransactionChainFailed(this, tx, cause);
         if (writeTx != null) {
             writeTx.cancel();
@@ -144,7 +158,7 @@ public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
         finished = true;
     }
 
-    static class CachedDataTreeService implements DOMDataTreeService {
+    private static class CachedDataTreeService implements DOMDataTreeService {
 
         private final DOMDataTreeService delegateTreeService;
         private final Map<LogicalDatastoreType, NoopCloseDataProducer> producersMap =
@@ -158,59 +172,56 @@ public class ShardedDOMTransactionChainAdapter implements DOMTransactionChain {
             producersMap.values().forEach(NoopCloseDataProducer::closeDelegate);
         }
 
-        @Nonnull
         @Override
-        public <T extends DOMDataTreeListener> ListenerRegistration<T> registerListener(
-                @Nonnull final T listener, @Nonnull final Collection<DOMDataTreeIdentifier> subtrees,
-                         final boolean allowRxMerges, @Nonnull final Collection<DOMDataTreeProducer> producers)
-                throws DOMDataTreeLoopException {
+        public <T extends DOMDataTreeListener> ListenerRegistration<T> registerListener(final T listener,
+                final Collection<DOMDataTreeIdentifier> subtrees, final boolean allowRxMerges,
+                final Collection<DOMDataTreeProducer> producers) throws DOMDataTreeLoopException {
             return delegateTreeService.registerListener(listener, subtrees, allowRxMerges, producers);
         }
 
         @Override
-        public DOMDataTreeProducer createProducer(@Nonnull final Collection<DOMDataTreeIdentifier> subtrees) {
-            Preconditions.checkState(subtrees.size() == 1);
-            NoopCloseDataProducer producer = null;
-            for (final DOMDataTreeIdentifier treeId : subtrees) {
-                producer =
-                        new NoopCloseDataProducer(delegateTreeService.createProducer(Collections.singleton(treeId)));
-                producersMap.putIfAbsent(treeId.getDatastoreType(),
-                        producer);
-            }
-            return producer;
+        public ClassToInstanceMap<DOMDataTreeServiceExtension> getExtensions() {
+            return delegateTreeService.getExtensions();
         }
 
-        static class NoopCloseDataProducer implements DOMDataTreeProducer {
+        @Override
+        public DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
+            checkState(subtrees.size() == 1);
+            DOMDataTreeIdentifier treeId = subtrees.iterator().next();
+            NoopCloseDataProducer producer = new NoopCloseDataProducer(delegateTreeService.createProducer(
+                Collections.singleton(treeId)));
+            producersMap.putIfAbsent(treeId.getDatastoreType(), producer);
+            return producer;
+        }
+    }
 
-            private final DOMDataTreeProducer delegateTreeProducer;
+    private static final class NoopCloseDataProducer implements DOMDataTreeProducer {
+        private final DOMDataTreeProducer delegateTreeProducer;
 
-            NoopCloseDataProducer(final DOMDataTreeProducer delegateTreeProducer) {
-                this.delegateTreeProducer = delegateTreeProducer;
-            }
+        NoopCloseDataProducer(final DOMDataTreeProducer delegateTreeProducer) {
+            this.delegateTreeProducer = delegateTreeProducer;
+        }
 
-            @Nonnull
-            @Override
-            public DOMDataTreeCursorAwareTransaction createTransaction(final boolean isolated) {
-                return delegateTreeProducer.createTransaction(isolated);
-            }
+        @Override
+        public DOMDataTreeCursorAwareTransaction createTransaction(final boolean isolated) {
+            return delegateTreeProducer.createTransaction(isolated);
+        }
 
-            @Nonnull
-            @Override
-            public DOMDataTreeProducer createProducer(@Nonnull final Collection<DOMDataTreeIdentifier> subtrees) {
-                return delegateTreeProducer.createProducer(subtrees);
-            }
+        @Override
+        public DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
+            return delegateTreeProducer.createProducer(subtrees);
+        }
 
-            @Override
-            public void close() throws DOMDataTreeProducerException {
-                // noop
-            }
+        @Override
+        public void close() {
+            // noop
+        }
 
-            public void closeDelegate() {
-                try {
-                    delegateTreeProducer.close();
-                } catch (final DOMDataTreeProducerException e) {
-                    throw new IllegalStateException("Trying to close DOMDataTreeProducer with open transaction", e);
-                }
+        public void closeDelegate() {
+            try {
+                delegateTreeProducer.close();
+            } catch (final DOMDataTreeProducerException e) {
+                throw new IllegalStateException("Trying to close DOMDataTreeProducer with open transaction", e);
             }
         }
     }