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)
83 def <T extends RpcService> getManagedDirectProxy(Class<T> service) {
85 var RpcProxyContext existing = null
86 if ((existing = managedProxies.get(service)) != null) {
89 val proxyClass = service.generateDirectProxy()
90 val rpcProxyCtx = new RpcProxyContext(proxyClass)
91 val properties = new Hashtable<String, String>()
92 rpcProxyCtx.proxy = proxyClass.newInstance as RpcService
94 properties.salServiceType = Constants.SAL_SERVICE_TYPE_CONSUMER_PROXY
95 rpcProxyCtx.registration = brokerBundleContext.registerService(service, rpcProxyCtx.proxy as T, properties)
96 managedProxies.put(service, rpcProxyCtx)
97 return rpcProxyCtx.proxy
100 protected def generateDirectProxy(Class<? extends RpcService> delegate) {
101 val targetFqn = delegate.generatedName(Constants.PROXY_DIRECT_SUFFIX)
102 log.debug("Generating DirectProxy for {} Proxy name: {}",delegate,targetFqn);
103 val objCls = clsPool.get(Object)
104 val delegateCls = clsPool.get(delegate)
105 val proxyCls = clsPool.makeClass(targetFqn)
106 proxyCls.addInterface(delegateCls)
107 val delField = new CtField(delegateCls, DELEGATE_FIELD, proxyCls);
108 delField.modifiers = Modifier.PUBLIC
109 proxyCls.addField(delField)
110 delegateCls.methods.filter[it.declaringClass != objCls].forEach [
111 val proxyMethod = new CtMethod(it, proxyCls, null);
112 proxyMethod.body = '''return ($r) «DELEGATE_FIELD».«it.name»($$);'''
113 proxyCls.addMethod(proxyMethod)
115 return proxyCls.toClass(delegate.classLoader)
118 def <T extends RpcService> registerRpcImplementation(Class<T> type, T service, OsgiProviderContext context,
119 Hashtable<String, String> properties) {
120 val proxy = getManagedDirectProxy(type)
121 if(proxy.delegate != null) {
122 throw new IllegalStateException("Service " + type + "is already registered");
124 val osgiReg = context.bundleContext.registerService(type, service, properties);
125 proxy.delegate = service;
126 return new RpcServiceRegistrationImpl<T>(type, service, osgiReg);
129 def <T extends RpcService> getDelegate(RpcService proxy) {
130 val field = proxy.class.getField(DELEGATE_FIELD)
131 if(field == null) throw new UnsupportedOperationException("Unable to get delegate from proxy");
132 return field.get(proxy) as T
135 def void setDelegate(RpcService proxy, RpcService delegate) {
136 val field = proxy.class.getField(DELEGATE_FIELD)
137 if(field == null) throw new UnsupportedOperationException("Unable to set delegate to proxy");
138 if (field.type.isAssignableFrom(delegate.class)) {
139 field.set(proxy,delegate)
140 } else throw new IllegalArgumentException("delegate class is not assignable to proxy");