Bump upstreams
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceRpc.java
index 2bf04875d31857e2a99def1514d8f950170e7bcc..48cbbb9dbc13d94bd12831dbc651478bcb94fe36 100644 (file)
@@ -10,58 +10,67 @@ package org.opendaylight.netconf.sal.connect.netconf.sal;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.Collections2;
-import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import com.google.common.util.concurrent.SettableFuture;
 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
 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.mdsal.dom.api.DefaultDOMRpcException;
 import org.opendaylight.netconf.api.NetconfMessage;
-import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
+import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
+import org.opendaylight.netconf.sal.connect.api.RpcTransformer;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.concepts.NoOpListenerRegistration;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 
 /**
- * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
+ * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to
+ * {@link ContainerNode}.
  */
-public final class NetconfDeviceRpc implements DOMRpcService {
+public final class NetconfDeviceRpc implements Rpcs.Normalized {
+    private final RemoteDeviceCommunicator communicator;
+    private final RpcTransformer<ContainerNode, DOMRpcResult> transformer;
+    private final EffectiveModelContext modelContext;
 
-    private final RemoteDeviceCommunicator<NetconfMessage> communicator;
-    private final MessageTransformer<NetconfMessage> transformer;
-    private final SchemaContext schemaContext;
-
-    public NetconfDeviceRpc(final SchemaContext schemaContext,
-            final RemoteDeviceCommunicator<NetconfMessage> communicator,
-            final MessageTransformer<NetconfMessage> transformer) {
+    public NetconfDeviceRpc(final EffectiveModelContext modelContext, final RemoteDeviceCommunicator communicator,
+            final RpcTransformer<ContainerNode, DOMRpcResult> transformer) {
+        this.modelContext = requireNonNull(modelContext);
         this.communicator = communicator;
         this.transformer = transformer;
-        this.schemaContext = requireNonNull(schemaContext);
     }
 
     @Override
-    public FluentFuture<DOMRpcResult> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
-        final FluentFuture<RpcResult<NetconfMessage>> delegateFuture = communicator.sendRequest(
-            transformer.toRpcRequest(type, input), type.getLastComponent());
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
+        final ListenableFuture<RpcResult<NetconfMessage>> delegateFuture = communicator.sendRequest(
+            transformer.toRpcRequest(type, input), type);
 
         final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
-        delegateFuture.addCallback(new FutureCallback<RpcResult<NetconfMessage>>() {
+        Futures.addCallback(delegateFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
             @Override
-            public void onSuccess(RpcResult<NetconfMessage> result) {
-                ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
-                        : new DefaultDOMRpcResult(result.getErrors()));
+            public void onSuccess(final RpcResult<NetconfMessage> result) {
+                final DOMRpcResult rpcResult;
+                try {
+                    rpcResult = transformer.toRpcResult(result, type);
+                } catch (Exception cause) {
+                    ret.setException(new DefaultDOMRpcException(
+                        "Unable to parse rpc reply. type: " + type + " input: " + input, cause));
+                    return;
+                }
+
+                ret.set(rpcResult);
             }
 
             @Override
-            public void onFailure(Throwable cause) {
+            public void onFailure(final Throwable cause) {
                 ret.setException(new DOMRpcImplementationNotAvailableException(cause, "Unable to invoke rpc %s", type));
             }
 
@@ -71,8 +80,8 @@ public final class NetconfDeviceRpc implements DOMRpcService {
 
     @Override
     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
-        listener.onRpcAvailable(Collections2.transform(schemaContext.getOperations(),
-            input -> DOMRpcIdentifier.create(input.getPath())));
+        listener.onRpcAvailable(Collections2.transform(modelContext.getOperations(),
+            input -> DOMRpcIdentifier.create(input.getQName())));
 
         // NOOP, no rpcs appear and disappear in this implementation
         return NoOpListenerRegistration.of(listener);