Bump MRI upstreams
[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 package org.opendaylight.netconf.topology.singleton.impl.actors;
9
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;
23
24 class ReadAdapter {
25
26     private final DOMDataTreeReadOperations tx;
27
28     ReadAdapter(final DOMDataTreeReadOperations tx) {
29         this.tx = tx;
30     }
31
32     @SuppressWarnings("checkstyle:IllegalThrows")
33     public void handle(final Object message, final ActorRef sender, final ActorRef self) {
34         if (message instanceof ReadRequest) {
35
36             final ReadRequest readRequest = (ReadRequest) message;
37             final YangInstanceIdentifier path = readRequest.getPath();
38             final LogicalDatastoreType store = readRequest.getStore();
39             read(path, store, sender, self);
40
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);
46         }
47     }
48
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>>() {
52             @Override
53             public void onSuccess(final Optional<NormalizedNode> result) {
54                 if (result.isEmpty()) {
55                     sender.tell(new EmptyReadResponse(), self);
56                     return;
57                 }
58                 sender.tell(new NormalizedNodeMessage(path, result.get()), self);
59             }
60
61             @Override
62             public void onFailure(final Throwable throwable) {
63                 sender.tell(new Failure(throwable), self);
64             }
65         }, MoreExecutors.directExecutor());
66     }
67
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>() {
71             @Override
72             public void onSuccess(final Boolean result) {
73                 if (result == null) {
74                     sender.tell(Boolean.FALSE, self);
75                 } else {
76                     sender.tell(result, self);
77                 }
78             }
79
80             @Override
81             public void onFailure(final Throwable throwable) {
82                 sender.tell(new Failure(throwable), self);
83             }
84         }, MoreExecutors.directExecutor());
85     }
86 }