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 / NetconfProxyDOMTransaction.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.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.Patterns;
15 import com.google.common.base.Optional;
16 import org.opendaylight.controller.config.util.xml.DocumentedException;
17 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
18 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
19 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
22 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
23 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
24 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
25 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitFailedReply;
26 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
30 import org.opendaylight.netconf.topology.singleton.messages.transactions.MergeRequest;
31 import org.opendaylight.netconf.topology.singleton.messages.transactions.PutRequest;
32 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
33 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitRequest;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import scala.concurrent.Await;
38 import scala.concurrent.Future;
39 import scala.concurrent.impl.Promise.DefaultPromise;
40
41
42 public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
43
44     private static final Logger LOG = LoggerFactory.getLogger(NetconfProxyDOMTransaction.class);
45
46     private final RemoteDeviceId id;
47     private final ActorSystem actorSystem;
48     private final ActorRef masterContextRef;
49
50     public NetconfProxyDOMTransaction(final RemoteDeviceId id,
51                                       final ActorSystem actorSystem,
52                                       final ActorRef masterContextRef) {
53         this.id = id;
54         this.actorSystem = actorSystem;
55         this.masterContextRef = masterContextRef;
56     }
57
58     @Override
59     public Future<Optional<NormalizedNodeMessage>> read(final LogicalDatastoreType store,
60                                                         final YangInstanceIdentifier path) {
61
62         final Future<Object> readScalaFuture =
63                 Patterns.ask(masterContextRef, new ReadRequest(store, path), NetconfTopologyUtils.TIMEOUT);
64
65         LOG.trace("{}: Read {} via NETCONF: {}", id, store, path);
66
67         final DefaultPromise<Optional<NormalizedNodeMessage>> promise = new DefaultPromise<>();
68
69         readScalaFuture.onComplete(new OnComplete<Object>() {
70             @Override
71             public void onComplete(final Throwable failure, final Object success) throws Throwable {
72                 if (failure != null) { // ask timeout
73                     Exception exception = new DocumentedException(id + ":Master is down. Please try again.",
74                             ErrorType.application, ErrorTag.operation_failed,
75                             ErrorSeverity.warning);
76                     promise.failure(exception);
77                     return;
78                 }
79                 if (success instanceof Throwable) { // Error sended by master
80                     promise.failure((Throwable) success);
81                     return;
82                 }
83                 if (success instanceof EmptyReadResponse) {
84                     promise.success(Optional.absent());
85                     return;
86                 }
87
88                 promise.success(Optional.of((NormalizedNodeMessage) success));
89             }
90         }, actorSystem.dispatcher());
91
92         return promise.future();
93     }
94
95     @Override
96     public Future<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
97         final Future<Object> existsScalaFuture =
98                 Patterns.ask(masterContextRef, new ExistsRequest(store, path), NetconfTopologyUtils.TIMEOUT);
99
100         LOG.trace("{}: Exists {} via NETCONF: {}", id, store, path);
101
102         final DefaultPromise<Boolean> promise = new DefaultPromise<>();
103         existsScalaFuture.onComplete(new OnComplete<Object>() {
104             @Override
105             public void onComplete(final Throwable failure, final Object success) throws Throwable {
106                 if (failure != null) { // ask timeout
107                     Exception exception = new DocumentedException(id + ":Master is down. Please try again.",
108                             ErrorType.application, ErrorTag.operation_failed,
109                             ErrorSeverity.warning);
110                     promise.failure(exception);
111                     return;
112                 }
113                 if (success instanceof Throwable) {
114                     promise.failure((Throwable) success);
115                     return;
116                 }
117                 promise.success((Boolean) success);
118             }
119         }, actorSystem.dispatcher());
120         return promise.future();
121     }
122
123     @Override
124     public void put(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
125         LOG.trace("{}: Write {} via NETCONF: {} with payload {}", id, store, data.getIdentifier(), data.getNode());
126
127         masterContextRef.tell(new PutRequest(store, data), ActorRef.noSender());
128
129     }
130
131     @Override
132     public void merge(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
133         LOG.trace("{}: Merge {} via NETCONF: {} with payload {}", id, store, data.getIdentifier(), data.getNode());
134
135         masterContextRef.tell(new MergeRequest(store, data), ActorRef.noSender());
136     }
137
138     @Override
139     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
140         LOG.trace("{}: Delete {} via NETCONF: {}", id, store, path);
141
142         masterContextRef.tell(new DeleteRequest(store, path), ActorRef.noSender());
143     }
144
145     @Override
146     public boolean cancel() {
147         final Future<Object> cancelScalaFuture =
148                 Patterns.ask(masterContextRef, new CancelRequest(), NetconfTopologyUtils.TIMEOUT);
149
150         LOG.trace("{}: Cancel {} via NETCONF", id);
151
152         try {
153             // here must be Await because AsyncWriteTransaction do not return future
154             return (boolean) Await.result(cancelScalaFuture, NetconfTopologyUtils.TIMEOUT.duration());
155         } catch (Exception e) {
156             return false;
157         }
158     }
159
160     @Override
161     public Future<Void> submit() {
162         final Future<Object> submitScalaFuture =
163                 Patterns.ask(masterContextRef, new SubmitRequest(), NetconfTopologyUtils.TIMEOUT);
164
165         LOG.trace("{}: Submit {} via NETCONF", id);
166
167         final DefaultPromise<Void> promise = new DefaultPromise<>();
168
169         submitScalaFuture.onComplete(new OnComplete<Object>() {
170             @Override
171             public void onComplete(final Throwable failure, final Object success) throws Throwable {
172                 if (failure != null) { // ask timeout
173                     Exception exception = new DocumentedException(id + ":Master is down. Please try again.",
174                             ErrorType.application, ErrorTag.operation_failed,
175                             ErrorSeverity.warning);
176                     promise.failure(exception);
177                     return;
178                 }
179                 if (success instanceof Throwable) {
180                     promise.failure((Throwable) success);
181                 } else {
182                     if (success instanceof SubmitFailedReply) {
183                         LOG.error("{}: Transaction was not submitted because already closed.", id);
184                     }
185                     promise.success(null);
186                 }
187             }
188         }, actorSystem.dispatcher());
189
190         return promise.future();
191     }
192
193 }