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.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.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.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;
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 ActorRef masterTxActor;
50 private final RemoteDeviceId id;
51 private final ExecutionContext executionContext;
52 private final Timeout askTimeout;
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);
63 public Object getIdentifier() {
68 public boolean cancel() {
69 LOG.debug("{}: Cancel via actor {}", id, masterTxActor);
71 final Future<Object> future = Patterns.ask(masterTxActor, new CancelRequest(), askTimeout);
73 future.onComplete(new OnComplete<Object>() {
75 public void onComplete(final Throwable failure, final Object response) {
76 if (failure != null) {
77 LOG.warn("{}: Cancel failed", id, failure);
81 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 Future<Object> future = Patterns.ask(masterTxActor, new ReadRequest(store, path), askTimeout);
100 final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
101 future.onComplete(new OnComplete<Object>() {
103 public void onComplete(final Throwable failure, final Object response) {
104 if (failure != null) {
105 LOG.debug("{}: Read {} {} failed", id, store, path, failure);
107 final Throwable processedFailure = processFailure(failure);
108 if (processedFailure instanceof ReadFailedException) {
109 settableFuture.setException(processedFailure);
111 settableFuture.setException(new ReadFailedException("Read of store " + store + " path " + path
112 + " failed", processedFailure));
117 LOG.debug("{}: Read {} {} succeeded: {}", id, store, path, response);
119 if (response instanceof EmptyReadResponse) {
120 settableFuture.set(Optional.empty());
124 if (response instanceof NormalizedNodeMessage) {
125 final NormalizedNodeMessage data = (NormalizedNodeMessage) response;
126 settableFuture.set(Optional.of(data.getNode()));
129 }, executionContext);
131 return settableFuture;
135 public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
136 LOG.debug("{}: Exists {} {} via actor {}", id, store, path, masterTxActor);
138 final Future<Object> future = Patterns.ask(masterTxActor, new ExistsRequest(store, path), askTimeout);
140 final SettableFuture<Boolean> settableFuture = SettableFuture.create();
141 future.onComplete(new OnComplete<Object>() {
143 public void onComplete(final Throwable failure, final Object response) {
144 if (failure != null) {
145 LOG.debug("{}: Exists {} {} failed", id, store, path, failure);
147 final Throwable processedFailure = processFailure(failure);
148 if (processedFailure instanceof ReadFailedException) {
149 settableFuture.setException(processedFailure);
151 settableFuture.setException(new ReadFailedException("Exists of store " + store + " path " + path
152 + " failed", processedFailure));
157 LOG.debug("{}: Exists {} {} succeeded: {}", id, store, path, response);
159 settableFuture.set((Boolean) response);
161 }, executionContext);
163 return settableFuture;
167 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
168 LOG.debug("{}: Delete {} {} via actor {}", id, store, path, masterTxActor);
169 masterTxActor.tell(new DeleteRequest(store, path), ActorRef.noSender());
173 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
174 final NormalizedNode<?, ?> data) {
175 LOG.debug("{}: Put {} {} via actor {}", id, store, path, masterTxActor);
176 masterTxActor.tell(new PutRequest(store, new NormalizedNodeMessage(path, data)), ActorRef.noSender());
180 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
181 final NormalizedNode<?, ?> data) {
182 LOG.debug("{}: Merge {} {} via actor {}", id, store, path, masterTxActor);
183 masterTxActor.tell(new MergeRequest(store, new NormalizedNodeMessage(path, data)), ActorRef.noSender());
187 public FluentFuture<? extends CommitInfo> commit() {
188 LOG.debug("{}: Commit via actor {}", id, masterTxActor);
190 final Future<Object> future = Patterns.ask(masterTxActor, new SubmitRequest(), askTimeout);
192 final SettableFuture<CommitInfo> settableFuture = SettableFuture.create();
193 future.onComplete(new OnComplete<Object>() {
195 public void onComplete(final Throwable failure, final Object response) {
196 if (failure != null) {
197 LOG.debug("{}: Commit failed", id, failure);
198 settableFuture.setException(newTransactionCommitFailedException(processFailure(failure)));
202 LOG.debug("{}: Commit succeeded", id);
204 settableFuture.set(CommitInfo.empty());
206 }, executionContext);
208 return settableFuture;
211 private TransactionCommitFailedException newTransactionCommitFailedException(final Throwable failure) {
212 return new TransactionCommitFailedException(String.format("%s: Commit of transaction failed", getIdentifier()),
216 private Throwable processFailure(final Throwable failure) {
217 if (failure instanceof AskTimeoutException) {
218 return NetconfTopologyUtils.createMasterIsDownException(id, (Exception)failure);