Change handling of netconf cluster transactions
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / actors / ReadTransactionActor.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.Props;
13 import akka.actor.UntypedActor;
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
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.DOMDataReadOnlyTransaction;
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 /**
30  * ReadTransactionActor is an interface to device's {@link DOMDataReadOnlyTransaction} for cluster nodes.
31  */
32 public class ReadTransactionActor extends UntypedActor {
33
34     private final DOMDataReadOnlyTransaction tx;
35
36     /**
37      * Creates new actor Props.
38      *
39      * @param tx delegate device read transaction
40      * @return props
41      */
42     static Props props(final DOMDataReadOnlyTransaction tx) {
43         return Props.create(ReadTransactionActor.class, () -> new ReadTransactionActor(tx));
44     }
45
46     private ReadTransactionActor(final DOMDataReadOnlyTransaction tx) {
47         this.tx = tx;
48     }
49
50     @Override
51     public void onReceive(final Object message) throws Throwable {
52         if (message instanceof ReadRequest) {
53
54             final ReadRequest readRequest = (ReadRequest) message;
55             final YangInstanceIdentifier path = readRequest.getPath();
56             final LogicalDatastoreType store = readRequest.getStore();
57             read(path, store, sender(), self());
58
59         } else if (message instanceof ExistsRequest) {
60             final ExistsRequest readRequest = (ExistsRequest) message;
61             final YangInstanceIdentifier path = readRequest.getPath();
62             final LogicalDatastoreType store = readRequest.getStore();
63             exists(path, store, sender(), self());
64
65         } else {
66             unhandled(message);
67         }
68     }
69
70     private void read(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
71                       final ActorRef self) {
72         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = tx.read(store, path);
73         Futures.addCallback(read, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
74
75             @Override
76             public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
77                 if (!result.isPresent()) {
78                     sender.tell(new EmptyReadResponse(), self);
79                     return;
80                 }
81                 sender.tell(new NormalizedNodeMessage(path, result.get()), self);
82             }
83
84             @Override
85             public void onFailure(@Nonnull final Throwable throwable) {
86                 sender.tell(throwable, self);
87             }
88         });
89     }
90
91     private void exists(final YangInstanceIdentifier path, final LogicalDatastoreType store, final ActorRef sender,
92                         final ActorRef self) {
93         final CheckedFuture<Boolean, ReadFailedException> readFuture = tx.exists(store, path);
94         Futures.addCallback(readFuture, new FutureCallback<Boolean>() {
95             @Override
96             public void onSuccess(final Boolean result) {
97                 if (result == null) {
98                     sender.tell(false, self);
99                 } else {
100                     sender.tell(result, self);
101                 }
102             }
103
104             @Override
105             public void onFailure(@Nonnull final Throwable throwable) {
106                 sender.tell(throwable, self);
107             }
108         });
109     }
110 }