Avoid ClassCastException in remote-rpc-connector
[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 com.google.common.base.Function;
4 import com.google.common.base.Optional;
5 import com.google.common.base.Preconditions;
6 import com.google.common.util.concurrent.ListenableFuture;
7 import java.util.concurrent.ExecutionException;
8 import java.util.concurrent.TimeUnit;
9 import java.util.concurrent.TimeoutException;
10 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
13 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
16 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22
23 public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
24
25     private final long defaultRequestTimeoutMillis;
26     protected final RemoteDeviceId id;
27     protected final NetconfBaseOps netOps;
28     protected final boolean rollbackSupport;
29     // Allow commit to be called only once
30     protected boolean finished = false;
31
32     public AbstractWriteTx(final long requestTimeoutMillis, final NetconfBaseOps netOps, final RemoteDeviceId id, final boolean rollbackSupport) {
33         this.defaultRequestTimeoutMillis = requestTimeoutMillis;
34         this.netOps = netOps;
35         this.id = id;
36         this.rollbackSupport = rollbackSupport;
37         init();
38     }
39
40     static boolean isSuccess(final DOMRpcResult result) {
41         return result.getErrors().isEmpty();
42     }
43
44     protected void checkNotFinished() {
45         Preconditions.checkState(!isFinished(), "%s: Transaction %s already finished", id, getIdentifier());
46     }
47
48     protected boolean isFinished() {
49         return finished;
50     }
51
52     protected void invokeBlocking(final String msg, final Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>> op) throws NetconfDocumentedException {
53         try {
54             final DOMRpcResult compositeNodeRpcResult = op.apply(netOps).get(defaultRequestTimeoutMillis, TimeUnit.MILLISECONDS);
55             if(isSuccess(compositeNodeRpcResult) == false) {
56                 throw new NetconfDocumentedException(id + ": " + msg + " failed: " + compositeNodeRpcResult.getErrors(), NetconfDocumentedException.ErrorType.application,
57                         NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
58             }
59         } catch (final InterruptedException e) {
60             Thread.currentThread().interrupt();
61             throw new RuntimeException(e);
62         } catch (final ExecutionException | TimeoutException e) {
63             throw new NetconfDocumentedException(id + ": " + msg + " failed: " + e.getMessage(), e, NetconfDocumentedException.ErrorType.application,
64                     NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
65         }
66     }
67
68     @Override
69     public synchronized boolean cancel() {
70         if(isFinished()) {
71             return false;
72         }
73
74         finished = true;
75         cleanup();
76         return true;
77     }
78
79     protected abstract void init();
80
81     protected abstract void cleanup();
82
83     @Override
84     public Object getIdentifier() {
85         return this;
86     }
87
88     @Override
89     public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
90         checkEditable(store);
91
92         try {
93             editConfig(
94                     netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data), Optional.of(ModifyAction.REPLACE), path), 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             editConfig(
109                     netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data), Optional.<ModifyAction>absent(), path), Optional.<ModifyAction>absent());
110         } catch (final NetconfDocumentedException e) {
111             handleEditException(path, data, e, "merge");
112         }
113     }
114
115     @Override
116     public synchronized void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
117         checkEditable(store);
118
119         try {
120             editConfig(
121                     netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>absent(), Optional.of(ModifyAction.DELETE), path), Optional.of(ModifyAction.NONE));
122         } catch (final NetconfDocumentedException e) {
123             handleDeleteException(path, e);
124         }
125     }
126
127     @Override
128     public final ListenableFuture<RpcResult<TransactionStatus>> commit() {
129         checkNotFinished();
130         finished = true;
131
132         return performCommit();
133     }
134
135     protected abstract ListenableFuture<RpcResult<TransactionStatus>> performCommit();
136
137     private void checkEditable(final LogicalDatastoreType store) {
138         checkNotFinished();
139         Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION, "Can edit only configuration data, not %s", store);
140     }
141
142     protected abstract void editConfig(DataContainerChild<?, ?> editStructure, Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException;
143 }