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.base.Optional;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
19 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
20 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
21 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
22 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 private final DOMDataReadTransaction tx;
30 ReadAdapter(final DOMDataReadTransaction tx) {
34 @SuppressWarnings("checkstyle:IllegalThrows")
35 public void handle(final Object message, final ActorRef sender, final ActorRef self) {
36 if (message instanceof ReadRequest) {
38 final ReadRequest readRequest = (ReadRequest) message;
39 final YangInstanceIdentifier path = readRequest.getPath();
40 final LogicalDatastoreType store = readRequest.getStore();
41 read(path, store, sender, self);
43 } else if (message instanceof ExistsRequest) {
44 final ExistsRequest readRequest = (ExistsRequest) message;
45 final YangInstanceIdentifier path = readRequest.getPath();
46 final LogicalDatastoreType store = readRequest.getStore();
47 exists(path, store, sender, self);
51 private void read(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
52 final ActorRef self) {
53 Futures.addCallback(tx.read(store, path), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
56 public void onSuccess(@Nonnull final Optional<NormalizedNode<?, ?>> result) {
57 if (!result.isPresent()) {
58 sender.tell(new EmptyReadResponse(), self);
61 sender.tell(new NormalizedNodeMessage(path, result.get()), self);
65 public void onFailure(@Nonnull final Throwable throwable) {
66 sender.tell(new Failure(throwable), self);
68 }, MoreExecutors.directExecutor());
71 private void exists(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
72 final ActorRef self) {
73 Futures.addCallback(tx.exists(store, path), new FutureCallback<Boolean>() {
75 public void onSuccess(final Boolean result) {
77 sender.tell(Boolean.FALSE, self);
79 sender.tell(result, self);
84 public void onFailure(@Nonnull final Throwable throwable) {
85 sender.tell(new Failure(throwable), self);
87 }, MoreExecutors.directExecutor());