74ef87ae5afd4713f135a8feb05d2a4e306f58a5
[controller.git] / opendaylight / sal / yang-prototype / sal / sal-binding-broker-impl / src / main / java / org / opendaylight / controller / sal / binding / impl / BindingAwareBrokerImpl.xtend
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.impl
9
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
17 import java.util.Map
18 import java.util.HashMap
19 import javassist.LoaderClassPath
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker
21 import java.util.Hashtable
22
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
32
33 class BindingAwareBrokerImpl implements BindingAwareBroker {
34     private static val DELEGATE_FIELD = "_delegate"
35     private static val log = LoggerFactory.getLogger(BindingAwareBrokerImpl)
36     
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
42     @Property
43     var BundleContext brokerBundleContext
44
45     def start() {
46         initGenerator();
47
48         // Initialization of notificationBroker
49         notifyBroker = new NotificationBrokerImpl(null);
50         val brokerProperties = PropertiesUtils.newProperties();
51         notifyBrokerRegistration = brokerBundleContext.registerService(NotificationProviderService, notifyBroker,
52             brokerProperties)
53         brokerBundleContext.registerService(NotificationService, notifyBroker, brokerProperties)
54     }
55
56     def initGenerator() {
57
58         // YANG Binding Class Loader
59         clsPool.appendClassPath(new LoaderClassPath(RpcService.classLoader))
60     }
61
62     override registerConsumer(BindingAwareConsumer consumer, BundleContext bundleCtx) {
63         val ctx = consumer.createContext(bundleCtx)
64         consumer.onSessionInitialized(ctx)
65         return ctx
66     }
67
68     override registerProvider(BindingAwareProvider provider, BundleContext bundleCtx) {
69         val ctx = provider.createContext(bundleCtx)
70         provider.onSessionInitialized(ctx)
71         provider.onSessionInitiated(ctx as ProviderContext)
72         return ctx
73     }
74
75     private def createContext(BindingAwareConsumer consumer, BundleContext consumerCtx) {
76         new OsgiConsumerContext(consumerCtx, this)
77     }
78
79     private def createContext(BindingAwareProvider provider, BundleContext providerCtx) {
80         new OsgiProviderContext(providerCtx, this)
81     }
82
83     def <T extends RpcService> getManagedDirectProxy(Class<T> service) {
84         
85         var RpcProxyContext existing = null
86         if ((existing = managedProxies.get(service)) != null) {
87             return existing.proxy
88         }
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
93
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
98     }
99
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)
114         ]
115         return proxyCls.toClass(delegate.classLoader)
116     }
117
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");
123         }
124         val osgiReg = context.bundleContext.registerService(type, service, properties);
125         proxy.delegate = service;
126         return new RpcServiceRegistrationImpl<T>(type, service, osgiReg);
127     }
128     
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
133     }
134     
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");
141     }
142     
143     
144 }