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
8 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.Optional;
15 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
16 import org.opendaylight.netconf.api.ModifyAction;
17 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
18 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
19 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Tx implementation for netconf devices that support only candidate datastore and no writable running.
29 * The sequence goes as:
31 * <li>Lock candidate datastore on tx construction
33 * <li>Lock has to succeed, if it does not, an attempt to discard changes is made</li>
34 * <li>Discard changes has to succeed</li>
35 * <li>If discard is successful, lock is reattempted</li>
36 * <li>Second lock attempt has to succeed</li>
39 * <li>Edit-config in candidate N times
41 * <li>If any issue occurs during edit,
42 * datastore is discarded using discard-changes rpc, unlocked and an exception is thrown async</li>
45 * <li>Commit and Unlock candidate datastore async</li>
48 public class WriteCandidateTx extends AbstractWriteTx {
50 private static final Logger LOG = LoggerFactory.getLogger(WriteCandidateTx.class);
52 public WriteCandidateTx(final RemoteDeviceId id, final NetconfBaseOps rpc, final boolean rollbackSupport) {
53 super(rpc, id, rollbackSupport);
57 protected synchronized void init() {
58 LOG.trace("{}: Initializing {} transaction", id, getClass().getSimpleName());
63 final FutureCallback<DOMRpcResult> lockCandidateCallback = new FutureCallback<DOMRpcResult>() {
65 public void onSuccess(final DOMRpcResult result) {
66 if (isSuccess(result)) {
67 if (LOG.isTraceEnabled()) {
68 LOG.trace("Lock candidate successful");
71 LOG.warn("{}: lock candidate invoked unsuccessfully: {}", id, result.getErrors());
76 public void onFailure(final Throwable throwable) {
77 LOG.warn("Lock candidate operation failed", throwable);
81 resultsFutures.add(netOps.lockCandidate(lockCandidateCallback));
85 protected void cleanup() {
91 * This has to be non blocking since it is called from a callback on commit
92 * and its netty threadpool that is really sensitive to blocking calls.
94 private void discardChanges() {
95 netOps.discardChanges(new NetconfRpcFutureCallback("Discarding candidate", id));
99 public synchronized ListenableFuture<RpcResult<Void>> performCommit() {
100 resultsFutures.add(netOps.commit(new NetconfRpcFutureCallback("Commit", id)));
101 final ListenableFuture<RpcResult<Void>> txResult = resultsToTxStatus();
103 Futures.addCallback(txResult, new FutureCallback<RpcResult<Void>>() {
105 public void onSuccess(final RpcResult<Void> result) {
110 public void onFailure(final Throwable throwable) {
111 // TODO If lock is cause of this failure cleanup will issue warning log
112 // cleanup is trying to do unlock, but this will fail
115 }, MoreExecutors.directExecutor());
120 protected void cleanupOnSuccess() {
125 protected void editConfig(final YangInstanceIdentifier path,
126 final Optional<NormalizedNode<?, ?>> data,
127 final DataContainerChild<?, ?> editStructure,
128 final Optional<ModifyAction> defaultOperation,
129 final String operation) {
131 final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit candidate", id);
133 if (defaultOperation.isPresent()) {
134 resultsFutures.add(netOps.editConfigCandidate(
135 editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport));
137 resultsFutures.add(netOps.editConfigCandidate(editConfigCallback, editStructure, rollbackSupport));
142 * This has to be non blocking since it is called from a callback on commit
143 * and its netty threadpool that is really sensitive to blocking calls.
145 private void unlock() {
146 netOps.unlockCandidate(new NetconfRpcFutureCallback("Unlock candidate", id));