Move getRpcMethodToSchema() 29/103329/3
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 23 Nov 2022 14:26:58 +0000 (15:26 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 24 Nov 2022 14:35:58 +0000 (15:35 +0100)
This method is only used by RpcServiceAdapter and depends only on
BindingRuntimeContext. Move it to RpcServiceAdapter to improve
containment.

JIRA: MDSAL-777
Change-Id: Ib4222bb85214d27d82feec17fc8c91809ebe44de
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/CurrentAdapterSerializer.java
binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/RpcServiceAdapter.java

index ac1a6fec292e529cd8c3f0053d205fd2e8b7ecff..212d26491d2cc52fa9e1fa75d6be87ae6e1e47b5 100644 (file)
@@ -29,7 +29,6 @@ import org.opendaylight.mdsal.binding.api.InstanceNotificationSpec;
 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
 import org.opendaylight.mdsal.binding.dom.codec.spi.ForwardingBindingDOMCodecServices;
 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
-import org.opendaylight.mdsal.binding.runtime.api.RpcRuntimeType;
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
@@ -46,7 +45,6 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
 import org.slf4j.Logger;
@@ -141,38 +139,6 @@ public final class CurrentAdapterSerializer extends ForwardingBindingDOMCodecSer
         return Map.entry(stack, lastNamespace);
     }
 
-    // FIXME: This should be probably part of Binding Runtime context and RpcServices perhaps should have their own
-    //        RuntimeType
-    ImmutableBiMap<Method, RpcRuntimeType> getRpcMethodToSchema(final Class<? extends RpcService> key) {
-        final var runtimeContext = getRuntimeContext();
-        final var types = runtimeContext.getTypes();
-        final var qnameModule = BindingReflections.getQNameModule(key);
-
-        // 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 " + key));
-        return module.streamEffectiveSubstatements(RpcEffectiveStatement.class)
-            .map(rpc -> {
-                final var rpcName = rpc.argument();
-                final var inputClz = runtimeContext.getRpcInput(rpcName);
-                final var methodName = BindingMapping.getRpcMethodName(rpcName);
-
-                final Method method;
-                try {
-                    method = key.getMethod(methodName, inputClz);
-                } catch (NoSuchMethodException e) {
-                    throw new IllegalStateException("Cannot find RPC method for " + rpc, e);
-                }
-
-                final var type = types.schemaTreeChild(rpcName);
-                if (!(type instanceof RpcRuntimeType rpcType)) {
-                    throw new IllegalStateException("Unexpected run-time type " + type + " for " + rpcName);
-                }
-                return Map.entry(method, rpcType);
-            })
-            .collect(ImmutableBiMap.toImmutableBiMap(Entry::getKey, Entry::getValue));
-    }
-
     // FIXME: This should be probably part of Binding Runtime context
     ImmutableBiMap<Method, QName> getRpcMethodToQName(final Class<? extends RpcService> key) {
         final Module module = getModule(key);
index bd1f68071e2c237e52273cbaf8502fe2452ad9dd..18b2f2e41fdb324a51f61c924f7a4e318782505e 100644 (file)
@@ -9,14 +9,21 @@ package org.opendaylight.mdsal.binding.dom.adapter;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.collect.ImmutableBiMap;
 import com.google.common.collect.ImmutableMap;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
+import java.util.Map;
+import java.util.Map.Entry;
 import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.binding.runtime.api.RpcRuntimeType;
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
+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.RpcService;
+import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
 
 class RpcServiceAdapter implements InvocationHandler {
     private final ImmutableMap<Method, RpcInvocationStrategy> rpcNames;
@@ -30,15 +37,15 @@ class RpcServiceAdapter implements InvocationHandler {
         this.type = requireNonNull(type);
         this.adapterContext = requireNonNull(adapterContext);
         delegate = requireNonNull(domService);
+        facade = (RpcService) Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type}, this);
 
-        final var methods = adapterContext.currentSerializer().getRpcMethodToSchema(type);
+        final var methods = getRpcMethodToSchema(adapterContext.currentSerializer(), type);
         final var rpcBuilder = ImmutableMap.<Method, RpcInvocationStrategy>builderWithExpectedSize(methods.size());
         for (var entry : methods.entrySet()) {
             final var method = entry.getKey();
             rpcBuilder.put(method, RpcInvocationStrategy.of(this, method, entry.getValue()));
         }
         rpcNames = rpcBuilder.build();
-        facade = (RpcService) Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type}, this);
     }
 
     final @NonNull CurrentAdapterSerializer currentSerializer() {
@@ -86,4 +93,37 @@ class RpcServiceAdapter implements InvocationHandler {
 
         throw new UnsupportedOperationException("Method " + method.toString() + "is unsupported.");
     }
+
+    // FIXME: This should be probably part of BindingRuntimeContext and RpcServices perhaps should have their own
+    //        RuntimeType
+    private static ImmutableBiMap<Method, RpcRuntimeType> getRpcMethodToSchema(
+            final CurrentAdapterSerializer serializer, final Class<? extends RpcService> key) {
+        final var runtimeContext = serializer.getRuntimeContext();
+        final var types = runtimeContext.getTypes();
+        final var qnameModule = BindingReflections.getQNameModule(key);
+
+        // 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 " + key));
+        return module.streamEffectiveSubstatements(RpcEffectiveStatement.class)
+            .map(rpc -> {
+                final var rpcName = rpc.argument();
+                final var inputClz = runtimeContext.getRpcInput(rpcName);
+                final var methodName = BindingMapping.getRpcMethodName(rpcName);
+
+                final Method method;
+                try {
+                    method = key.getMethod(methodName, inputClz);
+                } catch (NoSuchMethodException e) {
+                    throw new IllegalStateException("Cannot find RPC method for " + rpc, e);
+                }
+
+                final var type = types.schemaTreeChild(rpcName);
+                if (!(type instanceof RpcRuntimeType rpcType)) {
+                    throw new IllegalStateException("Unexpected run-time type " + type + " for " + rpcName);
+                }
+                return Map.entry(method, rpcType);
+            })
+            .collect(ImmutableBiMap.toImmutableBiMap(Entry::getKey, Entry::getValue));
+    }
 }