* @author mirehak
*/
public interface SwitchConnectionDistinguisher {
+
/**
* @return encoded switch session identifier
*/
- public byte[] getId();
+ public long getCookie();
}
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;
}
@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);
*/
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()) {
context.setPrimaryConductor(connectionConductor);
context.setFeatures(features);
context.setSessionKey(sessionKey);
+ context.setSeed((int) System.currentTimeMillis());
connectionConductor.setSessionContext(context);
context.setValid(true);
getSessionManager().addSessionContext(sessionKey, context);
+ 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())
* @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;
}
/**
* @return the sessionKey
*/
- SwitchConnectionDistinguisher getSessionKey();
+ SwitchSessionKeyOF getSessionKey();
/**
* Returns a map containing all OFPhysicalPorts of this switch.
* @return provider composite registration
*/
CompositeObjectRegistration<ModelDrivenSwitch> getProviderRegistration();
+
+ /**
+ * @return seed value for random operations
+ */
+ int getSeed();
}
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
/**
* @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;
}
public CompositeObjectRegistration<ModelDrivenSwitch> getProviderRegistration() {
return providerRegistration;
}
+
+ @Override
+ public int getSeed() {
+ return seed;
+ }
}
import java.util.EventListener;
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
-
/**
* listens on session changes
*/
* @param sessionKey
* @param context
*/
- void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context);
+ void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context);
/**
* fired upon session removed
* @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
* @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
* @param sessionKey
* @param connectionCookie
*/
- public void invalidateAuxiliary(SwitchConnectionDistinguisher sessionKey,
+ public void invalidateAuxiliary(SwitchSessionKeyOF sessionKey,
SwitchConnectionDistinguisher connectionCookie);
/**
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;
}
@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");
}
@Override
- public void addSessionContext(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
+ public void addSessionContext(SwitchSessionKeyOF sessionKey, SessionContext context) {
sessionLot.put(sessionKey, context);
sessionNotifier.onSessionAdded(sessionKey, context);
}
@Override
- public void invalidateAuxiliary(SwitchConnectionDistinguisher sessionKey,
+ public void invalidateAuxiliary(SwitchSessionKeyOF sessionKey,
SwitchConnectionDistinguisher connectionCookie) {
SessionContext context = getSessionContext(sessionKey);
invalidateAuxiliary(context, connectionCookie, true);
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);
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
}
/**
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;
}
+
+
}
--- /dev/null
+/**
+ * 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;
+ }
+}
+++ /dev/null
-/**
- * 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;
- }
-
-}
private IMessageDispatchService messageService;
private boolean isValid = true;
private CompositeObjectRegistration<ModelDrivenSwitch> registration;
+ private int seed;
MockSessionContext(int conductorNum) {
conductor = new MockConnectionConductor(conductorNum);
}
@Override
- public SwitchConnectionDistinguisher getSessionKey() {
+ public SwitchSessionKeyOF getSessionKey() {
// TODO Auto-generated method stub
return null;
}
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 {
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;
package org.opendaylight.openflowplugin.openflow.md.core.session;
-import java.math.BigInteger;
-import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
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;
}
*/
@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());
}
/**
@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
@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));
.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());
}
+++ /dev/null
-/**
- * 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());
- }
-
-}