2 * Copyright (c) 2016 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.topology.singleton.impl.tx;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
24 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
25 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceDataBroker;
26 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
28 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import scala.concurrent.Future;
35 import scala.concurrent.impl.Promise.DefaultPromise;
37 public class NetconfMasterDOMTransaction implements NetconfDOMTransaction {
39 private static final Logger LOG = LoggerFactory.getLogger(NetconfMasterDOMTransaction.class);
41 private final RemoteDeviceId id;
42 private final DOMDataBroker delegateBroker;
44 private DOMDataReadOnlyTransaction readTx;
45 private DOMDataWriteTransaction writeTx;
47 public NetconfMasterDOMTransaction(final RemoteDeviceId id,
48 final SchemaContext schemaContext,
49 final DOMRpcService rpc,
50 final NetconfSessionPreferences netconfSessionPreferences) {
51 this(id, new NetconfDeviceDataBroker(id, schemaContext, rpc, netconfSessionPreferences));
54 public NetconfMasterDOMTransaction(final RemoteDeviceId id, final DOMDataBroker delegateBroker) {
56 this.delegateBroker = delegateBroker;
58 // only ever need 1 readTx since it doesnt need to be closed
59 readTx = delegateBroker.newReadOnlyTransaction();
63 public void openTransaction() {
64 // TODO We don't have to do anything here since
65 // NetconfProxyDOMTransactions and RemoteOperationTxProcessor do all
66 // the work regarding opening transactions. But maybe we should check
67 // for open transaction here instead in RemoteOperationTxProcessor
71 public Future<Optional<NormalizedNodeMessage>> read(final LogicalDatastoreType store,
72 final YangInstanceIdentifier path) {
73 LOG.trace("{}: Read[{}] {} via NETCONF: {}", id, readTx.getIdentifier(), store, path);
75 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture = readTx.read(store, path);
77 final DefaultPromise<Optional<NormalizedNodeMessage>> promise = new DefaultPromise<>();
78 Futures.addCallback(readFuture, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
80 public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
81 if (!result.isPresent()) {
82 promise.success(Optional.absent());
84 promise.success(Optional.of(new NormalizedNodeMessage(path, result.get())));
89 public void onFailure(@Nonnull final Throwable throwable) {
90 promise.failure(throwable);
93 return promise.future();
97 public Future<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
98 LOG.trace("{}: Exists[{}] {} via NETCONF: {}", id, readTx.getIdentifier(), store, path);
100 final CheckedFuture<Boolean, ReadFailedException> existsFuture = readTx.exists(store, path);
102 final DefaultPromise<Boolean> promise = new DefaultPromise<>();
103 Futures.addCallback(existsFuture, new FutureCallback<Boolean>() {
105 public void onSuccess(final Boolean result) {
106 promise.success(result);
110 public void onFailure(@Nonnull final Throwable throwable) {
111 promise.failure(throwable);
114 return promise.future();
118 public void put(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
119 if (writeTx == null) {
120 writeTx = delegateBroker.newWriteOnlyTransaction();
123 LOG.trace("{}: Write[{}] {} via NETCONF: {} with payload {}", id, writeTx.getIdentifier(), store,
124 data.getIdentifier(), data.getNode());
126 writeTx.put(store, data.getIdentifier(), data.getNode());
130 public void merge(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
131 if (writeTx == null) {
132 writeTx = delegateBroker.newWriteOnlyTransaction();
135 LOG.trace("{}: Merge[{}] {} via NETCONF: {} with payload {}", id, writeTx.getIdentifier(),store,
136 data.getIdentifier(), data.getNode());
138 writeTx.merge(store, data.getIdentifier(), data.getNode());
142 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
143 if (writeTx == null) {
144 writeTx = delegateBroker.newWriteOnlyTransaction();
147 LOG.trace("{}: Delete[{}} {} via NETCONF: {}", id, writeTx.getIdentifier(), store, path);
149 writeTx.delete(store, path);
153 public boolean cancel() {
154 LOG.trace("{}: Cancel[{}} via NETCONF", id, writeTx.getIdentifier());
156 return writeTx.cancel();
160 public Future<Void> submit() {
161 LOG.trace("{}: Submit[{}} via NETCONF", id, writeTx.getIdentifier());
163 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
166 final DefaultPromise<Void> promise = new DefaultPromise<>();
167 Futures.addCallback(submitFuture, new FutureCallback<Void>() {
169 public void onSuccess(final Void result) {
170 promise.success(result);
174 public void onFailure(@Nonnull final Throwable throwable) {
175 promise.failure(throwable);
178 return promise.future();