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 / WriteCandidateRunningTx.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 org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
12 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
13 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Tx implementation for netconf devices that support only candidate datastore and writable running.
19  * The sequence goes exactly as with only candidate supported, with one addition:
20  * <ul>
21  *     <li>Running datastore is locked as the first thing and this lock has to succeed</li>
22  * </ul>
23  */
24 public class WriteCandidateRunningTx extends WriteCandidateTx {
25
26     private static final Logger LOG  = LoggerFactory.getLogger(WriteCandidateRunningTx.class);
27
28     public WriteCandidateRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
29                                    final boolean rollbackSupport) {
30         this(id, netOps, rollbackSupport, true);
31     }
32
33     public WriteCandidateRunningTx(RemoteDeviceId id, NetconfBaseOps netconfOps, boolean rollbackSupport,
34             boolean isLockAllowed) {
35         super(id, netconfOps, rollbackSupport, isLockAllowed);
36     }
37
38     @Override
39     protected synchronized void init() {
40         lockRunning();
41         super.init();
42     }
43
44     @Override
45     protected void cleanupOnSuccess() {
46         super.cleanupOnSuccess();
47         unlockRunning();
48     }
49
50     private void lockRunning() {
51         if (isLockAllowed) {
52             resultsFutures.add(netOps.lockRunning(new NetconfRpcFutureCallback("Lock running", id)));
53         } else {
54             LOG.trace("Lock is not allowed: {}", id);
55         }
56     }
57
58     /**
59      * This has to be non blocking since it is called from a callback on commit
60      * and its netty threadpool that is really sensitive to blocking calls.
61      */
62     private void unlockRunning() {
63         if (isLockAllowed) {
64             netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
65         } else {
66             LOG.trace("Unlock is not allowed: {}", id);
67         }
68     }
69 }