Rename netconf.sal.connect.netconf.sal
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / spi / 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.client.mdsal.spi;
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.api.EffectiveOperation;
16 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
17 import org.opendaylight.netconf.client.mdsal.impl.NetconfBaseOps;
18 import org.opendaylight.netconf.client.mdsal.impl.NetconfRpcFutureCallback;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
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 class WriteRunningTx extends AbstractWriteTx {
44     private static final Logger LOG  = LoggerFactory.getLogger(WriteRunningTx.class);
45
46     private final List<Change> changes = new ArrayList<>();
47
48     WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps, final boolean rollbackSupport) {
49         this(id, netOps, rollbackSupport, true);
50     }
51
52     WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netconfOps, final boolean rollbackSupport,
53             final boolean isLockAllowed) {
54         super(id, netconfOps, rollbackSupport, isLockAllowed);
55     }
56
57     @Override
58     synchronized void init() {
59         lock();
60     }
61
62     private void lock() {
63         if (isLockAllowed) {
64             resultsFutures.add(netOps.lockRunning(new NetconfRpcFutureCallback("Lock running", id)));
65         } else {
66             LOG.trace("Lock is not allowed: {}", id);
67         }
68     }
69
70     @Override
71     void cleanup() {
72         unlock();
73     }
74
75     @Override
76     public synchronized ListenableFuture<RpcResult<Void>> performCommit() {
77         for (final Change change : changes) {
78             resultsFutures.add(change.execute(id, netOps, rollbackSupport));
79         }
80         unlock();
81         return resultsToTxStatus();
82     }
83
84     @Override
85     protected void editConfig(final YangInstanceIdentifier path, final Optional<NormalizedNode> data,
86                               final DataContainerChild editStructure,
87                               final Optional<EffectiveOperation> defaultOperation,
88                               final String operation) {
89         changes.add(new Change(editStructure, defaultOperation));
90     }
91
92     private void unlock() {
93         if (isLockAllowed) {
94             netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
95         } else {
96             LOG.trace("Unlock is not allowed: {}", id);
97         }
98     }
99
100     private static final class Change {
101         private final DataContainerChild editStructure;
102         private final Optional<EffectiveOperation> defaultOperation;
103
104         Change(final DataContainerChild editStructure, final Optional<EffectiveOperation> defaultOperation) {
105             this.editStructure = editStructure;
106             this.defaultOperation = defaultOperation;
107         }
108
109         ListenableFuture<? extends DOMRpcResult> execute(final RemoteDeviceId id, final NetconfBaseOps netOps,
110                                                          final boolean rollbackSupport) {
111             final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit running", id);
112             if (defaultOperation.isPresent()) {
113                 return netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.orElseThrow(),
114                     rollbackSupport);
115             } else {
116                 return netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport);
117             }
118         }
119     }
120 }