2 * Copyright (c) 2015 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.pipeline.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.pipeline.ProxyNetconfDeviceDataBroker;
24 import org.opendaylight.netconf.topology.util.messages.NormalizedNodeMessage;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import scala.concurrent.Future;
29 public class ProxyReadOnlyTransaction implements DOMDataReadOnlyTransaction{
31 private final RemoteDeviceId id;
32 private final ProxyNetconfDeviceDataBroker delegate;
33 private final ActorSystem actorSystem;
35 public ProxyReadOnlyTransaction(final ActorSystem actorSystem, final RemoteDeviceId id, final ProxyNetconfDeviceDataBroker delegate) {
37 this.delegate = delegate;
38 this.actorSystem = actorSystem;
47 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
48 final Future<Optional<NormalizedNodeMessage>> future = delegate.read(store, path);
49 final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
50 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
53 public ReadFailedException apply(Exception cause) {
54 return new ReadFailedException("Read from transaction failed", cause);
57 future.onComplete(new OnComplete<Optional<NormalizedNodeMessage>>() {
59 public void onComplete(Throwable throwable, Optional<NormalizedNodeMessage> normalizedNodeMessage) throws Throwable {
60 if (throwable == null) {
61 settableFuture.set(normalizedNodeMessage.transform(new Function<NormalizedNodeMessage, NormalizedNode<?, ?>>() {
64 public NormalizedNode<?, ?> apply(NormalizedNodeMessage input) {
65 return input.getNode();
69 settableFuture.setException(throwable);
72 }, actorSystem.dispatcher());
77 public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
78 final Future<Boolean> existsFuture = delegate.exists(store, path);
79 final SettableFuture<Boolean> settableFuture = SettableFuture.create();
80 final CheckedFuture<Boolean, ReadFailedException> checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
83 public ReadFailedException apply(Exception cause) {
84 return new ReadFailedException("Read from transaction failed", cause);
87 existsFuture.onComplete(new OnComplete<Boolean>() {
89 public void onComplete(Throwable throwable, Boolean result) throws Throwable {
90 if (throwable == null) {
91 settableFuture.set(result);
93 settableFuture.setException(throwable);
96 }, actorSystem.dispatcher());
101 public Object getIdentifier() {