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.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
24 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
25 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
26 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import scala.concurrent.Future;
34 class ProxyReadAdapter {
35 private static final Logger LOG = LoggerFactory.getLogger(ProxyReadAdapter.class);
37 private final ActorRef masterTxActor;
38 private final RemoteDeviceId id;
39 private final ActorSystem actorSystem;
40 private final Timeout askTimeout;
42 public ProxyReadAdapter(final ActorRef masterTxActor, final RemoteDeviceId id, final ActorSystem actorSystem,
43 final Timeout askTimeout) {
44 this.masterTxActor = masterTxActor;
46 this.actorSystem = actorSystem;
47 this.askTimeout = askTimeout;
54 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
55 final YangInstanceIdentifier path) {
56 LOG.trace("{}: Read {} via NETCONF: {}", id, store, path);
58 final Future<Object> future = Patterns.ask(masterTxActor, new ReadRequest(store, path), askTimeout);
59 final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
60 future.onComplete(new OnComplete<Object>() {
62 public void onComplete(final Throwable failure,
63 final Object success) throws Throwable {
64 if (failure != null) { // ask timeout
65 final Exception exception = NetconfTopologyUtils.createMasterIsDownException(id);
66 settableFuture.setException(exception);
69 if (success instanceof Throwable) { // Error sended by master
70 settableFuture.setException((Throwable) success);
73 if (success instanceof EmptyReadResponse) {
74 settableFuture.set(Optional.absent());
77 if (success instanceof NormalizedNodeMessage) {
78 final NormalizedNodeMessage data = (NormalizedNodeMessage) success;
79 settableFuture.set(Optional.of(data.getNode()));
82 }, actorSystem.dispatcher());
83 return Futures.makeChecked(settableFuture, ReadFailedException.MAPPER);
86 public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
87 final YangInstanceIdentifier path) {
88 final Future<Object> existsScalaFuture =
89 Patterns.ask(masterTxActor, new ExistsRequest(store, path), askTimeout);
91 LOG.trace("{}: Exists {} via NETCONF: {}", id, store, path);
93 final SettableFuture<Boolean> settableFuture = SettableFuture.create();
94 existsScalaFuture.onComplete(new OnComplete<Object>() {
96 public void onComplete(final Throwable failure, final Object success) throws Throwable {
97 if (failure != null) { // ask timeout
98 final Exception exception = NetconfTopologyUtils.createMasterIsDownException(id);
99 settableFuture.setException(exception);
102 if (success instanceof Throwable) {
103 settableFuture.setException((Throwable) success);
106 settableFuture.set((Boolean) success);
108 }, actorSystem.dispatcher());
109 return Futures.makeChecked(settableFuture, ReadFailedException.MAPPER);