import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
+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.SettableFuture;
+import java.util.List;
+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.dom.api.DOMDataWriteTransaction;
import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
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;
protected final RemoteDeviceId id;
protected final NetconfBaseOps netOps;
protected final boolean rollbackSupport;
+ protected final List<ListenableFuture<DOMRpcResult>> resultsFutures;
// Allow commit to be called only once
protected boolean finished = false;
this.netOps = netOps;
this.id = id;
this.rollbackSupport = rollbackSupport;
+ this.resultsFutures = Lists.newArrayList();
init();
}
editConfig(path, Optional.fromNullable(data), editStructure, Optional.of(ModifyAction.NONE), "put");
}
- protected abstract void handleEditException(YangInstanceIdentifier path, NormalizedNode<?, ?> data, NetconfDocumentedException e, String editType);
-
@Override
public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
checkEditable(store);
}
protected abstract void editConfig(final YangInstanceIdentifier path, final Optional<NormalizedNode<?, ?>> data, final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation, final String operation);
+
+ protected ListenableFuture<RpcResult<TransactionStatus>> resultsToTxStatus() {
+ final SettableFuture<RpcResult<TransactionStatus>> transformed = SettableFuture.create();
+
+ Futures.addCallback(Futures.allAsList(resultsFutures), new FutureCallback<List<DOMRpcResult>>() {
+ @Override
+ public void onSuccess(final List<DOMRpcResult> domRpcResults) {
+ domRpcResults.forEach(domRpcResult -> {
+ if(!domRpcResult.getErrors().isEmpty() && !transformed.isDone()) {
+ NetconfDocumentedException exception =
+ new NetconfDocumentedException(id + ":RPC during tx failed",
+ DocumentedException.ErrorType.application,
+ DocumentedException.ErrorTag.operation_failed,
+ DocumentedException.ErrorSeverity.error);
+ transformed.setException(exception);
+ }
+ });
+
+ if(!transformed.isDone()) {
+ transformed.set(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
+ }
+ }
+
+ @Override
+ public void onFailure(Throwable throwable) {
+ // TODO should we wrap throwable in NetconfDocumentedException
+ transformed.setException(throwable);
+ }
+ });
+
+ return transformed;
+ }
}
* <li>Running datastore is locked as the first thing and this lock has to succeed</li>
* </ul>
*/
+//TODO replace custom RPCs future callbacks with NetconfRpcFutureCallback
public class WriteCandidateRunningTx extends WriteCandidateTx {
private static final Logger LOG = LoggerFactory.getLogger(WriteCandidateRunningTx.class);
@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);
+ resultsFutures.add(netOps.lockRunning(lockRunningCallback));
}
/**
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
+import javax.annotation.Nullable;
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;
* <li>Commit and Unlock candidate datastore async</li>
* </ol>
*/
+//TODO replace custom RPCs future callbacks with NetconfRpcFutureCallback
public class WriteCandidateTx extends AbstractWriteTx {
private static final Logger LOG = LoggerFactory.getLogger(WriteCandidateTx.class);
throw new RuntimeException(e);
}
};
- netOps.lockCandidate(lockCandidateCallback);
+ resultsFutures.add(netOps.lockCandidate(lockCandidateCallback));
}
@Override
cleanupOnSuccess();
}
- @Override
- protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
- LOG.warn("{}: Error {} data to (candidate){}, data: {}, canceling", id, editType, path, data, e);
- cancel();
- throw new RuntimeException(id + ": Error while " + editType + ": (candidate)" + path, e);
- }
-
@Override
public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
final ListenableFuture<Void> commitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
@Override
public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
- final ListenableFuture<DOMRpcResult> rpcResult = netOps.commit(new NetconfRpcFutureCallback("Commit", id) {
- @Override
- public void onSuccess(final DOMRpcResult result) {
- super.onSuccess(result);
- LOG.debug("{}: Write successful, transaction: {}. Unlocking", id, getIdentifier());
- cleanupOnSuccess();
- }
+ resultsFutures.add(netOps.commit(new NetconfRpcFutureCallback("Commit", id)));
+ ListenableFuture<RpcResult<TransactionStatus>> txResult = resultsToTxStatus();
+ Futures.addCallback(txResult, new FutureCallback<RpcResult<TransactionStatus>>() {
@Override
- protected void onUnsuccess(final DOMRpcResult result) {
- LOG.error("{}: Write failed, transaction {}, discarding changes, unlocking: {}", id, getIdentifier(), result.getErrors());
- cleanup();
+ public void onSuccess(@Nullable RpcResult<TransactionStatus> result) {
+ cleanupOnSuccess();
}
@Override
- public void onFailure(final Throwable t) {
- LOG.error("{}: Write failed, transaction {}, discarding changes, unlocking", id, getIdentifier(), t);
+ public void onFailure(Throwable t) {
+ // TODO If lock is cause of this failure cleanup will issue warning log
+ // cleanup is trying to do unlock, but this will fail
cleanup();
}
});
- return Futures.transform(rpcResult, RPC_RESULT_TO_TX_STATUS);
+ return txResult;
}
protected void cleanupOnSuccess() {
@Override
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);
+ resultsFutures.add(netOps.editConfigCandidate(
+ editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport));
} else {
- netOps.editConfigCandidate(editConfigCallback, editStructure, rollbackSupport);
+ resultsFutures.add(netOps.editConfigCandidate(editConfigCallback, editStructure, rollbackSupport));
}
}
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 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;
* <li>Unlock running datastore on tx commit</li>
* </ol>
*/
+//TODO replace custom RPCs future callbacks with NetconfRpcFutureCallback
public class WriteRunningTx extends AbstractWriteTx {
private static final Logger LOG = LoggerFactory.getLogger(WriteRunningTx.class);
@Override
public void onFailure(Throwable t) {
- LOG.warn("Lock running operation failed. {}", t);
- throw new RuntimeException(id + ": Failed to lock running datastore", t);
+ LOG.warn("{}: Lock running operation failed. {}", id, t);
}
};
- netOps.lockRunning(lockCallback);
+ resultsFutures.add(netOps.lockRunning(lockCallback));
}
@Override
unlock();
}
- @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);
- }
-
@Override
public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
@Override
public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
unlock();
- return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
+
+ return resultsToTxStatus();
}
@Override
@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);
+ LOG.warn("{}: Error {} data to (running){}, data: {}", id, operation, path, data.orNull(), t);
}
};
if (defaultOperation.isPresent()) {
- netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport);
+ resultsFutures.add(
+ netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport));
} else {
- netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport);
+ resultsFutures.add(netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport));
}
}
false);
tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
+ tx.submit();
// verify discard changes was sent
final InOrder inOrder = inOrder(rpc);
inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));