2 * Copyright (c) 2014 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;
13 import javassist.ClassPool;
14 import javassist.CtClass;
15 import javassist.CtMethod;
16 import javassist.NotFoundException;
18 import javax.annotation.concurrent.GuardedBy;
20 import org.eclipse.xtext.xbase.lib.Extension;
21 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
22 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
23 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.NotificationListener;
26 import org.opendaylight.yangtools.yang.binding.RpcService;
27 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
28 import org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils;
30 import com.google.common.base.Supplier;
32 abstract class AbstractRuntimeCodeGenerator implements org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator, NotificationInvokerFactory {
34 private final Map<Class<? extends NotificationListener>, RuntimeGeneratedInvokerPrototype> invokerClasses = new WeakHashMap<>();
35 private final CtClass brokerNotificationListener;
38 protected final JavassistUtils utils;
40 protected AbstractRuntimeCodeGenerator(final ClassPool pool) {
41 utils = JavassistUtils.forClassPool(pool);
44 * Make sure Javassist ClassPool sees the classloader of RpcService
46 utils.ensureClassLoader(RpcService.class);
48 brokerNotificationListener = utils.asCtClass(org.opendaylight.controller.sal.binding.api.NotificationListener.class);
51 protected final CtClass getBrokerNotificationListener() {
52 return brokerNotificationListener;
55 protected abstract RuntimeGeneratedInvokerPrototype generateListenerInvoker(Class<? extends NotificationListener> cls);
56 protected abstract <T extends RpcService> Supplier<T> directProxySupplier(final Class<T> serviceType);
57 protected abstract <T extends RpcService> Supplier<T> routerSupplier(final Class<T> serviceType, RpcServiceMetadata metadata);
59 private RpcServiceMetadata getRpcMetadata(final CtClass iface) throws ClassNotFoundException, NotFoundException {
60 final RpcServiceMetadata metadata = new RpcServiceMetadata();
62 for (CtMethod method : iface.getMethods()) {
63 if (iface.equals(method.getDeclaringClass()) && method.getParameterTypes().length == 1) {
64 final RpcMetadata routingPair = getRpcMetadata(method);
65 if (routingPair != null) {
66 metadata.addContext(routingPair.getContext());
67 metadata.addRpcMethod(method.getName(), routingPair);
70 * Force-load the RPC class representing the "input" of this RPC.
72 * FIXME: this is pre-existing side-effect of the original code, which
73 * kept a reference to the loaded class, but it did not use it.
75 * There was no explanation as to why forcing this load was
76 * necessary. As far as I can tell now is that it forces the
77 * resolution of method arguments, which would (according to
78 * my reading of JLS) occur only when the method is invoked via
79 * binding-aware class action, not when coming from
80 * binding-independent world. Whether that makes sense or not,
81 * remains to be investigated.
83 Thread.currentThread().getContextClassLoader().loadClass(routingPair.getInputType().getName());
91 private RpcMetadata getRpcMetadata(final CtMethod method) throws NotFoundException {
92 final CtClass inputClass = method.getParameterTypes()[0];
93 return rpcMethodMetadata(inputClass, inputClass, method.getName());
96 private RpcMetadata rpcMethodMetadata(final CtClass dataClass, final CtClass inputClass, final String rpcMethod) throws NotFoundException {
97 for (CtMethod method : dataClass.getMethods()) {
98 if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
99 for (Object annotation : method.getAvailableAnnotations()) {
100 if (annotation instanceof RoutingContext) {
101 boolean encapsulated = !method.getReturnType().equals(utils.asCtClass(InstanceIdentifier.class));
102 return new RpcMetadata(rpcMethod, ((RoutingContext)annotation).value(), method, encapsulated, inputClass);
108 for (CtClass iface : dataClass.getInterfaces()) {
109 final RpcMetadata ret = rpcMethodMetadata(iface, inputClass, rpcMethod);
117 private synchronized RuntimeGeneratedInvokerPrototype resolveInvokerClass(final Class<? extends NotificationListener> cls) {
118 RuntimeGeneratedInvokerPrototype invoker = invokerClasses.get(cls);
119 if (invoker != null) {
123 utils.getLock().lock();
125 invoker = ClassLoaderUtils.withClassLoader(cls.getClassLoader(), new Supplier<RuntimeGeneratedInvokerPrototype>() {
127 public RuntimeGeneratedInvokerPrototype get() {
128 return generateListenerInvoker(cls);
132 invokerClasses.put(cls, invoker);
135 utils.getLock().unlock();
140 public final NotificationInvokerFactory getInvokerFactory() {
145 public final <T extends RpcService> T getDirectProxyFor(final Class<T> serviceType) {
146 utils.getLock().lock();
148 return ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), directProxySupplier(serviceType));
150 utils.getLock().unlock();
155 public final <T extends RpcService> RpcRouter<T> getRouterFor(final Class<T> serviceType, final String name) {
156 final RpcServiceMetadata metadata = ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), new Supplier<RpcServiceMetadata>() {
158 public RpcServiceMetadata get() {
160 return getRpcMetadata(utils.asCtClass(serviceType));
161 } catch (ClassNotFoundException | NotFoundException e) {
162 throw new IllegalStateException(String.format("Failed to load metadata for class {}", serviceType), e);
167 utils.getLock().lock();
169 final T instance = ClassLoaderUtils.withClassLoader(serviceType.getClassLoader(), routerSupplier(serviceType, metadata));
170 return new RpcRouterCodegenInstance<T>(name, serviceType, instance, metadata.getContexts());
172 utils.getLock().unlock();
177 public NotificationInvoker invokerFor(final NotificationListener instance) {
178 final Class<? extends NotificationListener> cls = instance.getClass();
179 final RuntimeGeneratedInvokerPrototype prototype = resolveInvokerClass(cls);
182 return RuntimeGeneratedInvoker.create(instance, prototype);
183 } catch (InstantiationException | IllegalAccessException e) {
184 throw new IllegalStateException(String.format("Failed to create invoker for %s", instance), e);