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.math.BigInteger;
12 import java.util.Arrays;
13 import java.util.HashMap;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
26 public class SwitchSessionKeyOFImplTest {
28 private static final Logger LOG = LoggerFactory
29 .getLogger(SwitchSessionKeyOFImplTest.class);
31 private SwitchSessionKeyOFImpl switchConnectionKey;
34 * @throws java.lang.Exception
37 public void setUp() throws Exception {
38 switchConnectionKey = createSwitchSessionKey("a1a2a3a4a5a6");
45 private static SwitchSessionKeyOFImpl createSwitchSessionKey(
47 SwitchSessionKeyOFImpl key = new SwitchSessionKeyOFImpl();
48 key.setDatapathId(new BigInteger(datapathId, 16));
54 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#getId()}
58 public void testGetId() {
59 switchConnectionKey.initId();
60 LOG.debug("testKey.id: " + Arrays.toString(switchConnectionKey.getId()));
61 byte[] expected = new byte[] { -128, 17, -128, 123, -110, 55, 126, 122,
62 -81, 69, 47, -29, -70, -41, 0, -24, 60, 73, 9, 19 };
63 Assert.assertArrayEquals(expected, switchConnectionKey.getId());
68 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#initUUID()}
72 public void testInitId1() {
74 switchConnectionKey.setDatapathId(null);
75 switchConnectionKey.initId();
76 Assert.fail("init should fail with no datapathId");
77 } catch (Exception e) {
84 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#equals(Object)}
86 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#hashCode()}
90 public void testHashAndEquals() {
92 SwitchSessionKeyOFImpl key1 = createSwitchSessionKey("1234567890");
95 SwitchSessionKeyOFImpl key2 = createSwitchSessionKey("1234567890");
98 SwitchSessionKeyOFImpl key3 = createSwitchSessionKey("123456789");
101 Map<SwitchConnectionDistinguisher, Integer> keyLot = new HashMap<>();
102 keyLot.put(key1, System.identityHashCode(key1));
103 Assert.assertEquals(1, keyLot.size());
104 keyLot.put(key2, System.identityHashCode(key2));
105 Assert.assertEquals(1, keyLot.size());
106 keyLot.put(key3, System.identityHashCode(key3));
107 Assert.assertEquals(2, keyLot.size());
109 // lookup using inited key
110 Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key1)
112 Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key2)
114 Assert.assertEquals(System.identityHashCode(key3), keyLot.get(key3)
117 // lookup using not inited key
118 SwitchSessionKeyOFImpl keyWithoutInit = createSwitchSessionKey("123456789");
119 Assert.assertNull(keyLot.get(keyWithoutInit));
121 // creating brand new key and lookup
122 SwitchSessionKeyOFImpl keyWithInit = createSwitchSessionKey("123456789");
123 keyWithInit.initId();
125 Assert.assertEquals(System.identityHashCode(key3),
126 keyLot.get(keyWithInit).intValue());
128 // lookup with key containing encoded part only
129 LOG.debug("key3.id: " + Arrays.toString(key3.getId()));
130 SwitchSessionKeyOFImpl keyWithoutDPID = new SwitchSessionKeyOFImpl(
131 new byte[] { -106, 12, 30, 77, 23, -44, -116, -11, -49, 40,
132 -122, 5, -82, -33, 81, -65, 100, 51, 34, 76 });
133 Assert.assertEquals(System.identityHashCode(key3),
134 keyLot.get(keyWithoutDPID).intValue());