More netconf-topology module unit tests
[netconf.git] / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / pipeline / tx / ProxyReadOnlyTransaction.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.topology.pipeline.tx;
10
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;
28
29 public class ProxyReadOnlyTransaction implements DOMDataReadOnlyTransaction{
30
31     private final RemoteDeviceId id;
32     private final ProxyNetconfDeviceDataBroker delegate;
33     private final ActorSystem actorSystem;
34
35     public ProxyReadOnlyTransaction(final ActorSystem actorSystem, final RemoteDeviceId id, final ProxyNetconfDeviceDataBroker delegate) {
36         this.id = id;
37         this.delegate = delegate;
38         this.actorSystem = actorSystem;
39     }
40
41     @Override
42     public void close() {
43         //NOOP
44     }
45
46     @Override
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>() {
51             @Nullable
52             @Override
53             public ReadFailedException apply(Exception cause) {
54                 return new ReadFailedException("Read from transaction failed", cause);
55             }
56         });
57         future.onComplete(new OnComplete<Optional<NormalizedNodeMessage>>() {
58             @Override
59             public void onComplete(Throwable throwable, Optional<NormalizedNodeMessage> normalizedNodeMessage) throws Throwable {
60                 if (throwable == null) {
61                     if (normalizedNodeMessage.isPresent()) {
62                         settableFuture.set(normalizedNodeMessage.transform(new Function<NormalizedNodeMessage, NormalizedNode<?, ?>>() {
63                             @Nullable
64                             @Override
65                             public NormalizedNode<?, ?> apply(NormalizedNodeMessage input) {
66                                 return input.getNode();
67                             }
68                         }));
69                     } else {
70                         settableFuture.set(Optional.absent());
71                     }
72                 } else {
73                     settableFuture.setException(throwable);
74                 }
75             }
76         }, actorSystem.dispatcher());
77         return checkedFuture;
78     }
79
80     @Override
81     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
82         final Future<Boolean> existsFuture = delegate.exists(store, path);
83         final SettableFuture<Boolean> settableFuture = SettableFuture.create();
84         final CheckedFuture<Boolean, ReadFailedException> checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
85             @Nullable
86             @Override
87             public ReadFailedException apply(Exception cause) {
88                 return new ReadFailedException("Read from transaction failed", cause);
89             }
90         });
91         existsFuture.onComplete(new OnComplete<Boolean>() {
92             @Override
93             public void onComplete(Throwable throwable, Boolean result) throws Throwable {
94                 if (throwable == null) {
95                     settableFuture.set(result);
96                 } else {
97                     settableFuture.setException(throwable);
98                 }
99             }
100         }, actorSystem.dispatcher());
101         return checkedFuture;
102     }
103
104     @Override
105     public Object getIdentifier() {
106         return this;
107     }
108 }