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 javassist.ClassPool
12 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
13 import org.opendaylight.yangtools.yang.binding.Notification
14 import org.opendaylight.yangtools.yang.binding.RpcImplementation
15 import org.opendaylight.controller.sal.binding.codegen.YangtoolsMappingHelper
16 import org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils
18 import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification.*
20 class RuntimeCodeGenerator extends AbstractRuntimeCodeGenerator {
26 override directProxySupplier(Class iface) {
28 val proxyName = iface.directProxyName;
29 val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(proxyName)
30 if(potentialClass != null) {
31 return potentialClass.newInstance;
33 val supertype = iface.asCtClass
34 val createdCls = createClass(iface.directProxyName, supertype) [
35 field(DELEGATE_FIELD, iface);
36 implementsType(RpcImplementation.asCtClass)
37 implementMethodsFrom(supertype) [
40 if(«DELEGATE_FIELD» == null) {
41 throw new java.lang.IllegalStateException("No default provider is available");
43 return ($r) «DELEGATE_FIELD».«it.name»($$);
47 implementMethodsFrom(RpcImplementation.asCtClass) [
50 throw new java.lang.IllegalStateException("No provider is processing supplied message");
56 return createdCls.toClass(iface.classLoader).newInstance
60 override routerSupplier(Class iface, RpcServiceMetadata metadata) {
62 val supertype = iface.asCtClass
63 val routerName = iface.routerName;
64 val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(routerName)
65 if(potentialClass != null) {
66 return potentialClass.newInstance;
69 val targetCls = createClass(iface.routerName, supertype) [
72 field(DELEGATE_FIELD, iface)
73 //field(REMOTE_INVOKER_FIELD,iface);
74 implementsType(RpcImplementation.asCtClass)
76 for (ctx : metadata.contexts) {
77 field(ctx.routingTableField, Map)
79 implementMethodsFrom(supertype) [
80 if (parameterTypes.size === 1) {
81 val rpcMeta = metadata.getRpcMethod(name);
84 final «InstanceIdentifier.name» identifier = $1.«rpcMeta.inputRouteGetter.name»()«IF rpcMeta.
85 routeEncapsulated».getValue()«ENDIF»;
86 «supertype.name» instance = («supertype.name») «rpcMeta.context.routingTableField».get(identifier);
87 if(instance == null) {
88 instance = «DELEGATE_FIELD»;
90 if(instance == null) {
91 throw new java.lang.IllegalStateException("No routable provider is processing routed message for " + String.valueOf(identifier));
93 return ($r) instance.«it.name»($$);
96 } else if (parameterTypes.size === 0) {
97 body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
100 implementMethodsFrom(RpcImplementation.asCtClass) [
103 throw new java.lang.IllegalStateException("No provider is processing supplied message");
109 return targetCls.toClass(iface.classLoader,iface.protectionDomain).newInstance
113 override generateListenerInvoker(Class iface) {
114 val callbacks = iface.methods.filter[YangtoolsMappingHelper.isNotificationCallback(it)]
116 val supportedNotification = callbacks.map[parameterTypes.get(0) as Class<? extends Notification>].toSet;
118 val targetCls = createClass(iface.invokerName, brokerNotificationListener) [
119 field(DELEGATE_FIELD, iface)
120 implementMethodsFrom(brokerNotificationListener) [
123 «FOR callback : callbacks SEPARATOR " else "»
124 «val cls = callback.parameterTypes.get(0).name»
125 if($1 instanceof «cls») {
126 «DELEGATE_FIELD».«callback.name»((«cls») $1);
135 val finalClass = targetCls.toClass(iface.classLoader, iface.protectionDomain)
136 return new RuntimeGeneratedInvokerPrototype(supportedNotification,
137 finalClass as Class<? extends org.opendaylight.controller.sal.binding.api.NotificationListener<?>>);