Bug 1985 - NPE when running groupbasedpolicy POC demo
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / session / OFSessionUtil.java
index ad48183b63f01bdc1b422e160211527f3d3acb07..697bd7690382875fb5b049d70a4238fb3f78520f 100644 (file)
@@ -9,10 +9,20 @@
 package org.opendaylight.openflowplugin.openflow.md.core.session;
 
 import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
 
 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDestinguisher;
+import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
+import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
+import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
+import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterProvider;
+import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -31,12 +41,14 @@ public abstract class OFSessionUtil {
      */
     public static void registerSession(ConnectionConductor connectionConductor,
             GetFeaturesOutput features, short version) {
-        SwitchConnectionDestinguisher sessionKey = createSwitchSessionKey(features
+        SwitchSessionKeyOF sessionKey = createSwitchSessionKey(features
                 .getDatapathId());
-        SessionContext sessionContext = getSessionManager().getSessionContext(
-                sessionKey);
+        SessionContext sessionContext = getSessionManager().getSessionContext(sessionKey);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("registering sessionKey: {}", Arrays.toString(sessionKey.getId()));
+        }
 
-        if (features.getAuxiliaryId() == 0) {
+        if (features.getAuxiliaryId() == null || features.getAuxiliaryId() == 0) {
             // handle primary
             if (sessionContext != null) {
                 LOG.warn("duplicate datapathId occured while registering new switch session: "
@@ -47,15 +59,18 @@ public abstract class OFSessionUtil {
             SessionContextOFImpl context = new SessionContextOFImpl();
             context.setPrimaryConductor(connectionConductor);
             context.setFeatures(features);
+            context.setSessionKey(sessionKey);
+            context.setSeed((int) System.currentTimeMillis());
+            connectionConductor.setSessionContext(context);
             getSessionManager().addSessionContext(sessionKey, context);
         } else {
             // handle auxiliary
             if (sessionContext == null) {
-                LOG.warn("unexpected auxiliary connection - primary connection missing: "
+                throw new IllegalStateException("unexpected auxiliary connection - primary connection missing: "
                         + dumpDataPathId(features.getDatapathId()));
             } else {
                 // register auxiliary conductor into existing sessionContext
-                SwitchConnectionDestinguisher auxiliaryKey = createConnectionCookie(features);
+                SwitchConnectionDistinguisher auxiliaryKey = createConnectionCookie(features, sessionContext.getSeed());
                 if (sessionContext.getAuxiliaryConductor(auxiliaryKey) != null) {
                     LOG.warn("duplicate datapathId+auxiliary occured while registering switch session: "
                             + dumpDataPathId(features.getDatapathId())
@@ -67,6 +82,18 @@ public abstract class OFSessionUtil {
 
                 sessionContext.addAuxiliaryConductor(auxiliaryKey,
                         connectionConductor);
+                connectionConductor.setSessionContext(sessionContext);
+                connectionConductor.setConnectionCookie(auxiliaryKey);
+            }
+        }
+
+        // check registration result
+        SessionContext resulContext = getSessionManager().getSessionContext(sessionKey);
+        if (resulContext == null) {
+            throw new IllegalStateException("session context registration failed");
+        } else {
+            if (!resulContext.isValid()) {
+                throw new IllegalStateException("registered session context is invalid");
             }
         }
     }
@@ -83,39 +110,37 @@ public abstract class OFSessionUtil {
      * @param datapathId
      * @return new session key
      */
-    public static SwitchConnectionDestinguisher createSwitchSessionKey(
+    public static SwitchSessionKeyOF createSwitchSessionKey(
             BigInteger datapathId) {
-        SwitchSessionKeyOFImpl key = new SwitchSessionKeyOFImpl();
+        SwitchSessionKeyOF key = new SwitchSessionKeyOF();
         key.setDatapathId(datapathId);
-        key.initId();
         return key;
     }
 
     /**
      * @param features
+     * @param seed 
      * @return connection cookie key
-     * @see #createConnectionCookie(BigInteger, short)
+     * @see #createConnectionCookie(BigInteger,short, int)
      */
-    public static SwitchConnectionDestinguisher createConnectionCookie(
-            GetFeaturesOutput features) {
+    public static SwitchConnectionDistinguisher createConnectionCookie(
+            GetFeaturesOutput features, int seed) {
         return createConnectionCookie(features.getDatapathId(),
-                features.getAuxiliaryId());
+                features.getAuxiliaryId(), seed);
     }
 
     /**
      * @param datapathId
      * @param auxiliaryId
+     * @param seed 
      * @return connection cookie key
      */
-    public static SwitchConnectionDestinguisher createConnectionCookie(
-            BigInteger datapathId, short auxiliaryId) {
+    public static SwitchConnectionDistinguisher createConnectionCookie(
+            BigInteger datapathId, short auxiliaryId, int seed) {
         SwitchConnectionCookieOFImpl cookie = null;
-        if (auxiliaryId != 0) {
-            cookie = new SwitchConnectionCookieOFImpl();
-            cookie.setDatapathId(datapathId);
-            cookie.setAuxiliaryId(auxiliaryId);
-            cookie.initId();
-        }
+        cookie = new SwitchConnectionCookieOFImpl();
+        cookie.setAuxiliaryId(auxiliaryId);
+        cookie.init(datapathId.intValue() + seed);
         return cookie;
     }
 
@@ -125,5 +150,33 @@ 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
+    */
+    public static Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMap() {
+        return getSessionManager().getTranslatorMapping();
+    }
+
+    /**
+     * @return pop listener Map
+     */
+    public static Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
+        return getSessionManager().getPopListenerMapping();
+    }
+
+    /**
+     * @return extension converters provider
+     */
+    public static ExtensionConverterProvider getExtensionConvertorProvider() {
+        return getSessionManager().getExtensionConverterProvider();
+    }
 
 }