1 package org.opendaylight.controller.sal.connect.netconf.sal.tx;
3 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
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.NetconfSessionCapabilities;
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;
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 NetconfSessionCapabilities netconfSessionPreferences;
31 // Allow commit to be called only once
32 protected boolean finished = false;
34 public AbstractWriteTx(final NetconfBaseOps netOps, final RemoteDeviceId id, final DataNormalizer normalizer, final NetconfSessionCapabilities netconfSessionPreferences) {
37 this.normalizer = normalizer;
38 this.netconfSessionPreferences = netconfSessionPreferences;
42 protected void checkNotFinished() {
43 Preconditions.checkState(!isFinished(), "%s: Transaction %s already finished", id, getIdentifier());
46 protected boolean isFinished() {
50 protected void invokeBlocking(final String msg, final Function<NetconfBaseOps, ListenableFuture<RpcResult<CompositeNode>>> op) throws NetconfDocumentedException {
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);
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);
67 public synchronized boolean cancel() {
77 protected abstract void init();
79 protected abstract void cleanup();
82 public Object getIdentifier() {
87 public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
91 final YangInstanceIdentifier legacyPath = ReadOnlyTx.toLegacyPath(normalizer, path, id);
92 final CompositeNode legacyData = normalizer.toLegacy(path, data);
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");
100 protected abstract void handleEditException(YangInstanceIdentifier path, NormalizedNode<?, ?> data, NetconfDocumentedException e, String editType);
101 protected abstract void handleDeleteException(YangInstanceIdentifier path, NetconfDocumentedException e);
104 public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
105 checkEditable(store);
108 final YangInstanceIdentifier legacyPath = ReadOnlyTx.toLegacyPath(normalizer, path, id);
109 final CompositeNode legacyData = normalizer.toLegacy(path, data);
111 createEditConfigStructure(legacyPath, Optional.<ModifyAction>absent(), Optional.fromNullable(legacyData)), Optional.<ModifyAction>absent());
112 } catch (final NetconfDocumentedException e) {
113 handleEditException(path, data, e, "merge");
118 public synchronized void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
119 checkEditable(store);
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);
131 public final ListenableFuture<RpcResult<TransactionStatus>> commit() {
135 return performCommit();
138 protected abstract ListenableFuture<RpcResult<TransactionStatus>> performCommit();
140 private void checkEditable(final LogicalDatastoreType store) {
142 Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION, "Can edit only configuration data, not %s", store);
145 protected abstract void editConfig(CompositeNode editStructure, Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException;