Merge "Close read-only transactions"
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / tx / NetconfProxyDOMTransaction.java
index 9b2adf2efa102bbf884c388ce95f82b0a7865e77..535123bf11ebd6b9b491cf8ac361a1deaaa0fc5f 100644 (file)
@@ -12,13 +12,13 @@ import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
 import akka.dispatch.OnComplete;
 import akka.pattern.Patterns;
+import akka.util.Timeout;
 import com.google.common.base.Optional;
 import com.google.common.base.Throwables;
 import org.opendaylight.controller.config.util.xml.DocumentedException;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
-import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
@@ -45,13 +45,16 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
     private final RemoteDeviceId id;
     private final ActorSystem actorSystem;
     private final ActorRef masterContextRef;
+    private final Timeout actorResponseWaitTime;
 
     public NetconfProxyDOMTransaction(final RemoteDeviceId id,
                                       final ActorSystem actorSystem,
-                                      final ActorRef masterContextRef) {
+                                      final ActorRef masterContextRef,
+                                      final Timeout actorResponseWaitTime) {
         this.id = id;
         this.actorSystem = actorSystem;
         this.masterContextRef = masterContextRef;
+        this.actorResponseWaitTime = actorResponseWaitTime;
     }
 
     @Override
@@ -61,10 +64,10 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
         // node.
         LOG.debug("{}: Requesting leader {} to open new transaction", id, masterContextRef);
         final Future<Object> openTxFuture =
-                Patterns.ask(masterContextRef, new OpenTransaction(), NetconfTopologyUtils.TIMEOUT);
+                Patterns.ask(masterContextRef, new OpenTransaction(), actorResponseWaitTime);
         try {
             // we have to wait here so we can see if tx can be opened
-            Await.result(openTxFuture, NetconfTopologyUtils.TIMEOUT.duration());
+            Await.result(openTxFuture, actorResponseWaitTime.duration());
             LOG.debug("{}: New transaction opened successfully", id);
         } catch (final Exception e) {
             LOG.error("{}: Failed to open new transaction", id, e);
@@ -77,7 +80,7 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
                                                         final YangInstanceIdentifier path) {
 
         final Future<Object> readScalaFuture =
-                Patterns.ask(masterContextRef, new ReadRequest(store, path), NetconfTopologyUtils.TIMEOUT);
+                Patterns.ask(masterContextRef, new ReadRequest(store, path), actorResponseWaitTime);
 
         LOG.trace("{}: Read {} via NETCONF: {}", id, store, path);
 
@@ -87,9 +90,11 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
             @Override
             public void onComplete(final Throwable failure, final Object success) throws Throwable {
                 if (failure != null) { // ask timeout
-                    Exception exception = new DocumentedException(id + ":Master is down. Please try again.",
-                            DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED,
-                            DocumentedException.ErrorSeverity.WARNING);
+                    final Exception exception = new DocumentedException(
+                            id + ":Master is down. Please try again.",
+                            DocumentedException.ErrorType.TRANSPORT,
+                            DocumentedException.ErrorTag.RESOURCE_DENIED,
+                            DocumentedException.ErrorSeverity.ERROR);
                     promise.failure(exception);
                     return;
                 }
@@ -111,7 +116,7 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
     @Override
     public Future<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
         final Future<Object> existsScalaFuture =
-                Patterns.ask(masterContextRef, new ExistsRequest(store, path), NetconfTopologyUtils.TIMEOUT);
+                Patterns.ask(masterContextRef, new ExistsRequest(store, path), actorResponseWaitTime);
 
         LOG.trace("{}: Exists {} via NETCONF: {}", id, store, path);
 
@@ -120,9 +125,11 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
             @Override
             public void onComplete(final Throwable failure, final Object success) throws Throwable {
                 if (failure != null) { // ask timeout
-                    Exception exception = new DocumentedException(id + ":Master is down. Please try again.",
-                            DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED,
-                            DocumentedException.ErrorSeverity.WARNING);
+                    final Exception exception = new DocumentedException(
+                            id + ":Master is down. Please try again.",
+                            DocumentedException.ErrorType.TRANSPORT,
+                            DocumentedException.ErrorTag.RESOURCE_DENIED,
+                            DocumentedException.ErrorSeverity.ERROR);
                     promise.failure(exception);
                     return;
                 }
@@ -141,7 +148,6 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
         LOG.trace("{}: Write {} via NETCONF: {} with payload {}", id, store, data.getIdentifier(), data.getNode());
 
         masterContextRef.tell(new PutRequest(store, data), ActorRef.noSender());
-
     }
 
     @Override
@@ -161,13 +167,13 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
     @Override
     public boolean cancel() {
         final Future<Object> cancelScalaFuture =
-                Patterns.ask(masterContextRef, new CancelRequest(), NetconfTopologyUtils.TIMEOUT);
+                Patterns.ask(masterContextRef, new CancelRequest(), actorResponseWaitTime);
 
         LOG.trace("{}: Cancel {} via NETCONF", id);
 
         try {
             // here must be Await because AsyncWriteTransaction do not return future
-            return (boolean) Await.result(cancelScalaFuture, NetconfTopologyUtils.TIMEOUT.duration());
+            return (boolean) Await.result(cancelScalaFuture, actorResponseWaitTime.duration());
         } catch (Exception e) {
             return false;
         }
@@ -176,7 +182,7 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
     @Override
     public Future<Void> submit() {
         final Future<Object> submitScalaFuture =
-                Patterns.ask(masterContextRef, new SubmitRequest(), NetconfTopologyUtils.TIMEOUT);
+                Patterns.ask(masterContextRef, new SubmitRequest(), actorResponseWaitTime);
 
         LOG.trace("{}: Submit {} via NETCONF", id);
 
@@ -186,9 +192,11 @@ public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
             @Override
             public void onComplete(final Throwable failure, final Object success) throws Throwable {
                 if (failure != null) { // ask timeout
-                    Exception exception = new DocumentedException(id + ":Master is down. Please try again.",
-                            DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED,
-                            DocumentedException.ErrorSeverity.WARNING);
+                    final Exception exception = new DocumentedException(
+                            id + ":Master is down. Please try again.",
+                            DocumentedException.ErrorType.TRANSPORT,
+                            DocumentedException.ErrorTag.RESOURCE_DENIED,
+                            DocumentedException.ErrorSeverity.ERROR);
                     promise.failure(exception);
                     return;
                 }