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