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.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.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.common.impl.util.compat.DataNormalizer;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionCapabilities;
21 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
22 import org.opendaylight.controller.sal.connect.netconf.util.NetconfRpcFutureCallback;
23 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
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.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Tx implementation for netconf devices that support only writable-running with no candidate
35 * The sequence goes as:
37 * <li/> Lock running datastore on tx construction
39 * <li/> Lock has to succeed, if it does not, transaction is failed
41 * <li/> Edit-config in running N times
43 * <li/> If any issue occurs during edit, datastore is unlocked and an exception is thrown
45 * <li/> Unlock running datastore on tx commit
48 public class WriteRunningTx extends AbstractWriteTx {
50 private static final Logger LOG = LoggerFactory.getLogger(WriteRunningTx.class);
52 public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
53 final DataNormalizer normalizer, final NetconfSessionCapabilities netconfSessionPreferences) {
54 super(netOps, id, normalizer, netconfSessionPreferences);
58 protected synchronized void init() {
64 invokeBlocking("Lock running", new Function<NetconfBaseOps, ListenableFuture<RpcResult<CompositeNode>>>() {
66 public ListenableFuture<RpcResult<CompositeNode>> apply(final NetconfBaseOps input) {
67 return input.lockRunning(new NetconfRpcFutureCallback("Lock running", id));
70 } catch (final NetconfDocumentedException e) {
71 LOG.warn("{}: Failed to initialize netconf transaction (lock running)", e);
73 throw new RuntimeException(id + ": Failed to initialize netconf transaction (lock running)", e);
78 protected void cleanup() {
83 protected void handleEditException(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final NetconfDocumentedException e, final String editType) {
84 LOG.warn("{}: Error " + editType + " data to (running){}, data: {}, canceling", id, path, data, e);
86 throw new RuntimeException(id + ": Error while " + editType + ": (running)" + path, e);
90 protected void handleDeleteException(final YangInstanceIdentifier path, final NetconfDocumentedException e) {
91 LOG.warn("{}: Error deleting data (running){}, canceling", id, path, e);
93 throw new RuntimeException(id + ": Error while deleting (running)" + path, e);
97 public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
98 final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() {
100 public Void apply(final RpcResult<TransactionStatus> input) {
105 return Futures.makeChecked(commmitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() {
107 public TransactionCommitFailedException apply(final Exception input) {
108 return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input);
114 public synchronized ListenableFuture<RpcResult<TransactionStatus>> performCommit() {
116 return Futures.immediateFuture(RpcResultBuilder.success(TransactionStatus.COMMITED).build());
120 protected void editConfig(final CompositeNode editStructure, final Optional<ModifyAction> defaultOperation) throws NetconfDocumentedException {
121 invokeBlocking("Edit running", new Function<NetconfBaseOps, ListenableFuture<RpcResult<CompositeNode>>>() {
123 public ListenableFuture<RpcResult<CompositeNode>> apply(final NetconfBaseOps input) {
124 return defaultOperation.isPresent()
125 ? input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure, defaultOperation.get(),
126 netconfSessionPreferences.isRollbackSupported())
127 : input.editConfigRunning(new NetconfRpcFutureCallback("Edit running", id), editStructure,
128 netconfSessionPreferences.isRollbackSupported());
133 private void unlock() {
135 invokeBlocking("Unlocking running", new Function<NetconfBaseOps, ListenableFuture<RpcResult<CompositeNode>>>() {
137 public ListenableFuture<RpcResult<CompositeNode>> apply(final NetconfBaseOps input) {
138 return input.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
141 } catch (final NetconfDocumentedException e) {
142 LOG.warn("{}: Failed to unlock running datastore", e);
143 throw new RuntimeException(id + ": Failed to unlock running datastore", e);