Convert ProxyDOMDataBroker tx creation to async
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / actors / ReadAdapter.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.topology.singleton.impl.actors;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Status.Failure;
13 import com.google.common.base.Optional;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.MoreExecutors;
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.DOMDataReadTransaction;
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;
28
29 class ReadAdapter {
30
31     private final DOMDataReadTransaction tx;
32
33     ReadAdapter(final DOMDataReadTransaction tx) {
34         this.tx = tx;
35     }
36
37     @SuppressWarnings("checkstyle:IllegalThrows")
38     public void handle(final Object message, final ActorRef sender, final ActorRef self) throws Throwable {
39         if (message instanceof ReadRequest) {
40
41             final ReadRequest readRequest = (ReadRequest) message;
42             final YangInstanceIdentifier path = readRequest.getPath();
43             final LogicalDatastoreType store = readRequest.getStore();
44             read(path, store, sender, self);
45
46         } else if (message instanceof ExistsRequest) {
47             final ExistsRequest readRequest = (ExistsRequest) message;
48             final YangInstanceIdentifier path = readRequest.getPath();
49             final LogicalDatastoreType store = readRequest.getStore();
50             exists(path, store, sender, self);
51         }
52     }
53
54     private void read(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
55                       final ActorRef self) {
56         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = tx.read(store, path);
57         Futures.addCallback(read, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
58
59             @Override
60             public void onSuccess(@Nonnull final Optional<NormalizedNode<?, ?>> result) {
61                 if (!result.isPresent()) {
62                     sender.tell(new EmptyReadResponse(), self);
63                     return;
64                 }
65                 sender.tell(new NormalizedNodeMessage(path, result.get()), self);
66             }
67
68             @Override
69             public void onFailure(@Nonnull final Throwable throwable) {
70                 sender.tell(new Failure(throwable), self);
71             }
72         }, MoreExecutors.directExecutor());
73     }
74
75     private void exists(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
76                         final ActorRef self) {
77         final CheckedFuture<Boolean, ReadFailedException> readFuture = tx.exists(store, path);
78         Futures.addCallback(readFuture, new FutureCallback<Boolean>() {
79             @Override
80             public void onSuccess(final Boolean result) {
81                 if (result == null) {
82                     sender.tell(false, self);
83                 } else {
84                     sender.tell(result, self);
85                 }
86             }
87
88             @Override
89             public void onFailure(@Nonnull final Throwable throwable) {
90                 sender.tell(new Failure(throwable), self);
91             }
92         }, MoreExecutors.directExecutor());
93     }
94 }