Remove javax.annotation nullness annotations
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / AbstractWriteTx.java
index 60bf213f88880b4575aa80e4d6141d4005a6e15f..2ed86685a5baeb560547022a8f349bcb91c063c6 100644 (file)
@@ -5,35 +5,31 @@
  * 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.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
-import com.google.common.util.concurrent.CheckedFuture;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+
 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 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.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.mdsal.common.api.CommitInfo;
-import org.opendaylight.mdsal.common.api.MappingCheckedFuture;
+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.util.concurrent.ExceptionMapper;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
@@ -45,14 +41,14 @@ 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;
+    protected final List<ListenableFuture<DOMRpcResult>> resultsFutures = new ArrayList<>();
     private final List<TxListener> listeners = new CopyOnWriteArrayList<>();
     // Allow commit to be called only once
     protected volatile boolean finished = false;
@@ -61,7 +57,6 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
         this.netOps = netOps;
         this.id = id;
         this.rollbackSupport = rollbackSupport;
-        this.resultsFutures = Lists.newArrayList();
         init();
     }
 
@@ -70,7 +65,7 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
     }
 
     protected void checkNotFinished() {
-        Preconditions.checkState(!isFinished(), "%s: Transaction %s already finished", id, getIdentifier());
+        checkState(!isFinished(), "%s: Transaction %s already finished", id, getIdentifier());
     }
 
     protected boolean isFinished() {
@@ -109,10 +104,9 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
             return;
         }
 
-        final DataContainerChild<?, ?> editStructure =
-                netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data),
+        final DataContainerChild<?, ?> editStructure = netOps.createEditConfigStrcture(Optional.ofNullable(data),
                         Optional.of(ModifyAction.REPLACE), path);
-        editConfig(path, Optional.fromNullable(data), editStructure, Optional.<ModifyAction>absent(), "put");
+        editConfig(path, Optional.ofNullable(data), editStructure, Optional.empty(), "put");
     }
 
     @Override
@@ -127,10 +121,9 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
             return;
         }
 
-        final DataContainerChild<?, ?> editStructure =
-                netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data),
-                        Optional.<ModifyAction>absent(), path);
-        editConfig(path, Optional.fromNullable(data), editStructure, Optional.<ModifyAction>absent(), "merge");
+        final DataContainerChild<?, ?> editStructure =  netOps.createEditConfigStrcture(Optional.ofNullable(data),
+            Optional.empty(), path);
+        editConfig(path, Optional.ofNullable(data), editStructure, Optional.empty(), "merge");
     }
 
     /**
@@ -146,30 +139,17 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
     @Override
     public synchronized void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
         checkEditable(store);
-        final DataContainerChild<?, ?> editStructure =
-                netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>absent(),
+        final DataContainerChild<?, ?> editStructure = netOps.createEditConfigStrcture(Optional.empty(),
                         Optional.of(ModifyAction.DELETE), path);
-        editConfig(path, Optional.<NormalizedNode<?, ?>>absent(),
-                editStructure, Optional.of(ModifyAction.NONE), "delete");
-    }
-
-    @Override
-    public CheckedFuture<Void, TransactionCommitFailedException> submit() {
-        return MappingCheckedFuture.create(commit().transform(ignored -> null, MoreExecutors.directExecutor()),
-            new ExceptionMapper<TransactionCommitFailedException>("commit", TransactionCommitFailedException.class) {
-                @Override
-                protected TransactionCommitFailedException newWithCause(String message, Throwable cause) {
-                    return new TransactionCommitFailedException(message, cause);
-                }
-            });
+        editConfig(path, Optional.empty(), editStructure, Optional.of(ModifyAction.NONE), "delete");
     }
 
     @Override
-    public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
+    public FluentFuture<? extends CommitInfo> commit() {
         final SettableFuture<CommitInfo> resultFuture = SettableFuture.create();
         Futures.addCallback(commitConfiguration(), new FutureCallback<RpcResult<Void>>() {
             @Override
-            public void onSuccess(RpcResult<Void> result) {
+            public void onSuccess(final RpcResult<Void> result) {
                 if (!result.isSuccessful()) {
                     final Collection<RpcError> errors = result.getErrors();
                     resultFuture.setException(new TransactionCommitFailedException(
@@ -182,7 +162,7 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
             }
 
             @Override
-            public void onFailure(Throwable failure) {
+            public void onFailure(final Throwable failure) {
                 resultFuture.setException(new TransactionCommitFailedException(
                         String.format("Commit of transaction %s failed", getIdentifier()), failure));
             }
@@ -198,7 +178,7 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
         final ListenableFuture<RpcResult<Void>> result = performCommit();
         Futures.addCallback(result, new FutureCallback<RpcResult<Void>>() {
             @Override
-            public void onSuccess(@Nonnull final RpcResult<Void> rpcResult) {
+            public void onSuccess(final RpcResult<Void> rpcResult) {
                 if (rpcResult.isSuccessful()) {
                     listeners.forEach(txListener -> txListener.onTransactionSuccessful(AbstractWriteTx.this));
                 } else {
@@ -221,7 +201,7 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
 
     private void checkEditable(final LogicalDatastoreType store) {
         checkNotFinished();
-        Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION,
+        checkArgument(store == LogicalDatastoreType.CONFIGURATION,
                 "Can edit only configuration data, not %s", store);
     }
 
@@ -234,20 +214,9 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
 
         Futures.addCallback(Futures.allAsList(resultsFutures), new FutureCallback<List<DOMRpcResult>>() {
             @Override
-            public void onSuccess(@Nonnull final List<DOMRpcResult> domRpcResults) {
-                domRpcResults.forEach(domRpcResult -> {
-                    if (!domRpcResult.getErrors().isEmpty() && !transformed.isDone()) {
-                        final NetconfDocumentedException exception =
-                                new NetconfDocumentedException(id + ":RPC during tx failed",
-                                        DocumentedException.ErrorType.APPLICATION,
-                                        DocumentedException.ErrorTag.OPERATION_FAILED,
-                                        DocumentedException.ErrorSeverity.ERROR);
-                        transformed.setException(exception);
-                    }
-                });
-
+            public void onSuccess(final List<DOMRpcResult> domRpcResults) {
                 if (!transformed.isDone()) {
-                    transformed.set(RpcResultBuilder.<Void>success().build());
+                    extractResult(domRpcResults, transformed);
                 }
             }
 
@@ -255,7 +224,7 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
             public void onFailure(final Throwable throwable) {
                 final NetconfDocumentedException exception =
                         new NetconfDocumentedException(
-                                id + ":RPC during tx returned an exception",
+                                id + ":RPC during tx returned an exception" + throwable.getMessage(),
                                 new Exception(throwable),
                                 DocumentedException.ErrorType.APPLICATION,
                                 DocumentedException.ErrorTag.OPERATION_FAILED,
@@ -267,6 +236,64 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
         return transformed;
     }
 
+    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);