Migrate netconf to MD-SAL APIs
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / AbstractWriteTx.java
index eb48fdbd96ba56f49e372172855823758bf7ffee..1d1c125ff728b8aa30b289153eaa4025c83c4d3d 100644 (file)
@@ -8,7 +8,6 @@
 
 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.FluentFuture;
@@ -19,14 +18,15 @@ import com.google.common.util.concurrent.MoreExecutors;
 import com.google.common.util.concurrent.SettableFuture;
 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.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;
@@ -42,7 +42,7 @@ 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);
 
@@ -106,10 +106,9 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
             return;
         }
 
-        final DataContainerChild<?, ?> editStructure =
-                netOps.createEditConfigStrcture(Optional.fromNullable(data),
+        final DataContainerChild<?, ?> editStructure = netOps.createEditConfigStrcture(Optional.ofNullable(data),
                         Optional.of(ModifyAction.REPLACE), path);
-        editConfig(path, Optional.fromNullable(data), editStructure, Optional.absent(), "put");
+        editConfig(path, Optional.ofNullable(data), editStructure, Optional.empty(), "put");
     }
 
     @Override
@@ -124,10 +123,9 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
             return;
         }
 
-        final DataContainerChild<?, ?> editStructure =
-                netOps.createEditConfigStrcture(Optional.fromNullable(data),
-                        Optional.absent(), path);
-        editConfig(path, Optional.fromNullable(data), editStructure, Optional.absent(), "merge");
+        final DataContainerChild<?, ?> editStructure =  netOps.createEditConfigStrcture(Optional.ofNullable(data),
+            Optional.empty(), path);
+        editConfig(path, Optional.ofNullable(data), editStructure, Optional.empty(), "merge");
     }
 
     /**
@@ -143,11 +141,9 @@ 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.absent(),
+        final DataContainerChild<?, ?> editStructure = netOps.createEditConfigStrcture(Optional.empty(),
                         Optional.of(ModifyAction.DELETE), path);
-        editConfig(path, Optional.absent(),
-                editStructure, Optional.of(ModifyAction.NONE), "delete");
+        editConfig(path, Optional.empty(), editStructure, Optional.of(ModifyAction.NONE), "delete");
     }
 
     @Override
@@ -155,7 +151,7 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
         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(
@@ -168,7 +164,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));
             }
@@ -230,7 +226,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,
@@ -244,56 +240,59 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
 
     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();
-                final DocumentedException.ErrorType eType;
                 switch (errorType) {
                     case RPC:
-                        eType = DocumentedException.ErrorType.RPC;
+                        errType = DocumentedException.ErrorType.RPC;
                         break;
                     case PROTOCOL:
-                        eType = DocumentedException.ErrorType.PROTOCOL;
+                        errType = DocumentedException.ErrorType.PROTOCOL;
                         break;
                     case TRANSPORT:
-                        eType = DocumentedException.ErrorType.TRANSPORT;
+                        errType = DocumentedException.ErrorType.TRANSPORT;
                         break;
                     case APPLICATION:
-                        eType = DocumentedException.ErrorType.APPLICATION;
+                        errType = DocumentedException.ErrorType.APPLICATION;
                         break;
                     default:
-                        eType = DocumentedException.ErrorType.APPLICATION;
+                        errType = DocumentedException.ErrorType.APPLICATION;
                         break;
                 }
                 final RpcError.ErrorSeverity severity = error.getSeverity();
-                final DocumentedException.ErrorSeverity eSeverity;
                 switch (severity) {
                     case ERROR:
-                        eSeverity = DocumentedException.ErrorSeverity.ERROR;
+                        errSeverity = DocumentedException.ErrorSeverity.ERROR;
                         break;
                     case WARNING:
-                        eSeverity = DocumentedException.ErrorSeverity.WARNING;
+                        errSeverity = DocumentedException.ErrorSeverity.WARNING;
                         break;
                     default:
-                        eSeverity = DocumentedException.ErrorSeverity.ERROR;
+                        errSeverity = DocumentedException.ErrorSeverity.ERROR;
                         break;
                 }
-                final String message;
-                if (error.getMessage() == null || error.getMessage().isEmpty()) {
-                    message = id + ":RPC during tx failed";
-                } else {
-                    message = error.getMessage();
-                }
-                final NetconfDocumentedException exception = new NetconfDocumentedException(message,
-                        eType,
-                        DocumentedException.ErrorTag.from(error.getTag()),
-                        eSeverity);
-                transformed.setException(exception);
-                return;
+                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());
     }