Migrate netconf to MD-SAL APIs
[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 javax.annotation.Nonnull;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
18 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
19 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
20 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
21 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 class ReadAdapter {
26
27     private final DOMDataTreeReadTransaction tx;
28
29     ReadAdapter(final DOMDataTreeReadTransaction tx) {
30         this.tx = tx;
31     }
32
33     @SuppressWarnings("checkstyle:IllegalThrows")
34     public void handle(final Object message, final ActorRef sender, final ActorRef self) {
35         if (message instanceof ReadRequest) {
36
37             final ReadRequest readRequest = (ReadRequest) message;
38             final YangInstanceIdentifier path = readRequest.getPath();
39             final LogicalDatastoreType store = readRequest.getStore();
40             read(path, store, sender, self);
41
42         } else if (message instanceof ExistsRequest) {
43             final ExistsRequest readRequest = (ExistsRequest) message;
44             final YangInstanceIdentifier path = readRequest.getPath();
45             final LogicalDatastoreType store = readRequest.getStore();
46             exists(path, store, sender, self);
47         }
48     }
49
50     private void read(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
51                       final ActorRef self) {
52         tx.read(store, path).addCallback(new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
53             @Override
54             public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
55                 if (!result.isPresent()) {
56                     sender.tell(new EmptyReadResponse(), self);
57                     return;
58                 }
59                 sender.tell(new NormalizedNodeMessage(path, result.get()), self);
60             }
61
62             @Override
63             public void onFailure(@Nonnull final Throwable throwable) {
64                 sender.tell(new Failure(throwable), self);
65             }
66         }, MoreExecutors.directExecutor());
67     }
68
69     private void exists(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
70                         final ActorRef self) {
71         tx.exists(store, path).addCallback(new FutureCallback<Boolean>() {
72             @Override
73             public void onSuccess(final Boolean result) {
74                 if (result == null) {
75                     sender.tell(Boolean.FALSE, self);
76                 } else {
77                     sender.tell(result, self);
78                 }
79             }
80
81             @Override
82             public void onFailure(@Nonnull final Throwable throwable) {
83                 sender.tell(new Failure(throwable), self);
84             }
85         }, MoreExecutors.directExecutor());
86     }
87 }