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 / WriteRunningTx.java
index cf7594a7bcf0b9dbdadb9dc7a6aa6ef500d88a19..940ea1a6b9fe6f9799caee811c79bbf9a165ebd3 100644 (file)
@@ -5,23 +5,17 @@
  * 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.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
-import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
-import org.opendaylight.netconf.api.NetconfDocumentedException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 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;
 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;
@@ -30,7 +24,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Tx implementation for netconf devices that support only writable-running with no candidate
+ * Tx implementation for netconf devices that support only writable-running with no candidate.
  * The sequence goes as:
  * <ol>
  *   <li>Lock running datastore on tx construction
@@ -49,10 +43,16 @@ import org.slf4j.LoggerFactory;
 public class WriteRunningTx extends AbstractWriteTx {
 
     private static final Logger LOG  = LoggerFactory.getLogger(WriteRunningTx.class);
+    private final List<Change> changes = new ArrayList<>();
 
     public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
                           final boolean rollbackSupport) {
-        super(netOps, id, rollbackSupport);
+        this(id, netOps, rollbackSupport, true);
+    }
+
+    public WriteRunningTx(RemoteDeviceId id, NetconfBaseOps netconfOps, boolean rollbackSupport,
+            boolean isLockAllowed) {
+        super(id, netconfOps, rollbackSupport, isLockAllowed);
     }
 
     @Override
@@ -61,17 +61,10 @@ 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));
-                }
-            });
-        } 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);
+        if (isLockAllowed) {
+            resultsFutures.add(netOps.lockRunning(new NetconfRpcFutureCallback("Lock running", id)));
+        } else {
+            LOG.trace("Lock is not allowed: {}", id);
         }
     }
 
@@ -81,67 +74,50 @@ public class WriteRunningTx extends AbstractWriteTx {
     }
 
     @Override
-    protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
-        LOG.warn("{}: Error {} data to (running){}, data: {}, canceling", id, editType, path, data, e);
-        cancel();
-        throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
+    public synchronized ListenableFuture<RpcResult<Void>> performCommit() {
+        for (final Change change : changes) {
+            resultsFutures.add(change.execute(id, netOps, rollbackSupport));
+        }
+        unlock();
+        return resultsToTxStatus();
     }
 
     @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);
+    protected void editConfig(final YangInstanceIdentifier path,
+                              final Optional<NormalizedNode<?, ?>> data,
+                              final DataContainerChild<?, ?> editStructure,
+                              final Optional<ModifyAction> defaultOperation,
+                              final String operation) {
+        changes.add(new Change(editStructure, defaultOperation));
     }
 
-    @Override
-    public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
-        final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
-            @Override
-            public Void apply(final RpcResult<TransactionStatus> input) {
-                return null;
-            }
-        });
-
-        return Futures.makeChecked(commmitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
-            @Override
-            public TransactionCommitFailedException apply(final Exception input) {
-                return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
-            }
-        });
+    private void unlock() {
+        if (isLockAllowed) {
+            netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
+        } else {
+            LOG.trace("Unlock is not allowed: {}", id);
+        }
     }
 
-    @Override
-    public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
-        unlock();
-        return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
-    }
+    private static final class Change {
 
-    @Override
-    protected void editConfig(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
-        invokeBlocking("Edit running", new Function<NetconfBaseOps, ListenableFuture<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);
-            }
-        });
-    }
+        private final DataContainerChild<?, ?> editStructure;
+        private final Optional<ModifyAction> defaultOperation;
 
-    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);
+        Change(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) {
+            this.editStructure = editStructure;
+            this.defaultOperation = defaultOperation;
+        }
+
+        private ListenableFuture<DOMRpcResult> execute(final RemoteDeviceId id, final NetconfBaseOps netOps,
+                                                       final boolean rollbackSupport) {
+            final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit running", id);
+            if (defaultOperation.isPresent()) {
+                return netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.get(),
+                    rollbackSupport);
+            } else {
+                return netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport);
+            }
         }
     }
 }