2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.actors;
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.actor.UntypedActor;
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
22 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
23 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
24 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
25 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 * ReadTransactionActor is an interface to device's {@link DOMDataReadOnlyTransaction} for cluster nodes.
32 public class ReadTransactionActor extends UntypedActor {
34 private final DOMDataReadOnlyTransaction tx;
37 * Creates new actor Props.
39 * @param tx delegate device read transaction
42 static Props props(final DOMDataReadOnlyTransaction tx) {
43 return Props.create(ReadTransactionActor.class, () -> new ReadTransactionActor(tx));
46 private ReadTransactionActor(final DOMDataReadOnlyTransaction tx) {
51 public void onReceive(final Object message) throws Throwable {
52 if (message instanceof ReadRequest) {
54 final ReadRequest readRequest = (ReadRequest) message;
55 final YangInstanceIdentifier path = readRequest.getPath();
56 final LogicalDatastoreType store = readRequest.getStore();
57 read(path, store, sender(), self());
59 } else if (message instanceof ExistsRequest) {
60 final ExistsRequest readRequest = (ExistsRequest) message;
61 final YangInstanceIdentifier path = readRequest.getPath();
62 final LogicalDatastoreType store = readRequest.getStore();
63 exists(path, store, sender(), self());
70 private void read(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
71 final ActorRef self) {
72 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = tx.read(store, path);
73 Futures.addCallback(read, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
76 public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
77 if (!result.isPresent()) {
78 sender.tell(new EmptyReadResponse(), self);
81 sender.tell(new NormalizedNodeMessage(path, result.get()), self);
85 public void onFailure(@Nonnull final Throwable throwable) {
86 sender.tell(throwable, self);
91 private void exists(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
92 final ActorRef self) {
93 final CheckedFuture<Boolean, ReadFailedException> readFuture = tx.exists(store, path);
94 Futures.addCallback(readFuture, new FutureCallback<Boolean>() {
96 public void onSuccess(final Boolean result) {
98 sender.tell(false, self);
100 sender.tell(result, self);
105 public void onFailure(@Nonnull final Throwable throwable) {
106 sender.tell(throwable, self);