2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
23 import org.opendaylight.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
25 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
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.DataContainerChild;
30 import org.opendaylight.yangtools.yang.data.api.schema.MixinNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
38 private static final Logger LOG = LoggerFactory.getLogger(AbstractWriteTx.class);
40 protected final long defaultRequestTimeoutMillis;
41 protected final RemoteDeviceId id;
42 protected final NetconfBaseOps netOps;
43 protected final boolean rollbackSupport;
44 // Allow commit to be called only once
45 protected boolean finished = false;
47 public AbstractWriteTx(final long requestTimeoutMillis, final NetconfBaseOps netOps, final RemoteDeviceId id, final boolean rollbackSupport) {
48 this.defaultRequestTimeoutMillis = requestTimeoutMillis;
51 this.rollbackSupport = rollbackSupport;
55 static boolean isSuccess(final DOMRpcResult result) {
56 return result.getErrors().isEmpty();
59 protected void checkNotFinished() {
60 Preconditions.checkState(!isFinished(), "%s: Transaction %s already finished", id, getIdentifier());
63 protected boolean isFinished() {
67 protected void invokeBlocking(final String msg, final Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>> op) throws NetconfDocumentedException {
69 final DOMRpcResult compositeNodeRpcResult = op.apply(netOps).get();
70 if(isSuccess(compositeNodeRpcResult) == false) {
71 throw new NetconfDocumentedException(id + ": " + msg + " failed: " + compositeNodeRpcResult.getErrors(), NetconfDocumentedException.ErrorType.application,
72 NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
74 } catch (final InterruptedException e) {
75 Thread.currentThread().interrupt();
76 throw new RuntimeException(e);
77 } catch (final ExecutionException e) {
78 throw new NetconfDocumentedException(id + ": " + msg + " failed: " + e.getMessage(), e, NetconfDocumentedException.ErrorType.application,
79 NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
84 public synchronized boolean cancel() {
94 protected abstract void init();
96 protected abstract void cleanup();
99 public Object getIdentifier() {
104 public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
105 checkEditable(store);
107 // trying to write only mixin nodes (not visible when serialized). Ignoring. Some devices cannot handle empty edit-config rpc
108 if(containsOnlyNonVisibleData(path, data)) {
109 LOG.debug("Ignoring put for {} and data {}. Resulting data structure is empty.", path, data);
115 netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data), Optional.of(ModifyAction.REPLACE), path), Optional.of(ModifyAction.NONE));
116 } catch (final NetconfDocumentedException e) {
117 handleEditException(path, data, e, "putting");
121 protected abstract void handleEditException(YangInstanceIdentifier path, NormalizedNode<?, ?> data, NetconfDocumentedException e, String editType);
122 protected abstract void handleDeleteException(YangInstanceIdentifier path, NetconfDocumentedException e);
125 public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
126 checkEditable(store);
128 // trying to write only mixin nodes (not visible when serialized). Ignoring. Some devices cannot handle empty edit-config rpc
129 if (containsOnlyNonVisibleData(path, data)) {
130 LOG.debug("Ignoring merge for {} and data {}. Resulting data structure is empty.", path, data);
136 netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data), Optional.<ModifyAction>absent(), path), Optional.<ModifyAction>absent());
137 } catch (final NetconfDocumentedException e) {
138 handleEditException(path, data, e, "merge");
143 * Check whether the data to be written consists only from mixins
145 private static boolean containsOnlyNonVisibleData(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
146 // There's only one such case:top level list (pathArguments == 1 && data is Mixin)
147 // any other mixin nodes are contained by a "regular" node thus visible when serialized
148 return path.getPathArguments().size() == 1 && data instanceof MixinNode;
152 public synchronized void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
153 checkEditable(store);
157 netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>absent(), Optional.of(ModifyAction.DELETE), path), Optional.of(ModifyAction.NONE));
158 } catch (final NetconfDocumentedException e) {
159 handleDeleteException(path, e);
164 public final ListenableFuture<RpcResult<TransactionStatus>> commit() {
168 return performCommit();
171 protected abstract ListenableFuture<RpcResult<TransactionStatus>> performCommit();
173 private void checkEditable(final LogicalDatastoreType store) {
175 Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION, "Can edit only configuration data, not %s", store);
178 protected abstract void editConfig(DataContainerChild<?, ?> editStructure, Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException;
181 protected ListenableFuture<DOMRpcResult> perfomRequestWithTimeout(String operation, ListenableFuture<DOMRpcResult> future) {
183 future.get(defaultRequestTimeoutMillis, TimeUnit.MILLISECONDS);
184 } catch (InterruptedException | ExecutionException e) {
185 LOG.error("{}: {} failed with error", operation, id, e);
186 return Futures.immediateFailedCheckedFuture(new RuntimeException(id + ": " + operation + " failed"));
187 } catch (TimeoutException e) {
188 LOG.warn("{}: Unable to {} after {} milliseconds", id, operation, defaultRequestTimeoutMillis, e);
189 return Futures.immediateFailedCheckedFuture(new SchemaSourceException(e.getMessage()));