b45e7b46f325bddb19cb4205861b97a81b1848b6
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / session / SwitchConnectionCookieOFImplTest.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core.session;
10
11 import java.math.BigInteger;
12 import java.util.Arrays;
13 import java.util.HashMap;
14 import java.util.Map;
15
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;
22
23 /**
24  * @author mirehak
25  */
26 public class SwitchConnectionCookieOFImplTest {
27
28     private static final Logger LOG = LoggerFactory
29             .getLogger(SwitchConnectionCookieOFImplTest.class);
30
31     private SwitchConnectionCookieOFImpl switchConnectionKey;
32
33     /**
34      * @throws java.lang.Exception
35      */
36     @Before
37     public void setUp() throws Exception {
38         switchConnectionKey = createSwitchSessionKey("a1a2a3a4a5a6", (short) 42);
39     }
40
41     /**
42      * @param datapathId
43      * @return
44      */
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);
50         return key;
51     }
52
53     /**
54      * Test method for
55      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#getId()}
56      * .
57      */
58     @Test
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());
65     }
66
67     /**
68      * Test method for
69      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#initUUID()}
70      * .
71      */
72     @Test
73     public void testInitId1() {
74         try {
75             switchConnectionKey.setDatapathId(null);
76             switchConnectionKey.initId();
77             Assert.fail("init should fail with no datapathId");
78         } catch (Exception e) {
79             // expected
80         }
81     }
82
83     /**
84      * Test method for
85      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#equals(Object)}
86      * ,
87      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#hashCode()}
88      * .
89      */
90     @Test
91     public void testHashAndEquals() {
92         // insert equal keys
93         SwitchConnectionCookieOFImpl key1 = createSwitchSessionKey("1234567890",
94                 (short) 42);
95         key1.initId();
96
97         SwitchConnectionCookieOFImpl key2 = createSwitchSessionKey("1234567890",
98                 (short) 42);
99         key2.initId();
100
101         SwitchConnectionCookieOFImpl key3 = createSwitchSessionKey("123456789",
102                 (short) 42);
103         key3.initId();
104         SwitchConnectionCookieOFImpl key4 = createSwitchSessionKey("123456789",
105                 (short) 21);
106         key4.initId();
107
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());
117
118         // lookup using inited key
119         Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key1)
120                 .intValue());
121         Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key2)
122                 .intValue());
123         Assert.assertEquals(System.identityHashCode(key3), keyLot.get(key3)
124                 .intValue());
125         Assert.assertEquals(System.identityHashCode(key4), keyLot.get(key4)
126                 .intValue());
127
128         // lookup using not inited key
129         SwitchConnectionCookieOFImpl keyWithoutInit = createSwitchSessionKey(
130                 "123456789", (short) 42);
131         Assert.assertNull(keyLot.get(keyWithoutInit));
132
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());
139
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());
146     }
147
148 }