Fixed bug in discovering JVM loaded case classes during code generation
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / RuntimeCodeGenerator.xtend
index 90be6f3476841bf2f20e53f9cc66438148722d60..d9e0983cfa516f07e0285dab813b5b0c1b3aec17 100644 (file)
@@ -11,10 +11,6 @@ import javassist.ClassPool
 import org.opendaylight.yangtools.yang.binding.RpcService
 
 import javassist.CtClass
-import static com.google.common.base.Preconditions.*
-
-import javassist.CtField
-import javassist.Modifier
 import javassist.CtMethod
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext
@@ -22,71 +18,116 @@ import org.opendaylight.yangtools.yang.binding.BaseIdentity
 
 import java.util.Map
 import java.util.HashMap
-import javassist.NotFoundException
-import javassist.LoaderClassPath
-import org.opendaylight.controller.sal.binding.codegen.impl.JavassistUtils.MethodGenerator
-import org.opendaylight.controller.sal.binding.codegen.impl.JavassistUtils.ClassGenerator
+
+
 import org.opendaylight.yangtools.yang.binding.NotificationListener
 import org.opendaylight.yangtools.yang.binding.Notification
-import java.util.Arrays
+
 
 import static extension org.opendaylight.controller.sal.binding.codegen.YangtoolsMappingHelper.*
 import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification.*
 import java.util.HashSet
-import java.io.ObjectOutputStream.PutField
-import static org.opendaylight.controller.sal.binding.impl.osgi.ClassLoaderUtils.*
+import static org.opendaylight.yangtools.concepts.util.ClassLoaderUtils.*
+import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory
+import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker
+import java.util.Set
+import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper
+import java.util.WeakHashMap
+import org.opendaylight.yangtools.yang.binding.annotations.QName
+import org.opendaylight.yangtools.yang.binding.DataContainer
+import org.opendaylight.yangtools.yang.binding.RpcImplementation
+import org.opendaylight.controller.sal.binding.codegen.util.JavassistUtils
+import org.opendaylight.controller.sal.binding.impl.util.ClassLoaderUtils
+import javassist.LoaderClassPath
 
-class RuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator {
+class RuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator, NotificationInvokerFactory {
 
+    val CtClass BROKER_NOTIFICATION_LISTENER;
     val ClassPool classPool;
+    val extension JavassistUtils utils;
+    val Map<Class<? extends NotificationListener>, RuntimeGeneratedInvokerPrototype> invokerClasses;
+
 
-    public new(ClassPool pool) {
+    new(ClassPool pool) {
         classPool = pool;
+        utils = new JavassistUtils(pool);
+        invokerClasses = new WeakHashMap();
+        BROKER_NOTIFICATION_LISTENER = org.opendaylight.controller.sal.binding.api.NotificationListener.asCtClass;
+        pool.appendClassPath(new LoaderClassPath(RpcService.classLoader));
     }
 
     override <T extends RpcService> getDirectProxyFor(Class<T> iface) {
-        val supertype = iface.asCtClass
-        val targetCls = createClass(iface.directProxyName, supertype) [
-            field(DELEGATE_FIELD, iface);
-            implementMethodsFrom(supertype) [
-                body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
+        val T instance =  withClassLoaderAndLock(iface.classLoader,lock) [|
+            val proxyName = iface.directProxyName;
+            val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(proxyName)
+            if(potentialClass != null) {
+                return potentialClass.newInstance as T;
+            }
+            val supertype = iface.asCtClass
+            val createdCls = createClass(iface.directProxyName, supertype) [
+                field(DELEGATE_FIELD, iface);
+                implementsType(RpcImplementation.asCtClass)
+                implementMethodsFrom(supertype) [
+                    body = '''
+                    {
+                        if(«DELEGATE_FIELD» == null) {
+                            throw new java.lang.IllegalStateException("No default provider is available");
+                        }
+                        return ($r) «DELEGATE_FIELD».«it.name»($$);
+                    }
+                    '''
+                ]
+                implementMethodsFrom(RpcImplementation.asCtClass) [
+                    body = '''
+                    {
+                        throw new java.lang.IllegalStateException("No provider is processing supplied message");
+                        return ($r) null;
+                    }
+                    '''
+                ]
             ]
+            return createdCls.toClass(iface.classLoader).newInstance as T
         ]
-        return targetCls.toClass(iface.classLoader).newInstance as T
+        return instance;
     }
 
-    override <T extends RpcService> getRouterFor(Class<T> iface) {
-        val contexts = new HashSet<Class<? extends BaseIdentity>>
-
-        val instance = <T>withClassLoader(iface.classLoader) [ |
+    override <T extends RpcService> getRouterFor(Class<T> iface,String routerInstanceName) {
+        val metadata = withClassLoader(iface.classLoader) [|
             val supertype = iface.asCtClass
+            return supertype.rpcMetadata;
+        ]
+        
+        val instance = <T>withClassLoaderAndLock(iface.classLoader,lock) [ |
+            val supertype = iface.asCtClass
+            val routerName = iface.routerName;
+            val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(routerName)
+            if(potentialClass != null) {
+                return potentialClass.newInstance as T;
+            }
+            
             val targetCls = createClass(iface.routerName, supertype) [
-                //field(ROUTING_TABLE_FIELD,Map)
+                
+                
                 field(DELEGATE_FIELD, iface)
-                val ctxMap = new HashMap<String, Class<? extends BaseIdentity>>();
-                // We search for routing pairs and add fields
-                supertype.methods.filter[declaringClass == supertype && parameterTypes.size === 1].forEach [ method |
-                    val routingPair = method.routingContextInput;
-                    if (routingPair !== null) {
-                        ctxMap.put(routingPair.context.routingTableField, routingPair.context);
-                        contexts.add(routingPair.context)
-                    }
-                ]
-                for (ctx : ctxMap.entrySet) {
-                    field(ctx.key, Map)
+                //field(REMOTE_INVOKER_FIELD,iface);
+                implementsType(RpcImplementation.asCtClass)
+                
+                for (ctx : metadata.contexts) {
+                    field(ctx.routingTableField, Map)
                 }
                 implementMethodsFrom(supertype) [
                     if (parameterTypes.size === 1) {
-                        val routingPair = routingContextInput;
+                        val rpcMeta = metadata.rpcMethods.get(name);
                         val bodyTmp = '''
                         {
-                            final «InstanceIdentifier.name» identifier = $1.«routingPair.getter.name»()«IF routingPair.encapsulated».getValue()«ENDIF»;
-                            «supertype.name» instance = («supertype.name») «routingPair.context.routingTableField».get(identifier);
+                            final «InstanceIdentifier.name» identifier = $1.«rpcMeta.inputRouteGetter.name»()«IF rpcMeta.
+                            routeEncapsulated».getValue()«ENDIF»;
+                            «supertype.name» instance = («supertype.name») «rpcMeta.context.routingTableField».get(identifier);
                             if(instance == null) {
                                instance = «DELEGATE_FIELD»;
                             }
                             if(instance == null) {
-                                throw new java.lang.IllegalStateException("No provider is processing supplied message");
+                                throw new java.lang.IllegalStateException("No routable provider is processing routed message for " + String.valueOf(identifier));
                             }
                             return ($r) instance.«it.name»($$);
                         }'''
@@ -95,107 +136,190 @@ class RuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.co
                         body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
                     }
                 ]
-            ]
-            return targetCls.toClass(iface.classLoader).newInstance as T
-        ];
-        return new RpcRouterCodegenInstance(iface, instance, contexts);
-    }
-
-    def Class<?> generateListenerInvoker(Class<? extends NotificationListener> iface) {
-        val targetCls = createClass(iface.invokerName) [
-            field(DELEGATE_FIELD, iface)
-            it.method(Void, "invoke", Notification) [
-                val callbacks = iface.methods.filter[notificationCallback]
-                body = '''
+                implementMethodsFrom(RpcImplementation.asCtClass) [
+                    body = '''
                     {
-                        «FOR callback : callbacks SEPARATOR " else "»
-                            if($1 instanceof «val cls = callback.parameterTypes.get(0).name») {
-                                «DELEGATE_FIELD».«callback.name»((«cls») $1);
-                                return;
-                            }
-                        «ENDFOR»
+                        throw new java.lang.IllegalStateException("No provider is processing supplied message");
+                        return ($r) null;
                     }
-                '''
+                    '''
+                ]
             ]
-        ]
-        return targetCls.toClass(iface.classLoader);
+            return targetCls.toClass(iface.classLoader,iface.protectionDomain).newInstance as T
+            
+        ];
+        return new RpcRouterCodegenInstance(routerInstanceName,iface, instance, metadata.contexts,metadata.supportedInputs);
     }
 
-    def void method(CtClass it, Class<?> returnType, String name, Class<?> parameter, MethodGenerator function1) {
-        val method = new CtMethod(returnType.asCtClass, name, Arrays.asList(parameter.asCtClass), it);
-        function1.process(method);
-        it.addMethod(method);
+    private def RpcServiceMetadata getRpcMetadata(CtClass iface) {
+        val metadata = new RpcServiceMetadata;
+        
+        iface.methods.filter[declaringClass == iface && parameterTypes.size === 1].forEach [ method |
+            val routingPair = method.rpcMetadata;
+            if (routingPair !== null) {
+                metadata.contexts.add(routingPair.context)
+                metadata.rpcMethods.put(method.name,routingPair)
+                val input = routingPair.inputType.javaClass as Class<? extends DataContainer>;
+                metadata.supportedInputs.add(input);
+                metadata.rpcInputs.put(input,routingPair);
+            }
+        ]
+        return metadata;
     }
 
-    private def routingContextInput(CtMethod method) {
-        val inputClass = method.parameterTypes.get(0);
-        return inputClass.contextInstance;
+    private def getRpcMetadata(CtMethod method) {
+        val inputClass = method.parameterTypes.get(0); 
+        return inputClass.rpcMethodMetadata(inputClass,method.name);
     }
 
-    private def RoutingPair getContextInstance(CtClass dataClass) {
+    private def RpcMetadata rpcMethodMetadata(CtClass dataClass,CtClass inputClass,String rpcMethod) {
         for (method : dataClass.methods) {
             if (method.name.startsWith("get") && method.parameterTypes.size === 0) {
                 for (annotation : method.availableAnnotations) {
                     if (annotation instanceof RoutingContext) {
                         val encapsulated = !method.returnType.equals(InstanceIdentifier.asCtClass);
-                        
-                        return new RoutingPair((annotation as RoutingContext).value, method,encapsulated);
+                        return new RpcMetadata(null,rpcMethod,(annotation as RoutingContext).value, method, encapsulated,inputClass);
                     }
                 }
             }
         }
         for (iface : dataClass.interfaces) {
-            val ret = getContextInstance(iface);
+            val ret = rpcMethodMetadata(iface,inputClass,rpcMethod);
             if(ret != null) return ret;
         }
         return null;
     }
 
-    private def void implementMethodsFrom(CtClass target, CtClass source, MethodGenerator function1) {
-        for (method : source.methods) {
-            if (method.declaringClass == source) {
-                val redeclaredMethod = new CtMethod(method, target, null);
-                function1.process(redeclaredMethod);
-                target.addMethod(redeclaredMethod);
-            }
-        }
+    private def getJavaClass(CtClass cls) {
+        Thread.currentThread.contextClassLoader.loadClass(cls.name)
     }
 
-    private def CtClass createClass(String fqn, ClassGenerator cls) {
-        val target = classPool.makeClass(fqn);
-        cls.process(target);
-        return target;
+    override getInvokerFactory() {
+        return this;
     }
 
-    private def CtClass createClass(String fqn, CtClass superInterface, ClassGenerator cls) {
-        val target = classPool.makeClass(fqn);
-        target.implementsType(superInterface);
-        cls.process(target);
-        return target;
+    override invokerFor(NotificationListener instance) {
+        val cls = instance.class
+        val prototype = resolveInvokerClass(cls);
+
+        return new RuntimeGeneratedInvoker(instance, prototype)
     }
 
-    private def void implementsType(CtClass it, CtClass supertype) {
-        checkArgument(supertype.interface, "Supertype must be interface");
-        addInterface(supertype);
+    protected def generateListenerInvoker(Class<? extends NotificationListener> iface) {
+        val callbacks = iface.methods.filter[notificationCallback]
+
+        val supportedNotification = callbacks.map[parameterTypes.get(0) as Class<? extends Notification>].toSet;
+
+        val targetCls = createClass(iface.invokerName, BROKER_NOTIFICATION_LISTENER) [
+            field(DELEGATE_FIELD, iface)
+            implementMethodsFrom(BROKER_NOTIFICATION_LISTENER) [
+                body = '''
+                    {
+                        «FOR callback : callbacks SEPARATOR " else "»
+                            «val cls = callback.parameterTypes.get(0).name»
+                                if($1 instanceof «cls») {
+                                    «DELEGATE_FIELD».«callback.name»((«cls») $1);
+                                    return null;
+                                }
+                        «ENDFOR»
+                        return null;
+                    }
+                '''
+            ]
+        ]
+        val finalClass = targetCls.toClass(iface.classLoader, iface.protectionDomain)
+        return new RuntimeGeneratedInvokerPrototype(supportedNotification,
+            finalClass as Class<? extends org.opendaylight.controller.sal.binding.api.NotificationListener<?>>);
     }
 
-    private def asCtClass(Class<?> class1) {
-        classPool.get(class1);
+    
+
+
+
+    protected def resolveInvokerClass(Class<? extends NotificationListener> class1) {
+        return <RuntimeGeneratedInvokerPrototype>withClassLoaderAndLock(class1.classLoader,lock) [|
+            val invoker = invokerClasses.get(class1);
+            if (invoker !== null) {
+                return invoker;
+            }
+            val newInvoker = generateListenerInvoker(class1);
+            invokerClasses.put(class1, newInvoker);
+            return newInvoker
+            
+        ]
     }
+}
+
+@Data
+package class RuntimeGeneratedInvoker implements NotificationInvoker {
+
+    @Property
+    val NotificationListener delegate;
 
-    private def CtField field(CtClass it, String name, Class<?> returnValue) {
-        val field = new CtField(returnValue.asCtClass, name, it);
-        field.modifiers = Modifier.PUBLIC
-        addField(field);
-        return field;
+    @Property
+    var org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> invocationProxy;
+
+    @Property
+    var RuntimeGeneratedInvokerPrototype prototype;
+
+    new(NotificationListener delegate, RuntimeGeneratedInvokerPrototype prototype) {
+        _delegate = delegate;
+        _prototype = prototype;
+        _invocationProxy = prototype.protoClass.newInstance as org.opendaylight.controller.sal.binding.api.NotificationListener<Notification>;
+        RuntimeCodeHelper.setDelegate(_invocationProxy, delegate);
     }
 
-    def get(ClassPool pool, Class<?> cls) {
-        try {
-            return pool.get(cls.name)
-        } catch (NotFoundException e) {
-            pool.appendClassPath(new LoaderClassPath(cls.classLoader));
-            return pool.get(cls.name)
-        }
+    override getSupportedNotifications() {
+        prototype.supportedNotifications;
+    }
+
+    override close() {
     }
 }
+
+@Data
+package class RuntimeGeneratedInvokerPrototype {
+
+    @Property
+    val Set<Class<? extends Notification>> supportedNotifications;
+
+    @Property
+    val Class<? extends org.opendaylight.controller.sal.binding.api.NotificationListener<?>> protoClass;
+}
+
+package class RpcServiceMetadata {
+
+    @Property
+    val contexts = new HashSet<Class<? extends BaseIdentity>>();
+
+    @Property
+    val rpcMethods = new HashMap<String, RpcMetadata>();
+    
+    @Property
+    val rpcInputs = new HashMap<Class<? extends DataContainer>, RpcMetadata>();
+    
+    
+    @Property
+    val supportedInputs = new HashSet<Class<? extends DataContainer>>();
+}
+
+@Data
+package class RpcMetadata {
+
+    @Property
+    val QName qname;
+
+    @Property
+    val String methodName;
+
+    @Property
+    val Class<? extends BaseIdentity> context;
+    @Property
+    val CtMethod inputRouteGetter;
+
+    @Property
+    val boolean routeEncapsulated;
+    
+    @Property
+    val CtClass inputType;
+}