Netconf stack by default locks the data store before issuing
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / AbstractWriteTx.java
index 3aeff93da40082c2a4405b5213a759a434890360..51c73147020b508d8582be8569aaf09bd078bf05 100644 (file)
@@ -5,22 +5,34 @@
  * 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.netconf.sal.connect.netconf.sal.tx;
 
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+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 java.util.concurrent.ExecutionException;
-import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
+import com.google.common.util.concurrent.MoreExecutors;
+import com.google.common.util.concurrent.SettableFuture;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CopyOnWriteArrayList;
+import javax.annotation.Nonnull;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.common.api.CommitInfo;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
+import org.opendaylight.netconf.api.DocumentedException;
 import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
+import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
@@ -29,24 +41,29 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
+public abstract class AbstractWriteTx implements DOMDataTreeWriteTransaction {
 
     private static final Logger LOG  = LoggerFactory.getLogger(AbstractWriteTx.class);
 
     protected final RemoteDeviceId id;
     protected final NetconfBaseOps netOps;
     protected final boolean rollbackSupport;
+    protected final List<ListenableFuture<DOMRpcResult>> resultsFutures = new ArrayList<>();
+    private final List<TxListener> listeners = new CopyOnWriteArrayList<>();
     // Allow commit to be called only once
-    protected boolean finished = false;
+    protected volatile boolean finished = false;
+    protected final boolean isLockAllowed;
 
-    public AbstractWriteTx(final NetconfBaseOps netOps, final RemoteDeviceId id, final boolean rollbackSupport) {
-        this.netOps = netOps;
+    public AbstractWriteTx(final RemoteDeviceId id, final NetconfBaseOps netconfOps, final boolean rollbackSupport,
+            final boolean isLockAllowed) {
+        this.netOps = netconfOps;
         this.id = id;
         this.rollbackSupport = rollbackSupport;
+        this.isLockAllowed = isLockAllowed;
         init();
     }
 
-    static boolean isSuccess(final DOMRpcResult result) {
+    protected static boolean isSuccess(final DOMRpcResult result) {
         return result.getErrors().isEmpty();
     }
 
@@ -58,28 +75,12 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
         return finished;
     }
 
-    protected void invokeBlocking(final String msg, final Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>> op) throws NetconfDocumentedException {
-        try {
-            final DOMRpcResult compositeNodeRpcResult = op.apply(netOps).get();
-            if(isSuccess(compositeNodeRpcResult) == false) {
-                throw new NetconfDocumentedException(id + ": " + msg + " failed: " + compositeNodeRpcResult.getErrors(), NetconfDocumentedException.ErrorType.application,
-                        NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
-            }
-        } catch (final InterruptedException e) {
-            Thread.currentThread().interrupt();
-            throw new RuntimeException(e);
-        } catch (final ExecutionException e) {
-            throw new NetconfDocumentedException(id + ": " + msg + " failed: " + e.getMessage(), e, NetconfDocumentedException.ErrorType.application,
-                    NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
-        }
-    }
-
     @Override
     public synchronized boolean cancel() {
-        if(isFinished()) {
+        if (isFinished()) {
             return false;
         }
-
+        listeners.forEach(listener -> listener.onTransactionCancelled(this));
         finished = true;
         cleanup();
         return true;
@@ -95,48 +96,44 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
     }
 
     @Override
-    public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
+    public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
+                                 final NormalizedNode<?, ?> data) {
         checkEditable(store);
 
-        // trying to write only mixin nodes (not visible when serialized). Ignoring. Some devices cannot handle empty edit-config rpc
-        if(containsOnlyNonVisibleData(path, data)) {
+        // Trying to write only mixin nodes (not visible when serialized).
+        // Ignoring. Some devices cannot handle empty edit-config rpc
+        if (containsOnlyNonVisibleData(path, data)) {
             LOG.debug("Ignoring put for {} and data {}. Resulting data structure is empty.", path, data);
             return;
         }
 
-        try {
-            editConfig(
-                    netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data), Optional.of(ModifyAction.REPLACE), path), Optional.of(ModifyAction.NONE));
-        } catch (final NetconfDocumentedException e) {
-            handleEditException(path, data, e, "putting");
-        }
+        final DataContainerChild<?, ?> editStructure = netOps.createEditConfigStrcture(Optional.ofNullable(data),
+                        Optional.of(ModifyAction.REPLACE), path);
+        editConfig(path, Optional.ofNullable(data), editStructure, Optional.empty(), "put");
     }
 
-    protected abstract void handleEditException(YangInstanceIdentifier path, NormalizedNode<?, ?> data, NetconfDocumentedException e, String editType);
-    protected abstract void handleDeleteException(YangInstanceIdentifier path, NetconfDocumentedException e);
-
     @Override
-    public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
+    public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
+                                   final NormalizedNode<?, ?> data) {
         checkEditable(store);
 
-        // trying to write only mixin nodes (not visible when serialized). Ignoring. Some devices cannot handle empty edit-config rpc
+        // Trying to write only mixin nodes (not visible when serialized).
+        // Ignoring. Some devices cannot handle empty edit-config rpc
         if (containsOnlyNonVisibleData(path, data)) {
             LOG.debug("Ignoring merge for {} and data {}. Resulting data structure is empty.", path, data);
             return;
         }
 
-        try {
-            editConfig(
-                    netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data), Optional.<ModifyAction>absent(), path), Optional.<ModifyAction>absent());
-        } catch (final NetconfDocumentedException e) {
-            handleEditException(path, data, e, "merge");
-        }
+        final DataContainerChild<?, ?> editStructure =  netOps.createEditConfigStrcture(Optional.ofNullable(data),
+            Optional.empty(), path);
+        editConfig(path, Optional.ofNullable(data), editStructure, Optional.empty(), "merge");
     }
 
     /**
-     * Check whether the data to be written consists only from mixins
+     * Check whether the data to be written consists only from mixins.
      */
-    private static boolean containsOnlyNonVisibleData(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
+    private static boolean containsOnlyNonVisibleData(final YangInstanceIdentifier path,
+                                                      final NormalizedNode<?, ?> data) {
         // There's only one such case:top level list (pathArguments == 1 && data is Mixin)
         // any other mixin nodes are contained by a "regular" node thus visible when serialized
         return path.getPathArguments().size() == 1 && data instanceof MixinNode;
@@ -145,29 +142,163 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
     @Override
     public synchronized void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
         checkEditable(store);
-
-        try {
-            editConfig(
-                    netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>absent(), Optional.of(ModifyAction.DELETE), path), Optional.of(ModifyAction.NONE));
-        } catch (final NetconfDocumentedException e) {
-            handleDeleteException(path, e);
-        }
+        final DataContainerChild<?, ?> editStructure = netOps.createEditConfigStrcture(Optional.empty(),
+                        Optional.of(ModifyAction.DELETE), path);
+        editConfig(path, Optional.empty(), editStructure, Optional.of(ModifyAction.NONE), "delete");
     }
 
     @Override
-    public final ListenableFuture<RpcResult<TransactionStatus>> commit() {
+    public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
+        final SettableFuture<CommitInfo> resultFuture = SettableFuture.create();
+        Futures.addCallback(commitConfiguration(), new FutureCallback<RpcResult<Void>>() {
+            @Override
+            public void onSuccess(final RpcResult<Void> result) {
+                if (!result.isSuccessful()) {
+                    final Collection<RpcError> errors = result.getErrors();
+                    resultFuture.setException(new TransactionCommitFailedException(
+                        String.format("Commit of transaction %s failed", getIdentifier()),
+                            errors.toArray(new RpcError[errors.size()])));
+                    return;
+                }
+
+                resultFuture.set(CommitInfo.empty());
+            }
+
+            @Override
+            public void onFailure(final Throwable failure) {
+                resultFuture.setException(new TransactionCommitFailedException(
+                        String.format("Commit of transaction %s failed", getIdentifier()), failure));
+            }
+        }, MoreExecutors.directExecutor());
+
+        return FluentFuture.from(resultFuture);
+    }
+
+    protected final ListenableFuture<RpcResult<Void>> commitConfiguration() {
+        listeners.forEach(listener -> listener.onTransactionSubmitted(this));
         checkNotFinished();
         finished = true;
+        final ListenableFuture<RpcResult<Void>> result = performCommit();
+        Futures.addCallback(result, new FutureCallback<RpcResult<Void>>() {
+            @Override
+            public void onSuccess(@Nonnull final RpcResult<Void> rpcResult) {
+                if (rpcResult.isSuccessful()) {
+                    listeners.forEach(txListener -> txListener.onTransactionSuccessful(AbstractWriteTx.this));
+                } else {
+                    final TransactionCommitFailedException cause =
+                            new TransactionCommitFailedException("Transaction failed",
+                                    rpcResult.getErrors().toArray(new RpcError[rpcResult.getErrors().size()]));
+                    listeners.forEach(listener -> listener.onTransactionFailed(AbstractWriteTx.this, cause));
+                }
+            }
 
-        return performCommit();
+            @Override
+            public void onFailure(final Throwable throwable) {
+                listeners.forEach(listener -> listener.onTransactionFailed(AbstractWriteTx.this, throwable));
+            }
+        }, MoreExecutors.directExecutor());
+        return result;
     }
 
-    protected abstract ListenableFuture<RpcResult<TransactionStatus>> performCommit();
+    protected abstract ListenableFuture<RpcResult<Void>> performCommit();
 
     private void checkEditable(final LogicalDatastoreType store) {
         checkNotFinished();
-        Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION, "Can edit only configuration data, not %s", store);
+        Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION,
+                "Can edit only configuration data, not %s", store);
+    }
+
+    protected abstract void editConfig(YangInstanceIdentifier path, Optional<NormalizedNode<?, ?>> data,
+                                       DataContainerChild<?, ?> editStructure,
+                                       Optional<ModifyAction> defaultOperation, String operation);
+
+    protected ListenableFuture<RpcResult<Void>> resultsToTxStatus() {
+        final SettableFuture<RpcResult<Void>> transformed = SettableFuture.create();
+
+        Futures.addCallback(Futures.allAsList(resultsFutures), new FutureCallback<List<DOMRpcResult>>() {
+            @Override
+            public void onSuccess(@Nonnull final List<DOMRpcResult> domRpcResults) {
+                if (!transformed.isDone()) {
+                    extractResult(domRpcResults, transformed);
+                }
+            }
+
+            @Override
+            public void onFailure(final Throwable throwable) {
+                final NetconfDocumentedException exception =
+                        new NetconfDocumentedException(
+                                id + ":RPC during tx returned an exception" + throwable.getMessage(),
+                                new Exception(throwable),
+                                DocumentedException.ErrorType.APPLICATION,
+                                DocumentedException.ErrorTag.OPERATION_FAILED,
+                                DocumentedException.ErrorSeverity.ERROR);
+                transformed.setException(exception);
+            }
+        }, MoreExecutors.directExecutor());
+
+        return transformed;
     }
 
-    protected abstract void editConfig(DataContainerChild<?, ?> editStructure, Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException;
+    private void extractResult(final List<DOMRpcResult> domRpcResults,
+                               final SettableFuture<RpcResult<Void>> transformed) {
+        DocumentedException.ErrorType errType = DocumentedException.ErrorType.APPLICATION;
+        DocumentedException.ErrorSeverity errSeverity = DocumentedException.ErrorSeverity.ERROR;
+        StringBuilder msgBuilder = new StringBuilder();
+        boolean errorsEncouneterd = false;
+        String errorTag = "operation-failed";
+
+        for (final DOMRpcResult domRpcResult : domRpcResults) {
+            if (!domRpcResult.getErrors().isEmpty()) {
+                errorsEncouneterd = true;
+                final RpcError error = domRpcResult.getErrors().iterator().next();
+                final RpcError.ErrorType errorType = error.getErrorType();
+                switch (errorType) {
+                    case RPC:
+                        errType = DocumentedException.ErrorType.RPC;
+                        break;
+                    case PROTOCOL:
+                        errType = DocumentedException.ErrorType.PROTOCOL;
+                        break;
+                    case TRANSPORT:
+                        errType = DocumentedException.ErrorType.TRANSPORT;
+                        break;
+                    case APPLICATION:
+                        errType = DocumentedException.ErrorType.APPLICATION;
+                        break;
+                    default:
+                        errType = DocumentedException.ErrorType.APPLICATION;
+                        break;
+                }
+                final RpcError.ErrorSeverity severity = error.getSeverity();
+                switch (severity) {
+                    case ERROR:
+                        errSeverity = DocumentedException.ErrorSeverity.ERROR;
+                        break;
+                    case WARNING:
+                        errSeverity = DocumentedException.ErrorSeverity.WARNING;
+                        break;
+                    default:
+                        errSeverity = DocumentedException.ErrorSeverity.ERROR;
+                        break;
+                }
+                msgBuilder.append(error.getMessage());
+                errorTag = error.getTag();
+            }
+        }
+        if (errorsEncouneterd) {
+            final NetconfDocumentedException exception = new NetconfDocumentedException(id
+                    + ":RPC during tx failed. " + msgBuilder.toString(),
+                    errType,
+                    DocumentedException.ErrorTag.from(errorTag),
+                    errSeverity);
+            transformed.setException(exception);
+            return;
+        }
+        transformed.set(RpcResultBuilder.<Void>success().build());
+    }
+
+    AutoCloseable addListener(final TxListener listener) {
+        listeners.add(listener);
+        return () -> listeners.remove(listener);
+    }
 }