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