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