X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fcodegen%2Fimpl%2FRuntimeCodeGenerator.xtend;h=d9e0983cfa516f07e0285dab813b5b0c1b3aec17;hp=ea6dc131c6583b3587b23cc172e61f62c3ad68af;hb=178d185be418a9ed491201bd6a0e4d98efa9d820;hpb=d2f2d5a34ccd0a715ab6ea7c1f5ac61f16f6e6cc diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/RuntimeCodeGenerator.xtend b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/RuntimeCodeGenerator.xtend index ea6dc131c6..d9e0983cfa 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/RuntimeCodeGenerator.xtend +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/RuntimeCodeGenerator.xtend @@ -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,62 +18,99 @@ 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 static org.opendaylight.controller.sal.binding.impl.util.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 javassist.ClassClassPath 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, NotificationInvokerFactory { val CtClass BROKER_NOTIFICATION_LISTENER; val ClassPool classPool; + val extension JavassistUtils utils; val Map, 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 getDirectProxyFor(Class 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 getRouterFor(Class iface) { - val contexts = new HashSet> - - val instance = >withClassLoader(iface.classLoader) [ | + override getRouterFor(Class iface,String routerInstanceName) { + val metadata = withClassLoader(iface.classLoader) [| val supertype = iface.asCtClass - val metadata = supertype.rpcMetadata; + return supertype.rpcMetadata; + ] + + val instance = 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) [ - addInterface(RpcImplementation.asCtClass) + field(DELEGATE_FIELD, iface) //field(REMOTE_INVOKER_FIELD,iface); + implementsType(RpcImplementation.asCtClass) for (ctx : metadata.contexts) { field(ctx.routingTableField, Map) @@ -94,7 +127,7 @@ class RuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.co 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»($$); }''' @@ -104,35 +137,18 @@ class RuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.co } ] implementMethodsFrom(RpcImplementation.asCtClass) [ - switch (name) { - case "getSupportedInputs": - body = ''' - { - throw new java.lang.UnsupportedOperationException("Not implemented yet"); - return ($r) null; - }''' - case "invoke": { - val tmpBody = ''' - { - «FOR input : metadata.supportedInputs SEPARATOR " else "» - «val rpcMetadata = metadata.rpcInputs.get(input)» - if(«input.name».class.equals($1)) { - return ($r) this.«rpcMetadata.methodName»((«input.name») $2); - } - «ENDFOR» - throw new java.lang.IllegalArgumentException("Not supported message type"); - return ($r) null; - } - ''' - body = tmpBody - } + body = ''' + { + throw new java.lang.IllegalStateException("No provider is processing supplied message"); + return ($r) null; } + ''' ] ] - val instance = targetCls.toClass(iface.classLoader,iface.protectionDomain).newInstance as T - return new RpcRouterCodegenInstance(iface, instance, metadata.contexts,metadata.supportedInputs); + return targetCls.toClass(iface.classLoader,iface.protectionDomain).newInstance as T + ]; - return instance; + return new RpcRouterCodegenInstance(routerInstanceName,iface, instance, metadata.contexts,metadata.supportedInputs); } private def RpcServiceMetadata getRpcMetadata(CtClass iface) { @@ -213,77 +229,24 @@ class RuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.co ] val finalClass = targetCls.toClass(iface.classLoader, iface.protectionDomain) return new RuntimeGeneratedInvokerPrototype(supportedNotification, - finalClass as Class); + finalClass as Class>); } - private 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 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 CtClass createClass(String fqn, ClassGenerator cls) { - val target = classPool.makeClass(fqn); - cls.process(target); - return target; - } - - private def CtClass createClass(String fqn, CtClass superInterface, ClassGenerator cls) { - val target = classPool.makeClass(fqn); - target.implementsType(superInterface); - cls.process(target); - return target; - } - - private def void implementsType(CtClass it, CtClass supertype) { - checkArgument(supertype.interface, "Supertype must be interface"); - addInterface(supertype); - } - - private def asCtClass(Class class1) { - classPool.get(class1); - } + - 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; - } - def get(ClassPool pool, Class cls) { - try { - return pool.get(cls.name) - } catch (NotFoundException e) { - pool.appendClassPath(new LoaderClassPath(cls.classLoader)); - try { - return pool.get(cls.name) - - } catch (NotFoundException ef) { - pool.appendClassPath(new ClassClassPath(cls)); - return pool.get(cls.name) - } - } - } protected def resolveInvokerClass(Class class1) { - val invoker = invokerClasses.get(class1); - if (invoker !== null) { - return invoker; - } - val newInvoker = generateListenerInvoker(class1); - invokerClasses.put(class1, newInvoker); - return newInvoker + return withClassLoaderAndLock(class1.classLoader,lock) [| + val invoker = invokerClasses.get(class1); + if (invoker !== null) { + return invoker; + } + val newInvoker = generateListenerInvoker(class1); + invokerClasses.put(class1, newInvoker); + return newInvoker + + ] } } @@ -294,7 +257,7 @@ package class RuntimeGeneratedInvoker implements NotificationInvoker { val NotificationListener delegate; @Property - var org.opendaylight.controller.sal.binding.api.NotificationListener invocationProxy; + var org.opendaylight.controller.sal.binding.api.NotificationListener invocationProxy; @Property var RuntimeGeneratedInvokerPrototype prototype; @@ -302,7 +265,7 @@ package class RuntimeGeneratedInvoker implements NotificationInvoker { new(NotificationListener delegate, RuntimeGeneratedInvokerPrototype prototype) { _delegate = delegate; _prototype = prototype; - _invocationProxy = prototype.protoClass.newInstance; + _invocationProxy = prototype.protoClass.newInstance as org.opendaylight.controller.sal.binding.api.NotificationListener; RuntimeCodeHelper.setDelegate(_invocationProxy, delegate); } @@ -321,7 +284,7 @@ package class RuntimeGeneratedInvokerPrototype { val Set> supportedNotifications; @Property - val Class protoClass; + val Class> protoClass; } package class RpcServiceMetadata {