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