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