Bump upstreams
[netconf.git] / apps / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / tx / ActorProxyTransactionFacade.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies 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.tx;
9
10 import akka.actor.ActorRef;
11 import akka.dispatch.OnComplete;
12 import akka.pattern.AskTimeoutException;
13 import akka.pattern.Patterns;
14 import akka.util.Timeout;
15 import com.google.common.util.concurrent.FluentFuture;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.Objects;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.common.api.CommitInfo;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.common.api.ReadFailedException;
23 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
24 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
25 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
26 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
30 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
31 import org.opendaylight.netconf.topology.singleton.messages.transactions.MergeRequest;
32 import org.opendaylight.netconf.topology.singleton.messages.transactions.PutRequest;
33 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
34 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitRequest;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import scala.concurrent.ExecutionContext;
40
41 /**
42  * ProxyTransactionFacade implementation that interfaces with an actor.
43  *
44  * @author Thomas Pantelis
45  */
46 class ActorProxyTransactionFacade implements ProxyTransactionFacade {
47     private static final Logger LOG = LoggerFactory.getLogger(ActorProxyTransactionFacade.class);
48
49     private final SettableFuture<CommitInfo> settableFuture = SettableFuture.create();
50     private final @NonNull FluentFuture<CommitInfo> fluentFuture = FluentFuture.from(settableFuture);
51     private final ActorRef masterTxActor;
52     private final RemoteDeviceId id;
53     private final ExecutionContext executionContext;
54     private final Timeout askTimeout;
55
56     ActorProxyTransactionFacade(final ActorRef masterTxActor, final RemoteDeviceId id,
57             final ExecutionContext executionContext, final Timeout askTimeout) {
58         this.masterTxActor = Objects.requireNonNull(masterTxActor);
59         this.id = Objects.requireNonNull(id);
60         this.executionContext = Objects.requireNonNull(executionContext);
61         this.askTimeout = Objects.requireNonNull(askTimeout);
62     }
63
64     @Override
65     public Object getIdentifier() {
66         return id;
67     }
68
69     @Override
70     public FluentFuture<?> completionFuture() {
71         return fluentFuture;
72     }
73
74     @Override
75     public boolean cancel() {
76         LOG.debug("{}: Cancel via actor {}", id, masterTxActor);
77
78         Patterns.ask(masterTxActor, new CancelRequest(), askTimeout).onComplete(new OnComplete<>() {
79             @Override
80             public void onComplete(final Throwable failure, final Object response) {
81                 if (failure != null) {
82                     LOG.warn("{}: Cancel failed", id, failure);
83                     return;
84                 }
85
86                 LOG.debug("{}: Cancel succeeded", id);
87             }
88         }, executionContext);
89
90         return true;
91     }
92
93     @Override
94     public FluentFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
95             final YangInstanceIdentifier path) {
96         LOG.debug("{}: Read {} {} via actor {}", id, store, path, masterTxActor);
97
98         final var future = SettableFuture.<Optional<NormalizedNode>>create();
99         Patterns.ask(masterTxActor, new ReadRequest(store, path), askTimeout).onComplete(new OnComplete<>() {
100             @Override
101             public void onComplete(final Throwable failure, final Object response) {
102                 if (failure != null) {
103                     LOG.debug("{}: Read {} {} failed", id, store, path, failure);
104
105                     final Throwable processedFailure = processFailure(failure);
106                     if (processedFailure instanceof ReadFailedException) {
107                         future.setException(processedFailure);
108                     } else {
109                         future.setException(new ReadFailedException("Read of store " + store + " path " + path
110                             + " failed", processedFailure));
111                     }
112                     return;
113                 }
114
115                 LOG.debug("{}: Read {} {} succeeded: {}", id, store, path, response);
116
117                 if (response instanceof EmptyReadResponse) {
118                     future.set(Optional.empty());
119                     return;
120                 }
121
122                 if (response instanceof NormalizedNodeMessage data) {
123                     future.set(Optional.of(data.getNode()));
124                 }
125             }
126         }, executionContext);
127
128         return FluentFuture.from(future);
129     }
130
131     @Override
132     public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
133         LOG.debug("{}: Exists {} {} via actor {}", id, store, path, masterTxActor);
134
135         final var future = SettableFuture.<Boolean>create();
136
137         Patterns.ask(masterTxActor, new ExistsRequest(store, path), askTimeout).onComplete(new OnComplete<>() {
138             @Override
139             public void onComplete(final Throwable failure, final Object response) {
140                 if (failure != null) {
141                     LOG.debug("{}: Exists {} {} failed", id, store, path, failure);
142
143                     final Throwable processedFailure = processFailure(failure);
144                     if (processedFailure instanceof ReadFailedException) {
145                         future.setException(processedFailure);
146                     } else {
147                         future.setException(new ReadFailedException("Exists of store " + store + " path " + path
148                             + " failed", processedFailure));
149                     }
150                     return;
151                 }
152
153                 LOG.debug("{}: Exists {} {} succeeded: {}", id, store, path, response);
154
155                 future.set((Boolean) response);
156             }
157         }, executionContext);
158
159         return FluentFuture.from(future);
160     }
161
162     @Override
163     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
164         LOG.debug("{}: Delete {} {} via actor {}", id, store, path, masterTxActor);
165         masterTxActor.tell(new DeleteRequest(store, path), ActorRef.noSender());
166     }
167
168     @Override
169     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) {
170         LOG.debug("{}: Put {} {} via actor {}", id, store, path, masterTxActor);
171         masterTxActor.tell(new PutRequest(store, new NormalizedNodeMessage(path, data)), ActorRef.noSender());
172     }
173
174     @Override
175     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) {
176         LOG.debug("{}: Merge {} {} via actor {}", id, store, path, masterTxActor);
177         masterTxActor.tell(new MergeRequest(store, new NormalizedNodeMessage(path, data)), ActorRef.noSender());
178     }
179
180     @Override
181     public FluentFuture<? extends CommitInfo> commit() {
182         LOG.debug("{}: Commit via actor {}", id, masterTxActor);
183
184         Patterns.ask(masterTxActor, new SubmitRequest(), askTimeout).onComplete(new OnComplete<>() {
185             @Override
186             public void onComplete(final Throwable failure, final Object response) {
187                 if (failure != null) {
188                     LOG.debug("{}: Commit failed", id, failure);
189                     settableFuture.setException(newTransactionCommitFailedException(processFailure(failure)));
190                     return;
191                 }
192
193                 LOG.debug("{}: Commit succeeded", id);
194
195                 settableFuture.set(CommitInfo.empty());
196             }
197
198             private TransactionCommitFailedException newTransactionCommitFailedException(final Throwable failure) {
199                 return new TransactionCommitFailedException(String.format("%s: Commit of transaction failed",
200                     getIdentifier()), failure);
201             }
202         }, executionContext);
203
204         return FluentFuture.from(settableFuture);
205     }
206
207     private Throwable processFailure(final Throwable failure) {
208         return failure instanceof AskTimeoutException
209                 ? NetconfTopologyUtils.createMasterIsDownException(id, (Exception)failure) : failure;
210     }
211 }