fix of BUG 434 33/5633/1
authorMichal Rehak <mirehak@cisco.com>
Thu, 13 Mar 2014 16:50:12 +0000 (17:50 +0100)
committerMichal Rehak <mirehak@cisco.com>
Fri, 14 Mar 2014 10:10:30 +0000 (11:10 +0100)
- added AutoCloseable interface to MDController, MessageCountCommandProvider, SalRegistrationManager, SessionManager
- added init/start and close/destroy logging
- added closing/destroying upon bundle stop

Change-Id: I5fe94bbcfa37225790b68d2e1ef01ffb1f6ab675
Signed-off-by: Michal Rehak <mirehak@cisco.com>
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/Activator.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/SwitchInventory.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/MDController.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/cmd/MessageCountCommandProvider.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/OpenflowPluginProvider.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/SalRegistrationManager.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/OFSessionUtil.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SessionManager.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SessionManagerOFImpl.java

index fd162e64519757be54274002e43608695ccac257..43aa4729eb3d132b679e8c3f516cb1a9e2641acb 100644 (file)
@@ -32,6 +32,7 @@ public class Activator extends ComponentActivatorAbstractBase {
      *
      */
     public void init() {
+        logger.debug("init");
     }
 
     /**
@@ -40,12 +41,14 @@ public class Activator extends ComponentActivatorAbstractBase {
      *
      */
     public void destroy() {
+        logger.debug("destroy");
         pluginProvider.close();
         super.destroy();
     }
 
     @Override
     public void start(BundleContext arg0) {
+        logger.debug("start");
         super.start(arg0);
         pluginProvider.setContext(arg0);
     }
@@ -93,8 +96,7 @@ public class Activator extends ComponentActivatorAbstractBase {
      *         Object
      */
     public Object[] getGlobalImplementations() {
-        //TODO:: is MDController still needed here?
-        Object[] res = { MDController.class, pluginProvider };
+        Object[] res = { pluginProvider };
         return res;
     }
 
@@ -110,7 +112,6 @@ public class Activator extends ComponentActivatorAbstractBase {
      *            as the same routine can configure multiple implementations
      */
     public void configureGlobalInstance(Component c, Object imp) {
-
          if (imp == pluginProvider) {
             // c.setInterface(new String[] { IDiscoveryListener.class.getName(),
             // IContainerListener.class.getName(),
index dbcbcfb2442a3dbccf71cb25a8183e56c21d5cf0..fc345e7089a675d7388a44e414c6e17e85b27287 100644 (file)
@@ -8,8 +8,11 @@
 package org.opendaylight.openflowplugin.openflow.md;
 
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 
+/**
+ * @deprecated seems there is no use for this interface
+ */
+@Deprecated
 public interface SwitchInventory {
 
     ModelDrivenSwitch getSwitch(NodeRef node);
index 456de0f428f75c1c8a43f439ff159b15e757e704..2206091526082fc2e9cda4e6215e5d30f371ca49 100644 (file)
@@ -74,7 +74,7 @@ import com.google.common.collect.Lists;
  * @author mirehak
  *
  */
-public class MDController implements IMDController {
+public class MDController implements IMDController, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
 
@@ -99,7 +99,7 @@ public class MDController implements IMDController {
      * provisioning of translator mapping
      */
     public void init() {
-        LOG.debug("Initializing!");
+        LOG.debug("init");
         messageTranslators = new ConcurrentHashMap<>();
         popListeners = new ConcurrentHashMap<>();
         //TODO: move registration to factory
@@ -225,6 +225,7 @@ public class MDController implements IMDController {
         } catch (InterruptedException | ExecutionException | TimeoutException e) {
             LOG.error(e.getMessage(), e);
         }
+        close();
     }
 
     /**
@@ -234,7 +235,7 @@ public class MDController implements IMDController {
      *
      */
     public void destroy() {
-        // do nothing
+        close();
     }
 
     @Override
@@ -293,5 +294,14 @@ public class MDController implements IMDController {
             MessageSpy<OfHeader, DataObject> messageSpyCounter) {
         this.messageSpyCounter = messageSpyCounter;
     }
-
+    
+    @Override
+    public void close() {
+        LOG.debug("close");
+        messageSpyCounter = null;
+        messageTranslators = null;
+        popListeners = null;
+        switchConnectionProvider = null;
+        OFSessionUtil.releaseSessionManager();
+    }
 }
index 6f57f90267715bea616baaac851d06523a7f84e1..7db3eaea9fc0cd29bcd95953b2ef24fc5cf1f311 100644 (file)
@@ -13,15 +13,23 @@ import org.eclipse.osgi.framework.console.CommandProvider;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.openflowplugin.openflow.md.queue.MessageCountDumper;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * 
  */
-public class MessageCountCommandProvider implements CommandProvider {
+public class MessageCountCommandProvider implements CommandProvider, AutoCloseable {
+    
+    private static Logger LOG = LoggerFactory
+            .getLogger(MessageCountCommandProvider.class);
     
     private boolean sessionInitiated;
     private BundleContext ctx;
     private MessageCountDumper provider;
+
+    private ServiceRegistration commandRegistration;
     
     /**
      * @param ctx
@@ -43,7 +51,8 @@ public class MessageCountCommandProvider implements CommandProvider {
      * @param session
      */
     public void onSessionInitiated(ProviderContext session) {
-        ctx.registerService(CommandProvider.class.getName(), this, null);
+        LOG.debug("onSessionInitiated");
+        commandRegistration = ctx.registerService(CommandProvider.class.getName(), this, null);
         this.sessionInitiated = true;
     }
     
@@ -60,5 +69,16 @@ public class MessageCountCommandProvider implements CommandProvider {
             ci.println("Session not initiated, try again in a few seconds");
         }
     }
+    
+    @Override
+    public void close() {
+        LOG.debug("close");
+        if (commandRegistration != null) {
+            commandRegistration.unregister();
+        }
+        ctx = null;
+        provider = null;
+        sessionInitiated = false;
+    }
 
 }
index a7a3ca13dde9ad83a5eccb2d98b40f605dc38608..91fe5271ded349f6231d8fd8c095936cfc8f5a32 100644 (file)
@@ -23,12 +23,16 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.RpcService;
 import org.osgi.framework.BundleContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * OFPlugin provider implementation
  */
 public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseable {
-
+    
+    private static Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
+    
     private BindingAwareBroker broker;
 
     private BundleContext context;
@@ -41,6 +45,8 @@ public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseab
 
     private MessageObservatory<OfHeader, DataObject> messageCountProvider;
     
+    private SalRegistrationManager registrationManager;
+    
     public void unsetSwitchConnectionProvider() {
         switchConnectionProvider = null;
     }
@@ -59,12 +65,11 @@ public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseab
         this.context = context;
     }
 
-    SalRegistrationManager registrationManager = new SalRegistrationManager();
-
-
     @Override
     public void onSessionInitiated(ProviderContext session) {
+        LOG.debug("onSessionInitiated");
         messageCountProvider = new MessageSpyCounterImpl();
+        registrationManager = new SalRegistrationManager();
         registrationManager.onSessionInitiated(session);
         mdController = new MDController();
         mdController.setSwitchConnectionProvider(switchConnectionProvider);
@@ -77,7 +82,13 @@ public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseab
     
     @Override
     public void close() {
+        LOG.debug("close");
         mdController.stop();
+        mdController = null;
+        registrationManager.close();
+        registrationManager = null;
+        messageCountCommandProvider.close();
+        messageCountCommandProvider = null;
     }
 
     @Override
index c3f0b55d5948853aa48557d796512b5a7d9ea589..7ffa5591d1a0a1c80fd0460ad3bb066dc03ff63b 100644 (file)
@@ -42,12 +42,10 @@ import org.slf4j.LoggerFactory;
 /**
  * session and inventory listener implementation
  */
-public class SalRegistrationManager implements SessionListener, SwitchInventory {
+public class SalRegistrationManager implements SessionListener, AutoCloseable {
 
     private final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
 
-    Map<InstanceIdentifier<Node>, ModelDrivenSwitch> salSwitches = new ConcurrentHashMap<>();
-
     private ProviderContext providerContext;
 
     private NotificationProviderService publishService;
@@ -73,6 +71,7 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
     }
 
     public void onSessionInitiated(ProviderContext session) {
+        LOG.debug("onSessionInitiated");
         this.providerContext = session;
         this.publishService = session.getSALService(NotificationProviderService.class);
         this.dataService = session.getSALService(DataProviderService.class);
@@ -81,7 +80,6 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
         getSessionManager().setNotificationProviderService(publishService);
         getSessionManager().setDataProviderService(dataService);
         LOG.debug("SalRegistrationManager initialized");
-
     }
 
     @Override
@@ -93,7 +91,6 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
         NodeId nodeId = nodeIdFromDatapathId(datapathId);
         ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
         LLDPSpeaker.getInstance().addModelDrivenSwitch(identifier, ofSwitch);
-        salSwitches.put(identifier, ofSwitch);
         ofSwitch.register(providerContext);
 
         LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
@@ -131,11 +128,6 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
         return builder.build();
     }
 
-    @Override
-    public ModelDrivenSwitch getSwitch(NodeRef node) {
-        return salSwitches.get(node.getValue());
-    }
-
     public static InstanceIdentifier<Node> identifierFromDatapathId(BigInteger datapathId) {
         NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
         InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class,nodeKey);
@@ -155,4 +147,12 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
     public SessionManager getSessionManager() {
         return OFSessionUtil.getSessionManager();
     }
+    
+    @Override
+    public void close() {
+        LOG.debug("close");
+        dataService = null;
+        providerContext = null;
+        publishService = null;
+    }
 }
index abb87c11281239bd5eda116bf13f5d620ca5dfa7..41b4fae9068175f0b1809a674eb6e85a22f54658 100644 (file)
@@ -151,7 +151,14 @@ public abstract class OFSessionUtil {
     public static SessionManager getSessionManager() {
         return SessionManagerOFImpl.getInstance();
     }
-
+    
+    /**
+     * release session manager singleton instance
+     */
+    public static void releaseSessionManager() {
+        SessionManagerOFImpl.releaseInstance();
+    }
+    
     /**
     * @return session manager listener Map
     */
index 19f40ca5bd82f1d17fcd7ef455e1a759ccfce388..fd3bab743821d069460ef1fa78b7db1af7106d23 100644 (file)
@@ -27,7 +27,7 @@ import org.opendaylight.yangtools.yang.binding.DataObject;
 /**
  * @author mirehak
  */
-public interface SessionManager {
+public interface SessionManager extends AutoCloseable {
 
     /**
      * @param sessionKey
index 1fdef33b56240a960bbf1076d70fa12aaa131d03..9f4429dbc3521d58f2427f8f305f3060cf34a911 100644 (file)
@@ -35,13 +35,13 @@ import org.slf4j.LoggerFactory;
 public class SessionManagerOFImpl implements SessionManager {
 
     protected static final Logger LOG = LoggerFactory.getLogger(SessionManagerOFImpl.class);
-    private static SessionManager instance;
+    private static SessionManagerOFImpl instance;
     private ConcurrentHashMap<SwitchConnectionDistinguisher, SessionContext> sessionLot;
     private Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping;
     private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenerMapping;
 
 
-    protected final ListenerRegistry<SessionListener> sessionListeners = new ListenerRegistry<>();
+    protected ListenerRegistry<SessionListener> sessionListeners;
     private NotificationProviderService notificationProviderService;
 
     private DataProviderService dataProviderService;
@@ -55,9 +55,19 @@ public class SessionManagerOFImpl implements SessionManager {
         }
         return instance;
     }
+    
+    /**
+     * close and release singleton instace
+     */
+    public static synchronized void releaseInstance() {
+        instance.close();
+        instance = null;
+    }
 
     private SessionManagerOFImpl() {
+        LOG.debug("singleton creating");
         sessionLot = new ConcurrentHashMap<>();
+        sessionListeners = new ListenerRegistry<>();
     }
 
     @Override
@@ -157,6 +167,7 @@ public class SessionManagerOFImpl implements SessionManager {
 
     @Override
     public ListenerRegistration<SessionListener> registerSessionListener(SessionListener listener) {
+        LOG.debug("registerSessionListener");
         return sessionListeners.register(listener);
     }
 
@@ -224,5 +235,15 @@ public class SessionManagerOFImpl implements SessionManager {
             Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenerMapping) {
         this.popListenerMapping = popListenerMapping;
     }
-
+    
+    @Override
+    public void close() {
+        LOG.debug("close");
+        sessionListeners = null;
+        synchronized (sessionLot) {
+            for (SessionContext sessionContext : sessionLot.values()) {
+                sessionContext.getPrimaryConductor().disconnect();
+            }
+        }
+    }
 }