2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.binding.codegen.impl
11 import java.util.WeakHashMap
12 import javassist.ClassPool
13 import javassist.CtClass
14 import javassist.CtMethod
15 import javassist.LoaderClassPath
16 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper
17 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory
18 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker
19 import org.opendaylight.yangtools.sal.binding.generator.util.ClassLoaderUtils
20 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils
21 import org.opendaylight.yangtools.yang.binding.BaseIdentity
22 import org.opendaylight.yangtools.yang.binding.DataContainer
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
24 import org.opendaylight.yangtools.yang.binding.Notification
25 import org.opendaylight.yangtools.yang.binding.NotificationListener
26 import org.opendaylight.yangtools.yang.binding.RpcImplementation
27 import org.opendaylight.yangtools.yang.binding.RpcService
28 import org.opendaylight.yangtools.yang.binding.annotations.QName
29 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext
31 import static org.opendaylight.yangtools.concepts.util.ClassLoaderUtils.*
33 import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification.*
34 import static extension org.opendaylight.controller.sal.binding.codegen.YangtoolsMappingHelper.*
36 class RuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator, NotificationInvokerFactory {
38 val CtClass BROKER_NOTIFICATION_LISTENER;
39 val extension JavassistUtils utils;
40 val Map<Class<? extends NotificationListener>, RuntimeGeneratedInvokerPrototype> invokerClasses;
44 utils = new JavassistUtils(pool);
45 invokerClasses = new WeakHashMap();
46 BROKER_NOTIFICATION_LISTENER = org.opendaylight.controller.sal.binding.api.NotificationListener.asCtClass;
47 pool.appendClassPath(new LoaderClassPath(RpcService.classLoader));
50 override <T extends RpcService> getDirectProxyFor(Class<T> iface) {
51 val T instance = withClassLoaderAndLock(iface.classLoader,lock) [|
52 val proxyName = iface.directProxyName;
53 val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(proxyName)
54 if(potentialClass != null) {
55 return potentialClass.newInstance as T;
57 val supertype = iface.asCtClass
58 val createdCls = createClass(iface.directProxyName, supertype) [
59 field(DELEGATE_FIELD, iface);
60 implementsType(RpcImplementation.asCtClass)
61 implementMethodsFrom(supertype) [
64 if(«DELEGATE_FIELD» == null) {
65 throw new java.lang.IllegalStateException("No default provider is available");
67 return ($r) «DELEGATE_FIELD».«it.name»($$);
71 implementMethodsFrom(RpcImplementation.asCtClass) [
74 throw new java.lang.IllegalStateException("No provider is processing supplied message");
80 return createdCls.toClass(iface.classLoader).newInstance as T
85 override <T extends RpcService> getRouterFor(Class<T> iface,String routerInstanceName) {
86 val metadata = withClassLoader(iface.classLoader) [|
87 val supertype = iface.asCtClass
88 return supertype.rpcMetadata;
91 val instance = <T>withClassLoaderAndLock(iface.classLoader,lock) [ |
92 val supertype = iface.asCtClass
93 val routerName = iface.routerName;
94 val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(routerName)
95 if(potentialClass != null) {
96 return potentialClass.newInstance as T;
99 val targetCls = createClass(iface.routerName, supertype) [
102 field(DELEGATE_FIELD, iface)
103 //field(REMOTE_INVOKER_FIELD,iface);
104 implementsType(RpcImplementation.asCtClass)
106 for (ctx : metadata.contexts) {
107 field(ctx.routingTableField, Map)
109 implementMethodsFrom(supertype) [
110 if (parameterTypes.size === 1) {
111 val rpcMeta = metadata.rpcMethods.get(name);
114 final «InstanceIdentifier.name» identifier = $1.«rpcMeta.inputRouteGetter.name»()«IF rpcMeta.
115 routeEncapsulated».getValue()«ENDIF»;
116 «supertype.name» instance = («supertype.name») «rpcMeta.context.routingTableField».get(identifier);
117 if(instance == null) {
118 instance = «DELEGATE_FIELD»;
120 if(instance == null) {
121 throw new java.lang.IllegalStateException("No routable provider is processing routed message for " + String.valueOf(identifier));
123 return ($r) instance.«it.name»($$);
126 } else if (parameterTypes.size === 0) {
127 body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
130 implementMethodsFrom(RpcImplementation.asCtClass) [
133 throw new java.lang.IllegalStateException("No provider is processing supplied message");
139 return targetCls.toClass(iface.classLoader,iface.protectionDomain).newInstance as T
142 return new RpcRouterCodegenInstance(routerInstanceName,iface, instance, metadata.contexts,metadata.supportedInputs);
145 private def RpcServiceMetadata getRpcMetadata(CtClass iface) {
146 val metadata = new RpcServiceMetadata;
148 iface.methods.filter[declaringClass == iface && parameterTypes.size === 1].forEach [ method |
149 val routingPair = method.rpcMetadata;
150 if (routingPair !== null) {
151 metadata.contexts.add(routingPair.context)
152 metadata.rpcMethods.put(method.name,routingPair)
153 val input = routingPair.inputType.javaClass as Class<? extends DataContainer>;
154 metadata.supportedInputs.add(input);
155 metadata.rpcInputs.put(input,routingPair);
161 private def getRpcMetadata(CtMethod method) {
162 val inputClass = method.parameterTypes.get(0);
163 return inputClass.rpcMethodMetadata(inputClass,method.name);
166 private def RpcMetadata rpcMethodMetadata(CtClass dataClass,CtClass inputClass,String rpcMethod) {
167 for (method : dataClass.methods) {
168 if (method.name.startsWith("get") && method.parameterTypes.size === 0) {
169 for (annotation : method.availableAnnotations) {
170 if (annotation instanceof RoutingContext) {
171 val encapsulated = !method.returnType.equals(InstanceIdentifier.asCtClass);
172 return new RpcMetadata(null,rpcMethod,(annotation as RoutingContext).value, method, encapsulated,inputClass);
177 for (iface : dataClass.interfaces) {
178 val ret = rpcMethodMetadata(iface,inputClass,rpcMethod);
179 if(ret != null) return ret;
184 private def getJavaClass(CtClass cls) {
185 Thread.currentThread.contextClassLoader.loadClass(cls.name)
188 override getInvokerFactory() {
192 override invokerFor(NotificationListener instance) {
193 val cls = instance.class
194 val prototype = resolveInvokerClass(cls);
196 return new RuntimeGeneratedInvoker(instance, prototype)
199 protected def generateListenerInvoker(Class<? extends NotificationListener> iface) {
200 val callbacks = iface.methods.filter[notificationCallback]
202 val supportedNotification = callbacks.map[parameterTypes.get(0) as Class<? extends Notification>].toSet;
204 val targetCls = createClass(iface.invokerName, BROKER_NOTIFICATION_LISTENER) [
205 field(DELEGATE_FIELD, iface)
206 implementMethodsFrom(BROKER_NOTIFICATION_LISTENER) [
209 «FOR callback : callbacks SEPARATOR " else "»
210 «val cls = callback.parameterTypes.get(0).name»
211 if($1 instanceof «cls») {
212 «DELEGATE_FIELD».«callback.name»((«cls») $1);
221 val finalClass = targetCls.toClass(iface.classLoader, iface.protectionDomain)
222 return new RuntimeGeneratedInvokerPrototype(supportedNotification,
223 finalClass as Class<? extends org.opendaylight.controller.sal.binding.api.NotificationListener<?>>);
230 protected def resolveInvokerClass(Class<? extends NotificationListener> class1) {
231 return <RuntimeGeneratedInvokerPrototype>withClassLoaderAndLock(class1.classLoader,lock) [|
232 val invoker = invokerClasses.get(class1);
233 if (invoker !== null) {
236 val newInvoker = generateListenerInvoker(class1);
237 invokerClasses.put(class1, newInvoker);
245 package class RuntimeGeneratedInvoker implements NotificationInvoker {
248 val NotificationListener delegate;
251 var org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> invocationProxy;
254 var RuntimeGeneratedInvokerPrototype prototype;
256 new(NotificationListener delegate, RuntimeGeneratedInvokerPrototype prototype) {
257 _delegate = delegate;
258 _prototype = prototype;
259 _invocationProxy = prototype.protoClass.newInstance as org.opendaylight.controller.sal.binding.api.NotificationListener<Notification>;
260 RuntimeCodeHelper.setDelegate(_invocationProxy, delegate);
263 override getSupportedNotifications() {
264 prototype.supportedNotifications;
272 package class RpcMetadata {
278 val String methodName;
281 val Class<? extends BaseIdentity> context;
283 val CtMethod inputRouteGetter;
286 val boolean routeEncapsulated;
289 val CtClass inputType;