2bae9f515f40bfe4ed25e95ccd4426493c413a22
[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 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
32 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration
33 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration
34 import org.opendaylight.controller.sal.binding.api.data.DataProviderService
35 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService
36
37 class BindingAwareBrokerImpl implements BindingAwareBroker {
38     private static val log = LoggerFactory.getLogger(BindingAwareBrokerImpl)
39
40     private val clsPool = ClassPool.getDefault()
41     private var RuntimeCodeGenerator generator;
42     private Map<Class<? extends RpcService>, RpcProxyContext> managedProxies = new HashMap();
43     private var NotificationBrokerImpl notifyBroker
44     private var DataBrokerImpl dataBroker
45     private var ServiceRegistration<NotificationProviderService> notifyBrokerRegistration
46
47     @Property
48     var BundleContext brokerBundleContext
49
50     def start() {
51         initGenerator();
52
53         // Initialization of notificationBroker
54         notifyBroker = new NotificationBrokerImpl(null);
55         dataBroker = new DataBrokerImpl();
56         val brokerProperties = newProperties();
57         notifyBrokerRegistration = brokerBundleContext.registerService(NotificationProviderService, notifyBroker,
58             brokerProperties)
59         brokerBundleContext.registerService(NotificationService, notifyBroker, brokerProperties)
60         brokerBundleContext.registerService(DataProviderService,dataBroker,brokerProperties)
61         brokerBundleContext.registerService(DataBrokerService,dataBroker,brokerProperties)
62         
63     }
64
65     def initGenerator() {
66
67         // YANG Binding Class Loader
68         clsPool.appendClassPath(new LoaderClassPath(RpcService.classLoader));
69         generator = new RuntimeCodeGenerator(clsPool);
70     }
71
72     override registerConsumer(BindingAwareConsumer consumer, BundleContext bundleCtx) {
73         val ctx = consumer.createContext(bundleCtx)
74         consumer.onSessionInitialized(ctx)
75         return ctx
76     }
77
78     override registerProvider(BindingAwareProvider provider, BundleContext bundleCtx) {
79         val ctx = provider.createContext(bundleCtx)
80         provider.onSessionInitialized(ctx)
81         provider.onSessionInitiated(ctx as ProviderContext)
82         return ctx
83     }
84
85     private def createContext(BindingAwareConsumer consumer, BundleContext consumerCtx) {
86         new OsgiConsumerContext(consumerCtx, this)
87     }
88
89     private def createContext(BindingAwareProvider provider, BundleContext providerCtx) {
90         new OsgiProviderContext(providerCtx, this)
91     }
92
93     /**
94      * Returns a Managed Direct Proxy for supplied class
95      * 
96      * Managed direct proxy is a generated proxy class conforming to the supplied interface
97      * which delegates all calls to the backing delegate.
98      * 
99      * Proxy does not do any validation, null pointer checks or modifies data in any way, it
100      * is only use to avoid exposing direct references to backing implementation of service.
101      * 
102      * If proxy class does not exist for supplied service class it will be generated automatically.
103      */
104     def <T extends RpcService> getManagedDirectProxy(Class<T> service) {
105
106         var RpcProxyContext existing = null
107         if((existing = managedProxies.get(service)) != null) {
108             return existing.proxy
109         }
110         val proxyClass = generator.generateDirectProxy(service)
111         val rpcProxyCtx = new RpcProxyContext(proxyClass)
112         val properties = new Hashtable<String, String>()
113         rpcProxyCtx.proxy = proxyClass.newInstance as RpcService
114
115         properties.salServiceType = SAL_SERVICE_TYPE_CONSUMER_PROXY
116         rpcProxyCtx.registration = brokerBundleContext.registerService(service, rpcProxyCtx.proxy as T, properties)
117         managedProxies.put(service, rpcProxyCtx)
118         return rpcProxyCtx.proxy
119     }
120
121     /**
122      * Registers RPC Implementation
123      * 
124      */
125     def <T extends RpcService> registerRpcImplementation(Class<T> type, T service, OsgiProviderContext context,
126         Hashtable<String, String> properties) {
127         val proxy = getManagedDirectProxy(type)
128         if(proxy.delegate != null) {
129             throw new IllegalStateException("Service " + type + "is already registered");
130         }
131         val osgiReg = context.bundleContext.registerService(type, service, properties);
132         proxy.delegate = service;
133         return new RpcServiceRegistrationImpl<T>(type, service, osgiReg);
134     }
135
136     def <T extends RpcService> RpcRegistration<T> registerMountedRpcImplementation(Class<T> tyoe, T service, InstanceIdentifier<?> identifier,
137         OsgiProviderContext context, Hashtable<String, String> properties) {
138         throw new UnsupportedOperationException("TODO: auto-generated method stub")
139     }
140
141     def <T extends RpcService> RoutedRpcRegistration<T> registerRoutedRpcImplementation(Class<T> type, T service, OsgiProviderContext context,
142         Hashtable<String, String> properties) {
143         throw new UnsupportedOperationException("TODO: auto-generated method stub")
144     }
145
146 }