Proper unregistration of provider's session
[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
13 class ProviderContextImpl extends ConsumerContextImpl implements ProviderSession {
14
15     @Property
16     private val Provider provider;
17
18     private val rpcImpls = Collections.synchronizedMap(new HashMap<QName, RpcImplementation>());
19
20     new(Provider provider, BundleContext ctx) {
21         super(null, ctx);
22         this._provider = provider;
23     }
24
25     override addRpcImplementation(QName rpcType, RpcImplementation implementation) throws IllegalArgumentException {
26         if(rpcType == null) {
27             throw new IllegalArgumentException("rpcType must not be null");
28         }
29         if(implementation == null) {
30             throw new IllegalArgumentException("Implementation must not be null");
31         }
32         broker.addRpcImplementation(rpcType, implementation);
33         rpcImpls.put(rpcType, implementation);
34         
35         return new RpcRegistrationImpl(rpcType, implementation, this);
36     }
37
38     def removeRpcImplementation(RpcRegistrationImpl implToRemove) throws IllegalArgumentException {
39         val localImpl = rpcImpls.get(implToRemove.type);
40         if(localImpl !== implToRemove.instance) {
41             throw new IllegalStateException(
42                 "Implementation was not registered in this session");
43         }
44         broker.removeRpcImplementation(implToRemove.type);
45         rpcImpls.remove(implToRemove.type);
46     }
47     
48     override close() {
49                 removeAllRpcImlementations
50         super.close
51     }
52     
53     private def removeAllRpcImlementations() {
54         if (!rpcImpls.empty) {
55                 for (entry : rpcImpls.entrySet) {
56                         broker.removeRpcImplementation(entry.key);
57                 }
58                 rpcImpls.clear
59         }
60     }
61     
62     override addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
63         throw new UnsupportedOperationException("TODO: auto-generated method stub")
64     }
65     
66     override addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
67         throw new UnsupportedOperationException("TODO: auto-generated method stub")
68     }
69     
70 }
71
72 class RpcRegistrationImpl extends AbstractObjectRegistration<RpcImplementation> implements RpcRegistration {
73         
74         @Property
75         val QName type
76         
77         private var ProviderContextImpl context
78         
79         new(QName type, RpcImplementation instance, ProviderContextImpl ctx) {
80                 super(instance)
81                 _type = type
82                 context = ctx
83         }
84         
85         override protected removeRegistration() {
86                 context.removeRpcImplementation(this)
87                 context = null  
88         }
89
90 }