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);
89 public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
90 final YangInstanceIdentifier path) {
91 LOG.debug("{}: Read {} {} via actor {}", id, store, path, masterTxActor);
93 final Future<Object> future = Patterns.ask(masterTxActor, new ReadRequest(store, path), askTimeout);
95 final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
96 future.onComplete(new OnComplete<Object>() {
98 public void onComplete(final Throwable failure, final Object response) {
99 if (failure != null) {
100 LOG.debug("{}: Read {} {} failed", id, store, path, failure);
102 final Throwable processedFailure = processFailure(failure);
103 if (processedFailure instanceof ReadFailedException) {
104 settableFuture.setException(processedFailure);
106 settableFuture.setException(new ReadFailedException("Read of store " + store + " path " + path
107 + " failed", processedFailure));
112 LOG.debug("{}: Read {} {} succeeded: {}", id, store, path, response);
114 if (response instanceof EmptyReadResponse) {
115 settableFuture.set(Optional.empty());
119 if (response instanceof NormalizedNodeMessage) {
120 final NormalizedNodeMessage data = (NormalizedNodeMessage) response;
121 settableFuture.set(Optional.of(data.getNode()));
124 }, executionContext);
126 return FluentFuture.from(settableFuture);
130 public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
131 LOG.debug("{}: Exists {} {} via actor {}", id, store, path, masterTxActor);
133 final Future<Object> future = Patterns.ask(masterTxActor, new ExistsRequest(store, path), askTimeout);
135 final SettableFuture<Boolean> settableFuture = SettableFuture.create();
136 future.onComplete(new OnComplete<Object>() {
138 public void onComplete(final Throwable failure, final Object response) {
139 if (failure != null) {
140 LOG.debug("{}: Exists {} {} failed", id, store, path, failure);
142 final Throwable processedFailure = processFailure(failure);
143 if (processedFailure instanceof ReadFailedException) {
144 settableFuture.setException(processedFailure);
146 settableFuture.setException(new ReadFailedException("Exists of store " + store + " path " + path
147 + " failed", processedFailure));
152 LOG.debug("{}: Exists {} {} succeeded: {}", id, store, path, response);
154 settableFuture.set((Boolean) response);
156 }, executionContext);
158 return FluentFuture.from(settableFuture);
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());
168 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
169 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,
176 final NormalizedNode<?, ?> data) {
177 LOG.debug("{}: Merge {} {} via actor {}", id, store, path, masterTxActor);
178 masterTxActor.tell(new MergeRequest(store, new NormalizedNodeMessage(path, data)), ActorRef.noSender());
182 public FluentFuture<? extends CommitInfo> commit() {
183 LOG.debug("{}: Commit via actor {}", id, masterTxActor);
185 final Future<Object> future = Patterns.ask(masterTxActor, new SubmitRequest(), askTimeout);
187 final SettableFuture<CommitInfo> settableFuture = SettableFuture.create();
188 future.onComplete(new OnComplete<Object>() {
190 public void onComplete(final Throwable failure, final Object response) {
191 if (failure != null) {
192 LOG.debug("{}: Commit failed", id, failure);
193 settableFuture.setException(newTransactionCommitFailedException(processFailure(failure)));
197 LOG.debug("{}: Commit succeeded", id);
199 settableFuture.set(CommitInfo.empty());
201 }, executionContext);
203 return FluentFuture.from(settableFuture);
206 private TransactionCommitFailedException newTransactionCommitFailedException(final Throwable failure) {
207 return new TransactionCommitFailedException(String.format("%s: Commit of transaction failed", getIdentifier()),
211 private Throwable processFailure(final Throwable failure) {
212 if (failure instanceof AskTimeoutException) {
213 return NetconfTopologyUtils.createMasterIsDownException(id, (Exception)failure);