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.controller.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.ListenableFuture;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
22 import org.opendaylight.netconf.api.NetconfDocumentedException;
23 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
24 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
29 import org.opendaylight.yangtools.yang.data.api.schema.MixinNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public abstract class AbstractWriteTx implements DOMDataWriteTransaction {
36 private static final Logger LOG = LoggerFactory.getLogger(AbstractWriteTx.class);
38 private final long defaultRequestTimeoutMillis;
39 protected final RemoteDeviceId id;
40 protected final NetconfBaseOps netOps;
41 protected final boolean rollbackSupport;
42 // Allow commit to be called only once
43 protected boolean finished = false;
45 public AbstractWriteTx(final long requestTimeoutMillis, final NetconfBaseOps netOps, final RemoteDeviceId id, final boolean rollbackSupport) {
46 this.defaultRequestTimeoutMillis = requestTimeoutMillis;
49 this.rollbackSupport = rollbackSupport;
53 static boolean isSuccess(final DOMRpcResult result) {
54 return result.getErrors().isEmpty();
57 protected void checkNotFinished() {
58 Preconditions.checkState(!isFinished(), "%s: Transaction %s already finished", id, getIdentifier());
61 protected boolean isFinished() {
65 protected void invokeBlocking(final String msg, final Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>> op) throws NetconfDocumentedException {
67 final DOMRpcResult compositeNodeRpcResult = op.apply(netOps).get(defaultRequestTimeoutMillis, TimeUnit.MILLISECONDS);
68 if(isSuccess(compositeNodeRpcResult) == false) {
69 throw new NetconfDocumentedException(id + ": " + msg + " failed: " + compositeNodeRpcResult.getErrors(), NetconfDocumentedException.ErrorType.application,
70 NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
72 } catch (final InterruptedException e) {
73 Thread.currentThread().interrupt();
74 throw new RuntimeException(e);
75 } catch (final ExecutionException | TimeoutException e) {
76 throw new NetconfDocumentedException(id + ": " + msg + " failed: " + e.getMessage(), e, NetconfDocumentedException.ErrorType.application,
77 NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorSeverity.warning);
82 public synchronized boolean cancel() {
92 protected abstract void init();
94 protected abstract void cleanup();
97 public Object getIdentifier() {
102 public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
103 checkEditable(store);
105 // trying to write only mixin nodes (not visible when serialized). Ignoring. Some devices cannot handle empty edit-config rpc
106 if(containsOnlyNonVisibleData(path, data)) {
107 LOG.debug("Ignoring put for {} and data {}. Resulting data structure is empty.", path, data);
113 netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data), Optional.of(ModifyAction.REPLACE), path), Optional.of(ModifyAction.NONE));
114 } catch (final NetconfDocumentedException e) {
115 handleEditException(path, data, e, "putting");
119 protected abstract void handleEditException(YangInstanceIdentifier path, NormalizedNode<?, ?> data, NetconfDocumentedException e, String editType);
120 protected abstract void handleDeleteException(YangInstanceIdentifier path, NetconfDocumentedException e);
123 public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
124 checkEditable(store);
126 // trying to write only mixin nodes (not visible when serialized). Ignoring. Some devices cannot handle empty edit-config rpc
127 if (containsOnlyNonVisibleData(path, data)) {
128 LOG.debug("Ignoring merge for {} and data {}. Resulting data structure is empty.", path, data);
134 netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>fromNullable(data), Optional.<ModifyAction>absent(), path), Optional.<ModifyAction>absent());
135 } catch (final NetconfDocumentedException e) {
136 handleEditException(path, data, e, "merge");
141 * Check whether the data to be written consists only from mixins
143 private static boolean containsOnlyNonVisibleData(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
144 // There's only one such case:top level list (pathArguments == 1 && data is Mixin)
145 // any other mixin nodes are contained by a "regular" node thus visible when serialized
146 return path.getPathArguments().size() == 1 && data instanceof MixinNode;
150 public synchronized void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
151 checkEditable(store);
155 netOps.createEditConfigStrcture(Optional.<NormalizedNode<?, ?>>absent(), Optional.of(ModifyAction.DELETE), path), Optional.of(ModifyAction.NONE));
156 } catch (final NetconfDocumentedException e) {
157 handleDeleteException(path, e);
162 public final ListenableFuture<RpcResult<TransactionStatus>> commit() {
166 return performCommit();
169 protected abstract ListenableFuture<RpcResult<TransactionStatus>> performCommit();
171 private void checkEditable(final LogicalDatastoreType store) {
173 Preconditions.checkArgument(store == LogicalDatastoreType.CONFIGURATION, "Can edit only configuration data, not %s", store);
176 protected abstract void editConfig(DataContainerChild<?, ?> editStructure, Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException;