3af25c62b9da599d5aed57466f5af23a78c55dd2
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / tx / NetconfReadOnlyTransaction.java
1 /*
2  * Copyright (c) 2016 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.singleton.impl.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.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;
27
28 public class NetconfReadOnlyTransaction implements DOMDataReadOnlyTransaction {
29
30     private final NetconfDOMTransaction delegate;
31     private final ActorSystem actorSystem;
32
33     public NetconfReadOnlyTransaction(final ActorSystem actorSystem, final NetconfDOMTransaction delegate) {
34         this.delegate = delegate;
35         this.actorSystem = actorSystem;
36     }
37
38     @Override
39     public void close() {
40         //NOOP
41     }
42
43     @Override
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>() {
50             @Nullable
51             @Override
52             public ReadFailedException apply(Exception cause) {
53                 return new ReadFailedException("Read from transaction failed", cause);
54             }
55         });
56         future.onComplete(new OnComplete<Optional<NormalizedNodeMessage>>() {
57             @Override
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<?, ?>>() {
64
65                             @Nullable
66                             @Override
67                             public NormalizedNode<?, ?> apply(final NormalizedNodeMessage input) {
68                                 return input.getNode();
69                             }
70                         }));
71                     } else {
72                         settableFuture.set(Optional.absent());
73                     }
74                 } else {
75                     settableFuture.setException(throwable);
76                 }
77             }
78         }, actorSystem.dispatcher());
79         return checkedFuture;
80     }
81
82     @Override
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>() {
89             @Nullable
90             @Override
91             public ReadFailedException apply(Exception cause) {
92                 return new ReadFailedException("Read from transaction failed", cause);
93             }
94         });
95         existsFuture.onComplete(new OnComplete<Boolean>() {
96             @Override
97             public void onComplete(final Throwable throwable, final Boolean result) throws Throwable {
98                 if (throwable == null) {
99                     settableFuture.set(result);
100                 } else {
101                     settableFuture.setException(throwable);
102                 }
103             }
104         }, actorSystem.dispatcher());
105         return checkedFuture;
106     }
107
108     @Override
109     public Object getIdentifier() {
110         return this;
111     }
112 }