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