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 / WriteRunningTx.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.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
15 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
16 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
17 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
18 import org.opendaylight.yangtools.yang.common.RpcResult;
19 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Tx implementation for netconf devices that support only writable-running with no candidate.
28  * The sequence goes as:
29  * <ol>
30  *   <li>Lock running datastore on tx construction
31  *     <ul>
32  *       <li> Lock has to succeed, if it does not, transaction is failed</li>
33  *     </ul>
34  *   </li>
35  *   <li>Edit-config in running N times
36  *     <ul>
37  *       <li>If any issue occurs during edit, datastore is unlocked and an exception is thrown</li>
38  *     </ul>
39  *   </li>
40  *   <li>Unlock running datastore on tx commit</li>
41  * </ol>
42  */
43 public class WriteRunningTx extends AbstractWriteTx {
44
45     private static final Logger LOG  = LoggerFactory.getLogger(WriteRunningTx.class);
46     private final List<Change> changes = new ArrayList<>();
47
48     public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
49                           final boolean rollbackSupport) {
50         this(id, netOps, rollbackSupport, true);
51     }
52
53     public WriteRunningTx(RemoteDeviceId id, NetconfBaseOps netconfOps, boolean rollbackSupport,
54             boolean isLockAllowed) {
55         super(id, netconfOps, rollbackSupport, isLockAllowed);
56     }
57
58     @Override
59     protected synchronized void init() {
60         lock();
61     }
62
63     private void lock() {
64         if (isLockAllowed) {
65             resultsFutures.add(netOps.lockRunning(new NetconfRpcFutureCallback("Lock running", id)));
66         } else {
67             LOG.trace("Lock is not allowed: {}", id);
68         }
69     }
70
71     @Override
72     protected void cleanup() {
73         unlock();
74     }
75
76     @Override
77     public synchronized ListenableFuture<RpcResult<Void>> performCommit() {
78         for (final Change change : changes) {
79             resultsFutures.add(change.execute(id, netOps, rollbackSupport));
80         }
81         unlock();
82         return resultsToTxStatus();
83     }
84
85     @Override
86     protected void editConfig(final YangInstanceIdentifier path,
87                               final Optional<NormalizedNode<?, ?>> data,
88                               final DataContainerChild<?, ?> editStructure,
89                               final Optional<ModifyAction> defaultOperation,
90                               final String operation) {
91         changes.add(new Change(editStructure, defaultOperation));
92     }
93
94     private void unlock() {
95         if (isLockAllowed) {
96             netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
97         } else {
98             LOG.trace("Unlock is not allowed: {}", id);
99         }
100     }
101
102     private static final class Change {
103
104         private final DataContainerChild<?, ?> editStructure;
105         private final Optional<ModifyAction> defaultOperation;
106
107         Change(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) {
108             this.editStructure = editStructure;
109             this.defaultOperation = defaultOperation;
110         }
111
112         private ListenableFuture<DOMRpcResult> execute(final RemoteDeviceId id, final NetconfBaseOps netOps,
113                                                        final boolean rollbackSupport) {
114             final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit running", id);
115             if (defaultOperation.isPresent()) {
116                 return netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.get(),
117                     rollbackSupport);
118             } else {
119                 return netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport);
120             }
121         }
122     }
123 }