2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.openflow.md.core.session;
11 import java.util.HashMap;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
24 public class SwitchConnectionCookieOFImplTest {
26 private static final Logger LOG = LoggerFactory
27 .getLogger(SwitchConnectionCookieOFImplTest.class);
29 private SwitchConnectionCookieOFImpl switchConnectionKey;
34 * @throws java.lang.Exception
37 public void setUp() throws Exception {
39 switchConnectionKey = createSwitchSessionKey((short) 42);
46 private static SwitchConnectionCookieOFImpl createSwitchSessionKey(short auxiliary) {
47 SwitchConnectionCookieOFImpl key = new SwitchConnectionCookieOFImpl();
48 key.setAuxiliaryId(auxiliary);
54 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#getId()}
58 public void testGetId() {
59 switchConnectionKey.init(seed);
60 LOG.debug("testKey.id: " + Long.toHexString(switchConnectionKey.getCookie()));
61 long expected = 710033450L;
62 Assert.assertEquals(expected, switchConnectionKey.getCookie());
67 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#initUUID()}
71 public void testInitId1() {
73 switchConnectionKey.setAuxiliaryId((short) 0);
74 switchConnectionKey.init(seed);
75 Assert.fail("init should fail with no datapathId");
76 } catch (Exception e) {
83 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#equals(Object)}
85 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#hashCode()}
89 public void testHashAndEquals() {
91 SwitchConnectionCookieOFImpl key1 = createSwitchSessionKey((short) 42);
94 SwitchConnectionCookieOFImpl key2 = createSwitchSessionKey((short) 42);
97 SwitchConnectionCookieOFImpl key3 = createSwitchSessionKey((short) 43);
99 SwitchConnectionCookieOFImpl key4 = createSwitchSessionKey((short) 21);
102 Map<SwitchConnectionDistinguisher, Integer> keyLot = new HashMap<>();
103 keyLot.put(key1, System.identityHashCode(key1));
104 Assert.assertEquals(1, keyLot.size());
105 keyLot.put(key2, System.identityHashCode(key2));
106 Assert.assertEquals(1, keyLot.size());
107 keyLot.put(key3, System.identityHashCode(key3));
108 Assert.assertEquals(2, keyLot.size());
109 keyLot.put(key4, System.identityHashCode(key4));
110 Assert.assertEquals(3, keyLot.size());
112 // lookup using inited key
113 Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key1)
115 Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key2)
117 Assert.assertEquals(System.identityHashCode(key3), keyLot.get(key3)
119 Assert.assertEquals(System.identityHashCode(key4), keyLot.get(key4)
122 // lookup using not inited key
123 SwitchConnectionCookieOFImpl keyWithoutInit = createSwitchSessionKey((short) 42);
124 Assert.assertNull(keyLot.get(keyWithoutInit));
126 // creating brand new key and lookup
127 SwitchConnectionCookieOFImpl keyWithInit = createSwitchSessionKey((short) 43);
128 keyWithInit.init(seed);
129 Assert.assertEquals(System.identityHashCode(key3),
130 keyLot.get(keyWithInit).intValue());
132 // lookup with key containing encoded part only
133 LOG.debug("key3.id: " + Long.toHexString(key3.getCookie()));
134 SwitchConnectionCookieOFImpl keyWithoutDPID = new SwitchConnectionCookieOFImpl(734546075L);
135 Assert.assertEquals(System.identityHashCode(key3),
136 keyLot.get(keyWithoutDPID).intValue());