2 * Copyright (c) 2018 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.netconf.topology.singleton.impl.tx;
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;
42 * ProxyTransactionFacade implementation that interfaces with an actor.
44 * @author Thomas Pantelis
46 class ActorProxyTransactionFacade implements ProxyTransactionFacade {
47 private static final Logger LOG = LoggerFactory.getLogger(ActorProxyTransactionFacade.class);
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;
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);
65 public Object getIdentifier() {
70 public FluentFuture<?> completionFuture() {
75 public boolean cancel() {
76 LOG.debug("{}: Cancel via actor {}", id, masterTxActor);
78 Patterns.ask(masterTxActor, new CancelRequest(), askTimeout).onComplete(new OnComplete<>() {
80 public void onComplete(final Throwable failure, final Object response) {
81 if (failure != null) {
82 LOG.warn("{}: Cancel failed", id, failure);
86 LOG.debug("{}: Cancel succeeded", id);
94 public FluentFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
95 final YangInstanceIdentifier path) {
96 LOG.debug("{}: Read {} {} via actor {}", id, store, path, masterTxActor);
98 final var future = SettableFuture.<Optional<NormalizedNode>>create();
99 Patterns.ask(masterTxActor, new ReadRequest(store, path), askTimeout).onComplete(new OnComplete<>() {
101 public void onComplete(final Throwable failure, final Object response) {
102 if (failure != null) {
103 LOG.debug("{}: Read {} {} failed", id, store, path, failure);
105 final Throwable processedFailure = processFailure(failure);
106 if (processedFailure instanceof ReadFailedException) {
107 future.setException(processedFailure);
109 future.setException(new ReadFailedException("Read of store " + store + " path " + path
110 + " failed", processedFailure));
115 LOG.debug("{}: Read {} {} succeeded: {}", id, store, path, response);
117 if (response instanceof EmptyReadResponse) {
118 future.set(Optional.empty());
122 if (response instanceof NormalizedNodeMessage data) {
123 future.set(Optional.of(data.getNode()));
126 }, executionContext);
128 return FluentFuture.from(future);
132 public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
133 LOG.debug("{}: Exists {} {} via actor {}", id, store, path, masterTxActor);
135 final var future = SettableFuture.<Boolean>create();
137 Patterns.ask(masterTxActor, new ExistsRequest(store, path), askTimeout).onComplete(new OnComplete<>() {
139 public void onComplete(final Throwable failure, final Object response) {
140 if (failure != null) {
141 LOG.debug("{}: Exists {} {} failed", id, store, path, failure);
143 final Throwable processedFailure = processFailure(failure);
144 if (processedFailure instanceof ReadFailedException) {
145 future.setException(processedFailure);
147 future.setException(new ReadFailedException("Exists of store " + store + " path " + path
148 + " failed", processedFailure));
153 LOG.debug("{}: Exists {} {} succeeded: {}", id, store, path, response);
155 future.set((Boolean) response);
157 }, executionContext);
159 return FluentFuture.from(future);
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());
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());
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());
181 public FluentFuture<? extends CommitInfo> commit() {
182 LOG.debug("{}: Commit via actor {}", id, masterTxActor);
184 Patterns.ask(masterTxActor, new SubmitRequest(), askTimeout).onComplete(new OnComplete<>() {
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)));
193 LOG.debug("{}: Commit succeeded", id);
195 settableFuture.set(CommitInfo.empty());
198 private TransactionCommitFailedException newTransactionCommitFailedException(final Throwable failure) {
199 return new TransactionCommitFailedException(String.format("%s: Commit of transaction failed",
200 getIdentifier()), failure);
202 }, executionContext);
204 return FluentFuture.from(settableFuture);
207 private Throwable processFailure(final Throwable failure) {
208 return failure instanceof AskTimeoutException
209 ? NetconfTopologyUtils.createMasterIsDownException(id, (Exception)failure) : failure;