Remove netconf.sal.connect.util
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / 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;
9
10 import org.opendaylight.netconf.client.mdsal.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 class WriteCandidateRunningTx extends WriteCandidateTx {
24     private static final Logger LOG  = LoggerFactory.getLogger(WriteCandidateRunningTx.class);
25
26     WriteCandidateRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps, final boolean rollbackSupport) {
27         this(id, netOps, rollbackSupport, true);
28     }
29
30     WriteCandidateRunningTx(final RemoteDeviceId id, final NetconfBaseOps netconfOps, final boolean rollbackSupport,
31             final boolean isLockAllowed) {
32         super(id, netconfOps, rollbackSupport, isLockAllowed);
33     }
34
35     @Override
36     synchronized void init() {
37         lockRunning();
38         super.init();
39     }
40
41     @Override
42     void cleanupOnSuccess() {
43         super.cleanupOnSuccess();
44         unlockRunning();
45     }
46
47     private void lockRunning() {
48         if (isLockAllowed) {
49             resultsFutures.add(netOps.lockRunning(new NetconfRpcFutureCallback("Lock running", id)));
50         } else {
51             LOG.trace("Lock is not allowed: {}", id);
52         }
53     }
54
55     /**
56      * This has to be non blocking since it is called from a callback on commit
57      * and its netty threadpool that is really sensitive to blocking calls.
58      */
59     private void unlockRunning() {
60         if (isLockAllowed) {
61             netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
62         } else {
63             LOG.trace("Unlock is not allowed: {}", id);
64         }
65     }
66 }