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
8 package org.opendaylight.netconf.topology.singleton.impl.actors;
10 import akka.actor.ActorRef;
11 import akka.actor.Status.Failure;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.Optional;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
18 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
19 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
20 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
21 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 private final DOMDataTreeReadTransaction tx;
29 ReadAdapter(final DOMDataTreeReadTransaction tx) {
33 @SuppressWarnings("checkstyle:IllegalThrows")
34 public void handle(final Object message, final ActorRef sender, final ActorRef self) {
35 if (message instanceof ReadRequest) {
37 final ReadRequest readRequest = (ReadRequest) message;
38 final YangInstanceIdentifier path = readRequest.getPath();
39 final LogicalDatastoreType store = readRequest.getStore();
40 read(path, store, sender, self);
42 } else if (message instanceof ExistsRequest) {
43 final ExistsRequest readRequest = (ExistsRequest) message;
44 final YangInstanceIdentifier path = readRequest.getPath();
45 final LogicalDatastoreType store = readRequest.getStore();
46 exists(path, store, sender, self);
50 private void read(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
51 final ActorRef self) {
52 tx.read(store, path).addCallback(new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
54 public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
55 if (!result.isPresent()) {
56 sender.tell(new EmptyReadResponse(), self);
59 sender.tell(new NormalizedNodeMessage(path, result.get()), self);
63 public void onFailure(@Nonnull final Throwable throwable) {
64 sender.tell(new Failure(throwable), self);
66 }, MoreExecutors.directExecutor());
69 private void exists(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
70 final ActorRef self) {
71 tx.exists(store, path).addCallback(new FutureCallback<Boolean>() {
73 public void onSuccess(final Boolean result) {
75 sender.tell(Boolean.FALSE, self);
77 sender.tell(result, self);
82 public void onFailure(@Nonnull final Throwable throwable) {
83 sender.tell(new Failure(throwable), self);
85 }, MoreExecutors.directExecutor());