Merge "Startup archetype: remove 'Impl' from config subsystem Module name."
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / tx / AbstractWriteTx.java
1 package org.opendaylight.controller.sal.connect.netconf.sal.tx;
2
3 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
4
5 import com.google.common.base.Function;
6 import com.google.common.base.Optional;
7 import com.google.common.base.Preconditions;
8 import com.google.common.util.concurrent.ListenableFuture;
9 import java.util.concurrent.ExecutionException;
10 import java.util.concurrent.TimeUnit;
11 import java.util.concurrent.TimeoutException;
12 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
18 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
19 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
21 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
22 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25
26 public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
27     protected final RemoteDeviceId id;
28     protected final NetconfBaseOps netOps;
29     protected final DataNormalizer normalizer;
30     protected final NetconfSessionPreferences netconfSessionPreferences;
31     // Allow commit to be called only once
32     protected boolean finished = false;
33
34     public AbstractWriteTx(final NetconfBaseOps netOps, final RemoteDeviceId id, final DataNormalizer normalizer, final NetconfSessionPreferences netconfSessionPreferences) {
35         this.netOps = netOps;
36         this.id = id;
37         this.normalizer = normalizer;
38         this.netconfSessionPreferences = netconfSessionPreferences;
39         init();
40     }
41
42     protected void checkNotFinished() {
43         Preconditions.checkState(!isFinished(), "%s: Transaction %s already finished", id, getIdentifier());
44     }
45
46     protected boolean isFinished() {
47         return finished;
48     }
49
50     protected void invokeBlocking(final String msg, final Function<NetconfBaseOps, ListenableFuture<RpcResult<CompositeNode>>> op) throws NetconfDocumentedException {
51         try {
52             final RpcResult<CompositeNode> compositeNodeRpcResult = op.apply(netOps).get(1L, TimeUnit.MINUTES);
53             if(compositeNodeRpcResult.isSuccessful() == false) {
54                 throw new NetconfDocumentedException(id + ": " + msg + " failed: " + compositeNodeRpcResult.getErrors(), NetconfDocumentedException.ErrorType.application,
55                         NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
56             }
57         } catch (final InterruptedException e) {
58             Thread.currentThread().interrupt();
59             throw new RuntimeException(e);
60         } catch (final ExecutionException | TimeoutException e) {
61             throw new NetconfDocumentedException(id + ": " + msg + " failed: " + e.getMessage(), e, NetconfDocumentedException.ErrorType.application,
62                     NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
63         }
64     }
65
66     @Override
67     public synchronized boolean cancel() {
68         if(isFinished()) {
69             return false;
70         }
71
72         finished = true;
73         cleanup();
74         return true;
75     }
76
77     protected abstract void init();
78
79     protected abstract void cleanup();
80
81     @Override
82     public Object getIdentifier() {
83         return this;
84     }
85
86     @Override
87     public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
88         checkEditable(store);
89
90         try {
91             final YangInstanceIdentifier legacyPath = ReadOnlyTx.toLegacyPath(normalizer, path, id);
92             final CompositeNode legacyData = normalizer.toLegacy(path, data);
93             editConfig(
94                     createEditConfigStructure(legacyPath, Optional.of(ModifyAction.REPLACE), Optional.fromNullable(legacyData)), Optional.of(ModifyAction.NONE));
95         } catch (final NetconfDocumentedException e) {
96             handleEditException(path, data, e, "putting");
97         }
98     }
99
100     protected abstract void handleEditException(YangInstanceIdentifier path, NormalizedNode<?, ?> data, NetconfDocumentedException e, String editType);
101     protected abstract void handleDeleteException(YangInstanceIdentifier path, NetconfDocumentedException e);
102
103     @Override
104     public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
105         checkEditable(store);
106
107         try {
108             final YangInstanceIdentifier legacyPath = ReadOnlyTx.toLegacyPath(normalizer, path, id);
109             final CompositeNode legacyData = normalizer.toLegacy(path, data);
110             editConfig(
111                     createEditConfigStructure(legacyPath, Optional.<ModifyAction>absent(), Optional.fromNullable(legacyData)), Optional.<ModifyAction>absent());
112         } catch (final NetconfDocumentedException e) {
113             handleEditException(path, data, e, "merge");
114         }
115     }
116
117     @Override
118     public synchronized void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
119         checkEditable(store);
120
121         try {
122             editConfig(createEditConfigStructure(
123                     ReadOnlyTx.toLegacyPath(normalizer, path, id), Optional.of(ModifyAction.DELETE),
124                     Optional.<CompositeNode>absent()), Optional.of(ModifyAction.NONE));
125         } catch (final NetconfDocumentedException e) {
126             handleDeleteException(path, e);
127         }
128     }
129
130     @Override
131     public final ListenableFuture<RpcResult<TransactionStatus>> commit() {
132         checkNotFinished();
133         finished = true;
134
135         return performCommit();
136     }
137
138     protected abstract ListenableFuture<RpcResult<TransactionStatus>> performCommit();
139
140     private void checkEditable(final LogicalDatastoreType store) {
141         checkNotFinished();
142         Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION, "Can edit only configuration data, not %s", store);
143     }
144
145     protected abstract void editConfig(CompositeNode editStructure, Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException;
146 }