X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Fconnect%2Fnetconf%2Fsal%2Ftx%2FAbstractWriteTx.java;h=51c73147020b508d8582be8569aaf09bd078bf05;hb=a0833fb27c69d919a4420c6ecbd11a3b2a1119cb;hp=8e5c4de458f22a6a6703c9f72db2958fe4cf9dd9;hpb=647e61052064693a422d48be8046272934916ac9;p=netconf.git diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/AbstractWriteTx.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/AbstractWriteTx.java index 8e5c4de458..51c7314702 100644 --- a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/AbstractWriteTx.java +++ b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/AbstractWriteTx.java @@ -5,25 +5,28 @@ * 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.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.Nullable; -import org.opendaylight.controller.config.util.xml.DocumentedException; -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 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; @@ -38,23 +41,25 @@ 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> resultsFutures; + protected final List> resultsFutures = new ArrayList<>(); private final List 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.resultsFutures = Lists.newArrayList(); + this.isLockAllowed = isLockAllowed; init(); } @@ -102,10 +107,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.of(ModifyAction.NONE), "put"); + editConfig(path, Optional.ofNullable(data), editStructure, Optional.empty(), "put"); } @Override @@ -120,10 +124,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"); } /** @@ -139,11 +142,36 @@ 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 + public @NonNull FluentFuture commit() { + final SettableFuture resultFuture = SettableFuture.create(); + Futures.addCallback(commitConfiguration(), new FutureCallback>() { + @Override + public void onSuccess(final RpcResult result) { + if (!result.isSuccessful()) { + final Collection 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> commitConfiguration() { @@ -153,13 +181,13 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction { final ListenableFuture> result = performCommit(); Futures.addCallback(result, new FutureCallback>() { @Override - public void onSuccess(@Nullable final RpcResult result) { - if (result != null && result.isSuccessful()) { + public void onSuccess(@Nonnull final RpcResult rpcResult) { + if (rpcResult.isSuccessful()) { listeners.forEach(txListener -> txListener.onTransactionSuccessful(AbstractWriteTx.this)); } else { final TransactionCommitFailedException cause = new TransactionCommitFailedException("Transaction failed", - result.getErrors().toArray(new RpcError[result.getErrors().size()])); + rpcResult.getErrors().toArray(new RpcError[rpcResult.getErrors().size()])); listeners.forEach(listener -> listener.onTransactionFailed(AbstractWriteTx.this, cause)); } } @@ -189,20 +217,9 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction { Futures.addCallback(Futures.allAsList(resultsFutures), new FutureCallback>() { @Override - public void onSuccess(final List 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(@Nonnull final List domRpcResults) { if (!transformed.isDone()) { - transformed.set(RpcResultBuilder.success().build()); + extractResult(domRpcResults, transformed); } } @@ -210,7 +227,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, @@ -222,6 +239,64 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction { return transformed; } + private void extractResult(final List domRpcResults, + final SettableFuture> 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.success().build()); + } + AutoCloseable addListener(final TxListener listener) { listeners.add(listener); return () -> listeners.remove(listener);