Proper unregistration of provider's session 68/2068/2
authormsunal <msunal@cisco.com>
Tue, 22 Oct 2013 07:43:28 +0000 (09:43 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 22 Oct 2013 13:36:43 +0000 (13:36 +0000)
- if provider's session is closed, all RPCs from broker are removed

Change-Id: Ie59c19868b340588c2b7114830493986d1be3b6b
Signed-off-by: Martin Sunal <msunal@cisco.com>
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/BrokerImpl.xtend
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/ProviderContextImpl.xtend

index 6b57cdc4db64c72990359c8a9dc9191f967debdd..bdd4491d016ac747bc17d8a7b10860e7f17c4eed 100644 (file)
@@ -7,25 +7,26 @@
  */
 package org.opendaylight.controller.sal.dom.broker;
 
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
-import org.opendaylight.controller.sal.core.api.Broker;
-import org.opendaylight.controller.sal.core.api.BrokerService;
-import org.opendaylight.controller.sal.core.api.Consumer;
-import org.opendaylight.controller.sal.core.api.Provider;
-import org.opendaylight.controller.sal.core.api.RpcImplementation;
-import org.opendaylight.controller.sal.core.spi.BrokerModule;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.osgi.framework.BundleContext;
-import org.slf4j.LoggerFactory;
+import java.util.Collections
+import java.util.HashMap
+import java.util.HashSet
+import java.util.Map
+import java.util.Set
+import java.util.concurrent.Callable
+import java.util.concurrent.ExecutorService
+import java.util.concurrent.Executors
+import java.util.concurrent.Future
+import org.opendaylight.controller.sal.core.api.Broker
+import org.opendaylight.controller.sal.core.api.BrokerService
+import org.opendaylight.controller.sal.core.api.Consumer
+import org.opendaylight.controller.sal.core.api.Provider
+import org.opendaylight.controller.sal.core.api.RpcImplementation
+import org.opendaylight.controller.sal.core.spi.BrokerModule
+import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.common.RpcResult
+import org.opendaylight.yangtools.yang.data.api.CompositeNode
+import org.osgi.framework.BundleContext
+import org.slf4j.LoggerFactory
 
 public class BrokerImpl implements Broker {
     private static val log = LoggerFactory.getLogger(BrokerImpl);
@@ -44,7 +45,7 @@ public class BrokerImpl implements Broker {
 
     // Implementation specific
     @Property
-    private var ExecutorService executor;
+    private var ExecutorService executor = Executors.newFixedThreadPool(5);
     @Property
     private var BundleContext bundleContext;
 
@@ -99,10 +100,8 @@ public class BrokerImpl implements Broker {
         rpcImpls.put(rpcType, implementation);
     }
 
-    protected def void removeRpcImplementation(QName rpcType, RpcImplementation implToRemove) {
-        if(implToRemove == rpcImpls.get(rpcType)) {
-            rpcImpls.remove(rpcType);
-        }
+    protected def void removeRpcImplementation(QName rpcType) {
+        rpcImpls.remove(rpcType);
     }
 
     protected def Future<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
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