28173b1da392186e924b1fd25defbae543315b48
[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.common.impl.util.compat.DataNormalizer;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionCapabilities;
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.CompositeNode;
27 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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 DataNormalizer normalizer, final NetconfSessionCapabilities netconfSessionPreferences) {
54         super(netOps, id, normalizer, netconfSessionPreferences);
55     }
56
57     @Override
58     protected synchronized void init() {
59         lock();
60     }
61
62     private void lock() {
63         try {
64             invokeBlocking("Lock running", new Function<NetconfBaseOps, ListenableFuture<RpcResult<CompositeNode>>>() {
65                 @Override
66                 public ListenableFuture<RpcResult<CompositeNode>> apply(final NetconfBaseOps input) {
67                     return input.lockRunning(new NetconfRpcFutureCallback("Lock running", id));
68                 }
69             });
70         } catch (final NetconfDocumentedException e) {
71             LOG.warn("{}: Failed to initialize netconf transaction (lock running)", e);
72             finished = true;
73             throw new RuntimeException(id + ": Failed to initialize netconf transaction (lock running)", e);
74         }
75     }
76
77     @Override
78     protected void cleanup() {
79         unlock();
80     }
81
82     @Override
83     protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
84         LOG.warn("{}: Error " + editType + " data to (running){}, data: {}, canceling", id, path, data, e);
85         cancel();
86         throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
87     }
88
89     @Override
90     protected void handleDeleteException(final YangInstanceIdentifier path, final NetconfDocumentedException e) {
91         LOG.warn("{}: Error deleting data (running){}, canceling", id, path, e);
92         cancel();
93         throw new RuntimeException(id + ": Error while deleting (running)" + path, e);
94     }
95
96     @Override
97     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
98         final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
99             @Override
100             public Void apply(final RpcResult<TransactionStatus> input) {
101                 return null;
102             }
103         });
104
105         return Futures.makeChecked(commmitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
106             @Override
107             public TransactionCommitFailedException apply(final Exception input) {
108                 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
109             }
110         });
111     }
112
113     @Override
114     public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
115         unlock();
116         return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
117     }
118
119     @Override
120     protected void editConfig(final CompositeNode editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
121         invokeBlocking("Edit running", new Function<NetconfBaseOps, ListenableFuture<RpcResult<CompositeNode>>>() {
122             @Override
123             public ListenableFuture<RpcResult<CompositeNode>> apply(final NetconfBaseOps input) {
124                         return defaultOperation.isPresent()
125                                 ? input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure, defaultOperation.get(),
126                                 netconfSessionPreferences.isRollbackSupported())
127                                 : input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure,
128                                 netconfSessionPreferences.isRollbackSupported());
129             }
130         });
131     }
132
133     private void unlock() {
134         try {
135             invokeBlocking("Unlocking running", new Function<NetconfBaseOps, ListenableFuture<RpcResult<CompositeNode>>>() {
136                 @Override
137                 public ListenableFuture<RpcResult<CompositeNode>> apply(final NetconfBaseOps input) {
138                     return input.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
139                 }
140             });
141         } catch (final NetconfDocumentedException e) {
142             LOG.warn("{}: Failed to unlock running datastore", e);
143             throw new RuntimeException(id + ": Failed to unlock running datastore", e);
144         }
145     }
146 }

©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.