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;
init();
}
- static boolean isSuccess(final DOMRpcResult result) {
+ protected static boolean isSuccess(final DOMRpcResult result) {
return result.getErrors().isEmpty();
}
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()) {
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) {
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");
}
/**
@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
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);
}
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;
}
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);
}
/**
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;
@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
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>() {
}
@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);
+ }
}
/**
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;
}
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
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>() {
}
@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));
}
}
@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));
}
}