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