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;
11 import akka.actor.ActorRef;
12 import com.google.common.base.Optional;
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.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.netconf.topology.singleton.api.RemoteOperationTxProcessor;
25 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
26 import org.opendaylight.netconf.topology.singleton.messages.SubmitFailedReply;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitReply;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class RemoteOperationTxProcessorImpl implements RemoteOperationTxProcessor, AutoCloseable {
36 private static final Logger LOG = LoggerFactory.getLogger(RemoteOperationTxProcessorImpl.class);
38 private final DOMDataBroker dataBroker;
39 private final RemoteDeviceId id;
40 private DOMDataWriteTransaction writeTx;
41 private DOMDataReadOnlyTransaction readTx;
43 public RemoteOperationTxProcessorImpl(final DOMDataBroker dataBroker, final RemoteDeviceId id) {
44 this.dataBroker = dataBroker;
46 this.readTx = dataBroker.newReadOnlyTransaction();
50 public void doDelete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
51 if (writeTx == null) {
52 writeTx = dataBroker.newWriteOnlyTransaction();
54 writeTx.delete(store, path);
58 public void doSubmit(final ActorRef recipient, final ActorRef sender) {
59 if (writeTx != null) {
60 CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
61 Futures.addCallback(submitFuture, new FutureCallback<Void>() {
63 public void onSuccess(Void result) {
64 recipient.tell(new SubmitReply(), sender);
68 public void onFailure(@Nonnull Throwable throwable) {
69 recipient.tell(throwable, sender);
73 recipient.tell(new SubmitFailedReply(), sender);
74 LOG.warn("{}: Couldn't submit transaction because it was already closed.", id);
79 public void doCancel(final ActorRef recipient, final ActorRef sender) {
80 boolean cancel = false;
81 if (writeTx != null) {
82 cancel = writeTx.cancel();
84 recipient.tell(cancel, sender);
88 public void doPut(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
89 if (writeTx == null) {
90 writeTx = dataBroker.newWriteOnlyTransaction();
92 writeTx.put(store, data.getIdentifier(), data.getNode());
96 public void doMerge(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
97 if (writeTx == null) {
98 writeTx = dataBroker.newWriteOnlyTransaction();
100 writeTx.merge(store, data.getIdentifier(), data.getNode());
104 public void doRead(final LogicalDatastoreType store, final YangInstanceIdentifier path, final ActorRef recipient,
105 final ActorRef sender) {
106 final CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> readFuture =
107 readTx.read(store, path);
109 Futures.addCallback(readFuture, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
112 public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
113 if (!result.isPresent()) {
114 recipient.tell(new EmptyReadResponse(), sender);
117 recipient.tell(new NormalizedNodeMessage(path, result.get()), sender);
121 public void onFailure(@Nonnull final Throwable throwable) {
122 recipient.tell(throwable, sender);
128 public void doExists(final LogicalDatastoreType store, final YangInstanceIdentifier path, final ActorRef recipient,
129 final ActorRef sender) {
130 final CheckedFuture<Boolean, ReadFailedException> readFuture =
131 readTx.exists(store, path);
132 Futures.addCallback(readFuture, new FutureCallback<Boolean>() {
134 public void onSuccess(final Boolean result) {
135 if (result == null) {
136 recipient.tell(false, sender);
138 recipient.tell(result, sender);
143 public void onFailure(@Nonnull final Throwable throwable) {
144 recipient.tell(throwable, sender);
150 public void close() throws Exception {
151 if (readTx != null) {