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.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
24 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceDataBroker;
25 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
26 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
27 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import scala.concurrent.Future;
34 import scala.concurrent.impl.Promise.DefaultPromise;
36 public class NetconfMasterDOMTransaction implements NetconfDOMTransaction {
38 private static final Logger LOG = LoggerFactory.getLogger(NetconfMasterDOMTransaction.class);
40 private final RemoteDeviceId id;
41 private final DOMDataBroker delegateBroker;
43 private DOMDataReadOnlyTransaction readTx;
44 private DOMDataWriteTransaction writeTx;
46 public NetconfMasterDOMTransaction(final RemoteDeviceId id,
47 final SchemaContext schemaContext,
48 final DOMRpcService rpc,
49 final NetconfSessionPreferences netconfSessionPreferences) {
50 this(id, new NetconfDeviceDataBroker(id, schemaContext, rpc, netconfSessionPreferences));
53 public NetconfMasterDOMTransaction(final RemoteDeviceId id, final DOMDataBroker delegateBroker) {
55 this.delegateBroker = delegateBroker;
57 // only ever need 1 readTx since it doesnt need to be closed
58 readTx = delegateBroker.newReadOnlyTransaction();
62 public Future<Optional<NormalizedNodeMessage>> read(final LogicalDatastoreType store,
63 final YangInstanceIdentifier path) {
64 LOG.trace("{}: Read[{}] {} via NETCONF: {}", id, readTx.getIdentifier(), store, path);
66 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture = readTx.read(store, path);
68 final DefaultPromise<Optional<NormalizedNodeMessage>> promise = new DefaultPromise<>();
69 Futures.addCallback(readFuture, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
71 public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
72 if (!result.isPresent()) {
73 promise.success(Optional.absent());
75 promise.success(Optional.of(new NormalizedNodeMessage(path, result.get())));
80 public void onFailure(@Nonnull final Throwable throwable) {
81 promise.failure(throwable);
84 return promise.future();
88 public Future<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
89 LOG.trace("{}: Exists[{}] {} via NETCONF: {}", id, readTx.getIdentifier(), store, path);
91 final CheckedFuture<Boolean, ReadFailedException> existsFuture = readTx.exists(store, path);
93 final DefaultPromise<Boolean> promise = new DefaultPromise<>();
94 Futures.addCallback(existsFuture, new FutureCallback<Boolean>() {
96 public void onSuccess(final Boolean result) {
97 promise.success(result);
101 public void onFailure(@Nonnull final Throwable throwable) {
102 promise.failure(throwable);
105 return promise.future();
109 public void put(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
110 if (writeTx == null) {
111 writeTx = delegateBroker.newWriteOnlyTransaction();
114 LOG.trace("{}: Write[{}] {} via NETCONF: {} with payload {}", id, writeTx.getIdentifier(), store,
115 data.getIdentifier(), data.getNode());
117 writeTx.put(store, data.getIdentifier(), data.getNode());
121 public void merge(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
122 if (writeTx == null) {
123 writeTx = delegateBroker.newWriteOnlyTransaction();
126 LOG.trace("{}: Merge[{}] {} via NETCONF: {} with payload {}", id, writeTx.getIdentifier(),store,
127 data.getIdentifier(), data.getNode());
129 writeTx.merge(store, data.getIdentifier(), data.getNode());
133 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
134 if (writeTx == null) {
135 writeTx = delegateBroker.newWriteOnlyTransaction();
138 LOG.trace("{}: Delete[{}} {} via NETCONF: {}", id, writeTx.getIdentifier(), store, path);
140 writeTx.delete(store, path);
144 public boolean cancel() {
145 LOG.trace("{}: Cancel[{}} via NETCONF", id, writeTx.getIdentifier());
147 return writeTx.cancel();
151 public Future<Void> submit() {
152 LOG.trace("{}: Submit[{}} via NETCONF", id, writeTx.getIdentifier());
154 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
155 final DefaultPromise<Void> promise = new DefaultPromise<>();
156 Futures.addCallback(submitFuture, new FutureCallback<Void>() {
158 public void onSuccess(final Void result) {
159 promise.success(result);
164 public void onFailure(@Nonnull final Throwable throwable) {
165 promise.failure(throwable);
169 return promise.future();