Merge "Stop using blocking invoke for lock and editConfig."
authorTomas Cere <tcere@cisco.com>
Tue, 21 Jun 2016 10:31:17 +0000 (10:31 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 21 Jun 2016 10:31:17 +0000 (10:31 +0000)
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/AbstractWriteTx.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateRunningTx.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateTx.java
netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteRunningTx.java
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/NetconfDeviceWriteOnlyTxTest.java

index 3aeff93da40082c2a4405b5213a759a434890360..b9c3f72a60e912e597cb8889cc08ef7dca0b89f2 100644 (file)
@@ -8,11 +8,9 @@
 
 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.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;
@@ -46,7 +44,7 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
         init();
     }
 
-    static boolean isSuccess(final DOMRpcResult result) {
+    protected static boolean isSuccess(final DOMRpcResult result) {
         return result.getErrors().isEmpty();
     }
 
@@ -58,22 +56,6 @@ 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()) {
@@ -104,16 +86,11 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
             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.<NormalizedNode<?, ?>>fromNullable(data), Optional.of(ModifyAction.REPLACE), path);
+        editConfig(path, Optional.fromNullable(data), editStructure, Optional.of(ModifyAction.NONE), "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) {
@@ -125,12 +102,8 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
             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.<NormalizedNode<?, ?>>fromNullable(data), Optional.<ModifyAction>absent(), path);
+        editConfig(path, Optional.fromNullable(data), editStructure, Optional.<ModifyAction>absent(), "merge");
     }
 
     /**
@@ -145,13 +118,8 @@ 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.<NormalizedNode<?, ?>>absent(), Optional.of(ModifyAction.DELETE), path);
+        editConfig(path, Optional.<NormalizedNode<?, ?>>absent(), editStructure, Optional.of(ModifyAction.NONE), "delete");
     }
 
     @Override
@@ -169,5 +137,5 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
         Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION, "Can edit only configuration data, not %s", store);
     }
 
-    protected abstract void editConfig(DataContainerChild<?, ?> editStructure, Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException;
+    protected abstract void editConfig(final YangInstanceIdentifier path, final Optional<NormalizedNode<?, ?>> data, final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation, final String operation);
 }
index c076d0b7a8fc6833d820f3abdb9ee69028f76c48..a693e8c59c3db5a07fc0538a675e72f550582b86 100644 (file)
@@ -8,10 +8,8 @@
 
 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
 
-import com.google.common.base.Function;
-import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.FutureCallback;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
-import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
@@ -46,18 +44,25 @@ public class WriteCandidateRunningTx extends WriteCandidateTx {
     }
 
     private void lockRunning() {
-        try {
-            invokeBlocking("Lock running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
-                @Override
-                public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
-                    return input.lockRunning(new NetconfRpcFutureCallback("Lock running", id));
+        final FutureCallback<DOMRpcResult> lockRunningCallback = new FutureCallback<DOMRpcResult>() {
+            @Override
+            public void onSuccess(DOMRpcResult result) {
+                if (isSuccess(result)) {
+                    if (LOG.isTraceEnabled()) {
+                        LOG.trace("Lock running succesfull");
+                    }
+                } else {
+                    LOG.warn("{}: lock running invoked unsuccessfully: {}", id, result.getErrors());
                 }
-            });
-        } catch (final NetconfDocumentedException e) {
-            LOG.warn("{}: Failed to lock running. Failed to initialize transaction", id, e);
-            finished = true;
-            throw new RuntimeException(id + ": Failed to lock running. Failed to initialize transaction", e);
-        }
+            }
+
+            @Override
+            public void onFailure(Throwable t) {
+                LOG.warn("{}: Failed to lock running. Failed to initialize transaction", id, t);
+                throw new RuntimeException(id + ": Failed to lock running. Failed to initialize transaction", t);
+            }
+        };
+        netOps.lockRunning(lockRunningCallback);
     }
 
     /**
index b143a3bf8e969fb227e70f660862131c6eb509bf..4001c3186b385d16b376421256c9ac66c3dba224 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
@@ -78,34 +79,32 @@ public class WriteCandidateTx extends AbstractWriteTx {
     @Override
     protected synchronized void init() {
         LOG.trace("{}: Initializing {} transaction", id, getClass().getSimpleName());
-
-        try {
-            lock();
-        } catch (final NetconfDocumentedException e) {
-            try {
-                LOG.warn("{}: Failed to lock candidate, attempting discard changes", id);
-                discardChanges();
-                LOG.warn("{}: Changes discarded successfully, attempting lock", id);
-                lock();
-            } catch (final NetconfDocumentedException secondE) {
-                LOG.error("{}: Failed to prepare candidate. Failed to initialize transaction", id, secondE);
-                throw new RuntimeException(id + ": Failed to prepare candidate. Failed to initialize transaction", secondE);
-            }
-        }
+        lock();
     }
 
-    private void lock() throws NetconfDocumentedException {
-        try {
-            invokeBlocking("Lock candidate", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
-                @Override
-                public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
-                    return input.lockCandidate(new NetconfRpcFutureCallback("Lock candidate", id));
+    private void lock() {
+        final FutureCallback<DOMRpcResult> lockCandidateCallback = new FutureCallback<DOMRpcResult>() {
+            @Override
+            public void onSuccess(DOMRpcResult result) {
+                if (isSuccess(result)) {
+                    if (LOG.isTraceEnabled()) {
+                        LOG.trace("Lock candidate succesfull");
+                    }
+                } else {
+                    LOG.warn("{}: lock candidate invoked unsuccessfully: {}", id, result.getErrors());
                 }
-            });
-        } catch (final NetconfDocumentedException e) {
-            LOG.warn("{}: Failed to lock candidate", id, e);
-            throw e;
-        }
+            }
+
+            @Override
+            public void onFailure(Throwable t) {
+                LOG.warn("Lock candidate operation failed. {}", t);
+                NetconfDocumentedException e = new NetconfDocumentedException(id + ": Lock candidate operation failed.", NetconfDocumentedException.ErrorType.application,
+                        NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
+                discardChanges();
+                throw new RuntimeException(e);
+            }
+        };
+        netOps.lockCandidate(lockCandidateCallback);
     }
 
     @Override
@@ -121,13 +120,6 @@ public class WriteCandidateTx extends AbstractWriteTx {
         throw new RuntimeException(id + ": Error while " + editType + ": (candidate)" + path, e);
     }
 
-    @Override
-    protected void handleDeleteException(final YangInstanceIdentifier path, final NetconfDocumentedException e) {
-        LOG.warn("{}: Error deleting data (candidate){}, canceling", id, path, e);
-        cancel();
-        throw new RuntimeException(id + ": Error while deleting (candidate)" + path, e);
-    }
-
     @Override
     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
         final ListenableFuture<Void> commitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
@@ -184,17 +176,36 @@ public class WriteCandidateTx extends AbstractWriteTx {
     }
 
     @Override
-    protected void editConfig(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
-        invokeBlocking("Edit candidate", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
+    protected void editConfig(final YangInstanceIdentifier path,
+                              final Optional<NormalizedNode<?, ?>> data,
+                              final DataContainerChild<?, ?> editStructure,
+                              final Optional<ModifyAction> defaultOperation,
+                              final String operation) {
+        FutureCallback<DOMRpcResult> editConfigCallback = new FutureCallback<DOMRpcResult>() {
+            @Override
+            public void onSuccess(DOMRpcResult result) {
+                if (isSuccess(result)) {
+                    if (LOG.isTraceEnabled()) {
+                        LOG.trace("Edit candidate succesfull");
+                    }
+                } else {
+                    LOG.warn("{}: Edit candidate invoked unsuccessfully: {}", id, result.getErrors());
+                }
+            }
+
             @Override
-            public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
-                    return defaultOperation.isPresent()
-                            ? input.editConfigCandidate(new NetconfRpcFutureCallback("Edit candidate", id), editStructure, defaultOperation.get(),
-                            rollbackSupport)
-                            : input.editConfigCandidate(new NetconfRpcFutureCallback("Edit candidate", id), editStructure,
-                            rollbackSupport);
+            public void onFailure(Throwable t) {
+                LOG.warn("Edit candidate operation failed. {}", t);
+                NetconfDocumentedException e = new NetconfDocumentedException(id + ": Edit candidate operation failed.", NetconfDocumentedException.ErrorType.application,
+                        NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
+                handleEditException(path, data.orNull(), e, operation);
             }
-        });
+        };
+        if (defaultOperation.isPresent()) {
+            netOps.editConfigCandidate(editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport);
+        } else {
+            netOps.editConfigCandidate(editConfigCallback, editStructure, rollbackSupport);
+        }
     }
 
     /**
index cf7594a7bcf0b9dbdadb9dc7a6aa6ef500d88a19..4b3ee7de9f735d41d29a5f15f9c85e4581273262 100644 (file)
@@ -11,6 +11,7 @@ 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.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
@@ -61,18 +62,25 @@ public class WriteRunningTx extends AbstractWriteTx {
     }
 
     private void lock() {
-        try {
-            invokeBlocking("Lock running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
-                @Override
-                public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
-                    return input.lockRunning(new NetconfRpcFutureCallback("Lock running", id));
+        final FutureCallback<DOMRpcResult> lockCallback = new FutureCallback<DOMRpcResult>() {
+            @Override
+            public void onSuccess(DOMRpcResult result) {
+                if (isSuccess(result)) {
+                    if (LOG.isTraceEnabled()) {
+                        LOG.trace("Lock running succesfull");
+                    }
+                } else {
+                    LOG.warn("{}: lock running invoked unsuccessfully: {}", id, result.getErrors());
                 }
-            });
-        } catch (final NetconfDocumentedException e) {
-            LOG.warn("{}: Failed to initialize netconf transaction (lock running)", id, e);
-            finished = true;
-            throw new RuntimeException(id + ": Failed to initialize netconf transaction (lock running)", e);
-        }
+            }
+
+            @Override
+            public void onFailure(Throwable t) {
+                LOG.warn("Lock running operation failed. {}", t);
+                throw new RuntimeException(id + ": Failed to lock running datastore", t);
+            }
+        };
+        netOps.lockRunning(lockCallback);
     }
 
     @Override
@@ -87,13 +95,6 @@ public class WriteRunningTx extends AbstractWriteTx {
         throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
     }
 
-    @Override
-    protected void handleDeleteException(final YangInstanceIdentifier path, final NetconfDocumentedException e) {
-        LOG.warn("{}: Error deleting data (running){}, canceling", id, path, e);
-        cancel();
-        throw new RuntimeException(id + ": Error while deleting (running)" + path, e);
-    }
-
     @Override
     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
         final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
@@ -118,30 +119,39 @@ public class WriteRunningTx extends AbstractWriteTx {
     }
 
     @Override
-    protected void editConfig(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
-        invokeBlocking("Edit running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
+    protected void editConfig(final YangInstanceIdentifier path,
+                              final Optional<NormalizedNode<?, ?>> data,
+                              final DataContainerChild<?, ?> editStructure,
+                              final Optional<ModifyAction> defaultOperation,
+                              final String operation) {
+        FutureCallback<DOMRpcResult> editConfigCallback = new FutureCallback<DOMRpcResult>() {
             @Override
-            public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
-                    return defaultOperation.isPresent()
-                            ? input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure, defaultOperation.get(),
-                            rollbackSupport)
-                            : input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure,
-                            rollbackSupport);
+            public void onSuccess(DOMRpcResult result) {
+                if (isSuccess(result)) {
+                    if (LOG.isTraceEnabled()) {
+                        LOG.trace("Edit running succesfull");
+                    }
+                } else {
+                    LOG.warn("{}: Edit running invoked unsuccessfully: {}", id, result.getErrors());
+                }
             }
-        });
+
+            @Override
+            public void onFailure(Throwable t) {
+                LOG.warn("Edit running operation failed. {}", t);
+                NetconfDocumentedException e = new NetconfDocumentedException(id + ": Edit running operation failed.", NetconfDocumentedException.ErrorType.application,
+                        NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
+                handleEditException(path, data.orNull(), e, operation);
+            }
+        };
+        if (defaultOperation.isPresent()) {
+            netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport);
+        } else {
+            netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport);
+        }
     }
 
     private void unlock() {
-        try {
-            invokeBlocking("Unlocking running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
-                @Override
-                public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
-                    return input.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
-                }
-            });
-        } catch (final NetconfDocumentedException e) {
-            LOG.warn("{}: Failed to unlock running datastore", id, e);
-            throw new RuntimeException(id + ": Failed to unlock running datastore", e);
-        }
+        netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
     }
 }
index 33cddf22d303b6c6ad7bf304a6c9616adf1b1f53..18169510972b611b85b92c7a64bb66ab62175482 100644 (file)
@@ -124,24 +124,19 @@ public class NetconfDeviceWriteOnlyTxTest {
 
     @Test
     public void testDiscardChangesNotSentWithoutCandidate() {
-        doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(((NormalizedNode<?, ?>) null))))
+        doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
                 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
 
         final WriteRunningTx tx = new WriteRunningTx(id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext()),
                 false);
-        try {
-            tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
-        } catch (final Exception e) {
-            // verify discard changes was sent
-            final InOrder inOrder = inOrder(rpc);
-            inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
-            inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), any(NormalizedNode.class));
-            inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME), NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
-            return;
-        }
 
-        fail("Delete should fail");
+        tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
+        // verify discard changes was sent
+        final InOrder inOrder = inOrder(rpc);
+        inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
+        inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), any(NormalizedNode.class));
+        inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME), NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
     }
 
 }