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 org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadOperations;
17 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
18 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
19 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
20 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 private final DOMDataTreeReadOperations tx;
28 ReadAdapter(final DOMDataTreeReadOperations tx) {
32 @SuppressWarnings("checkstyle:IllegalThrows")
33 public void handle(final Object message, final ActorRef sender, final ActorRef self) {
34 if (message instanceof ReadRequest) {
36 final ReadRequest readRequest = (ReadRequest) message;
37 final YangInstanceIdentifier path = readRequest.getPath();
38 final LogicalDatastoreType store = readRequest.getStore();
39 read(path, store, sender, self);
41 } else if (message instanceof ExistsRequest) {
42 final ExistsRequest readRequest = (ExistsRequest) message;
43 final YangInstanceIdentifier path = readRequest.getPath();
44 final LogicalDatastoreType store = readRequest.getStore();
45 exists(path, store, sender, self);
49 private void read(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
50 final ActorRef self) {
51 tx.read(store, path).addCallback(new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
53 public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
54 if (!result.isPresent()) {
55 sender.tell(new EmptyReadResponse(), self);
58 sender.tell(new NormalizedNodeMessage(path, result.get()), self);
62 public void onFailure(final Throwable throwable) {
63 sender.tell(new Failure(throwable), self);
65 }, MoreExecutors.directExecutor());
68 private void exists(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
69 final ActorRef self) {
70 tx.exists(store, path).addCallback(new FutureCallback<Boolean>() {
72 public void onSuccess(final Boolean result) {
74 sender.tell(Boolean.FALSE, self);
76 sender.tell(result, self);
81 public void onFailure(final Throwable throwable) {
82 sender.tell(new Failure(throwable), self);
84 }, MoreExecutors.directExecutor());