Fix occasional NPEs in Connection manager
[controller.git] / opendaylight / md-sal / sal-binding-broker / 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 org.osgi.framework.BundleContext
15 import java.util.Map
16 import java.util.HashMap
17 import javassist.LoaderClassPath
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker
19 import java.util.Hashtable
20 import static extension org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.*
21
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService
23 import org.osgi.framework.ServiceRegistration
24 import static org.opendaylight.controller.sal.binding.impl.osgi.Constants.*
25 import static extension org.opendaylight.controller.sal.binding.impl.osgi.PropertiesUtils.*
26 import org.opendaylight.controller.sal.binding.api.NotificationService
27 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext
28
29 import org.slf4j.LoggerFactory
30 import org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator
31
32 class BindingAwareBrokerImpl implements BindingAwareBroker {
33     private static val log = LoggerFactory.getLogger(BindingAwareBrokerImpl)
34     
35     private val clsPool = ClassPool.getDefault()
36     private var RuntimeCodeGenerator generator;
37     private Map<Class<? extends RpcService>, RpcProxyContext> managedProxies = new HashMap();
38     private var NotificationBrokerImpl notifyBroker
39     private var ServiceRegistration<NotificationProviderService> notifyBrokerRegistration
40     
41     @Property
42     var BundleContext brokerBundleContext
43
44     def start() {
45         initGenerator();
46
47         // Initialization of notificationBroker
48         notifyBroker = new NotificationBrokerImpl(null);
49         val brokerProperties = newProperties();
50         notifyBrokerRegistration = brokerBundleContext.registerService(NotificationProviderService, notifyBroker,
51             brokerProperties)
52         brokerBundleContext.registerService(NotificationService, notifyBroker, brokerProperties)
53     }
54
55     def initGenerator() {
56
57         // YANG Binding Class Loader
58         clsPool.appendClassPath(new LoaderClassPath(RpcService.classLoader));
59         generator = new RuntimeCodeGenerator(clsPool);
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     /**
84      * Returns a Managed Direct Proxy for supplied class
85      * 
86      * Managed direct proxy is a generated proxy class conforming to the supplied interface
87      * which delegates all calls to the backing delegate.
88      * 
89      * Proxy does not do any validation, null pointer checks or modifies data in any way, it
90      * is only use to avoid exposing direct references to backing implementation of service.
91      * 
92      * If proxy class does not exist for supplied service class it will be generated automatically.
93      */
94     def <T extends RpcService> getManagedDirectProxy(Class<T> service) {
95         
96         var RpcProxyContext existing = null
97         if ((existing = managedProxies.get(service)) != null) {
98             return existing.proxy
99         }
100         val proxyClass = generator.generateDirectProxy(service)
101         val rpcProxyCtx = new RpcProxyContext(proxyClass)
102         val properties = new Hashtable<String, String>()
103         rpcProxyCtx.proxy = proxyClass.newInstance as RpcService
104
105         properties.salServiceType = SAL_SERVICE_TYPE_CONSUMER_PROXY
106         rpcProxyCtx.registration = brokerBundleContext.registerService(service, rpcProxyCtx.proxy as T, properties)
107         managedProxies.put(service, rpcProxyCtx)
108         return rpcProxyCtx.proxy
109     }
110     /**
111      * Registers RPC Implementation
112      * 
113      */
114     def <T extends RpcService> registerRpcImplementation(Class<T> type, T service, OsgiProviderContext context,
115         Hashtable<String, String> properties) {
116         val proxy = getManagedDirectProxy(type)
117         if(proxy.delegate != null) {
118             throw new IllegalStateException("Service " + type + "is already registered");
119         }
120         val osgiReg = context.bundleContext.registerService(type, service, properties);
121         proxy.delegate = service;
122         return new RpcServiceRegistrationImpl<T>(type, service, osgiReg);
123     }
124 }