Value for transactions moved to yang module
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / tx / 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
9 package org.opendaylight.controller.sal.connect.netconf.sal.tx;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
21 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
22 import org.opendaylight.controller.sal.connect.netconf.util.NetconfRpcFutureCallback;
23 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Tx implementation for netconf devices that support only writable-running with no candidate
35  * The sequence goes as:
36  * <ol>
37  * <li/> Lock running datastore on tx construction
38  * <ul>
39  * <li/> Lock has to succeed, if it does not, transaction is failed
40  * </ul>
41  * <li/> Edit-config in running N times
42  * <ul>
43  * <li/> If any issue occurs during edit, datastore is unlocked and an exception is thrown
44  * </ul>
45  * <li/> Unlock running datastore on tx commit
46  * </ol>
47  */
48 public class WriteRunningTx extends AbstractWriteTx {
49
50     private static final Logger LOG  = LoggerFactory.getLogger(WriteRunningTx.class);
51
52     public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
53                           final NetconfSessionPreferences netconfSessionPreferences,
54                           final long defaultRequestTimeoutMillis) {
55         super(netOps, id, netconfSessionPreferences, defaultRequestTimeoutMillis);
56     }
57
58     @Override
59     protected synchronized void init() {
60         lock();
61     }
62
63     private void lock() {
64         try {
65             invokeBlocking("Lock running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
66                 @Override
67                 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
68                     return input.lockRunning(new NetconfRpcFutureCallback("Lock running", id));
69                 }
70             });
71         } catch (final NetconfDocumentedException e) {
72             LOG.warn("{}: Failed to initialize netconf transaction (lock running)", e);
73             finished = true;
74             throw new RuntimeException(id + ": Failed to initialize netconf transaction (lock running)", e);
75         }
76     }
77
78     @Override
79     protected void cleanup() {
80         unlock();
81     }
82
83     @Override
84     protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
85         LOG.warn("{}: Error " + editType + " data to (running){}, data: {}, canceling", id, path, data, e);
86         cancel();
87         throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
88     }
89
90     @Override
91     protected void handleDeleteException(final YangInstanceIdentifier path, final NetconfDocumentedException e) {
92         LOG.warn("{}: Error deleting data (running){}, canceling", id, path, e);
93         cancel();
94         throw new RuntimeException(id + ": Error while deleting (running)" + path, e);
95     }
96
97     @Override
98     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
99         final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
100             @Override
101             public Void apply(final RpcResult<TransactionStatus> input) {
102                 return null;
103             }
104         });
105
106         return Futures.makeChecked(commmitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
107             @Override
108             public TransactionCommitFailedException apply(final Exception input) {
109                 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
110             }
111         });
112     }
113
114     @Override
115     public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
116         unlock();
117         return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
118     }
119
120     @Override
121     protected void editConfig(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
122         invokeBlocking("Edit running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
123             @Override
124             public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
125                         return defaultOperation.isPresent()
126                                 ? input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure, defaultOperation.get(),
127                                 netconfSessionPreferences.isRollbackSupported())
128                                 : input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure,
129                                 netconfSessionPreferences.isRollbackSupported());
130             }
131         });
132     }
133
134     private void unlock() {
135         try {
136             invokeBlocking("Unlocking running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
137                 @Override
138                 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
139                     return input.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
140                 }
141             });
142         } catch (final NetconfDocumentedException e) {
143             LOG.warn("{}: Failed to unlock running datastore", e);
144             throw new RuntimeException(id + ": Failed to unlock running datastore", e);
145         }
146     }
147 }