2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
22 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
23 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
24 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Tx implementation for netconf devices that support only candidate datastore and no writable running
35 * The sequence goes as:
37 * <li>Lock candidate datastore on tx construction
39 * <li>Lock has to succeed, if it does not, an attempt to discard changes is made</li>
40 * <li>Discard changes has to succeed</li>
41 * <li>If discard is successful, lock is reattempted</li>
42 * <li>Second lock attempt has to succeed</li>
45 * <li>Edit-config in candidate N times
47 * <li>If any issue occurs during edit, datastore is discarded using discard-changes rpc, unlocked and an exception is thrown async</li>
50 * <li>Commit and Unlock candidate datastore async</li>
53 public class WriteCandidateTx extends AbstractWriteTx {
55 private static final Logger LOG = LoggerFactory.getLogger(WriteCandidateTx.class);
57 public WriteCandidateTx(final RemoteDeviceId id, final NetconfBaseOps rpc, final boolean rollbackSupport) {
58 super(rpc, id, rollbackSupport);
62 protected synchronized void init() {
63 LOG.trace("{}: Initializing {} transaction", id, getClass().getSimpleName());
68 final FutureCallback<DOMRpcResult> lockCandidateCallback = new FutureCallback<DOMRpcResult>() {
70 public void onSuccess(final DOMRpcResult result) {
71 if (isSuccess(result)) {
72 if (LOG.isTraceEnabled()) {
73 LOG.trace("Lock candidate successful");
76 LOG.warn("{}: lock candidate invoked unsuccessfully: {}", id, result.getErrors());
81 public void onFailure(final Throwable t) {
82 LOG.warn("Lock candidate operation failed. {}", t);
86 resultsFutures.add(netOps.lockCandidate(lockCandidateCallback));
90 protected void cleanup() {
96 public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
97 final ListenableFuture<Void> commitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
99 public Void apply(final RpcResult<TransactionStatus> input) {
100 Preconditions.checkArgument(input.isSuccessful() && input.getErrors().isEmpty(), "Submit failed with errors: %s", input.getErrors());
105 return Futures.makeChecked(commitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
107 public TransactionCommitFailedException apply(final Exception input) {
108 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
114 * This has to be non blocking since it is called from a callback on commit and its netty threadpool that is really sensitive to blocking calls
116 private void discardChanges() {
117 netOps.discardChanges(new NetconfRpcFutureCallback("Discarding candidate", id));
121 public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
122 resultsFutures.add(netOps.commit(new NetconfRpcFutureCallback("Commit", id)));
123 final ListenableFuture<RpcResult<TransactionStatus>> txResult = resultsToTxStatus();
125 Futures.addCallback(txResult, new FutureCallback<RpcResult<TransactionStatus>>() {
127 public void onSuccess(@Nullable final RpcResult<TransactionStatus> result) {
132 public void onFailure(final Throwable t) {
133 // TODO If lock is cause of this failure cleanup will issue warning log
134 // cleanup is trying to do unlock, but this will fail
142 protected void cleanupOnSuccess() {
147 protected void editConfig(final YangInstanceIdentifier path,
148 final Optional<NormalizedNode<?, ?>> data,
149 final DataContainerChild<?, ?> editStructure,
150 final Optional<ModifyAction> defaultOperation,
151 final String operation) {
153 final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit candidate", id);
155 if (defaultOperation.isPresent()) {
156 resultsFutures.add(netOps.editConfigCandidate(
157 editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport));
159 resultsFutures.add(netOps.editConfigCandidate(editConfigCallback, editStructure, rollbackSupport));
164 * This has to be non blocking since it is called from a callback on commit and its netty threadpool that is really sensitive to blocking calls
166 private void unlock() {
167 netOps.unlockCandidate(new NetconfRpcFutureCallback("Unlock candidate", id));