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=8e5c4de458f22a6a6703c9f72db2958fe4cf9dd9;hb=74f902f0d61c2077a50c57f40c5f7b040fdddc29;hp=34e6bb5bcbdea6a78339be09e6e0a594340b39cd;hpb=9013e567cd592fccefd801bd8c97440a810c3574;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 34e6bb5bcb..8e5c4de458 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 @@ -14,16 +14,20 @@ import com.google.common.collect.Lists; 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.List; +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.TransactionStatus; 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.netconf.api.NetconfDocumentedException; import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps; import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId; +import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.opendaylight.yangtools.yang.data.api.ModifyAction; @@ -42,6 +46,7 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction { protected final NetconfBaseOps netOps; protected final boolean rollbackSupport; protected final List> resultsFutures; + private final List listeners = new CopyOnWriteArrayList<>(); // Allow commit to be called only once protected boolean finished = false; @@ -67,10 +72,10 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction { @Override public synchronized boolean cancel() { - if(isFinished()) { + if (isFinished()) { return false; } - + listeners.forEach(listener -> listener.onTransactionCancelled(this)); finished = true; cleanup(); return true; @@ -86,37 +91,46 @@ public abstract class AbstractWriteTx implements DOMDataWriteTransaction { } @Override - public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) { + public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, + final NormalizedNode data) { checkEditable(store); - // trying to write only mixin nodes (not visible when serialized). Ignoring. Some devices cannot handle empty edit-config rpc - if(containsOnlyNonVisibleData(path, data)) { + // Trying to write only mixin nodes (not visible when serialized). + // Ignoring. Some devices cannot handle empty edit-config rpc + if (containsOnlyNonVisibleData(path, data)) { LOG.debug("Ignoring put for {} and data {}. Resulting data structure is empty.", path, data); return; } - final DataContainerChild editStructure = netOps.createEditConfigStrcture(Optional.>fromNullable(data), Optional.of(ModifyAction.REPLACE), path); + final DataContainerChild editStructure = + netOps.createEditConfigStrcture(Optional.>fromNullable(data), + Optional.of(ModifyAction.REPLACE), path); editConfig(path, Optional.fromNullable(data), editStructure, Optional.of(ModifyAction.NONE), "put"); } @Override - public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) { + public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, + final NormalizedNode data) { checkEditable(store); - // trying to write only mixin nodes (not visible when serialized). Ignoring. Some devices cannot handle empty edit-config rpc + // Trying to write only mixin nodes (not visible when serialized). + // Ignoring. Some devices cannot handle empty edit-config rpc if (containsOnlyNonVisibleData(path, data)) { LOG.debug("Ignoring merge for {} and data {}. Resulting data structure is empty.", path, data); return; } - final DataContainerChild editStructure = netOps.createEditConfigStrcture(Optional.>fromNullable(data), Optional.absent(), path); + final DataContainerChild editStructure = + netOps.createEditConfigStrcture(Optional.>fromNullable(data), + Optional.absent(), path); editConfig(path, Optional.fromNullable(data), editStructure, Optional.absent(), "merge"); } /** - * Check whether the data to be written consists only from mixins + * Check whether the data to be written consists only from mixins. */ - private static boolean containsOnlyNonVisibleData(final YangInstanceIdentifier path, final NormalizedNode data) { + private static boolean containsOnlyNonVisibleData(final YangInstanceIdentifier path, + final NormalizedNode data) { // There's only one such case:top level list (pathArguments == 1 && data is Mixin) // any other mixin nodes are contained by a "regular" node thus visible when serialized return path.getPathArguments().size() == 1 && data instanceof MixinNode; @@ -125,62 +139,91 @@ 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(), Optional.of(ModifyAction.DELETE), path); - editConfig(path, Optional.>absent(), editStructure, Optional.of(ModifyAction.NONE), "delete"); + final DataContainerChild editStructure = + netOps.createEditConfigStrcture(Optional.>absent(), + Optional.of(ModifyAction.DELETE), path); + editConfig(path, Optional.>absent(), + editStructure, Optional.of(ModifyAction.NONE), "delete"); } - @Override - public final ListenableFuture> commit() { + protected final ListenableFuture> commitConfiguration() { + listeners.forEach(listener -> listener.onTransactionSubmitted(this)); checkNotFinished(); finished = true; + final ListenableFuture> result = performCommit(); + Futures.addCallback(result, new FutureCallback>() { + @Override + public void onSuccess(@Nullable final RpcResult result) { + if (result != null && result.isSuccessful()) { + listeners.forEach(txListener -> txListener.onTransactionSuccessful(AbstractWriteTx.this)); + } else { + final TransactionCommitFailedException cause = + new TransactionCommitFailedException("Transaction failed", + result.getErrors().toArray(new RpcError[result.getErrors().size()])); + listeners.forEach(listener -> listener.onTransactionFailed(AbstractWriteTx.this, cause)); + } + } - return performCommit(); + @Override + public void onFailure(final Throwable throwable) { + listeners.forEach(listener -> listener.onTransactionFailed(AbstractWriteTx.this, throwable)); + } + }, MoreExecutors.directExecutor()); + return result; } - protected abstract ListenableFuture> performCommit(); + protected abstract ListenableFuture> performCommit(); private void checkEditable(final LogicalDatastoreType store) { checkNotFinished(); - Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION, "Can edit only configuration data, not %s", store); + Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION, + "Can edit only configuration data, not %s", store); } - protected abstract void editConfig(final YangInstanceIdentifier path, final Optional> data, final DataContainerChild editStructure, final Optional defaultOperation, final String operation); + protected abstract void editConfig(YangInstanceIdentifier path, Optional> data, + DataContainerChild editStructure, + Optional defaultOperation, String operation); - protected ListenableFuture> resultsToTxStatus() { - final SettableFuture> transformed = SettableFuture.create(); + protected ListenableFuture> resultsToTxStatus() { + final SettableFuture> transformed = SettableFuture.create(); Futures.addCallback(Futures.allAsList(resultsFutures), new FutureCallback>() { @Override public void onSuccess(final List domRpcResults) { domRpcResults.forEach(domRpcResult -> { - if(!domRpcResult.getErrors().isEmpty() && !transformed.isDone()) { - NetconfDocumentedException exception = + 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); + DocumentedException.ErrorType.APPLICATION, + DocumentedException.ErrorTag.OPERATION_FAILED, + DocumentedException.ErrorSeverity.ERROR); transformed.setException(exception); } }); - if(!transformed.isDone()) { - transformed.set(RpcResultBuilder.success(TransactionStatus.COMMITED).build()); + if (!transformed.isDone()) { + transformed.set(RpcResultBuilder.success().build()); } } @Override - public void onFailure(Throwable throwable) { - NetconfDocumentedException exception = + public void onFailure(final Throwable throwable) { + final NetconfDocumentedException exception = new NetconfDocumentedException( - new DocumentedException(id + ":RPC during tx returned an exception", - new Exception(throwable), - DocumentedException.ErrorType.application, - DocumentedException.ErrorTag.operation_failed, - DocumentedException.ErrorSeverity.error) ); + id + ":RPC during tx returned an exception", + new Exception(throwable), + DocumentedException.ErrorType.APPLICATION, + DocumentedException.ErrorTag.OPERATION_FAILED, + DocumentedException.ErrorSeverity.ERROR); transformed.setException(exception); } - }); + }, MoreExecutors.directExecutor()); return transformed; } + + AutoCloseable addListener(final TxListener listener) { + listeners.add(listener); + return () -> listeners.remove(listener); + } }