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) {
55 super(netOps, id, rollbackSupport);
59 protected synchronized void init() {
65 invokeBlocking("Lock running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
67 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
68 return input.lockRunning(new NetconfRpcFutureCallback("Lock running", id));
71 } catch (final NetconfDocumentedException e) {
72 LOG.warn("{}: Failed to initialize netconf transaction (lock running)", id, e);
74 throw new RuntimeException(id + ": Failed to initialize netconf transaction (lock running)", e);
79 protected void cleanup() {
84 protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
85 LOG.warn("{}: Error {} data to (running){}, data: {}, canceling", id, editType, path, data, e);
87 throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
91 protected void handleDeleteException(final YangInstanceIdentifier path, final NetconfDocumentedException e) {
92 LOG.warn("{}: Error deleting data (running){}, canceling", id, path, e);
94 throw new RuntimeException(id + ": Error while deleting (running)" + path, e);
98 public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
99 final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
101 public Void apply(final RpcResult<TransactionStatus> input) {
106 return Futures.makeChecked(commmitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
108 public TransactionCommitFailedException apply(final Exception input) {
109 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
115 public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
117 return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
121 protected void editConfig(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
122 invokeBlocking("Edit running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
124 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
125 return defaultOperation.isPresent()
126 ? input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure, defaultOperation.get(),
128 : input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure,
134 private void unlock() {
136 invokeBlocking("Unlocking running", new Function<NetconfBaseOps, ListenableFuture<DOMRpcResult>>() {
138 public ListenableFuture<DOMRpcResult> apply(final NetconfBaseOps input) {
139 return input.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
142 } catch (final NetconfDocumentedException e) {
143 LOG.warn("{}: Failed to unlock running datastore", id, e);
144 throw new RuntimeException(id + ": Failed to unlock running datastore", e);