Seal LeafReference
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / RpcServiceAdapter.java
index 62e39cf17d2f8ff8fddaf7424b523b3b59a5d3f5..0659d4f712753fae751e44df19e91551f951a049 100644 (file)
@@ -10,80 +10,63 @@ package org.opendaylight.mdsal.binding.dom.adapter;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableMap;
-import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import org.eclipse.jdt.annotation.NonNull;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.opendaylight.mdsal.binding.runtime.api.RpcRuntimeType;
+import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
-import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.RpcInput;
 import org.opendaylight.yangtools.yang.binding.RpcService;
+import org.opendaylight.yangtools.yang.binding.contract.Naming;
+import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
 
-class RpcServiceAdapter implements InvocationHandler {
+@Deprecated
+final class RpcServiceAdapter extends AbstractRpcAdapter {
     private final ImmutableMap<Method, RpcInvocationStrategy> rpcNames;
-    private final @NonNull Class<? extends RpcService> type;
-    private final @NonNull AdapterContext adapterContext;
-    private final @NonNull DOMRpcService delegate;
-    private final @NonNull RpcService facade;
 
     RpcServiceAdapter(final Class<? extends RpcService> type, final AdapterContext adapterContext,
             final DOMRpcService domService) {
-        this.type = requireNonNull(type);
-        this.adapterContext = requireNonNull(adapterContext);
-        delegate = requireNonNull(domService);
+        super(adapterContext, domService, type);
 
-        final var methods = adapterContext.currentSerializer().getRpcMethodToSchema(type);
-        final var rpcBuilder = ImmutableMap.<Method, RpcInvocationStrategy>builderWithExpectedSize(methods.size());
-        for (var rpc : methods.entrySet()) {
-            final var method = rpc.getKey();
-            rpcBuilder.put(method, RpcInvocationStrategy.of(this, method, rpc.getValue().asEffectiveStatement()));
-        }
-        rpcNames = rpcBuilder.build();
-        facade = (RpcService) Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type}, this);
-    }
+        // FIXME: This should be probably part of BindingRuntimeContext and RpcServices perhaps should have their own
+        //        RuntimeType. At any rate, we are dancing around to reconstruct information RpcServiceRuntimeType would
+        //        carry and the runtime context would bind to actual classes.
+        final var serializer = adapterContext.currentSerializer();
+        final var runtimeContext = serializer.getRuntimeContext();
+        final var types = runtimeContext.getTypes();
+        final var qnameModule = BindingReflections.getQNameModule(type);
 
-    final @NonNull CurrentAdapterSerializer currentSerializer() {
-        return adapterContext.currentSerializer();
-    }
+        // We are dancing a bit here to reconstruct things a RpcServiceRuntimeType could easily hold
+        final var module = runtimeContext.getEffectiveModelContext().findModuleStatement(qnameModule)
+            .orElseThrow(() -> new IllegalStateException("No module found for " + qnameModule + " service " + type));
+        rpcNames = module.streamEffectiveSubstatements(RpcEffectiveStatement.class)
+            .map(rpc -> {
+                final var rpcName = rpc.argument();
+                final var inputClz = runtimeContext.getRpcInput(rpcName);
+                final var methodName = Naming.getRpcMethodName(rpcName);
 
-    final @NonNull DOMRpcService delegate() {
-        return delegate;
-    }
+                final Method method;
+                try {
+                    method = type.getMethod(methodName, inputClz);
+                } catch (NoSuchMethodException e) {
+                    throw new IllegalStateException("Cannot find RPC method for " + rpc, e);
+                }
+
+                final var runtimeType = types.schemaTreeChild(rpcName);
+                if (!(runtimeType instanceof RpcRuntimeType rpcType)) {
+                    throw new IllegalStateException("Unexpected run-time type " + runtimeType + " for " + rpcName);
+                }
 
-    final @NonNull RpcService facade() {
-        return facade;
+                return Map.entry(method, createStrategy(serializer, rpcType));
+            })
+            .collect(ImmutableMap.toImmutableMap(Entry::getKey, Entry::getValue));
     }
 
     @Override
-    public Object invoke(final Object proxy, final Method method, final Object[] args) {
+    public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
         final var strategy = rpcNames.get(method);
-        if (strategy != null) {
-            if (args.length != 1) {
-                throw new IllegalArgumentException("Input must be provided.");
-            }
-            return strategy.invoke((DataObject) requireNonNull(args[0]));
-        }
-
-        switch (method.getName()) {
-            case "toString":
-                if (method.getReturnType().equals(String.class) && method.getParameterCount() == 0) {
-                    return type.getName() + "$Adapter{delegate=" + delegate.toString() + "}";
-                }
-                break;
-            case "hashCode":
-                if (method.getReturnType().equals(int.class) && method.getParameterCount() == 0) {
-                    return System.identityHashCode(proxy);
-                }
-                break;
-            case "equals":
-                if (method.getReturnType().equals(boolean.class) && method.getParameterCount() == 1
-                        && method.getParameterTypes()[0] == Object.class) {
-                    return proxy == args[0];
-                }
-                break;
-            default:
-                break;
-        }
-
-        throw new UnsupportedOperationException("Method " + method.toString() + "is unsupported.");
+        return strategy != null ? strategy.invoke((RpcInput) requireNonNull(args[0]))
+            : defaultInvoke(proxy, method, args);
     }
 }