Expose completion future from WriteOperations
[mdsal.git] / binding / mdsal-binding-util / src / main / java / org / opendaylight / mdsal / binding / util / TransactionAdapter.java
index f3e4c466c57e721eaa2c5130cf4cdeb0c39f83ed..0d0c5e7bc9d1e45c06d9d0457ec2791763c8f1e9 100644 (file)
@@ -12,7 +12,6 @@ import static com.google.common.base.Preconditions.checkArgument;
 import com.google.common.collect.ForwardingObject;
 import com.google.common.util.concurrent.FluentFuture;
 import java.util.Optional;
-import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
 import org.opendaylight.mdsal.binding.api.WriteTransaction;
@@ -46,13 +45,11 @@ public final class TransactionAdapter {
     public static ReadWriteTransaction toReadWriteTransaction(
             final TypedReadWriteTransaction<? extends Datastore> datastoreTx) {
         if (datastoreTx instanceof TypedReadWriteTransactionImpl) {
-            TypedReadWriteTransactionImpl nonSubmitCancelableDatastoreReadWriteTransaction =
-                    (TypedReadWriteTransactionImpl) datastoreTx;
-            return new ReadWriteTransactionAdapter(nonSubmitCancelableDatastoreReadWriteTransaction.datastoreType,
-                    nonSubmitCancelableDatastoreReadWriteTransaction);
+            final TypedReadWriteTransactionImpl<?> txImpl = (TypedReadWriteTransactionImpl<?>) datastoreTx;
+            return new ReadWriteTransactionAdapter<>(txImpl.getDatastoreType(), txImpl);
         }
-        throw new IllegalArgumentException(
-                "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
+        throw new IllegalArgumentException("Unsupported TypedWriteTransaction implementation "
+                + datastoreTx.getClass());
     }
 
     /**
@@ -64,23 +61,19 @@ public final class TransactionAdapter {
      */
     public static WriteTransaction toWriteTransaction(final TypedWriteTransaction<? extends Datastore> datastoreTx) {
         if (datastoreTx instanceof TypedWriteTransactionImpl) {
-            TypedWriteTransactionImpl nonSubmitCancelableDatastoreWriteTransaction =
-                    (TypedWriteTransactionImpl) datastoreTx;
-            return new WriteTransactionAdapter(nonSubmitCancelableDatastoreWriteTransaction.datastoreType,
-                    nonSubmitCancelableDatastoreWriteTransaction);
+            final TypedWriteTransactionImpl<?, ?> txImpl = (TypedWriteTransactionImpl<?, ?>) datastoreTx;
+            return new WriteTransactionAdapter<>(txImpl.getDatastoreType(), txImpl);
         }
-        throw new IllegalArgumentException(
-                "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
+        throw new IllegalArgumentException("Unsupported TypedWriteTransaction implementation "
+                + datastoreTx.getClass());
     }
 
-    // We want to subclass this class, even though it has a private constructor
-    @SuppressWarnings("FinalClass")
-    private static class WriteTransactionAdapter<D extends Datastore, T extends TypedWriteTransaction<D>>
+    private static class WriteTransactionAdapter<S extends Datastore, D extends TypedWriteTransaction<S>>
             extends ForwardingObject implements WriteTransaction {
         private final LogicalDatastoreType datastoreType;
-        private final T delegate;
+        private final D delegate;
 
-        private WriteTransactionAdapter(final LogicalDatastoreType datastoreType, final T delegate) {
+        WriteTransactionAdapter(final LogicalDatastoreType datastoreType, final D delegate) {
             this.datastoreType = datastoreType;
             this.delegate = delegate;
         }
@@ -93,10 +86,10 @@ public final class TransactionAdapter {
         }
 
         @Override
-        public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
-                final T data, final boolean createMissingParents) {
+        public <T extends DataObject> void mergeParentStructurePut(final LogicalDatastoreType store,
+                final InstanceIdentifier<T> path, final T data) {
             checkStore(store);
-            delegate.put(path, data, createMissingParents);
+            delegate.mergeParentStructurePut(path, data);
         }
 
         @Override
@@ -107,10 +100,10 @@ public final class TransactionAdapter {
         }
 
         @Override
-        public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
-                final T data, final boolean createMissingParents) {
+        public <T extends DataObject> void mergeParentStructureMerge(final LogicalDatastoreType store,
+                final InstanceIdentifier<T> path, final T data) {
             checkStore(store);
-            delegate.merge(path, data, createMissingParents);
+            delegate.mergeParentStructureMerge(path, data);
         }
 
         @Override
@@ -125,29 +118,34 @@ public final class TransactionAdapter {
         }
 
         @Override
-        public @NonNull FluentFuture<? extends CommitInfo> commit() {
+        public FluentFuture<? extends CommitInfo> commit() {
             throw new UnsupportedOperationException("Managed transactions must not be committed");
         }
 
-        void checkStore(final LogicalDatastoreType store) {
-            checkArgument(datastoreType.equals(store), "Invalid datastore %s used instead of %s", store, datastoreType);
-        }
-
         @Override
         public Object getIdentifier() {
             return delegate.getIdentifier();
         }
 
         @Override
-        protected T delegate() {
+        public FluentFuture<?> completionFuture() {
+            return delegate.completionFuture();
+        }
+
+        @Override
+        protected D delegate() {
             return delegate;
         }
+
+        void checkStore(final LogicalDatastoreType store) {
+            checkArgument(datastoreType.equals(store), "Invalid datastore %s used instead of %s", store, datastoreType);
+        }
     }
 
-    private static final class ReadWriteTransactionAdapter<D extends Datastore>
-            extends WriteTransactionAdapter<D, TypedReadWriteTransaction<D>> implements ReadWriteTransaction {
-        private ReadWriteTransactionAdapter(final LogicalDatastoreType datastoreType,
-                final TypedReadWriteTransaction<D> delegate) {
+    private static final class ReadWriteTransactionAdapter<S extends Datastore>
+            extends WriteTransactionAdapter<S, TypedReadWriteTransaction<S>> implements ReadWriteTransaction {
+        ReadWriteTransactionAdapter(final LogicalDatastoreType datastoreType,
+                final TypedReadWriteTransaction<S> delegate) {
             super(datastoreType, delegate);
         }
 
@@ -157,5 +155,11 @@ public final class TransactionAdapter {
             checkStore(store);
             return delegate().read(path);
         }
+
+        @Override
+        public FluentFuture<Boolean> exists(final LogicalDatastoreType store,final InstanceIdentifier<?> path) {
+            checkStore(store);
+            return delegate().exists(path);
+        }
     }
 }