Merge "BUG-985 - finisher queue is unbound"
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / session / SwitchSessionKeyOFImplTest.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.SwitchConnectionDistinguisher;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * @author mirehak
25  */
26 public class SwitchSessionKeyOFImplTest {
27
28     private static final Logger LOG = LoggerFactory
29             .getLogger(SwitchSessionKeyOFImplTest.class);
30
31     private SwitchSessionKeyOFImpl switchConnectionKey;
32
33     /**
34      * @throws java.lang.Exception
35      */
36     @Before
37     public void setUp() throws Exception {
38         switchConnectionKey = createSwitchSessionKey("a1a2a3a4a5a6");
39     }
40
41     /**
42      * @param datapathId
43      * @return
44      */
45     private static SwitchSessionKeyOFImpl createSwitchSessionKey(
46             String datapathId) {
47         SwitchSessionKeyOFImpl key = new SwitchSessionKeyOFImpl();
48         key.setDatapathId(new BigInteger(datapathId, 16));
49         return key;
50     }
51
52     /**
53      * Test method for
54      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#getId()}
55      * .
56      */
57     @Test
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());
64     }
65
66     /**
67      * Test method for
68      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#initUUID()}
69      * .
70      */
71     @Test
72     public void testInitId1() {
73         try {
74             switchConnectionKey.setDatapathId(null);
75             switchConnectionKey.initId();
76             Assert.fail("init should fail with no datapathId");
77         } catch (Exception e) {
78             // expected
79         }
80     }
81
82     /**
83      * Test method for
84      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#equals(Object)}
85      * ,
86      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOFImpl#hashCode()}
87      * .
88      */
89     @Test
90     public void testHashAndEquals() {
91         // insert equal keys
92         SwitchSessionKeyOFImpl key1 = createSwitchSessionKey("1234567890");
93         key1.initId();
94
95         SwitchSessionKeyOFImpl key2 = createSwitchSessionKey("1234567890");
96         key2.initId();
97
98         SwitchSessionKeyOFImpl key3 = createSwitchSessionKey("123456789");
99         key3.initId();
100
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());
108
109         // lookup using inited key
110         Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key1)
111                 .intValue());
112         Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key2)
113                 .intValue());
114         Assert.assertEquals(System.identityHashCode(key3), keyLot.get(key3)
115                 .intValue());
116
117         // lookup using not inited key
118         SwitchSessionKeyOFImpl keyWithoutInit = createSwitchSessionKey("123456789");
119         Assert.assertNull(keyLot.get(keyWithoutInit));
120
121         // creating brand new key and lookup
122         SwitchSessionKeyOFImpl keyWithInit = createSwitchSessionKey("123456789");
123         keyWithInit.initId();
124
125         Assert.assertEquals(System.identityHashCode(key3),
126                 keyLot.get(keyWithInit).intValue());
127
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());
135     }
136
137 }