Bug 1764 - implementation of default config pusher
[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.util.HashMap;
12 import java.util.Map;
13
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * @author mirehak
23  */
24 public class SwitchConnectionCookieOFImplTest {
25
26     private static final Logger LOG = LoggerFactory
27             .getLogger(SwitchConnectionCookieOFImplTest.class);
28
29     private SwitchConnectionCookieOFImpl switchConnectionKey;
30
31     private int seed;
32
33     /**
34      * @throws java.lang.Exception
35      */
36     @Before
37     public void setUp() throws Exception {
38         seed = 4242;
39         switchConnectionKey = createSwitchSessionKey((short) 42);
40     }
41
42     /**
43      * @param datapathId
44      * @return
45      */
46     private static SwitchConnectionCookieOFImpl createSwitchSessionKey(short auxiliary) {
47         SwitchConnectionCookieOFImpl key = new SwitchConnectionCookieOFImpl();
48         key.setAuxiliaryId(auxiliary);
49         return key;
50     }
51
52     /**
53      * Test method for
54      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#getId()}
55      * .
56      */
57     @Test
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());
63     }
64
65     /**
66      * Test method for
67      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#initUUID()}
68      * .
69      */
70     @Test
71     public void testInitId1() {
72         try {
73             switchConnectionKey.setAuxiliaryId((short) 0);
74             switchConnectionKey.init(seed);
75             Assert.fail("init should fail with no datapathId");
76         } catch (Exception e) {
77             // expected
78         }
79     }
80
81     /**
82      * Test method for
83      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#equals(Object)}
84      * ,
85      * {@link org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl#hashCode()}
86      * .
87      */
88     @Test
89     public void testHashAndEquals() {
90         // insert equal keys
91         SwitchConnectionCookieOFImpl key1 = createSwitchSessionKey((short) 42);
92         key1.init(seed);
93
94         SwitchConnectionCookieOFImpl key2 = createSwitchSessionKey((short) 42);
95         key2.init(seed);
96
97         SwitchConnectionCookieOFImpl key3 = createSwitchSessionKey((short) 43);
98         key3.init(seed);
99         SwitchConnectionCookieOFImpl key4 = createSwitchSessionKey((short) 21);
100         key4.init(seed);
101
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());
111
112         // lookup using inited key
113         Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key1)
114                 .intValue());
115         Assert.assertEquals(System.identityHashCode(key2), keyLot.get(key2)
116                 .intValue());
117         Assert.assertEquals(System.identityHashCode(key3), keyLot.get(key3)
118                 .intValue());
119         Assert.assertEquals(System.identityHashCode(key4), keyLot.get(key4)
120                 .intValue());
121
122         // lookup using not inited key
123         SwitchConnectionCookieOFImpl keyWithoutInit = createSwitchSessionKey((short) 42);
124         Assert.assertNull(keyLot.get(keyWithoutInit));
125
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());
131
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());
137     }
138
139 }