Change handling of netconf cluster transactions
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / tx / ProxyReadTransaction.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.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
25 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
26 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import scala.concurrent.Future;
34
35 /**
36  * ProxyReadTransaction uses provided {@link ActorRef} to delegate method calls to master
37  * {@link org.opendaylight.netconf.topology.singleton.impl.actors.ReadTransactionActor}.
38  */
39 public class ProxyReadTransaction implements DOMDataReadOnlyTransaction {
40
41     private static final Logger LOG = LoggerFactory.getLogger(ProxyReadTransaction.class);
42
43     private final ActorRef masterTxActor;
44     private final RemoteDeviceId id;
45     private final ActorSystem actorSystem;
46     private final Timeout askTimeout;
47
48     /**
49      * @param masterTxActor {@link org.opendaylight.netconf.topology.singleton.impl.actors.ReadTransactionActor} ref
50      * @param id            device id
51      * @param actorSystem   system
52      * @param askTimeout
53      */
54     public ProxyReadTransaction(final ActorRef masterTxActor, final RemoteDeviceId id, final ActorSystem actorSystem,
55                                 final Timeout askTimeout) {
56         this.masterTxActor = masterTxActor;
57         this.id = id;
58         this.actorSystem = actorSystem;
59         this.askTimeout = askTimeout;
60     }
61
62     @Override
63     public void close() {
64         //noop
65     }
66
67     @Override
68     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
69                                                                                    final YangInstanceIdentifier path) {
70         LOG.trace("{}: Read {} via NETCONF: {}", id, store, path);
71
72         final Future<Object> future = Patterns.ask(masterTxActor, new ReadRequest(store, path), askTimeout);
73         final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
74         future.onComplete(new OnComplete<Object>() {
75             @Override
76             public void onComplete(final Throwable failure,
77                                    final Object success) throws Throwable {
78                 if (failure != null) { // ask timeout
79                     final Exception exception = NetconfTopologyUtils.createMasterIsDownException(id);
80                     settableFuture.setException(exception);
81                     return;
82                 }
83                 if (success instanceof Throwable) { // Error sended by master
84                     settableFuture.setException((Throwable) success);
85                     return;
86                 }
87                 if (success instanceof EmptyReadResponse) {
88                     settableFuture.set(Optional.absent());
89                     return;
90                 }
91                 if (success instanceof NormalizedNodeMessage) {
92                     final NormalizedNodeMessage data = (NormalizedNodeMessage) success;
93                     settableFuture.set(Optional.of(data.getNode()));
94                 }
95             }
96         }, actorSystem.dispatcher());
97         return Futures.makeChecked(settableFuture, ReadFailedException.MAPPER);
98     }
99
100     @Override
101     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
102                                                               final YangInstanceIdentifier path) {
103         final Future<Object> existsScalaFuture =
104                 Patterns.ask(masterTxActor, new ExistsRequest(store, path), askTimeout);
105
106         LOG.trace("{}: Exists {} via NETCONF: {}", id, store, path);
107
108         final SettableFuture<Boolean> settableFuture = SettableFuture.create();
109         existsScalaFuture.onComplete(new OnComplete<Object>() {
110             @Override
111             public void onComplete(final Throwable failure, final Object success) throws Throwable {
112                 if (failure != null) { // ask timeout
113                     final Exception exception = NetconfTopologyUtils.createMasterIsDownException(id);
114                     settableFuture.setException(exception);
115                     return;
116                 }
117                 if (success instanceof Throwable) {
118                     settableFuture.setException((Throwable) success);
119                     return;
120                 }
121                 settableFuture.set((Boolean) success);
122             }
123         }, actorSystem.dispatcher());
124         return Futures.makeChecked(settableFuture, ReadFailedException.MAPPER);
125     }
126
127
128     @Override
129     public Object getIdentifier() {
130         return this;
131     }
132 }