Integrate netconf-events-mdsal
[netconf.git] / plugins / 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.EffectiveOperation;
17 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
18 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
19 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
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     private static final Logger LOG  = LoggerFactory.getLogger(WriteCandidateTx.class);
50
51     public WriteCandidateTx(final RemoteDeviceId id, final NetconfBaseOps netconfOps, final boolean rollbackSupport) {
52         this(id, netconfOps, rollbackSupport, true);
53     }
54
55     public WriteCandidateTx(final RemoteDeviceId id, final NetconfBaseOps netconfOps, final boolean rollbackSupport,
56             final boolean isLockAllowed) {
57         super(id, netconfOps, rollbackSupport, isLockAllowed);
58     }
59
60     @Override
61     protected synchronized void init() {
62         LOG.trace("{}: Initializing {} transaction", id, getClass().getSimpleName());
63         lock();
64     }
65
66     private void lock() {
67         if (!isLockAllowed) {
68             LOG.trace("Lock is not allowed.");
69             return;
70         }
71         final var lockCandidateCallback = new FutureCallback<DOMRpcResult>() {
72             @Override
73             public void onSuccess(final DOMRpcResult result) {
74                 if (isSuccess(result)) {
75                     if (LOG.isTraceEnabled()) {
76                         LOG.trace("Lock candidate successful");
77                     }
78                 } else {
79                     LOG.warn("{}: lock candidate invoked unsuccessfully: {}", id, result.errors());
80                 }
81             }
82
83             @Override
84             public void onFailure(final Throwable throwable) {
85                 LOG.warn("Lock candidate operation failed", throwable);
86                 discardChanges();
87             }
88         };
89         resultsFutures.add(netOps.lockCandidate(lockCandidateCallback));
90     }
91
92     @Override
93     protected void cleanup() {
94         discardChanges();
95         cleanupOnSuccess();
96     }
97
98     /**
99      * This has to be non blocking since it is called from a callback on commit
100      * and its netty threadpool that is really sensitive to blocking calls.
101      */
102     private void discardChanges() {
103         netOps.discardChanges(new NetconfRpcFutureCallback("Discarding candidate", id));
104     }
105
106     @Override
107     public synchronized ListenableFuture<RpcResult<Void>> performCommit() {
108         resultsFutures.add(netOps.commit(new NetconfRpcFutureCallback("Commit", id)));
109         final var txResult = resultsToTxStatus();
110
111         Futures.addCallback(txResult, new FutureCallback<>() {
112             @Override
113             public void onSuccess(final RpcResult<Void> result) {
114                 cleanupOnSuccess();
115             }
116
117             @Override
118             public void onFailure(final Throwable throwable) {
119                 // TODO If lock is cause of this failure cleanup will issue warning log
120                 // cleanup is trying to do unlock, but this will fail
121                 cleanup();
122             }
123         }, MoreExecutors.directExecutor());
124
125         return txResult;
126     }
127
128     protected void cleanupOnSuccess() {
129         unlock();
130     }
131
132     @Override
133     protected void editConfig(final YangInstanceIdentifier path, final Optional<NormalizedNode> data,
134             final DataContainerChild editStructure, final Optional<EffectiveOperation> defaultOperation,
135             final String operation) {
136
137         final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit candidate", id);
138
139         if (defaultOperation.isPresent()) {
140             resultsFutures.add(netOps.editConfigCandidate(
141                     editConfigCallback, editStructure, defaultOperation.orElseThrow(), rollbackSupport));
142         } else {
143             resultsFutures.add(netOps.editConfigCandidate(editConfigCallback, editStructure, rollbackSupport));
144         }
145     }
146
147     /**
148      * This has to be non blocking since it is called from a callback on commit
149      * and its netty threadpool that is really sensitive to blocking calls.
150      */
151     private void unlock() {
152         if (isLockAllowed) {
153             netOps.unlockCandidate(new NetconfRpcFutureCallback("Unlock candidate", id));
154         } else {
155             LOG.trace("Unlock is not allowed: {}", id);
156         }
157     }
158 }