Add logging in tx facade along with the RemoteDeviceId
[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.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
24 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Future;
30
31 public class NetconfReadOnlyTransaction implements DOMDataReadOnlyTransaction {
32
33     private static final Logger LOG = LoggerFactory.getLogger(NetconfReadOnlyTransaction.class);
34
35     private final RemoteDeviceId id;
36     private final NetconfDOMTransaction delegate;
37     private final ActorSystem actorSystem;
38
39     public NetconfReadOnlyTransaction(final RemoteDeviceId id,
40                                       final ActorSystem actorSystem,
41                                       final NetconfDOMTransaction delegate) {
42         this.id = id;
43         this.delegate = delegate;
44         this.actorSystem = actorSystem;
45     }
46
47     @Override
48     public void close() {
49         //NOOP
50     }
51
52     @Override
53     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
54                                                                                    final YangInstanceIdentifier path) {
55
56         LOG.trace("{}: Read {} via NETCONF: {}", id, store, path);
57
58         final Future<Optional<NormalizedNodeMessage>> future = delegate.read(store, path);
59         final SettableFuture<Optional<NormalizedNode<?, ?>>> settableFuture = SettableFuture.create();
60         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> checkedFuture;
61         checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
62             @Nullable
63             @Override
64             public ReadFailedException apply(Exception cause) {
65                 return new ReadFailedException("Read from transaction failed", cause);
66             }
67         });
68         future.onComplete(new OnComplete<Optional<NormalizedNodeMessage>>() {
69             @Override
70             public void onComplete(final Throwable throwable,
71                                    final Optional<NormalizedNodeMessage> normalizedNodeMessage) throws Throwable {
72                 if (throwable == null) {
73                     if (normalizedNodeMessage.isPresent()) {
74                         settableFuture.set(normalizedNodeMessage.transform(new Function<NormalizedNodeMessage,
75                                 NormalizedNode<?, ?>>() {
76
77                             @Nullable
78                             @Override
79                             public NormalizedNode<?, ?> apply(final NormalizedNodeMessage input) {
80                                 return input.getNode();
81                             }
82                         }));
83                     } else {
84                         settableFuture.set(Optional.absent());
85                     }
86                 } else {
87                     settableFuture.setException(throwable);
88                 }
89             }
90         }, actorSystem.dispatcher());
91         return checkedFuture;
92     }
93
94     @Override
95     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
96                                                               final YangInstanceIdentifier path) {
97
98         LOG.trace("{}: Exists {} via NETCONF: {}", id, store, path);
99
100         final Future<Boolean> existsFuture = delegate.exists(store, path);
101         final SettableFuture<Boolean> settableFuture = SettableFuture.create();
102         final CheckedFuture<Boolean, ReadFailedException> checkedFuture;
103         checkedFuture = Futures.makeChecked(settableFuture, new Function<Exception, ReadFailedException>() {
104             @Nullable
105             @Override
106             public ReadFailedException apply(Exception cause) {
107                 return new ReadFailedException("Read from transaction failed", cause);
108             }
109         });
110         existsFuture.onComplete(new OnComplete<Boolean>() {
111             @Override
112             public void onComplete(final Throwable throwable, final Boolean result) throws Throwable {
113                 if (throwable == null) {
114                     settableFuture.set(result);
115                 } else {
116                     settableFuture.setException(throwable);
117                 }
118             }
119         }, actorSystem.dispatcher());
120         return checkedFuture;
121     }
122
123     @Override
124     public Object getIdentifier() {
125         return this;
126     }
127 }