RemoteDeviceDataBroker proxy
[netconf.git] / opendaylight / 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                     settableFuture.set(normalizedNodeMessage.transform(new Function<NormalizedNodeMessage, NormalizedNode<?, ?>>() {
62                         @Nullable
63                         @Override
64                         public NormalizedNode<?, ?> apply(NormalizedNodeMessage input) {
65                             return input.getNode();
66                         }
67                     }));
68                 } else {
69                     settableFuture.setException(throwable);
70                 }
71             }
72         }, actorSystem.dispatcher());
73         return checkedFuture;
74     }
75
76     @Override
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>() {
81             @Nullable
82             @Override
83             public ReadFailedException apply(Exception cause) {
84                 return new ReadFailedException("Read from transaction failed", cause);
85             }
86         });
87         existsFuture.onComplete(new OnComplete<Boolean>() {
88             @Override
89             public void onComplete(Throwable throwable, Boolean result) throws Throwable {
90                 if (throwable == null) {
91                     settableFuture.set(result);
92                 } else {
93                     settableFuture.setException(throwable);
94                 }
95             }
96         }, actorSystem.dispatcher());
97         return checkedFuture;
98     }
99
100     @Override
101     public Object getIdentifier() {
102         return this;
103     }
104 }