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.yangtools.concepts.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.yangtools.sal.binding.generator.util.JavassistUtils
40 import org.opendaylight.yangtools.sal.binding.generator.util.ClassLoaderUtils
41 import javassist.LoaderClassPath
43 class RuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator, NotificationInvokerFactory {
45 val CtClass BROKER_NOTIFICATION_LISTENER;
46 val extension JavassistUtils utils;
47 val Map<Class<? extends NotificationListener>, RuntimeGeneratedInvokerPrototype> invokerClasses;
51 utils = new JavassistUtils(pool);
52 invokerClasses = new WeakHashMap();
53 BROKER_NOTIFICATION_LISTENER = org.opendaylight.controller.sal.binding.api.NotificationListener.asCtClass;
54 pool.appendClassPath(new LoaderClassPath(RpcService.classLoader));
57 override <T extends RpcService> getDirectProxyFor(Class<T> iface) {
58 val T instance = withClassLoaderAndLock(iface.classLoader,lock) [|
59 val proxyName = iface.directProxyName;
60 val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(proxyName)
61 if(potentialClass != null) {
62 return potentialClass.newInstance as T;
64 val supertype = iface.asCtClass
65 val createdCls = createClass(iface.directProxyName, supertype) [
66 field(DELEGATE_FIELD, iface);
67 implementsType(RpcImplementation.asCtClass)
68 implementMethodsFrom(supertype) [
71 if(«DELEGATE_FIELD» == null) {
72 throw new java.lang.IllegalStateException("No default provider is available");
74 return ($r) «DELEGATE_FIELD».«it.name»($$);
78 implementMethodsFrom(RpcImplementation.asCtClass) [
81 throw new java.lang.IllegalStateException("No provider is processing supplied message");
87 return createdCls.toClass(iface.classLoader).newInstance as T
92 override <T extends RpcService> getRouterFor(Class<T> iface,String routerInstanceName) {
93 val metadata = withClassLoader(iface.classLoader) [|
94 val supertype = iface.asCtClass
95 return supertype.rpcMetadata;
98 val instance = <T>withClassLoaderAndLock(iface.classLoader,lock) [ |
99 val supertype = iface.asCtClass
100 val routerName = iface.routerName;
101 val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(routerName)
102 if(potentialClass != null) {
103 return potentialClass.newInstance as T;
106 val targetCls = createClass(iface.routerName, supertype) [
109 field(DELEGATE_FIELD, iface)
110 //field(REMOTE_INVOKER_FIELD,iface);
111 implementsType(RpcImplementation.asCtClass)
113 for (ctx : metadata.contexts) {
114 field(ctx.routingTableField, Map)
116 implementMethodsFrom(supertype) [
117 if (parameterTypes.size === 1) {
118 val rpcMeta = metadata.rpcMethods.get(name);
121 final «InstanceIdentifier.name» identifier = $1.«rpcMeta.inputRouteGetter.name»()«IF rpcMeta.
122 routeEncapsulated».getValue()«ENDIF»;
123 «supertype.name» instance = («supertype.name») «rpcMeta.context.routingTableField».get(identifier);
124 if(instance == null) {
125 instance = «DELEGATE_FIELD»;
127 if(instance == null) {
128 throw new java.lang.IllegalStateException("No routable provider is processing routed message for " + String.valueOf(identifier));
130 return ($r) instance.«it.name»($$);
133 } else if (parameterTypes.size === 0) {
134 body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
137 implementMethodsFrom(RpcImplementation.asCtClass) [
140 throw new java.lang.IllegalStateException("No provider is processing supplied message");
146 return targetCls.toClass(iface.classLoader,iface.protectionDomain).newInstance as T
149 return new RpcRouterCodegenInstance(routerInstanceName,iface, instance, metadata.contexts,metadata.supportedInputs);
152 private def RpcServiceMetadata getRpcMetadata(CtClass iface) {
153 val metadata = new RpcServiceMetadata;
155 iface.methods.filter[declaringClass == iface && parameterTypes.size === 1].forEach [ method |
156 val routingPair = method.rpcMetadata;
157 if (routingPair !== null) {
158 metadata.contexts.add(routingPair.context)
159 metadata.rpcMethods.put(method.name,routingPair)
160 val input = routingPair.inputType.javaClass as Class<? extends DataContainer>;
161 metadata.supportedInputs.add(input);
162 metadata.rpcInputs.put(input,routingPair);
168 private def getRpcMetadata(CtMethod method) {
169 val inputClass = method.parameterTypes.get(0);
170 return inputClass.rpcMethodMetadata(inputClass,method.name);
173 private def RpcMetadata rpcMethodMetadata(CtClass dataClass,CtClass inputClass,String rpcMethod) {
174 for (method : dataClass.methods) {
175 if (method.name.startsWith("get") && method.parameterTypes.size === 0) {
176 for (annotation : method.availableAnnotations) {
177 if (annotation instanceof RoutingContext) {
178 val encapsulated = !method.returnType.equals(InstanceIdentifier.asCtClass);
179 return new RpcMetadata(null,rpcMethod,(annotation as RoutingContext).value, method, encapsulated,inputClass);
184 for (iface : dataClass.interfaces) {
185 val ret = rpcMethodMetadata(iface,inputClass,rpcMethod);
186 if(ret != null) return ret;
191 private def getJavaClass(CtClass cls) {
192 Thread.currentThread.contextClassLoader.loadClass(cls.name)
195 override getInvokerFactory() {
199 override invokerFor(NotificationListener instance) {
200 val cls = instance.class
201 val prototype = resolveInvokerClass(cls);
203 return new RuntimeGeneratedInvoker(instance, prototype)
206 protected def generateListenerInvoker(Class<? extends NotificationListener> iface) {
207 val callbacks = iface.methods.filter[notificationCallback]
209 val supportedNotification = callbacks.map[parameterTypes.get(0) as Class<? extends Notification>].toSet;
211 val targetCls = createClass(iface.invokerName, BROKER_NOTIFICATION_LISTENER) [
212 field(DELEGATE_FIELD, iface)
213 implementMethodsFrom(BROKER_NOTIFICATION_LISTENER) [
216 «FOR callback : callbacks SEPARATOR " else "»
217 «val cls = callback.parameterTypes.get(0).name»
218 if($1 instanceof «cls») {
219 «DELEGATE_FIELD».«callback.name»((«cls») $1);
228 val finalClass = targetCls.toClass(iface.classLoader, iface.protectionDomain)
229 return new RuntimeGeneratedInvokerPrototype(supportedNotification,
230 finalClass as Class<? extends org.opendaylight.controller.sal.binding.api.NotificationListener<?>>);
237 protected def resolveInvokerClass(Class<? extends NotificationListener> class1) {
238 return <RuntimeGeneratedInvokerPrototype>withClassLoaderAndLock(class1.classLoader,lock) [|
239 val invoker = invokerClasses.get(class1);
240 if (invoker !== null) {
243 val newInvoker = generateListenerInvoker(class1);
244 invokerClasses.put(class1, newInvoker);
252 package class RuntimeGeneratedInvoker implements NotificationInvoker {
255 val NotificationListener delegate;
258 var org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> invocationProxy;
261 var RuntimeGeneratedInvokerPrototype prototype;
263 new(NotificationListener delegate, RuntimeGeneratedInvokerPrototype prototype) {
264 _delegate = delegate;
265 _prototype = prototype;
266 _invocationProxy = prototype.protoClass.newInstance as org.opendaylight.controller.sal.binding.api.NotificationListener<Notification>;
267 RuntimeCodeHelper.setDelegate(_invocationProxy, delegate);
270 override getSupportedNotifications() {
271 prototype.supportedNotifications;
279 package class RuntimeGeneratedInvokerPrototype {
282 val Set<Class<? extends Notification>> supportedNotifications;
285 val Class<? extends org.opendaylight.controller.sal.binding.api.NotificationListener<?>> protoClass;
288 package class RpcServiceMetadata {
291 val contexts = new HashSet<Class<? extends BaseIdentity>>();
294 val rpcMethods = new HashMap<String, RpcMetadata>();
297 val rpcInputs = new HashMap<Class<? extends DataContainer>, RpcMetadata>();
301 val supportedInputs = new HashSet<Class<? extends DataContainer>>();
305 package class RpcMetadata {
311 val String methodName;
314 val Class<? extends BaseIdentity> context;
316 val CtMethod inputRouteGetter;
319 val boolean routeEncapsulated;
322 val CtClass inputType;