0f23c11f2e27610e34214da4b2393d1e7badc5fb
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / tx / ProxyReadAdapter.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.tx;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.Patterns;
15 import akka.util.Timeout;
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.SettableFuture;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
24 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
25 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
26 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import scala.concurrent.Future;
33
34 class ProxyReadAdapter {
35     private static final Logger LOG = LoggerFactory.getLogger(ProxyReadAdapter.class);
36
37     private final ActorRef masterTxActor;
38     private final RemoteDeviceId id;
39     private final ActorSystem actorSystem;
40     private final Timeout askTimeout;
41
42     ProxyReadAdapter(final ActorRef masterTxActor, final RemoteDeviceId id, final ActorSystem actorSystem,
43                             final Timeout askTimeout) {
44         this.masterTxActor = masterTxActor;
45         this.id = id;
46         this.actorSystem = actorSystem;
47         this.askTimeout = askTimeout;
48     }
49
50     public void close() {
51         //noop
52     }
53
54     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
55                                                                                    final YangInstanceIdentifier path) {
56         LOG.trace("{}: Read {} via NETCONF: {}", id, store, path);
57
58         final Future<Object> future = Patterns.ask(masterTxActor, new ReadRequest(store, path), askTimeout);
59         final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
60         future.onComplete(new OnComplete<Object>() {
61             @Override
62             public void onComplete(final Throwable failure,
63                                    final Object success) throws Throwable {
64                 if (failure != null) { // ask timeout
65                     final Exception exception = NetconfTopologyUtils.createMasterIsDownException(id);
66                     settableFuture.setException(exception);
67                     return;
68                 }
69                 if (success instanceof Throwable) { // Error sended by master
70                     settableFuture.setException((Throwable) success);
71                     return;
72                 }
73                 if (success instanceof EmptyReadResponse) {
74                     settableFuture.set(Optional.absent());
75                     return;
76                 }
77                 if (success instanceof NormalizedNodeMessage) {
78                     final NormalizedNodeMessage data = (NormalizedNodeMessage) success;
79                     settableFuture.set(Optional.of(data.getNode()));
80                 }
81             }
82         }, actorSystem.dispatcher());
83         return Futures.makeChecked(settableFuture, ReadFailedException.MAPPER);
84     }
85
86     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
87                                                               final YangInstanceIdentifier path) {
88         final Future<Object> existsScalaFuture =
89                 Patterns.ask(masterTxActor, new ExistsRequest(store, path), askTimeout);
90
91         LOG.trace("{}: Exists {} via NETCONF: {}", id, store, path);
92
93         final SettableFuture<Boolean> settableFuture = SettableFuture.create();
94         existsScalaFuture.onComplete(new OnComplete<Object>() {
95             @Override
96             public void onComplete(final Throwable failure, final Object success) throws Throwable {
97                 if (failure != null) { // ask timeout
98                     final Exception exception = NetconfTopologyUtils.createMasterIsDownException(id);
99                     settableFuture.setException(exception);
100                     return;
101                 }
102                 if (success instanceof Throwable) {
103                     settableFuture.setException((Throwable) success);
104                     return;
105                 }
106                 settableFuture.set((Boolean) success);
107             }
108         }, actorSystem.dispatcher());
109         return Futures.makeChecked(settableFuture, ReadFailedException.MAPPER);
110     }
111
112 }