Added listener for rpc registrations.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / ProviderContextImpl.xtend
1 package org.opendaylight.controller.sal.dom.broker
2
3 import java.util.Collections
4 import java.util.HashMap
5 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession
6 import org.opendaylight.controller.sal.core.api.Provider
7 import org.opendaylight.controller.sal.core.api.RpcImplementation
8 import org.opendaylight.yangtools.yang.common.QName
9 import org.osgi.framework.BundleContext
10 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration
11 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration
12 import static java.util.Collections.*
13 import java.util.Collections
14 import java.util.HashMap
15 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener
16
17 class ProviderContextImpl extends ConsumerContextImpl implements ProviderSession {
18
19     @Property
20     private val Provider provider;
21
22     private val rpcImpls = Collections.synchronizedMap(new HashMap<QName, RpcImplementation>());
23
24     new(Provider provider, BundleContext ctx) {
25         super(null, ctx);
26         this._provider = provider;
27     }
28
29     override addRpcImplementation(QName rpcType, RpcImplementation implementation) throws IllegalArgumentException {
30         if(rpcType == null) {
31             throw new IllegalArgumentException("rpcType must not be null");
32         }
33         if(implementation == null) {
34             throw new IllegalArgumentException("Implementation must not be null");
35         }
36         broker.addRpcImplementation(rpcType, implementation);
37         rpcImpls.put(rpcType, implementation);
38         
39         return new RpcRegistrationImpl(rpcType, implementation, this);
40     }
41
42     def removeRpcImplementation(RpcRegistrationImpl implToRemove) throws IllegalArgumentException {
43         val localImpl = rpcImpls.get(implToRemove.type);
44         if(localImpl !== implToRemove.instance) {
45             throw new IllegalStateException(
46                 "Implementation was not registered in this session");
47         }
48         broker.removeRpcImplementation(implToRemove.type,localImpl);
49         rpcImpls.remove(implToRemove.type);
50     }
51     
52     override close() {
53                 removeAllRpcImlementations
54         super.close
55     }
56     
57     private def removeAllRpcImlementations() {
58         if (!rpcImpls.empty) {
59                 for (entry : rpcImpls.entrySet) {
60                         broker.removeRpcImplementation(entry.key,entry.value);
61                 }
62                 rpcImpls.clear
63         }
64     }
65     
66     override addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
67         throw new UnsupportedOperationException("TODO: auto-generated method stub")
68     }
69     
70     override addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
71         throw new UnsupportedOperationException("TODO: auto-generated method stub")
72     }
73     
74     override getSupportedRpcs() {
75         broker.getSupportedRpcs();
76     }
77     
78     override addRpcRegistrationListener(RpcRegistrationListener listener) {
79         broker.addRpcRegistrationListener(listener);
80     }
81 }
82
83 class RpcRegistrationImpl extends AbstractObjectRegistration<RpcImplementation> implements RpcRegistration {
84         
85         @Property
86         val QName type
87         
88         private var ProviderContextImpl context
89         
90         new(QName type, RpcImplementation instance, ProviderContextImpl ctx) {
91                 super(instance)
92                 _type = type
93                 context = ctx
94         }
95         
96         override protected removeRegistration() {
97                 context.removeRpcImplementation(this)
98                 context = null  
99         }
100
101 }