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
10 import javassist.ClassPool
11 import org.opendaylight.yangtools.yang.binding.RpcService
13 import javassist.CtClass
14 import javassist.CtMethod
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
16 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext
17 import org.opendaylight.yangtools.yang.binding.BaseIdentity
20 import java.util.HashMap
23 import org.opendaylight.yangtools.yang.binding.NotificationListener
24 import org.opendaylight.yangtools.yang.binding.Notification
27 import static extension org.opendaylight.controller.sal.binding.codegen.YangtoolsMappingHelper.*
28 import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification.*
29 import java.util.HashSet
30 import static org.opendaylight.controller.sal.binding.impl.util.ClassLoaderUtils.*
31 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory
32 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker
34 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper
35 import java.util.WeakHashMap
36 import org.opendaylight.yangtools.yang.binding.annotations.QName
37 import org.opendaylight.yangtools.yang.binding.DataContainer
38 import org.opendaylight.yangtools.yang.binding.RpcImplementation
39 import org.opendaylight.controller.sal.binding.codegen.util.JavassistUtils
41 class RuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator, NotificationInvokerFactory {
43 val CtClass BROKER_NOTIFICATION_LISTENER;
44 val ClassPool classPool;
45 val extension JavassistUtils utils;
46 val Map<Class<? extends NotificationListener>, RuntimeGeneratedInvokerPrototype> invokerClasses;
48 public new(ClassPool pool) {
50 utils = new JavassistUtils(pool);
51 invokerClasses = new WeakHashMap();
52 BROKER_NOTIFICATION_LISTENER = org.opendaylight.controller.sal.binding.api.NotificationListener.asCtClass;
55 override <T extends RpcService> getDirectProxyFor(Class<T> iface) {
56 val supertype = iface.asCtClass
57 val targetCls = createClass(iface.directProxyName, supertype) [
58 field(DELEGATE_FIELD, iface);
59 implementMethodsFrom(supertype) [
62 if(«DELEGATE_FIELD» == null) {
63 throw new java.lang.IllegalStateException("No provider is processing supplied message");
65 return ($r) «DELEGATE_FIELD».«it.name»($$);
70 return targetCls.toClass(iface.classLoader).newInstance as T
73 override <T extends RpcService> getRouterFor(Class<T> iface) {
74 val instance = <RpcRouterCodegenInstance<T>>withClassLoaderAndLock(iface.classLoader,lock) [ |
75 val supertype = iface.asCtClass
76 val metadata = supertype.rpcMetadata;
77 val targetCls = createClass(iface.routerName, supertype) [
78 addInterface(RpcImplementation.asCtClass)
80 field(DELEGATE_FIELD, iface)
81 //field(REMOTE_INVOKER_FIELD,iface);
83 for (ctx : metadata.contexts) {
84 field(ctx.routingTableField, Map)
86 implementMethodsFrom(supertype) [
87 if (parameterTypes.size === 1) {
88 val rpcMeta = metadata.rpcMethods.get(name);
91 final «InstanceIdentifier.name» identifier = $1.«rpcMeta.inputRouteGetter.name»()«IF rpcMeta.
92 routeEncapsulated».getValue()«ENDIF»;
93 «supertype.name» instance = («supertype.name») «rpcMeta.context.routingTableField».get(identifier);
94 if(instance == null) {
95 instance = «DELEGATE_FIELD»;
97 if(instance == null) {
98 throw new java.lang.IllegalStateException("No provider is processing supplied message");
100 return ($r) instance.«it.name»($$);
103 } else if (parameterTypes.size === 0) {
104 body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
107 implementMethodsFrom(RpcImplementation.asCtClass) [
109 case "getSupportedInputs":
112 throw new java.lang.UnsupportedOperationException("Not implemented yet");
118 «FOR input : metadata.supportedInputs SEPARATOR " else "»
119 «val rpcMetadata = metadata.rpcInputs.get(input)»
120 if(«input.name».class.equals($1)) {
121 return ($r) this.«rpcMetadata.methodName»((«input.name») $2);
124 throw new java.lang.IllegalArgumentException("Not supported message type");
133 val instance = targetCls.toClass(iface.classLoader,iface.protectionDomain).newInstance as T
134 return new RpcRouterCodegenInstance(iface, instance, metadata.contexts,metadata.supportedInputs);
139 private def RpcServiceMetadata getRpcMetadata(CtClass iface) {
140 val metadata = new RpcServiceMetadata;
142 iface.methods.filter[declaringClass == iface && parameterTypes.size === 1].forEach [ method |
143 val routingPair = method.rpcMetadata;
144 if (routingPair !== null) {
145 metadata.contexts.add(routingPair.context)
146 metadata.rpcMethods.put(method.name,routingPair)
147 val input = routingPair.inputType.javaClass as Class<? extends DataContainer>;
148 metadata.supportedInputs.add(input);
149 metadata.rpcInputs.put(input,routingPair);
155 private def getRpcMetadata(CtMethod method) {
156 val inputClass = method.parameterTypes.get(0);
157 return inputClass.rpcMethodMetadata(inputClass,method.name);
160 private def RpcMetadata rpcMethodMetadata(CtClass dataClass,CtClass inputClass,String rpcMethod) {
161 for (method : dataClass.methods) {
162 if (method.name.startsWith("get") && method.parameterTypes.size === 0) {
163 for (annotation : method.availableAnnotations) {
164 if (annotation instanceof RoutingContext) {
165 val encapsulated = !method.returnType.equals(InstanceIdentifier.asCtClass);
166 return new RpcMetadata(null,rpcMethod,(annotation as RoutingContext).value, method, encapsulated,inputClass);
171 for (iface : dataClass.interfaces) {
172 val ret = rpcMethodMetadata(iface,inputClass,rpcMethod);
173 if(ret != null) return ret;
178 private def getJavaClass(CtClass cls) {
179 Thread.currentThread.contextClassLoader.loadClass(cls.name)
182 override getInvokerFactory() {
186 override invokerFor(NotificationListener instance) {
187 val cls = instance.class
188 val prototype = resolveInvokerClass(cls);
190 return new RuntimeGeneratedInvoker(instance, prototype)
193 protected def generateListenerInvoker(Class<? extends NotificationListener> iface) {
194 val callbacks = iface.methods.filter[notificationCallback]
196 val supportedNotification = callbacks.map[parameterTypes.get(0) as Class<? extends Notification>].toSet;
198 val targetCls = createClass(iface.invokerName, BROKER_NOTIFICATION_LISTENER) [
199 field(DELEGATE_FIELD, iface)
200 implementMethodsFrom(BROKER_NOTIFICATION_LISTENER) [
203 «FOR callback : callbacks SEPARATOR " else "»
204 «val cls = callback.parameterTypes.get(0).name»
205 if($1 instanceof «cls») {
206 «DELEGATE_FIELD».«callback.name»((«cls») $1);
215 val finalClass = targetCls.toClass(iface.classLoader, iface.protectionDomain)
216 return new RuntimeGeneratedInvokerPrototype(supportedNotification,
217 finalClass as Class<? extends org.opendaylight.controller.sal.binding.api.NotificationListener<?>>);
224 protected def resolveInvokerClass(Class<? extends NotificationListener> class1) {
225 return <RuntimeGeneratedInvokerPrototype>withClassLoaderAndLock(class1.classLoader,lock) [|
226 val invoker = invokerClasses.get(class1);
227 if (invoker !== null) {
230 val newInvoker = generateListenerInvoker(class1);
231 invokerClasses.put(class1, newInvoker);
239 package class RuntimeGeneratedInvoker implements NotificationInvoker {
242 val NotificationListener delegate;
245 var org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> invocationProxy;
248 var RuntimeGeneratedInvokerPrototype prototype;
250 new(NotificationListener delegate, RuntimeGeneratedInvokerPrototype prototype) {
251 _delegate = delegate;
252 _prototype = prototype;
253 _invocationProxy = prototype.protoClass.newInstance as org.opendaylight.controller.sal.binding.api.NotificationListener<Notification>;
254 RuntimeCodeHelper.setDelegate(_invocationProxy, delegate);
257 override getSupportedNotifications() {
258 prototype.supportedNotifications;
266 package class RuntimeGeneratedInvokerPrototype {
269 val Set<Class<? extends Notification>> supportedNotifications;
272 val Class<? extends org.opendaylight.controller.sal.binding.api.NotificationListener<?>> protoClass;
275 package class RpcServiceMetadata {
278 val contexts = new HashSet<Class<? extends BaseIdentity>>();
281 val rpcMethods = new HashMap<String, RpcMetadata>();
284 val rpcInputs = new HashMap<Class<? extends DataContainer>, RpcMetadata>();
288 val supportedInputs = new HashSet<Class<? extends DataContainer>>();
292 package class RpcMetadata {
298 val String methodName;
301 val Class<? extends BaseIdentity> context;
303 val CtMethod inputRouteGetter;
306 val boolean routeEncapsulated;
309 val CtClass inputType;