BUG-2310 - widen yang model for netconf-node.
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.sal.connect.netconf.sal.tx;
10
11 import com.google.common.base.Function;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
16 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
17 import org.opendaylight.controller.sal.connect.netconf.util.NetconfRpcFutureCallback;
18 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Tx implementation for netconf devices that support only candidate datastore and writable running
26  * The sequence goes exactly as with only candidate supported, with one addition:
27  * <ul>
28  *     <li>Running datastore is locked as the first thing and this lock has to succeed</li>
29  * </ul>
30  */
31 public class WriteCandidateRunningTx extends WriteCandidateTx {
32
33     private static final Logger LOG  = LoggerFactory.getLogger(WriteCandidateRunningTx.class);
34
35     public WriteCandidateRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps, final DataNormalizer normalizer, final NetconfSessionPreferences netconfSessionPreferences) {
36         super(id, netOps, normalizer, netconfSessionPreferences);
37     }
38
39     @Override
40     protected synchronized void init() {
41         lockRunning();
42         super.init();
43     }
44
45     @Override
46     protected void cleanupOnSuccess() {
47         super.cleanupOnSuccess();
48         unlockRunning();
49     }
50
51     private void lockRunning() {
52         try {
53             invokeBlocking("Lock running", new Function<NetconfBaseOps, ListenableFuture<RpcResult<CompositeNode>>>() {
54                 @Override
55                 public ListenableFuture<RpcResult<CompositeNode>> apply(final NetconfBaseOps input) {
56                     return input.lockRunning(new NetconfRpcFutureCallback("Lock running", id));
57                 }
58             });
59         } catch (final NetconfDocumentedException e) {
60             LOG.warn("{}: Failed to lock running. Failed to initialize transaction", e);
61             finished = true;
62             throw new RuntimeException(id + ": Failed to lock running. Failed to initialize transaction", e);
63         }
64     }
65
66     /**
67      * This has to be non blocking since it is called from a callback on commit and its netty threadpool that is really sensitive to blocking calls
68      */
69     private void unlockRunning() {
70         netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
71     }
72 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.