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
41 private var DataBrokerImpl dataBroker
44 var BundleContext brokerBundleContext
49 // Initialization of notificationBroker
50 notifyBroker = new NotificationBrokerImpl(null);
51 val brokerProperties = PropertiesUtils.newProperties();
52 notifyBrokerRegistration = brokerBundleContext.registerService(NotificationProviderService, notifyBroker,
54 brokerBundleContext.registerService(NotificationService, notifyBroker, brokerProperties)
59 // YANG Binding Class Loader
60 clsPool.appendClassPath(new LoaderClassPath(RpcService.classLoader))
63 override registerConsumer(BindingAwareConsumer consumer, BundleContext bundleCtx) {
64 val ctx = consumer.createContext(bundleCtx)
65 consumer.onSessionInitialized(ctx)
69 override registerProvider(BindingAwareProvider provider, BundleContext bundleCtx) {
70 val ctx = provider.createContext(bundleCtx)
71 provider.onSessionInitialized(ctx)
72 provider.onSessionInitiated(ctx as ProviderContext)
76 private def createContext(BindingAwareConsumer consumer, BundleContext consumerCtx) {
77 new OsgiConsumerContext(consumerCtx, this)
80 private def createContext(BindingAwareProvider provider, BundleContext providerCtx) {
81 new OsgiProviderContext(providerCtx, this)
85 * Returns a Managed Direct Proxy for supplied class
87 * Managed direct proxy is a generated proxy class conforming to the supplied interface
88 * which delegates all calls to the backing delegate.
90 * Proxy does not do any validation, null pointer checks or modifies data in any way, it
91 * is only use to avoid exposing direct references to backing implementation of service.
93 * If proxy class does not exist for supplied service class it will be generated automatically.
95 def <T extends RpcService> getManagedDirectProxy(Class<T> service) {
97 var RpcProxyContext existing = null
98 if ((existing = managedProxies.get(service)) != null) {
101 val proxyClass = service.generateDirectProxy()
102 val rpcProxyCtx = new RpcProxyContext(proxyClass)
103 val properties = new Hashtable<String, String>()
104 rpcProxyCtx.proxy = proxyClass.newInstance as RpcService
106 properties.salServiceType = Constants.SAL_SERVICE_TYPE_CONSUMER_PROXY
107 rpcProxyCtx.registration = brokerBundleContext.registerService(service, rpcProxyCtx.proxy as T, properties)
108 managedProxies.put(service, rpcProxyCtx)
109 return rpcProxyCtx.proxy
112 protected def generateDirectProxy(Class<? extends RpcService> delegate) {
113 val targetFqn = delegate.generatedName(Constants.PROXY_DIRECT_SUFFIX)
114 log.debug("Generating DirectProxy for {} Proxy name: {}",delegate,targetFqn);
115 val objCls = clsPool.get(Object)
116 val delegateCls = clsPool.get(delegate)
117 val proxyCls = clsPool.makeClass(targetFqn)
118 proxyCls.addInterface(delegateCls)
119 val delField = new CtField(delegateCls, DELEGATE_FIELD, proxyCls);
120 delField.modifiers = Modifier.PUBLIC
121 proxyCls.addField(delField)
122 delegateCls.methods.filter[it.declaringClass != objCls].forEach [
123 val proxyMethod = new CtMethod(it, proxyCls, null);
124 proxyMethod.body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
125 proxyCls.addMethod(proxyMethod)
127 return proxyCls.toClass(delegate.classLoader)
131 * Registers RPC Implementation
134 def <T extends RpcService> registerRpcImplementation(Class<T> type, T service, OsgiProviderContext context,
135 Hashtable<String, String> properties) {
136 val proxy = getManagedDirectProxy(type)
137 if(proxy.delegate != null) {
138 throw new IllegalStateException("Service " + type + "is already registered");
140 val osgiReg = context.bundleContext.registerService(type, service, properties);
141 proxy.delegate = service;
142 return new RpcServiceRegistrationImpl<T>(type, service, osgiReg);
146 * Helper method to return delegate from ManagedDirectedProxy with use of reflection.
148 * Note: This method uses reflection, but access to delegate field should be
149 * avoided and called only if neccessary.
152 def <T extends RpcService> getDelegate(RpcService proxy) {
153 val field = proxy.class.getField(DELEGATE_FIELD)
154 if(field == null) throw new UnsupportedOperationException("Unable to get delegate from proxy");
155 return field.get(proxy) as T
159 * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
161 * Note: This method uses reflection, but setting delegate field should not occur too much
162 * to introduce any significant performance hits.
165 def void setDelegate(RpcService proxy, RpcService delegate) {
166 val field = proxy.class.getField(DELEGATE_FIELD)
167 if(field == null) throw new UnsupportedOperationException("Unable to set delegate to proxy");
168 if (field.type.isAssignableFrom(delegate.class)) {
169 field.set(proxy,delegate)
170 } else throw new IllegalArgumentException("delegate class is not assignable to proxy");