Netconf stack by default locks the data store before issuing
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / WriteCandidateTx.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Optional;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
19 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
20 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
21 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Tx implementation for netconf devices that support only candidate datastore and no writable running.
32  * The sequence goes as:
33  * <ol>
34  *   <li>Lock candidate datastore on tx construction
35  *     <ul>
36  *       <li>Lock has to succeed, if it does not, an attempt to discard changes is made</li>
37  *       <li>Discard changes has to succeed</li>
38  *       <li>If discard is successful, lock is reattempted</li>
39  *       <li>Second lock attempt has to succeed</li>
40  *     </ul>
41  *   </li>
42  *   <li>Edit-config in candidate N times
43  *     <ul>
44  *       <li>If any issue occurs during edit,
45  *       datastore is discarded using discard-changes rpc, unlocked and an exception is thrown async</li>
46  *     </ul>
47  *   </li>
48  *   <li>Commit and Unlock candidate datastore async</li>
49  * </ol>
50  */
51 public class WriteCandidateTx extends AbstractWriteTx {
52
53     private static final Logger LOG  = LoggerFactory.getLogger(WriteCandidateTx.class);
54
55     public WriteCandidateTx(final RemoteDeviceId id, final NetconfBaseOps netconfOps, final boolean rollbackSupport) {
56         this(id, netconfOps, rollbackSupport, true);
57     }
58
59     public WriteCandidateTx(RemoteDeviceId id, NetconfBaseOps netconfOps, boolean rollbackSupport,
60             boolean isLockAllowed) {
61         super(id, netconfOps, rollbackSupport, isLockAllowed);
62     }
63
64     @Override
65     protected synchronized void init() {
66         LOG.trace("{}: Initializing {} transaction", id, getClass().getSimpleName());
67         lock();
68     }
69
70     private void lock() {
71         if (!isLockAllowed) {
72             LOG.trace("Lock is not allowed.");
73             return;
74         }
75         final FutureCallback<DOMRpcResult> lockCandidateCallback = new FutureCallback<DOMRpcResult>() {
76             @Override
77             public void onSuccess(@Nonnull final DOMRpcResult result) {
78                 if (isSuccess(result)) {
79                     if (LOG.isTraceEnabled()) {
80                         LOG.trace("Lock candidate successful");
81                     }
82                 } else {
83                     LOG.warn("{}: lock candidate invoked unsuccessfully: {}", id, result.getErrors());
84                 }
85             }
86
87             @Override
88             public void onFailure(final Throwable throwable) {
89                 LOG.warn("Lock candidate operation failed", throwable);
90                 discardChanges();
91             }
92         };
93         resultsFutures.add(netOps.lockCandidate(lockCandidateCallback));
94     }
95
96     @Override
97     protected void cleanup() {
98         discardChanges();
99         cleanupOnSuccess();
100     }
101
102     /**
103      * This has to be non blocking since it is called from a callback on commit
104      * and its netty threadpool that is really sensitive to blocking calls.
105      */
106     private void discardChanges() {
107         netOps.discardChanges(new NetconfRpcFutureCallback("Discarding candidate", id));
108     }
109
110     @Override
111     public synchronized ListenableFuture<RpcResult<Void>> performCommit() {
112         resultsFutures.add(netOps.commit(new NetconfRpcFutureCallback("Commit", id)));
113         final ListenableFuture<RpcResult<Void>> txResult = resultsToTxStatus();
114
115         Futures.addCallback(txResult, new FutureCallback<RpcResult<Void>>() {
116             @Override
117             public void onSuccess(@Nullable final RpcResult<Void> result) {
118                 cleanupOnSuccess();
119             }
120
121             @Override
122             public void onFailure(final Throwable throwable) {
123                 // TODO If lock is cause of this failure cleanup will issue warning log
124                 // cleanup is trying to do unlock, but this will fail
125                 cleanup();
126             }
127         }, MoreExecutors.directExecutor());
128
129         return txResult;
130     }
131
132     protected void cleanupOnSuccess() {
133         unlock();
134     }
135
136     @Override
137     protected void editConfig(final YangInstanceIdentifier path,
138                               final Optional<NormalizedNode<?, ?>> data,
139                               final DataContainerChild<?, ?> editStructure,
140                               final Optional<ModifyAction> defaultOperation,
141                               final String operation) {
142
143         final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit candidate", id);
144
145         if (defaultOperation.isPresent()) {
146             resultsFutures.add(netOps.editConfigCandidate(
147                     editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport));
148         } else {
149             resultsFutures.add(netOps.editConfigCandidate(editConfigCallback, editStructure, rollbackSupport));
150         }
151     }
152
153     /**
154      * This has to be non blocking since it is called from a callback on commit
155      * and its netty threadpool that is really sensitive to blocking calls.
156      */
157     private void unlock() {
158         if (isLockAllowed) {
159             netOps.unlockCandidate(new NetconfRpcFutureCallback("Unlock candidate", id));
160         } else {
161             LOG.trace("Unlock is not allowed: {}", id);
162         }
163     }
164
165 }