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 com.google.common.util.concurrent.MoreExecutors;
19 import javax.annotation.Nullable;
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,
48 * datastore is discarded using discard-changes rpc, unlocked and an exception is thrown async</li>
51 * <li>Commit and Unlock candidate datastore async</li>
54 public class WriteCandidateTx extends AbstractWriteTx {
56 private static final Logger LOG = LoggerFactory.getLogger(WriteCandidateTx.class);
58 public WriteCandidateTx(final RemoteDeviceId id, final NetconfBaseOps rpc, final boolean rollbackSupport) {
59 super(rpc, id, rollbackSupport);
63 protected synchronized void init() {
64 LOG.trace("{}: Initializing {} transaction", id, getClass().getSimpleName());
69 final FutureCallback<DOMRpcResult> lockCandidateCallback = new FutureCallback<DOMRpcResult>() {
71 public void onSuccess(final DOMRpcResult result) {
72 if (isSuccess(result)) {
73 if (LOG.isTraceEnabled()) {
74 LOG.trace("Lock candidate successful");
77 LOG.warn("{}: lock candidate invoked unsuccessfully: {}", id, result.getErrors());
82 public void onFailure(final Throwable throwable) {
83 LOG.warn("Lock candidate operation failed. {}", throwable);
87 resultsFutures.add(netOps.lockCandidate(lockCandidateCallback));
91 protected void cleanup() {
97 public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
98 final ListenableFuture<Void> commitFutureAsVoid = Futures.transform(commitConfiguration(),
99 (Function<RpcResult<Void>, Void>) input -> {
100 Preconditions.checkArgument(input.isSuccessful() && input.getErrors().isEmpty(),
101 "Submit failed with errors: %s", input.getErrors());
103 }, MoreExecutors.directExecutor());
105 return Futures.makeChecked(commitFutureAsVoid, input -> new TransactionCommitFailedException(
106 "Submit of transaction " + getIdentifier() + " failed", input));
110 * This has to be non blocking since it is called from a callback on commit
111 * and its netty threadpool that is really sensitive to blocking calls.
113 private void discardChanges() {
114 netOps.discardChanges(new NetconfRpcFutureCallback("Discarding candidate", id));
118 public synchronized ListenableFuture<RpcResult<Void>> performCommit() {
119 resultsFutures.add(netOps.commit(new NetconfRpcFutureCallback("Commit", id)));
120 final ListenableFuture<RpcResult<Void>> txResult = resultsToTxStatus();
122 Futures.addCallback(txResult, new FutureCallback<RpcResult<Void>>() {
124 public void onSuccess(@Nullable final RpcResult<Void> result) {
129 public void onFailure(final Throwable throwable) {
130 // TODO If lock is cause of this failure cleanup will issue warning log
131 // cleanup is trying to do unlock, but this will fail
134 }, MoreExecutors.directExecutor());
139 protected void cleanupOnSuccess() {
144 protected void editConfig(final YangInstanceIdentifier path,
145 final Optional<NormalizedNode<?, ?>> data,
146 final DataContainerChild<?, ?> editStructure,
147 final Optional<ModifyAction> defaultOperation,
148 final String operation) {
150 final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit candidate", id);
152 if (defaultOperation.isPresent()) {
153 resultsFutures.add(netOps.editConfigCandidate(
154 editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport));
156 resultsFutures.add(netOps.editConfigCandidate(editConfigCallback, editStructure, rollbackSupport));
161 * This has to be non blocking since it is called from a callback on commit
162 * and its netty threadpool that is really sensitive to blocking calls.
164 private void unlock() {
165 netOps.unlockCandidate(new NetconfRpcFutureCallback("Unlock candidate", id));