Attempt netconf remount regardless of error-type
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / SchemalessNetconfDeviceRpc.java
index 68d20c7e42159b2a1b13d844f672c4f953c58bd7..0cfe7b5b7150b56a3840e6b29c74666fb2e712c5 100644 (file)
@@ -7,18 +7,17 @@
  */
 package org.opendaylight.netconf.sal.connect.netconf.sal;
 
-import com.google.common.base.Function;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.FluentFuture;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.MoreExecutors;
+import com.google.common.util.concurrent.SettableFuture;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
-import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
+import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
+import org.opendaylight.mdsal.dom.api.DOMRpcService;
+import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
@@ -27,12 +26,11 @@ import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMes
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
@@ -44,7 +42,8 @@ public final class SchemalessNetconfDeviceRpc implements DOMRpcService {
     private final SchemalessMessageTransformer schemalessTransformer;
     private final RemoteDeviceId deviceId;
 
-    public SchemalessNetconfDeviceRpc(RemoteDeviceId deviceId, final RemoteDeviceCommunicator<NetconfMessage> listener,
+    public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId,
+                                      final RemoteDeviceCommunicator<NetconfMessage> listener,
                                       final BaseRpcSchemalessTransformer baseRpcTransformer,
                                       final SchemalessMessageTransformer messageTransformer) {
         this.deviceId = deviceId;
@@ -53,56 +52,51 @@ public final class SchemalessNetconfDeviceRpc implements DOMRpcService {
         this.schemalessTransformer = messageTransformer;
     }
 
-    @Nonnull
     @Override
-    public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
-                                                                  @Nullable final NormalizedNode<?, ?> input) {
+    public FluentFuture<DOMRpcResult> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
         final MessageTransformer<NetconfMessage> transformer;
         if (input instanceof AnyXmlNode) {
             transformer = schemalessTransformer;
         } else if (isBaseRpc(type)) {
             transformer = baseRpcTransformer;
         } else {
-            return Futures.immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("Unable to invoke rpc %s", type));
+            return FluentFutures.immediateFailedFluentFuture(new DOMRpcImplementationNotAvailableException(
+                "Unable to invoke rpc %s", type));
         }
         return handleRpc(type, input, transformer);
     }
 
-    private CheckedFuture<DOMRpcResult, DOMRpcException> handleRpc(@Nonnull final SchemaPath type,
-                                                                   @Nullable final NormalizedNode<?, ?> input,
-                                                                   final MessageTransformer<NetconfMessage> transformer) {
-        final NetconfMessage netconfMessage = transformer.toRpcRequest(type, input);
-        final ListenableFuture<RpcResult<NetconfMessage>> rpcResultListenableFuture = listener.sendRequest(netconfMessage, type.getLastComponent());
+    private FluentFuture<DOMRpcResult> handleRpc(
+            @Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input,
+            final MessageTransformer<NetconfMessage> transformer) {
+        final FluentFuture<RpcResult<NetconfMessage>> delegateFuture = listener.sendRequest(
+            transformer.toRpcRequest(type, input), type.getLastComponent());
 
-        final ListenableFuture<DOMRpcResult> transformed = Futures.transform(rpcResultListenableFuture, new Function<RpcResult<NetconfMessage>, DOMRpcResult>() {
+        final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
+        delegateFuture.addCallback(new FutureCallback<RpcResult<NetconfMessage>>() {
             @Override
-            public DOMRpcResult apply(final RpcResult<NetconfMessage> input) {
-                if (input.isSuccessful()) {
-                    return transformer.toRpcResult(input.getResult(), type);
-                } else {
-                    return new DefaultDOMRpcResult(input.getErrors());
-                }
+            public void onSuccess(RpcResult<NetconfMessage> result) {
+                ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
+                        : new DefaultDOMRpcResult(result.getErrors()));
             }
-        });
 
-        return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
-            @Nullable
             @Override
-            public DOMRpcException apply(@Nullable final Exception e) {
-                return new DOMRpcImplementationNotAvailableException(e, "Unable to invoke rpc %s on device %s", type, deviceId);
+            public void onFailure(Throwable cause) {
+                ret.setException(new DOMRpcImplementationNotAvailableException(cause,
+                    "Unable to invoke rpc %s on device %s", type, deviceId));
             }
-        });
-    }
 
+        }, MoreExecutors.directExecutor());
+        return ret;
+    }
 
-    private boolean isBaseRpc(final SchemaPath type) {
+    private static boolean isBaseRpc(final SchemaPath type) {
         return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getLastComponent().getNamespace());
     }
 
     @Nonnull
     @Override
-    public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
+    public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T lsnr) {
         throw new UnsupportedOperationException("Not available for netconf 1.0");
     }
-
 }