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.Optional;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
16 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
17 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
18 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Tx implementation for netconf devices that support only writable-running with no candidate.
29 * The sequence goes as:
31 * <li>Lock running datastore on tx construction
33 * <li> Lock has to succeed, if it does not, transaction is failed</li>
36 * <li>Edit-config in running N times
38 * <li>If any issue occurs during edit, datastore is unlocked and an exception is thrown</li>
41 * <li>Unlock running datastore on tx commit</li>
44 public class WriteRunningTx extends AbstractWriteTx {
46 private static final Logger LOG = LoggerFactory.getLogger(WriteRunningTx.class);
47 private final List<Change> changes = new ArrayList<>();
49 public WriteRunningTx(final RemoteDeviceId id, final NetconfBaseOps netOps,
50 final boolean rollbackSupport) {
51 super(netOps, id, rollbackSupport);
55 protected synchronized void init() {
60 resultsFutures.add(netOps.lockRunning(new NetconfRpcFutureCallback("Lock running", id)));
64 protected void cleanup() {
69 public synchronized ListenableFuture<RpcResult<Void>> performCommit() {
70 for (final Change change : changes) {
71 resultsFutures.add(change.execute(id, netOps, rollbackSupport));
74 return resultsToTxStatus();
78 protected void editConfig(final YangInstanceIdentifier path,
79 final Optional<NormalizedNode<?, ?>> data,
80 final DataContainerChild<?, ?> editStructure,
81 final Optional<ModifyAction> defaultOperation,
82 final String operation) {
83 changes.add(new Change(editStructure, defaultOperation));
86 private void unlock() {
87 netOps.unlockRunning(new NetconfRpcFutureCallback("Unlock running", id));
90 private static final class Change {
92 private final DataContainerChild<?, ?> editStructure;
93 private final Optional<ModifyAction> defaultOperation;
95 Change(final DataContainerChild<?, ?> editStructure, final Optional<ModifyAction> defaultOperation) {
96 this.editStructure = editStructure;
97 this.defaultOperation = defaultOperation;
100 private ListenableFuture<DOMRpcResult> execute(final RemoteDeviceId id, final NetconfBaseOps netOps,
101 final boolean rollbackSupport) {
102 final NetconfRpcFutureCallback editConfigCallback = new NetconfRpcFutureCallback("Edit running", id);
103 if (defaultOperation.isPresent()) {
104 return netOps.editConfigRunning(editConfigCallback, editStructure, defaultOperation.get(),
107 return netOps.editConfigRunning(editConfigCallback, editStructure, rollbackSupport);