Added YANG models for flow configuration, statistics and inventory.
[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     private var DataBrokerImpl dataBroker
42     
43     @Property
44     var BundleContext brokerBundleContext
45
46     def start() {
47         initGenerator();
48
49         // Initialization of notificationBroker
50         notifyBroker = new NotificationBrokerImpl(null);
51         val brokerProperties = PropertiesUtils.newProperties();
52         notifyBrokerRegistration = brokerBundleContext.registerService(NotificationProviderService, notifyBroker,
53             brokerProperties)
54         brokerBundleContext.registerService(NotificationService, notifyBroker, brokerProperties)
55     }
56
57     def initGenerator() {
58
59         // YANG Binding Class Loader
60         clsPool.appendClassPath(new LoaderClassPath(RpcService.classLoader))
61     }
62
63     override registerConsumer(BindingAwareConsumer consumer, BundleContext bundleCtx) {
64         val ctx = consumer.createContext(bundleCtx)
65         consumer.onSessionInitialized(ctx)
66         return ctx
67     }
68
69     override registerProvider(BindingAwareProvider provider, BundleContext bundleCtx) {
70         val ctx = provider.createContext(bundleCtx)
71         provider.onSessionInitialized(ctx)
72         provider.onSessionInitiated(ctx as ProviderContext)
73         return ctx
74     }
75
76     private def createContext(BindingAwareConsumer consumer, BundleContext consumerCtx) {
77         new OsgiConsumerContext(consumerCtx, this)
78     }
79
80     private def createContext(BindingAwareProvider provider, BundleContext providerCtx) {
81         new OsgiProviderContext(providerCtx, this)
82     }
83
84     /**
85      * Returns a Managed Direct Proxy for supplied class
86      * 
87      * Managed direct proxy is a generated proxy class conforming to the supplied interface
88      * which delegates all calls to the backing delegate.
89      * 
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.
92      * 
93      * If proxy class does not exist for supplied service class it will be generated automatically.
94      */
95     def <T extends RpcService> getManagedDirectProxy(Class<T> service) {
96         
97         var RpcProxyContext existing = null
98         if ((existing = managedProxies.get(service)) != null) {
99             return existing.proxy
100         }
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
105
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
110     }
111
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)
126         ]
127         return proxyCls.toClass(delegate.classLoader)
128     }
129
130     /**
131      * Registers RPC Implementation
132      * 
133      */
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");
139         }
140         val osgiReg = context.bundleContext.registerService(type, service, properties);
141         proxy.delegate = service;
142         return new RpcServiceRegistrationImpl<T>(type, service, osgiReg);
143     }
144     
145     /**
146      * Helper method to return delegate from ManagedDirectedProxy with use of reflection.
147      * 
148      * Note: This method uses reflection, but access to delegate field should be 
149      * avoided and called only if neccessary.
150      * 
151      */
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
156     }
157     
158         /**
159      * Helper method to set delegate to ManagedDirectedProxy with use of reflection.
160      * 
161      * Note: This method uses reflection, but setting delegate field should not occur too much
162      * to introduce any significant performance hits.
163      * 
164      */
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");
171     }
172     
173     
174 }