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.yangtools.yang.binding.util.BindingReflections
16 import org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils
18 import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeSpecification.*
19 import org.opendaylight.yangtools.yang.binding.RpcService
21 class RuntimeCodeGenerator extends AbstractRuntimeCodeGenerator {
27 override directProxySupplier(Class iface) {
29 val proxyName = iface.directProxyName;
30 val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(proxyName)
31 if(potentialClass != null) {
32 return potentialClass.newInstance as RpcService;
34 val supertype = iface.asCtClass
35 val createdCls = createClass(iface.directProxyName, supertype) [
36 field(DELEGATE_FIELD, iface);
37 implementsType(RpcImplementation.asCtClass)
38 implementMethodsFrom(supertype) [
41 if(«DELEGATE_FIELD» == null) {
42 throw new java.lang.IllegalStateException("No default provider is available");
44 return ($r) «DELEGATE_FIELD».«it.name»($$);
48 implementMethodsFrom(RpcImplementation.asCtClass) [
51 throw new java.lang.IllegalStateException("No provider is processing supplied message");
57 return createdCls.toClass(iface.classLoader).newInstance as RpcService
61 override routerSupplier(Class iface, RpcServiceMetadata metadata) {
63 val supertype = iface.asCtClass
64 val routerName = iface.routerName;
65 val potentialClass = ClassLoaderUtils.tryToLoadClassWithTCCL(routerName)
66 if(potentialClass != null) {
67 return potentialClass.newInstance as RpcService;
70 val targetCls = createClass(iface.routerName, supertype) [
73 field(DELEGATE_FIELD, iface)
74 //field(REMOTE_INVOKER_FIELD,iface);
75 implementsType(RpcImplementation.asCtClass)
77 for (ctx : metadata.contexts) {
78 field(ctx.routingTableField, Map)
80 implementMethodsFrom(supertype) [
81 if (parameterTypes.size === 1) {
82 val rpcMeta = metadata.getRpcMethod(name);
85 final «InstanceIdentifier.name» identifier = $1.«rpcMeta.inputRouteGetter.name»()«IF rpcMeta.
86 routeEncapsulated».getValue()«ENDIF»;
87 «supertype.name» instance = («supertype.name») «rpcMeta.context.routingTableField».get(identifier);
88 if(instance == null) {
89 instance = «DELEGATE_FIELD»;
91 if(instance == null) {
92 throw new java.lang.IllegalStateException("No routable provider is processing routed message for " + String.valueOf(identifier));
94 return ($r) instance.«it.name»($$);
97 } else if (parameterTypes.size === 0) {
98 body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
101 implementMethodsFrom(RpcImplementation.asCtClass) [
104 throw new java.lang.IllegalStateException("No provider is processing supplied message");
110 return targetCls.toClass(iface.classLoader,iface.protectionDomain).newInstance as RpcService
114 override generateListenerInvoker(Class iface) {
115 val callbacks = iface.methods.filter[BindingReflections.isNotificationCallback(it)]
117 val supportedNotification = callbacks.map[parameterTypes.get(0) as Class<? extends Notification>].toSet;
119 val targetCls = createClass(iface.invokerName, brokerNotificationListener) [
120 field(DELEGATE_FIELD, iface)
121 implementMethodsFrom(brokerNotificationListener) [
124 «FOR callback : callbacks SEPARATOR " else "»
125 «val cls = callback.parameterTypes.get(0).name»
126 if($1 instanceof «cls») {
127 «DELEGATE_FIELD».«callback.name»((«cls») $1);
136 val finalClass = targetCls.toClass(iface.classLoader, iface.protectionDomain)
137 return new RuntimeGeneratedInvokerPrototype(supportedNotification,
138 finalClass as Class<? extends org.opendaylight.controller.sal.binding.api.NotificationListener<?>>);