Merge "Add simulated discard-changes to testtool default datastore"
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / 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.netconf.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.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.netconf.api.NetconfDocumentedException;
21 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
22 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
23 import org.opendaylight.netconf.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</li>
40  *     </ul>
41  *   </li>
42  *   <li>Edit-config in running N times
43  *     <ul>
44  *       <li>If any issue occurs during edit, datastore is unlocked and an exception is thrown</li>
45  *     </ul>
46  *   </li>
47  *   <li>Unlock running datastore on tx commit</li>
48  * </ol>
49  */
50 public class WriteRunningTx extends AbstractWriteTx {
51
52     private static final Logger LOG  = LoggerFactory.getLogger(WriteRunningTx.class);
53
54     public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
55                           final boolean rollbackSupport) {
56         super(netOps, id, rollbackSupport);
57     }
58
59     @Override
60     protected synchronized void init() {
61         lock();
62     }
63
64     private void lock() {
65         final FutureCallback<DOMRpcResult> lockCallback = new FutureCallback<DOMRpcResult>() {
66             @Override
67             public void onSuccess(DOMRpcResult result) {
68                 if (isSuccess(result)) {
69                     if (LOG.isTraceEnabled()) {
70                         LOG.trace("Lock running succesfull");
71                     }
72                 } else {
73                     LOG.warn("{}: lock running invoked unsuccessfully: {}", id, result.getErrors());
74                 }
75             }
76
77             @Override
78             public void onFailure(Throwable t) {
79                 LOG.warn("Lock running operation failed. {}", t);
80                 throw new RuntimeException(id + ": Failed to lock running datastore", t);
81             }
82         };
83         netOps.lockRunning(lockCallback);
84     }
85
86     @Override
87     protected void cleanup() {
88         unlock();
89     }
90
91     @Override
92     protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
93         LOG.warn("{}: Error {} data to (running){}, data: {}, canceling", id, editType, path, data, e);
94         cancel();
95         throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
96     }
97
98     @Override
99     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
100         final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
101             @Override
102             public Void apply(final RpcResult<TransactionStatus> input) {
103                 return null;
104             }
105         });
106
107         return Futures.makeChecked(commmitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
108             @Override
109             public TransactionCommitFailedException apply(final Exception input) {
110                 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
111             }
112         });
113     }
114
115     @Override
116     public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
117         unlock();
118         return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
119     }
120
121     @Override
122     protected void editConfig(final YangInstanceIdentifier path,
123                               final Optional<NormalizedNode<?, ?>> data,
124                               final DataContainerChild<?, ?> editStructure,
125                               final Optional<ModifyAction> defaultOperation,
126                               final String operation) {
127         FutureCallback<DOMRpcResult> editConfigCallback = new FutureCallback<DOMRpcResult>() {
128             @Override
129             public void onSuccess(DOMRpcResult result) {
130                 if (isSuccess(result)) {
131                     if (LOG.isTraceEnabled()) {
132                         LOG.trace("Edit running succesfull");
133                     }
134                 } else {
135                     LOG.warn("{}: Edit running invoked unsuccessfully: {}", id, result.getErrors());
136                 }
137             }
138
139             @Override
140             public void onFailure(Throwable t) {
141                 LOG.warn("Edit running operation failed. {}", t);
142                 NetconfDocumentedException e = new NetconfDocumentedException(id + ": Edit running operation failed.", NetconfDocumentedException.ErrorType.application,
143                         NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
144                 handleEditException(path, data.orNull(), e, operation);
145             }
146         };
147         if (defaultOperation.isPresent()) {
148             netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.get(), rollbackSupport);
149         } else {
150             netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport);
151         }
152     }
153
154     private void unlock() {
155         netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
156     }
157 }