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.SwitchConnectionDestinguisher;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
26 public class SwitchConnectionCookieOFImplTest {
28 private static final Logger LOG = LoggerFactory
29 .getLogger(SwitchConnectionCookieOFImplTest.class);
31 private SwitchConnectionCookieOFImpl switchConnectionKey;
34 * @throws java.lang.Exception
37 public void setUp() throws Exception {
38 switchConnectionKey = createSwitchSessionKey("a1a2a3a4a5a6", (short) 42);
45 private static SwitchConnectionCookieOFImpl createSwitchSessionKey(
46 String datapathId, short auxiliary) {
47 SwitchConnectionCookieOFImpl key = new SwitchConnectionCookieOFImpl();
48 key.setDatapathId(new BigInteger(datapathId, 16));
49 key.setAuxiliaryId(auxiliary);
55 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#getId()}
59 public void testGetId() {
60 switchConnectionKey.initId();
61 LOG.debug("testKey.id: " + Arrays.toString(switchConnectionKey.getId()));
62 byte[] expected = new byte[] { 32, 9, -69, 43, -68, -53, 49, 105, -109,
63 -15, -20, 50, -121, -34, -22, 3, 27, 42, 90, -59 };
64 Assert.assertArrayEquals(expected, switchConnectionKey.getId());
69 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#initUUID()}
73 public void testInitId1() {
75 switchConnectionKey.setDatapathId(null);
76 switchConnectionKey.initId();
77 Assert.fail("init should fail with no datapathId");
78 } catch (Exception e) {
85 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#equals(Object)}
87 * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#hashCode()}
91 public void testHashAndEquals() {
93 SwitchConnectionCookieOFImpl key1 = createSwitchSessionKey("1234567890",
97 SwitchConnectionCookieOFImpl key2 = createSwitchSessionKey("1234567890",
101 SwitchConnectionCookieOFImpl key3 = createSwitchSessionKey("123456789",
104 SwitchConnectionCookieOFImpl key4 = createSwitchSessionKey("123456789",
108 Map<SwitchConnectionDestinguisher, Integer> keyLot = new HashMap<>();
109 keyLot.put(key1, System.identityHashCode(key1));
110 Assert.assertEquals(1, keyLot.size());
111 keyLot.put(key2, System.identityHashCode(key2));
112 Assert.assertEquals(1, keyLot.size());
113 keyLot.put(key3, System.identityHashCode(key3));
114 Assert.assertEquals(2, keyLot.size());
115 keyLot.put(key4, System.identityHashCode(key4));
116 Assert.assertEquals(3, keyLot.size());
118 // lookup using inited key
119 Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key1)
121 Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key2)
123 Assert.assertEquals(System.identityHashCode(key3), keyLot.get(key3)
125 Assert.assertEquals(System.identityHashCode(key4), keyLot.get(key4)
128 // lookup using not inited key
129 SwitchConnectionCookieOFImpl keyWithoutInit = createSwitchSessionKey(
130 "123456789", (short) 42);
131 Assert.assertNull(keyLot.get(keyWithoutInit));
133 // creating brand new key and lookup
134 SwitchConnectionCookieOFImpl keyWithInit = createSwitchSessionKey(
135 "123456789", (short) 42);
136 keyWithInit.initId();
137 Assert.assertEquals(System.identityHashCode(key3),
138 keyLot.get(keyWithInit).intValue());
140 // lookup with key containing encoded part only
141 LOG.debug("key3.id: " + Arrays.toString(key3.getId()));
142 SwitchConnectionCookieOFImpl keyWithoutDPID = new SwitchConnectionCookieOFImpl(
143 new byte[] { -12, -121, -45, -16, 98, 33, -19, -66, -93, -46, -52, 79, -13, -116, -97, 0, 121, 78, -104, 29 });
144 Assert.assertEquals(System.identityHashCode(key3),
145 keyLot.get(keyWithoutDPID).intValue());