2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.netconf.topology.singleton.impl.tx;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.Patterns;
15 import akka.util.Timeout;
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.SettableFuture;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
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.EmptyReadResponse;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import scala.concurrent.Future;
36 * ProxyReadTransaction uses provided {@link ActorRef} to delegate method calls to master
37 * {@link org.opendaylight.netconf.topology.singleton.impl.actors.ReadTransactionActor}.
39 public class ProxyReadTransaction implements DOMDataReadOnlyTransaction {
41 private static final Logger LOG = LoggerFactory.getLogger(ProxyReadTransaction.class);
43 private final ActorRef masterTxActor;
44 private final RemoteDeviceId id;
45 private final ActorSystem actorSystem;
46 private final Timeout askTimeout;
49 * @param masterTxActor {@link org.opendaylight.netconf.topology.singleton.impl.actors.ReadTransactionActor} ref
51 * @param actorSystem system
54 public ProxyReadTransaction(final ActorRef masterTxActor, final RemoteDeviceId id, final ActorSystem actorSystem,
55 final Timeout askTimeout) {
56 this.masterTxActor = masterTxActor;
58 this.actorSystem = actorSystem;
59 this.askTimeout = askTimeout;
68 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
69 final YangInstanceIdentifier path) {
70 LOG.trace("{}: Read {} via NETCONF: {}", id, store, path);
72 final Future<Object> future = Patterns.ask(masterTxActor, new ReadRequest(store, path), askTimeout);
73 final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
74 future.onComplete(new OnComplete<Object>() {
76 public void onComplete(final Throwable failure,
77 final Object success) throws Throwable {
78 if (failure != null) { // ask timeout
79 final Exception exception = NetconfTopologyUtils.createMasterIsDownException(id);
80 settableFuture.setException(exception);
83 if (success instanceof Throwable) { // Error sended by master
84 settableFuture.setException((Throwable) success);
87 if (success instanceof EmptyReadResponse) {
88 settableFuture.set(Optional.absent());
91 if (success instanceof NormalizedNodeMessage) {
92 final NormalizedNodeMessage data = (NormalizedNodeMessage) success;
93 settableFuture.set(Optional.of(data.getNode()));
96 }, actorSystem.dispatcher());
97 return Futures.makeChecked(settableFuture, ReadFailedException.MAPPER);
101 public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
102 final YangInstanceIdentifier path) {
103 final Future<Object> existsScalaFuture =
104 Patterns.ask(masterTxActor, new ExistsRequest(store, path), askTimeout);
106 LOG.trace("{}: Exists {} via NETCONF: {}", id, store, path);
108 final SettableFuture<Boolean> settableFuture = SettableFuture.create();
109 existsScalaFuture.onComplete(new OnComplete<Object>() {
111 public void onComplete(final Throwable failure, final Object success) throws Throwable {
112 if (failure != null) { // ask timeout
113 final Exception exception = NetconfTopologyUtils.createMasterIsDownException(id);
114 settableFuture.setException(exception);
117 if (success instanceof Throwable) {
118 settableFuture.setException((Throwable) success);
121 settableFuture.set((Boolean) success);
123 }, actorSystem.dispatcher());
124 return Futures.makeChecked(settableFuture, ReadFailedException.MAPPER);
129 public Object getIdentifier() {