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
index 11f68d6e04fb41a64c26d6a4a0a92833390f6e1b..27e559cb9b5011161a25050287555d8ee0c6165c 100644 (file)
@@ -1,13 +1,14 @@
 package org.opendaylight.controller.sal.dom.broker
 
+import java.util.Collections
+import java.util.HashMap
 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession
 import org.opendaylight.controller.sal.core.api.Provider
-import org.opendaylight.yangtools.yang.common.QName
 import org.opendaylight.controller.sal.core.api.RpcImplementation
+import org.opendaylight.yangtools.yang.common.QName
 import org.osgi.framework.BundleContext
-import static java.util.Collections.*
-import java.util.Collections
-import java.util.HashMap
+import org.opendaylight.yangtools.concepts.AbstractObjectRegistration
+import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration
 
 class ProviderContextImpl extends ConsumerContextImpl implements ProviderSession {
 
@@ -30,19 +31,32 @@ class ProviderContextImpl extends ConsumerContextImpl implements ProviderSession
         }
         broker.addRpcImplementation(rpcType, implementation);
         rpcImpls.put(rpcType, implementation);
-        //FIXME: Return registration
-        return null;
+        
+        return new RpcRegistrationImpl(rpcType, implementation, this);
     }
 
-    def removeRpcImplementation(QName rpcType, RpcImplementation implToRemove) throws IllegalArgumentException {
-        val localImpl = rpcImpls.get(rpcType);
-        if(localImpl != implToRemove) {
+    def removeRpcImplementation(RpcRegistrationImpl implToRemove) throws IllegalArgumentException {
+        val localImpl = rpcImpls.get(implToRemove.type);
+        if(localImpl !== implToRemove.instance) {
             throw new IllegalStateException(
                 "Implementation was not registered in this session");
         }
-
-        broker.removeRpcImplementation(rpcType, implToRemove);
-        rpcImpls.remove(rpcType);
+        broker.removeRpcImplementation(implToRemove.type);
+        rpcImpls.remove(implToRemove.type);
+    }
+    
+    override close() {
+               removeAllRpcImlementations
+       super.close
+    }
+    
+    private def removeAllRpcImlementations() {
+       if (!rpcImpls.empty) {
+               for (entry : rpcImpls.entrySet) {
+                       broker.removeRpcImplementation(entry.key);
+               }
+               rpcImpls.clear
+       }
     }
     
     override addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
@@ -54,3 +68,23 @@ class ProviderContextImpl extends ConsumerContextImpl implements ProviderSession
     }
     
 }
+
+class RpcRegistrationImpl extends AbstractObjectRegistration<RpcImplementation> implements RpcRegistration {
+       
+       @Property
+       val QName type
+       
+       private var ProviderContextImpl context
+       
+       new(QName type, RpcImplementation instance, ProviderContextImpl ctx) {
+               super(instance)
+               _type = type
+               context = ctx
+       }
+       
+       override protected removeRegistration() {
+               context.removeRpcImplementation(this)
+               context = null  
+       }
+
+}
\ No newline at end of file