2 * Copyright (c) 2016 Cisco Systems, Inc. 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.ActorSystem;
12 import akka.dispatch.OnComplete;
13 import com.google.common.base.Function;
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.SettableFuture;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
24 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Future;
31 public class NetconfReadOnlyTransaction implements DOMDataReadOnlyTransaction {
33 private static final Logger LOG = LoggerFactory.getLogger(NetconfReadOnlyTransaction.class);
35 private final RemoteDeviceId id;
36 private final NetconfDOMTransaction delegate;
37 private final ActorSystem actorSystem;
39 public NetconfReadOnlyTransaction(final RemoteDeviceId id,
40 final ActorSystem actorSystem,
41 final NetconfDOMTransaction delegate) {
43 this.delegate = delegate;
44 this.actorSystem = actorSystem;
53 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
54 final YangInstanceIdentifier path) {
56 LOG.trace("{}: Read {} via NETCONF: {}", id, store, path);
58 final Future<Optional<NormalizedNodeMessage>> future = delegate.read(store, path);
59 final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
60 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> checkedFuture;
61 checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
64 public ReadFailedException apply(Exception cause) {
65 return new ReadFailedException("Read from transaction failed", cause);
68 future.onComplete(new OnComplete<Optional<NormalizedNodeMessage>>() {
70 public void onComplete(final Throwable throwable,
71 final Optional<NormalizedNodeMessage> normalizedNodeMessage) throws Throwable {
72 if (throwable == null) {
73 if (normalizedNodeMessage.isPresent()) {
74 settableFuture.set(normalizedNodeMessage.transform(new Function<NormalizedNodeMessage,
75 NormalizedNode<?, ?>>() {
79 public NormalizedNode<?, ?> apply(final NormalizedNodeMessage input) {
80 return input.getNode();
84 settableFuture.set(Optional.absent());
87 settableFuture.setException(throwable);
90 }, actorSystem.dispatcher());
95 public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
96 final YangInstanceIdentifier path) {
98 LOG.trace("{}: Exists {} via NETCONF: {}", id, store, path);
100 final Future<Boolean> existsFuture = delegate.exists(store, path);
101 final SettableFuture<Boolean> settableFuture = SettableFuture.create();
102 final CheckedFuture<Boolean, ReadFailedException> checkedFuture;
103 checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
106 public ReadFailedException apply(Exception cause) {
107 return new ReadFailedException("Read from transaction failed", cause);
110 existsFuture.onComplete(new OnComplete<Boolean>() {
112 public void onComplete(final Throwable throwable, final Boolean result) throws Throwable {
113 if (throwable == null) {
114 settableFuture.set(result);
116 settableFuture.setException(throwable);
119 }, actorSystem.dispatcher());
120 return checkedFuture;
124 public Object getIdentifier() {