2 * Copyright (c) 2014 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.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
19 import org.opendaylight.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
21 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Tx implementation for netconf devices that support only writable-running with no candidate
34 * The sequence goes as:
36 * <li>Lock running datastore on tx construction
38 * <li> Lock has to succeed, if it does not, transaction is failed</li>
41 * <li>Edit-config in running N times
43 * <li>If any issue occurs during edit, datastore is unlocked and an exception is thrown</li>
46 * <li>Unlock running datastore on tx commit</li>
49 public class WriteRunningTx extends AbstractWriteTx {
51 private static final Logger LOG = LoggerFactory.getLogger(WriteRunningTx.class);
53 public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
54 final boolean rollbackSupport, long requestTimeoutMillis) {
55 super(requestTimeoutMillis, netOps, id, rollbackSupport);
59 protected synchronized void init() {
64 final String operation = "Lock running";
66 invokeBlocking(operation, new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
68 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
69 return perfomRequestWithTimeout(operation, input.lockRunning(new NetconfRpcFutureCallback(operation, id)));
72 } catch (final NetconfDocumentedException e) {
73 LOG.warn("{}: Failed to initialize netconf transaction (lock running)", id, e);
75 throw new RuntimeException(id + ": Failed to initialize netconf transaction (lock running)", e);
80 protected void cleanup() {
85 protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
86 LOG.warn("{}: Error {} data to (running){}, data: {}, canceling", id, editType, path, data, e);
88 throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
92 protected void handleDeleteException(final YangInstanceIdentifier path, final NetconfDocumentedException e) {
93 LOG.warn("{}: Error deleting data (running){}, canceling", id, path, e);
95 throw new RuntimeException(id + ": Error while deleting (running)" + path, e);
99 public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
100 final ListenableFuture<Void> commitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
102 public Void apply(final RpcResult<TransactionStatus> input) {
107 return Futures.makeChecked(commitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
109 public TransactionCommitFailedException apply(final Exception input) {
110 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
116 public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
118 return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
122 protected void editConfig(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
123 final String operation = "Edit running";
124 invokeBlocking(operation, new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
126 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
127 return perfomRequestWithTimeout(operation, defaultOperation.isPresent()
128 ? input.editConfigRunning(new NetconfRpcFutureCallback(operation, id), editStructure, defaultOperation.get(),
130 : input.editConfigRunning(new NetconfRpcFutureCallback(operation, id), editStructure,
136 private void unlock() {
137 final String operation = "Unlocking running";
139 invokeBlocking(operation, new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
141 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
142 return perfomRequestWithTimeout(operation, input.unlockRunning(new NetconfRpcFutureCallback(operation, id)));
145 } catch (final NetconfDocumentedException e) {
146 LOG.warn("{}: Failed to unlock running datastore", id, e);
147 throw new RuntimeException(id + ": Failed to unlock running datastore", e);