fix for BUG-709 - connectionCookie generate, use 61/6661/5
authorMichal Rehak <mirehak@cisco.com>
Fri, 2 May 2014 07:47:24 +0000 (09:47 +0200)
committerPrasanna Huddar <prasanna.huddar@ericsson.com>
Tue, 13 May 2014 04:31:07 +0000 (04:31 +0000)
- session cookie is based on datapathId only (not exposed, based in BigInteger)
- connection cookie is based on auxiliaryId, unique hashed key is exposed

Change-Id: Ifc293f876b3e57be38622141e92f8cdb0e47d056
Signed-off-by: Michal Rehak <mirehak@cisco.com>
14 files changed:
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/SwitchConnectionDistinguisher.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/SessionContext.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SessionContextOFImpl.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SessionListener.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
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchConnectionCookieOFImpl.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchSessionKeyOF.java [new file with mode: 0644]
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchSessionKeyOFImpl.java [deleted file]
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/session/MessageDispatchServiceImplTest.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchConnectionCookieOFImplTest.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchSessionKeyOFImplTest.java [deleted file]

index 909093a926b6f13b9bd761a1c9494747bde4d71f..69f960b9695bdf6492ebf6728dddd85a34c6fd9d 100644 (file)
@@ -12,9 +12,10 @@ package org.opendaylight.openflowplugin.openflow.md.core;
  * @author mirehak
  */
 public interface SwitchConnectionDistinguisher {
+    
     /**
      * @return encoded switch session identifier
      */
-    public byte[] getId();
+    public long getCookie();
 
 }
index 84079a8bf915db352bc9e2734006c2d61bcc31b2..e949d9998e86c3b9d08321be9b0c5aa98b3b6f9b 100644 (file)
@@ -8,19 +8,16 @@
 package org.opendaylight.openflowplugin.openflow.md.core.sal;
 
 import java.math.BigInteger;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
 import org.opendaylight.openflowplugin.openflow.md.ModelDrivenSwitch;
-import org.opendaylight.openflowplugin.openflow.md.SwitchInventory;
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionListener;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
+import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOF;
 import org.opendaylight.openflowplugin.openflow.md.lldp.LLDPSpeaker;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdatedBuilder;
@@ -84,7 +81,7 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
     }
 
     @Override
-    public void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
+    public void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context) {
         GetFeaturesOutput features = context.getFeatures();
         BigInteger datapathId = features.getDatapathId();
         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
index e6f3df76446a6bba2bf9f51408a051aedb51191a..9465f985a47566a92c76cbe0cabf54da0ea36bf0 100644 (file)
@@ -40,7 +40,7 @@ public abstract class OFSessionUtil {
      */
     public static void registerSession(ConnectionConductor connectionConductor,
             GetFeaturesOutput features, short version) {
-        SwitchConnectionDistinguisher sessionKey = createSwitchSessionKey(features
+        SwitchSessionKeyOF sessionKey = createSwitchSessionKey(features
                 .getDatapathId());
         SessionContext sessionContext = getSessionManager().getSessionContext(sessionKey);
         if (LOG.isDebugEnabled()) {
@@ -59,6 +59,7 @@ public abstract class OFSessionUtil {
             context.setPrimaryConductor(connectionConductor);
             context.setFeatures(features);
             context.setSessionKey(sessionKey);
+            context.setSeed((int) System.currentTimeMillis());
             connectionConductor.setSessionContext(context);
             context.setValid(true);
             getSessionManager().addSessionContext(sessionKey, context);
@@ -69,7 +70,7 @@ public abstract class OFSessionUtil {
                         + dumpDataPathId(features.getDatapathId()));
             } else {
                 // register auxiliary conductor into existing sessionContext
-                SwitchConnectionDistinguisher 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())
@@ -109,39 +110,37 @@ public abstract class OFSessionUtil {
      * @param datapathId
      * @return new session key
      */
-    public static SwitchConnectionDistinguisher 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)
      */
     public static SwitchConnectionDistinguisher createConnectionCookie(
-            GetFeaturesOutput features) {
+            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 SwitchConnectionDistinguisher createConnectionCookie(
-            BigInteger datapathId, short auxiliaryId) {
+            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;
     }
 
index 67154f0a54eac2ab75b63ead5be05892d5786cfc..82457dacceb8ae749f5066ba2dd094817c553fd1 100644 (file)
@@ -79,7 +79,7 @@ public interface SessionContext {
     /**
      * @return the sessionKey
      */
-    SwitchConnectionDistinguisher getSessionKey();
+    SwitchSessionKeyOF getSessionKey();
 
     /**
      * Returns a map containing all OFPhysicalPorts of this switch.
@@ -161,4 +161,9 @@ public interface SessionContext {
      * @return provider composite registration
      */
     CompositeObjectRegistration<ModelDrivenSwitch> getProviderRegistration();
+    
+    /**
+     * @return seed value for random operations
+     */
+    int getSeed();
 }
index 06f7b4a12263712af86e64f2f643c0110ca4e2e8..9c53224e705cdd872be923ef7412a446bc8ca1a5 100644 (file)
@@ -39,14 +39,15 @@ public class SessionContextOFImpl implements SessionContext {
     private ConnectionConductor primaryConductor;
     private ConcurrentHashMap<SwitchConnectionDistinguisher, ConnectionConductor> auxiliaryConductors;
     private boolean valid;
-    private SwitchConnectionDistinguisher sessionKey;
+    private SwitchSessionKeyOF sessionKey;
     private IMessageDispatchService mdService;
     private final AtomicLong xid;
     private final Map<Long, PortGrouping> physicalPorts;
     private final Map<Long, Boolean> portBandwidth;
-    public static Cache<TransactionKey, Object> bulkTransactionCache = CacheBuilder.newBuilder().expireAfterWrite(10000, TimeUnit.MILLISECONDS).concurrencyLevel(1).build();
+    private Cache<TransactionKey, Object> bulkTransactionCache = CacheBuilder.newBuilder().expireAfterWrite(10000, TimeUnit.MILLISECONDS).concurrencyLevel(1).build();
     private CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration;
-
+    private int seed;
+    
 
     /**
      * default ctor
@@ -127,12 +128,19 @@ public class SessionContextOFImpl implements SessionContext {
     /**
      * @param sessionKey the sessionKey to set
      */
-    public void setSessionKey(SwitchConnectionDistinguisher sessionKey) {
+    public void setSessionKey(SwitchSessionKeyOF sessionKey) {
         this.sessionKey = sessionKey;
     }
+    
+    /**
+     * @param seed the seed to set
+     */
+    public void setSeed(int seed) {
+        this.seed = seed;
+    }
 
     @Override
-    public SwitchConnectionDistinguisher getSessionKey() {
+    public SwitchSessionKeyOF getSessionKey() {
         return sessionKey;
     }
 
@@ -216,4 +224,9 @@ public class SessionContextOFImpl implements SessionContext {
     public CompositeObjectRegistration<ModelDrivenSwitch> getProviderRegistration() {
         return providerRegistration;
     }
+    
+    @Override
+    public int getSeed() {
+        return seed;
+    }
 }
index 16409bac8096aab8b18677c4c518e12243232b42..8c92199dbd92095411ea129131e3c39cb6c70781 100644 (file)
@@ -9,8 +9,6 @@ package org.opendaylight.openflowplugin.openflow.md.core.session;
 
 import java.util.EventListener;
 
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
-
 /**
  * listens on session changes
  */
@@ -21,7 +19,7 @@ public interface SessionListener extends EventListener {
      * @param sessionKey
      * @param context
      */
-    void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context);
+    void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context);
 
     /**
      * fired upon session removed
index 19c9180c8e5b806629dbff3f3f4f051b3483edce..a3204b3b76db26da01d85724d75765db214442ad 100644 (file)
@@ -35,14 +35,14 @@ public interface SessionManager extends AutoCloseable {
      * @return corresponding conductor, holding {@link ConnectionAdapter} to
      *         primary connection
      */
-    public SessionContext getSessionContext(SwitchConnectionDistinguisher sessionKey);
+    public SessionContext getSessionContext(SwitchSessionKeyOF sessionKey);
 
     /**
      * disconnect all underlying {@link ConnectionAdapter}s and notify listeners
      *
      * @param sessionKey
      */
-    public void invalidateSessionContext(SwitchConnectionDistinguisher sessionKey);
+    public void invalidateSessionContext(SwitchSessionKeyOF sessionKey);
 
     /**
      * register session context
@@ -50,7 +50,7 @@ public interface SessionManager extends AutoCloseable {
      * @param sessionKey
      * @param context
      */
-    public void addSessionContext(SwitchConnectionDistinguisher sessionKey, SessionContext context);
+    public void addSessionContext(SwitchSessionKeyOF sessionKey, SessionContext context);
 
     /**
      * disconnect particular auxiliary {@link ConnectionAdapter}, identified by
@@ -59,7 +59,7 @@ public interface SessionManager extends AutoCloseable {
      * @param sessionKey
      * @param connectionCookie
      */
-    public void invalidateAuxiliary(SwitchConnectionDistinguisher sessionKey,
+    public void invalidateAuxiliary(SwitchSessionKeyOF sessionKey,
             SwitchConnectionDistinguisher connectionCookie);
 
     /**
index 0af916d6dcb45c06465816c1fbdaafbe735dd600..0caa5de64c9730fc0cdf026a9a3fd6d0faca628b 100644 (file)
@@ -37,7 +37,7 @@ public class SessionManagerOFImpl implements SessionManager {
 
     protected static final Logger LOG = LoggerFactory.getLogger(SessionManagerOFImpl.class);
     private static SessionManagerOFImpl instance;
-    private ConcurrentHashMap<SwitchConnectionDistinguisher, SessionContext> sessionLot;
+    private ConcurrentHashMap<SwitchSessionKeyOF, SessionContext> sessionLot;
     private Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping;
     private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenerMapping;
 
@@ -72,12 +72,12 @@ public class SessionManagerOFImpl implements SessionManager {
     }
 
     @Override
-    public SessionContext getSessionContext(SwitchConnectionDistinguisher sessionKey) {
+    public SessionContext getSessionContext(SwitchSessionKeyOF sessionKey) {
         return sessionLot.get(sessionKey);
     }
 
     @Override
-    public void invalidateSessionContext(SwitchConnectionDistinguisher sessionKey) {
+    public void invalidateSessionContext(SwitchSessionKeyOF sessionKey) {
         SessionContext context = getSessionContext(sessionKey);
         if (context == null) {
             LOG.warn("context for invalidation not found");
@@ -115,7 +115,7 @@ public class SessionManagerOFImpl implements SessionManager {
     }
 
     @Override
-    public void addSessionContext(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
+    public void addSessionContext(SwitchSessionKeyOF sessionKey, SessionContext context) {
         sessionLot.put(sessionKey, context);
 
         sessionNotifier.onSessionAdded(sessionKey, context);
@@ -123,7 +123,7 @@ public class SessionManagerOFImpl implements SessionManager {
     }
 
     @Override
-    public void invalidateAuxiliary(SwitchConnectionDistinguisher sessionKey,
+    public void invalidateAuxiliary(SwitchSessionKeyOF sessionKey,
             SwitchConnectionDistinguisher connectionCookie) {
         SessionContext context = getSessionContext(sessionKey);
         invalidateAuxiliary(context, connectionCookie, true);
@@ -175,7 +175,7 @@ public class SessionManagerOFImpl implements SessionManager {
     private final SessionListener sessionNotifier = new SessionListener() {
 
         @Override
-        public void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
+        public void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context) {
             for (ListenerRegistration<SessionListener> listener : sessionListeners) {
                 try {
                     listener.getInstance().onSessionAdded(sessionKey, context);
index 992d7cf011d1fac83465e852f87eca39aeb9a566..fa84ea03509b073df53b775e659b8be929448550 100644 (file)
@@ -10,26 +10,32 @@ package org.opendaylight.openflowplugin.openflow.md.core.session;
 
 import java.security.MessageDigest;
 
+import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
+
+import com.google.common.hash.HashFunction;
+import com.google.common.hash.Hasher;
+import com.google.common.hash.Hashing;
+
 /**
  * @author mirehak
  */
-public class SwitchConnectionCookieOFImpl extends SwitchSessionKeyOFImpl {
+public class SwitchConnectionCookieOFImpl implements SwitchConnectionDistinguisher {
 
     private short auxiliaryId;
+    private long cookie;
 
     /**
-     * @param encodedId
-     * @see {@link SwitchSessionKeyOFImpl#SwitchSessionKeyOFImpl(byte[])}
+     * @param cookie
      */
-    public SwitchConnectionCookieOFImpl(byte[] encodedId) {
-        super(encodedId);
+    public SwitchConnectionCookieOFImpl(long cookie) {
+        this.cookie = cookie;
     }
 
     /**
      * default ctor
      */
     public SwitchConnectionCookieOFImpl() {
-        // do nothing
+        // NOOP
     }
 
     /**
@@ -39,11 +45,56 @@ public class SwitchConnectionCookieOFImpl extends SwitchSessionKeyOFImpl {
     public void setAuxiliaryId(short auxiliaryId) {
         this.auxiliaryId = auxiliaryId;
     }
+    
+    /**
+     * compute pseudorandom key unique for given seed and {@link #auxiliaryId}
+     * @param seed random int but fixed per session
+     */
+    public void init(int seed) {
+        if (auxiliaryId <= 0) {
+            throw new IllegalStateException("auxiliaryId must be greater than 0");
+        }
+        
+        HashFunction mm32Hf = Hashing.murmur3_32(seed);
+        Hasher hasher = mm32Hf.newHasher(8);
+        hasher.putInt(auxiliaryId);
+        long hash = 0xFFFFFFFFL & hasher.hash().asInt();
+        cookie = (auxiliaryId << 24) | (hash >> 8);
+    }
+    
+    @Override
+    public long getCookie() {
+        return cookie;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + (int) (cookie ^ (cookie >>> 32));
+        return result;
+    }
 
+    /* (non-Javadoc)
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
     @Override
-    protected void extend(MessageDigest medi) {
-        super.extend(medi);
-        medi.update(new byte[] { (byte) (auxiliaryId >> 8), (byte) auxiliaryId });
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        SwitchConnectionCookieOFImpl other = (SwitchConnectionCookieOFImpl) obj;
+        if (cookie != other.cookie)
+            return false;
+        return true;
     }
+    
+    
 
 }
diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchSessionKeyOF.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchSessionKeyOF.java
new file mode 100644 (file)
index 0000000..3d24a67
--- /dev/null
@@ -0,0 +1,66 @@
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.openflow.md.core.session;
+
+import java.math.BigInteger;
+
+/**
+ * @author mirehak
+ */
+public class SwitchSessionKeyOF {
+
+    private BigInteger datapathId;
+
+    /**
+     * default ctor
+     */
+    public SwitchSessionKeyOF() {
+        // NOOP
+    }
+    
+    /**
+     * @param datapathId the datapathId to set
+     */
+    public void setDatapathId(BigInteger datapathId) {
+        this.datapathId = datapathId;
+    }
+    
+    /**
+     * @return the datapathId
+     */
+    public byte[] getId() {
+        return datapathId.toByteArray();
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result
+                + ((datapathId == null) ? 0 : datapathId.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        SwitchSessionKeyOF other = (SwitchSessionKeyOF) obj;
+        if (datapathId == null) {
+            if (other.datapathId != null)
+                return false;
+        } else if (!datapathId.equals(other.datapathId))
+            return false;
+        return true;
+    }
+}
diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchSessionKeyOFImpl.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchSessionKeyOFImpl.java
deleted file mode 100644 (file)
index 50e7c69..0000000
+++ /dev/null
@@ -1,101 +0,0 @@
-/**
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.openflowplugin.openflow.md.core.session;
-
-import java.math.BigInteger;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
-
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
-
-/**
- * @author mirehak
- */
-public class SwitchSessionKeyOFImpl implements SwitchConnectionDistinguisher {
-
-    protected byte[] encodedId;
-    private BigInteger datapathId;
-
-    /**
-     * default ctor
-     */
-    public SwitchSessionKeyOFImpl() {
-        // do nothing
-    }
-
-    /**
-     * Special constructor for situation where no datapathId available, do not
-     * call {@link #initId()} on this instance otherwise id will be overwritten.
-     *
-     * @param encodedId
-     */
-    public SwitchSessionKeyOFImpl(byte[] encodedId) {
-        this.encodedId = encodedId;
-    }
-
-    @Override
-    public byte[] getId() {
-        return encodedId;
-    }
-
-    /**
-     * @param datapathId
-     *            the datapathId to set
-     */
-    public void setDatapathId(BigInteger datapathId) {
-        this.datapathId = datapathId;
-    }
-
-    /**
-     * compute and set {@link #encodedId} based on {@link #datapathId} and
-     * {@link #auxiliaryId}
-     */
-    public void initId() {
-        try {
-            MessageDigest medi = MessageDigest.getInstance("sha-1");
-            extend(medi);
-            encodedId = medi.digest();
-        } catch (NoSuchAlgorithmException | NullPointerException e) {
-            throw new IllegalArgumentException("can not proceed datapathId: "
-                    + datapathId);
-        }
-    }
-
-    /**
-     * extend the content the hash sum is computed from
-     * @param medi
-     */
-    protected void extend(MessageDigest medi) {
-        medi.update(datapathId.toByteArray());
-    }
-
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + Arrays.hashCode(encodedId);
-        return result;
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj)
-            return true;
-        if (obj == null)
-            return false;
-        if (getClass() != obj.getClass())
-            return false;
-        SwitchSessionKeyOFImpl other = (SwitchSessionKeyOFImpl) obj;
-        if (!Arrays.equals(encodedId, other.encodedId))
-            return false;
-        return true;
-    }
-
-}
index 6edf1be74ff0c69f0a532b4628a5c3e80351dfbd..97e8aba2382f2e0d4e8d8f16c7924e22b48ecf5b 100644 (file)
@@ -184,6 +184,7 @@ class MockSessionContext implements SessionContext {
     private IMessageDispatchService messageService;
     private boolean isValid = true;
     private CompositeObjectRegistration<ModelDrivenSwitch> registration;
+    private int seed;
 
     MockSessionContext(int conductorNum) {
         conductor = new MockConnectionConductor(conductorNum);
@@ -237,7 +238,7 @@ class MockSessionContext implements SessionContext {
     }
 
     @Override
-    public SwitchConnectionDistinguisher getSessionKey() {
+    public SwitchSessionKeyOF getSessionKey() {
         // TODO Auto-generated method stub
         return null;
     }
@@ -317,6 +318,18 @@ class MockSessionContext implements SessionContext {
             CompositeObjectRegistration<ModelDrivenSwitch> registration) {
         this.registration = registration;
     }
+
+    @Override
+    public int getSeed() {
+        return seed;
+    }
+    
+    /**
+     * @param seed the seed to set
+     */
+    public void setSeed(int seed) {
+        this.seed = seed;
+    }
 }
 
 class MockConnectionConductor implements ConnectionConductor {
@@ -381,9 +394,8 @@ class MockConnectionConductor implements ConnectionConductor {
     public SwitchConnectionDistinguisher getAuxiliaryKey() {
         if (0 != conductorNum) {
             SwitchConnectionCookieOFImpl key = new SwitchConnectionCookieOFImpl();
-            key.setDatapathId(BigInteger.valueOf(10L));
             key.setAuxiliaryId((short) conductorNum);
-            key.initId();
+            key.init(42);
             return key;
         }
         return null;
index ac7930d6f77e17f5efd28c5654aefe7a6611ffd8..7aba276ec86f36e1da7256e80a764e49c71e6da2 100644 (file)
@@ -8,8 +8,6 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core.session;
 
-import java.math.BigInteger;
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -30,22 +28,23 @@ public class SwitchConnectionCookieOFImplTest {
 
     private SwitchConnectionCookieOFImpl switchConnectionKey;
 
+    private int seed;
+
     /**
      * @throws java.lang.Exception
      */
     @Before
     public void setUp() throws Exception {
-        switchConnectionKey = createSwitchSessionKey("a1a2a3a4a5a6", (short) 42);
+        seed = 4242;
+        switchConnectionKey = createSwitchSessionKey((short) 42);
     }
 
     /**
      * @param datapathId
      * @return
      */
-    private static SwitchConnectionCookieOFImpl createSwitchSessionKey(
-            String datapathId, short auxiliary) {
+    private static SwitchConnectionCookieOFImpl createSwitchSessionKey(short auxiliary) {
         SwitchConnectionCookieOFImpl key = new SwitchConnectionCookieOFImpl();
-        key.setDatapathId(new BigInteger(datapathId, 16));
         key.setAuxiliaryId(auxiliary);
         return key;
     }
@@ -57,11 +56,10 @@ public class SwitchConnectionCookieOFImplTest {
      */
     @Test
     public void testGetId() {
-        switchConnectionKey.initId();
-        LOG.debug("testKey.id: " + Arrays.toString(switchConnectionKey.getId()));
-        byte[] expected = new byte[] { 32, 9, -69, 43, -68, -53, 49, 105, -109,
-                -15, -20, 50, -121, -34, -22, 3, 27, 42, 90, -59 };
-        Assert.assertArrayEquals(expected, switchConnectionKey.getId());
+        switchConnectionKey.init(seed);
+        LOG.debug("testKey.id: " + Long.toHexString(switchConnectionKey.getCookie()));
+        long expected = 710033450L;
+        Assert.assertEquals(expected, switchConnectionKey.getCookie());
     }
 
     /**
@@ -72,8 +70,8 @@ public class SwitchConnectionCookieOFImplTest {
     @Test
     public void testInitId1() {
         try {
-            switchConnectionKey.setDatapathId(null);
-            switchConnectionKey.initId();
+            switchConnectionKey.setAuxiliaryId((short) 0);
+            switchConnectionKey.init(seed);
             Assert.fail("init should fail with no datapathId");
         } catch (Exception e) {
             // expected
@@ -90,20 +88,16 @@ public class SwitchConnectionCookieOFImplTest {
     @Test
     public void testHashAndEquals() {
         // insert equal keys
-        SwitchConnectionCookieOFImpl key1 = createSwitchSessionKey("1234567890",
-                (short) 42);
-        key1.initId();
+        SwitchConnectionCookieOFImpl key1 = createSwitchSessionKey((short) 42);
+        key1.init(seed);
 
-        SwitchConnectionCookieOFImpl key2 = createSwitchSessionKey("1234567890",
-                (short) 42);
-        key2.initId();
+        SwitchConnectionCookieOFImpl key2 = createSwitchSessionKey((short) 42);
+        key2.init(seed);
 
-        SwitchConnectionCookieOFImpl key3 = createSwitchSessionKey("123456789",
-                (short) 42);
-        key3.initId();
-        SwitchConnectionCookieOFImpl key4 = createSwitchSessionKey("123456789",
-                (short) 21);
-        key4.initId();
+        SwitchConnectionCookieOFImpl key3 = createSwitchSessionKey((short) 43);
+        key3.init(seed);
+        SwitchConnectionCookieOFImpl key4 = createSwitchSessionKey((short) 21);
+        key4.init(seed);
 
         Map<SwitchConnectionDistinguisher, Integer> keyLot = new HashMap<>();
         keyLot.put(key1, System.identityHashCode(key1));
@@ -126,21 +120,18 @@ public class SwitchConnectionCookieOFImplTest {
                 .intValue());
 
         // lookup using not inited key
-        SwitchConnectionCookieOFImpl keyWithoutInit = createSwitchSessionKey(
-                "123456789", (short) 42);
+        SwitchConnectionCookieOFImpl keyWithoutInit = createSwitchSessionKey((short) 42);
         Assert.assertNull(keyLot.get(keyWithoutInit));
 
         // creating brand new key and lookup
-        SwitchConnectionCookieOFImpl keyWithInit = createSwitchSessionKey(
-                "123456789", (short) 42);
-        keyWithInit.initId();
+        SwitchConnectionCookieOFImpl keyWithInit = createSwitchSessionKey((short) 43);
+        keyWithInit.init(seed);
         Assert.assertEquals(System.identityHashCode(key3),
                 keyLot.get(keyWithInit).intValue());
 
         // lookup with key containing encoded part only
-        LOG.debug("key3.id: " + Arrays.toString(key3.getId()));
-        SwitchConnectionCookieOFImpl keyWithoutDPID = new SwitchConnectionCookieOFImpl(
-                new byte[] { -12, -121, -45, -16, 98, 33, -19, -66, -93, -46, -52, 79, -13, -116, -97, 0, 121, 78, -104, 29 });
+        LOG.debug("key3.id: " + Long.toHexString(key3.getCookie()));
+        SwitchConnectionCookieOFImpl keyWithoutDPID = new SwitchConnectionCookieOFImpl(734546075L);
         Assert.assertEquals(System.identityHashCode(key3),
                 keyLot.get(keyWithoutDPID).intValue());
     }
diff --git a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchSessionKeyOFImplTest.java b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/session/SwitchSessionKeyOFImplTest.java
deleted file mode 100644 (file)
index 408af7d..0000000
+++ /dev/null
@@ -1,137 +0,0 @@
-/**
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.openflowplugin.openflow.md.core.session;
-
-import java.math.BigInteger;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * @author mirehak
- */
-public class SwitchSessionKeyOFImplTest {
-
-    private static final Logger LOG = LoggerFactory
-            .getLogger(SwitchSessionKeyOFImplTest.class);
-
-    private SwitchSessionKeyOFImpl switchConnectionKey;
-
-    /**
-     * @throws java.lang.Exception
-     */
-    @Before
-    public void setUp() throws Exception {
-        switchConnectionKey = createSwitchSessionKey("a1a2a3a4a5a6");
-    }
-
-    /**
-     * @param datapathId
-     * @return
-     */
-    private static SwitchSessionKeyOFImpl createSwitchSessionKey(
-            String datapathId) {
-        SwitchSessionKeyOFImpl key = new SwitchSessionKeyOFImpl();
-        key.setDatapathId(new BigInteger(datapathId, 16));
-        return key;
-    }
-
-    /**
-     * Test method for
-     * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#getId()}
-     * .
-     */
-    @Test
-    public void testGetId() {
-        switchConnectionKey.initId();
-        LOG.debug("testKey.id: " + Arrays.toString(switchConnectionKey.getId()));
-        byte[] expected = new byte[] { -128, 17, -128, 123, -110, 55, 126, 122,
-                -81, 69, 47, -29, -70, -41, 0, -24, 60, 73, 9, 19 };
-        Assert.assertArrayEquals(expected, switchConnectionKey.getId());
-    }
-
-    /**
-     * Test method for
-     * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#initUUID()}
-     * .
-     */
-    @Test
-    public void testInitId1() {
-        try {
-            switchConnectionKey.setDatapathId(null);
-            switchConnectionKey.initId();
-            Assert.fail("init should fail with no datapathId");
-        } catch (Exception e) {
-            // expected
-        }
-    }
-
-    /**
-     * Test method for
-     * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#equals(Object)}
-     * ,
-     * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#hashCode()}
-     * .
-     */
-    @Test
-    public void testHashAndEquals() {
-        // insert equal keys
-        SwitchSessionKeyOFImpl key1 = createSwitchSessionKey("1234567890");
-        key1.initId();
-
-        SwitchSessionKeyOFImpl key2 = createSwitchSessionKey("1234567890");
-        key2.initId();
-
-        SwitchSessionKeyOFImpl key3 = createSwitchSessionKey("123456789");
-        key3.initId();
-
-        Map<SwitchConnectionDistinguisher, Integer> keyLot = new HashMap<>();
-        keyLot.put(key1, System.identityHashCode(key1));
-        Assert.assertEquals(1, keyLot.size());
-        keyLot.put(key2, System.identityHashCode(key2));
-        Assert.assertEquals(1, keyLot.size());
-        keyLot.put(key3, System.identityHashCode(key3));
-        Assert.assertEquals(2, keyLot.size());
-
-        // lookup using inited key
-        Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key1)
-                .intValue());
-        Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key2)
-                .intValue());
-        Assert.assertEquals(System.identityHashCode(key3), keyLot.get(key3)
-                .intValue());
-
-        // lookup using not inited key
-        SwitchSessionKeyOFImpl keyWithoutInit = createSwitchSessionKey("123456789");
-        Assert.assertNull(keyLot.get(keyWithoutInit));
-
-        // creating brand new key and lookup
-        SwitchSessionKeyOFImpl keyWithInit = createSwitchSessionKey("123456789");
-        keyWithInit.initId();
-
-        Assert.assertEquals(System.identityHashCode(key3),
-                keyLot.get(keyWithInit).intValue());
-
-        // lookup with key containing encoded part only
-        LOG.debug("key3.id: " + Arrays.toString(key3.getId()));
-        SwitchSessionKeyOFImpl keyWithoutDPID = new SwitchSessionKeyOFImpl(
-                new byte[] { -106, 12, 30, 77, 23, -44, -116, -11, -49, 40,
-                        -122, 5, -82, -33, 81, -65, 100, 51, 34, 76 });
-        Assert.assertEquals(System.identityHashCode(key3),
-                keyLot.get(keyWithoutDPID).intValue());
-    }
-
-}