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.impl
10 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer
11 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider
12 import org.opendaylight.yangtools.yang.binding.RpcService
13 import javassist.ClassPool
14 import javassist.CtMethod
15 import javassist.CtField
16 import org.osgi.framework.BundleContext
18 import java.util.HashMap
19 import javassist.LoaderClassPath
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker
21 import java.util.Hashtable
23 import static extension org.opendaylight.controller.sal.binding.impl.utils.PropertiesUtils.*
24 import static extension org.opendaylight.controller.sal.binding.impl.utils.GeneratorUtils.*
25 import org.opendaylight.controller.sal.binding.api.NotificationProviderService
26 import org.osgi.framework.ServiceRegistration
27 import org.opendaylight.controller.sal.binding.impl.utils.PropertiesUtils
28 import org.opendaylight.controller.sal.binding.api.NotificationService
29 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext
30 import javassist.Modifier
31 import org.slf4j.LoggerFactory
33 class BindingAwareBrokerImpl implements BindingAwareBroker {
34 private static val DELEGATE_FIELD = "_delegate"
35 private static val log = LoggerFactory.getLogger(BindingAwareBrokerImpl)
37 private val clsPool = ClassPool.getDefault()
38 private Map<Class<? extends RpcService>, RpcProxyContext> managedProxies = new HashMap();
39 private var NotificationBrokerImpl notifyBroker
40 private var ServiceRegistration<NotificationProviderService> notifyBrokerRegistration
43 var BundleContext brokerBundleContext
48 // Initialization of notificationBroker
49 notifyBroker = new NotificationBrokerImpl(null);
50 val brokerProperties = PropertiesUtils.newProperties();
51 notifyBrokerRegistration = brokerBundleContext.registerService(NotificationProviderService, notifyBroker,
53 brokerBundleContext.registerService(NotificationService, notifyBroker, brokerProperties)
58 // YANG Binding Class Loader
59 clsPool.appendClassPath(new LoaderClassPath(RpcService.classLoader))
62 override registerConsumer(BindingAwareConsumer consumer, BundleContext bundleCtx) {
63 val ctx = consumer.createContext(bundleCtx)
64 consumer.onSessionInitialized(ctx)
68 override registerProvider(BindingAwareProvider provider, BundleContext bundleCtx) {
69 val ctx = provider.createContext(bundleCtx)
70 provider.onSessionInitialized(ctx)
71 provider.onSessionInitiated(ctx as ProviderContext)
75 private def createContext(BindingAwareConsumer consumer, BundleContext consumerCtx) {
76 new OsgiConsumerContext(consumerCtx, this)
79 private def createContext(BindingAwareProvider provider, BundleContext providerCtx) {
80 new OsgiProviderContext(providerCtx, this)
84 * Returns a Managed Direct Proxy for supplied class
86 * Managed direct proxy is a generated proxy class conforming to the supplied interface
87 * which delegates all calls to the backing delegate.
89 * Proxy does not do any validation, null pointer checks or modifies data in any way, it
90 * is only use to avoid exposing direct references to backing implementation of service.
92 * If proxy class does not exist for supplied service class it will be generated automatically.
94 def <T extends RpcService> getManagedDirectProxy(Class<T> service) {
96 var RpcProxyContext existing = null
97 if ((existing = managedProxies.get(service)) != null) {
100 val proxyClass = service.generateDirectProxy()
101 val rpcProxyCtx = new RpcProxyContext(proxyClass)
102 val properties = new Hashtable<String, String>()
103 rpcProxyCtx.proxy = proxyClass.newInstance as RpcService
105 properties.salServiceType = Constants.SAL_SERVICE_TYPE_CONSUMER_PROXY
106 rpcProxyCtx.registration = brokerBundleContext.registerService(service, rpcProxyCtx.proxy as T, properties)
107 managedProxies.put(service, rpcProxyCtx)
108 return rpcProxyCtx.proxy
111 protected def generateDirectProxy(Class<? extends RpcService> delegate) {
112 val targetFqn = delegate.generatedName(Constants.PROXY_DIRECT_SUFFIX)
113 log.debug("Generating DirectProxy for {} Proxy name: {}",delegate,targetFqn);
114 val objCls = clsPool.get(Object)
115 val delegateCls = clsPool.get(delegate)
116 val proxyCls = clsPool.makeClass(targetFqn)
117 proxyCls.addInterface(delegateCls)
118 val delField = new CtField(delegateCls, DELEGATE_FIELD, proxyCls);
119 delField.modifiers = Modifier.PUBLIC
120 proxyCls.addField(delField)
121 delegateCls.methods.filter[it.declaringClass != objCls].forEach [
122 val proxyMethod = new CtMethod(it, proxyCls, null);
123 proxyMethod.body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
124 proxyCls.addMethod(proxyMethod)
126 return proxyCls.toClass(delegate.classLoader)
130 * Registers RPC Implementation
133 def <T extends RpcService> registerRpcImplementation(Class<T> type, T service, OsgiProviderContext context,
134 Hashtable<String, String> properties) {
135 val proxy = getManagedDirectProxy(type)
136 if(proxy.delegate != null) {
137 throw new IllegalStateException("Service " + type + "is already registered");
139 val osgiReg = context.bundleContext.registerService(type, service, properties);
140 proxy.delegate = service;
141 return new RpcServiceRegistrationImpl<T>(type, service, osgiReg);
145 * Helper method to return delegate from ManagedDirectedProxy with use of reflection.
147 * Note: This method uses reflection, but access to delegate field should be
148 * avoided and called only if neccessary.
151 def <T extends RpcService> getDelegate(RpcService proxy) {
152 val field = proxy.class.getField(DELEGATE_FIELD)
153 if(field == null) throw new UnsupportedOperationException("Unable to get delegate from proxy");
154 return field.get(proxy) as T
158 * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
160 * Note: This method uses reflection, but setting delegate field should not occur too much
161 * to introduce any significant performance hits.
164 def void setDelegate(RpcService proxy, RpcService delegate) {
165 val field = proxy.class.getField(DELEGATE_FIELD)
166 if(field == null) throw new UnsupportedOperationException("Unable to set delegate to proxy");
167 if (field.type.isAssignableFrom(delegate.class)) {
168 field.set(proxy,delegate)
169 } else throw new IllegalArgumentException("delegate class is not assignable to proxy");