Clean up of binding broker implementation
[controller.git] / opendaylight / sal / yang-prototype / sal / sal-binding-broker-impl / src / main / java / org / opendaylight / controller / sal / binding / impl / OsgiConsumerContext.xtend
1 package org.opendaylight.controller.sal.binding.impl;
2
3 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
4 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
5 import org.opendaylight.yangtools.yang.binding.RpcService;
6 import org.osgi.framework.BundleContext;
7 import org.osgi.framework.InvalidSyntaxException;
8 import org.osgi.framework.ServiceReference;
9 import org.slf4j.LoggerFactory
10
11 class OsgiConsumerContext implements ConsumerContext {
12
13         static val log = LoggerFactory.getLogger(OsgiConsumerContext)
14         protected val BundleContext bundleContext;
15         protected val BindingAwareBrokerImpl broker;
16         
17         new(BundleContext ctx,BindingAwareBrokerImpl broker) {
18                 this.bundleContext = ctx;
19                 this.broker = broker;
20         }
21
22         
23         override def <T extends BindingAwareService> getSALService(Class<T> service) {
24                 // SAL Services are global
25                 var ref =  bundleContext.getServiceReference(service);
26                 return bundleContext.getService(ref) as T;
27         }
28         
29         
30
31         override def <T extends RpcService> T getRpcService(Class<T> module) {
32                 try {
33                         
34                         val services = bundleContext.getServiceReferences(module, getProxyFilter());
35                         
36                         // Proxy service found / using first implementation
37                         // FIXME: Add advanced logic to retrieve service with right set of models
38                         if(false == services.empty) {
39                                 val ref = services.iterator().next() as ServiceReference<T>;
40                                 return bundleContext.getService(ref) as T;
41                         } 
42                 } catch (InvalidSyntaxException e) {
43                         log.error("Created filter was invalid:", e.message,e)
44                 }
45                 return null;
46                 
47
48         }
49
50         private def getProxyFilter() {
51                 return '''(«Constants.SAL_SERVICE_TYPE»=«Constants.SAL_SERVICE_TYPE_CONSUMER_PROXY»)'''
52         }
53 }