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.topology.singleton.api.NetconfDOMTransaction;
23 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import scala.concurrent.Future;
28 public class NetconfReadOnlyTransaction implements DOMDataReadOnlyTransaction {
30 private final NetconfDOMTransaction delegate;
31 private final ActorSystem actorSystem;
33 public NetconfReadOnlyTransaction(final ActorSystem actorSystem, final NetconfDOMTransaction delegate) {
34 this.delegate = delegate;
35 this.actorSystem = actorSystem;
44 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
45 final YangInstanceIdentifier path) {
46 final Future<Optional<NormalizedNodeMessage>> future = delegate.read(store, path);
47 final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
48 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> checkedFuture;
49 checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
52 public ReadFailedException apply(Exception cause) {
53 return new ReadFailedException("Read from transaction failed", cause);
56 future.onComplete(new OnComplete<Optional<NormalizedNodeMessage>>() {
58 public void onComplete(final Throwable throwable,
59 final Optional<NormalizedNodeMessage> normalizedNodeMessage) throws Throwable {
60 if (throwable == null) {
61 if (normalizedNodeMessage.isPresent()) {
62 settableFuture.set(normalizedNodeMessage.transform(new Function<NormalizedNodeMessage,
63 NormalizedNode<?, ?>>() {
67 public NormalizedNode<?, ?> apply(final NormalizedNodeMessage input) {
68 return input.getNode();
72 settableFuture.set(Optional.absent());
75 settableFuture.setException(throwable);
78 }, actorSystem.dispatcher());
83 public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
84 final YangInstanceIdentifier path) {
85 final Future<Boolean> existsFuture = delegate.exists(store, path);
86 final SettableFuture<Boolean> settableFuture = SettableFuture.create();
87 final CheckedFuture<Boolean, ReadFailedException> checkedFuture;
88 checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
91 public ReadFailedException apply(Exception cause) {
92 return new ReadFailedException("Read from transaction failed", cause);
95 existsFuture.onComplete(new OnComplete<Boolean>() {
97 public void onComplete(final Throwable throwable, final Boolean result) throws Throwable {
98 if (throwable == null) {
99 settableFuture.set(result);
101 settableFuture.setException(throwable);
104 }, actorSystem.dispatcher());
105 return checkedFuture;
109 public Object getIdentifier() {