Bug 8289 - 409 in cluster restperfclient test
[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 com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
20 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
21 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
22 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
23 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26
27 class ReadAdapter {
28
29     private final DOMDataReadTransaction tx;
30
31     public ReadAdapter(final DOMDataReadTransaction tx) {
32         this.tx = tx;
33     }
34
35     public void handle(final Object message, final ActorRef sender, final ActorRef self) throws Throwable {
36         if (message instanceof ReadRequest) {
37
38             final ReadRequest readRequest = (ReadRequest) message;
39             final YangInstanceIdentifier path = readRequest.getPath();
40             final LogicalDatastoreType store = readRequest.getStore();
41             read(path, store, sender, self);
42
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);
48         }
49     }
50
51     private void read(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
52                       final ActorRef self) {
53         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = tx.read(store, path);
54         Futures.addCallback(read, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
55
56             @Override
57             public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
58                 if (!result.isPresent()) {
59                     sender.tell(new EmptyReadResponse(), self);
60                     return;
61                 }
62                 sender.tell(new NormalizedNodeMessage(path, result.get()), self);
63             }
64
65             @Override
66             public void onFailure(@Nonnull final Throwable throwable) {
67                 sender.tell(throwable, self);
68             }
69         });
70     }
71
72     private void exists(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
73                         final ActorRef self) {
74         final CheckedFuture<Boolean, ReadFailedException> readFuture = tx.exists(store, path);
75         Futures.addCallback(readFuture, new FutureCallback<Boolean>() {
76             @Override
77             public void onSuccess(final Boolean result) {
78                 if (result == null) {
79                     sender.tell(false, self);
80                 } else {
81                     sender.tell(result, self);
82                 }
83             }
84
85             @Override
86             public void onFailure(@Nonnull final Throwable throwable) {
87                 sender.tell(throwable, self);
88             }
89         });
90     }
91 }