Bug 2233 - RPC register exception when rpc has no input
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / AbstractRuntimeCodeGenerator.java
index 9605a4d3723b6f21ccf8ce5810ad078ba3e3bbd6..eba5b8756b88cfaa936e3aa20828706a29abaf5d 100644 (file)
@@ -7,27 +7,27 @@
  */
 package org.opendaylight.controller.sal.binding.codegen.impl;
 
-import java.util.Map;
-import java.util.WeakHashMap;
-
+import com.google.common.base.Supplier;
+import com.google.common.collect.Iterables;
 import javassist.ClassPool;
 import javassist.CtClass;
 import javassist.CtMethod;
 import javassist.NotFoundException;
-
-import javax.annotation.concurrent.GuardedBy;
-
 import org.eclipse.xtext.xbase.lib.Extension;
 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
+import org.opendaylight.controller.sal.binding.codegen.RpcIsNotRoutedException;
 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
+import org.opendaylight.yangtools.yang.binding.BindingMapping;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.NotificationListener;
 import org.opendaylight.yangtools.yang.binding.RpcService;
 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
 import org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils;
 
-import com.google.common.base.Supplier;
+import javax.annotation.concurrent.GuardedBy;
+import java.util.Map;
+import java.util.WeakHashMap;
 
 abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator, NotificationInvokerFactory {
     @GuardedBy("this")
@@ -56,11 +56,11 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
     protected abstract <T extends RpcService> Supplier<T> directProxySupplier(final Class<T> serviceType);
     protected abstract <T extends RpcService> Supplier<T> routerSupplier(final Class<T> serviceType, RpcServiceMetadata metadata);
 
-    private RpcServiceMetadata getRpcMetadata(final CtClass iface) throws ClassNotFoundException, NotFoundException {
+    private RpcServiceMetadata getRpcMetadata(final CtClass iface) throws ClassNotFoundException, NotFoundException, RpcIsNotRoutedException {
         final RpcServiceMetadata metadata = new RpcServiceMetadata();
 
         for (CtMethod method : iface.getMethods()) {
-            if (iface.equals(method.getDeclaringClass()) && method.getParameterTypes().length == 1) {
+            if (isRpcMethodWithInput(iface, method)) {
                 final RpcMetadata routingPair = getRpcMetadata(method);
                 if (routingPair != null) {
                     metadata.addContext(routingPair.getContext());
@@ -81,6 +81,8 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
                      *        remains to be investigated.
                      */
                     Thread.currentThread().getContextClassLoader().loadClass(routingPair.getInputType().getName());
+                } else {
+                    throw new RpcIsNotRoutedException("RPC " + method.getName() + " from "+ iface.getName() +" is not routed");
                 }
             }
         }
@@ -88,6 +90,18 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
         return metadata;
     }
 
+
+    private boolean isRpcMethodWithInput(final CtClass iface, final CtMethod method) throws NotFoundException {
+        if(iface.equals(method.getDeclaringClass())
+                && method.getParameterTypes().length == 1) {
+            final CtClass onlyArg = method.getParameterTypes()[0];
+            if(onlyArg.isInterface() && onlyArg.getName().endsWith(BindingMapping.RPC_INPUT_SUFFIX)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private RpcMetadata getRpcMetadata(final CtMethod method) throws NotFoundException {
         final CtClass inputClass = method.getParameterTypes()[0];
         return rpcMethodMetadata(inputClass, inputClass, method.getName());
@@ -120,20 +134,17 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
             return invoker;
         }
 
-        utils.getLock().lock();
-        try {
+        synchronized (utils) {
             invoker = ClassLoaderUtils.withClassLoader(cls.getClassLoader(), new Supplier<RuntimeGeneratedInvokerPrototype>() {
                 @Override
                 public RuntimeGeneratedInvokerPrototype get() {
                     return generateListenerInvoker(cls);
                 }
             });
-
-            invokerClasses.put(cls, invoker);
-            return invoker;
-        } finally {
-            utils.getLock().unlock();
         }
+
+        invokerClasses.put(cls, invoker);
+        return invoker;
     }
 
     @Override
@@ -143,16 +154,13 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
 
     @Override
     public final <T extends RpcService> T getDirectProxyFor(final Class<T> serviceType) {
-        utils.getLock().lock();
-        try {
+        synchronized (utils) {
             return ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), directProxySupplier(serviceType));
-        } finally {
-            utils.getLock().unlock();
         }
     }
 
     @Override
-    public final <T extends RpcService> RpcRouter<T> getRouterFor(final Class<T> serviceType, final String name) {
+    public final <T extends RpcService> RpcRouter<T> getRouterFor(final Class<T> serviceType, final String name) throws RpcIsNotRoutedException {
         final RpcServiceMetadata metadata = ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), new Supplier<RpcServiceMetadata>() {
             @Override
             public RpcServiceMetadata get() {
@@ -164,12 +172,13 @@ abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controll
             }
         });
 
-        utils.getLock().lock();
-        try {
+        if (Iterables.isEmpty(metadata.getContexts())) {
+            throw new RpcIsNotRoutedException("Service doesn't have routing context associated.");
+        }
+
+        synchronized (utils) {
             final T instance = ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), routerSupplier(serviceType, metadata));
             return new RpcRouterCodegenInstance<T>(name, serviceType, instance, metadata.getContexts());
-        } finally {
-            utils.getLock().unlock();
         }
     }