Add help menu
[controller.git] / opendaylight / samples / simpleforwarding / src / test / java / org / opendaylight / controller / samples / simpleforwarding / internal / HostSwitchTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.samples.simpleforwarding.internal;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.HashMap;
15 import org.junit.Test;
16 import org.junit.Assert;
17
18 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
19 import org.opendaylight.controller.sal.core.ConstructionException;
20 import org.opendaylight.controller.sal.utils.NodeCreator;
21 import org.opendaylight.controller.samples.simpleforwarding.internal.HostNodePair;
22
23 public class HostSwitchTest {
24     @Test
25     public void TestEquality() {
26         HostNodeConnector h1 = null;
27         HostNodeConnector h2 = null;
28
29         InetAddress ip1 = null;
30         try {
31             ip1 = InetAddress.getByName("10.0.0.1");
32         } catch (UnknownHostException e) {
33             return;
34         }
35         InetAddress ip2 = null;
36         try {
37             ip2 = InetAddress.getByName("10.0.0.1");
38         } catch (UnknownHostException e) {
39             return;
40         }
41         try {
42             h1 = new HostNodeConnector(ip1);
43         } catch (ConstructionException e) {
44             return;
45         }
46         try {
47             h2 = new HostNodeConnector(ip2);
48         } catch (ConstructionException e) {
49             return;
50         }
51         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
52         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1l));
53         Assert.assertTrue(hsw1.equals(hsw2));
54     }
55
56     @Test
57     public void TestDiversityHost() {
58         HostNodeConnector h1 = null;
59         HostNodeConnector h2 = null;
60         InetAddress ip1 = null;
61         try {
62             ip1 = InetAddress.getByName("10.0.0.1");
63         } catch (UnknownHostException e) {
64             return;
65         }
66         InetAddress ip2 = null;
67         try {
68             ip2 = InetAddress.getByName("10.0.0.2");
69         } catch (UnknownHostException e) {
70             return;
71         }
72         try {
73             h1 = new HostNodeConnector(ip1);
74         } catch (ConstructionException e) {
75             return;
76         }
77         try {
78             h2 = new HostNodeConnector(ip2);
79         } catch (ConstructionException e) {
80             return;
81         }
82
83         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
84         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1l));
85         Assert.assertTrue(!hsw1.equals(hsw2));
86     }
87
88     @Test
89     public void TestDiversitySwitch() {
90         HostNodeConnector h1 = null;
91         HostNodeConnector h2 = null;
92         InetAddress ip1 = null;
93         try {
94             ip1 = InetAddress.getByName("10.0.0.1");
95         } catch (UnknownHostException e) {
96             return;
97         }
98         InetAddress ip2 = null;
99         try {
100             ip2 = InetAddress.getByName("10.0.0.1");
101         } catch (UnknownHostException e) {
102             return;
103         }
104         try {
105             h1 = new HostNodeConnector(ip1);
106         } catch (ConstructionException e) {
107             return;
108         }
109         try {
110             h2 = new HostNodeConnector(ip2);
111         } catch (ConstructionException e) {
112             return;
113         }
114
115         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
116         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2l));
117         Assert.assertTrue(!hsw1.equals(hsw2));
118     }
119
120     @Test
121     public void TestDiversityAll() {
122         HostNodeConnector h1 = null;
123         HostNodeConnector h2 = null;
124         InetAddress ip1 = null;
125         try {
126             ip1 = InetAddress.getByName("10.0.0.1");
127         } catch (UnknownHostException e) {
128             return;
129         }
130         InetAddress ip2 = null;
131         try {
132             ip2 = InetAddress.getByName("10.0.0.2");
133         } catch (UnknownHostException e) {
134             return;
135         }
136         try {
137             h1 = new HostNodeConnector(ip1);
138         } catch (ConstructionException e) {
139             return;
140         }
141         try {
142             h2 = new HostNodeConnector(ip2);
143         } catch (ConstructionException e) {
144             return;
145         }
146
147         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
148         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2l));
149         Assert.assertTrue(!hsw1.equals(hsw2));
150     }
151
152     @Test
153     public void TestEqualHashCode1() {
154         HostNodeConnector h1 = null;
155         HostNodeConnector h2 = null;
156         InetAddress ip1 = null;
157         try {
158             ip1 = InetAddress.getByName("10.0.0.1");
159         } catch (UnknownHostException e) {
160             return;
161         }
162         InetAddress ip2 = null;
163         try {
164             ip2 = InetAddress.getByName("10.0.0.1");
165         } catch (UnknownHostException e) {
166             return;
167         }
168         try {
169             h1 = new HostNodeConnector(ip1);
170         } catch (ConstructionException e) {
171             return;
172         }
173         try {
174             h2 = new HostNodeConnector(ip2);
175         } catch (ConstructionException e) {
176             return;
177         }
178         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
179         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1l));
180         Assert.assertTrue(hsw1.hashCode() == hsw2.hashCode());
181     }
182
183     @Test
184     public void TestEqualHashCode2() {
185         HostNodeConnector h1 = null;
186         HostNodeConnector h2 = null;
187         InetAddress ip1 = null;
188         try {
189             ip1 = InetAddress.getByName("10.0.0.2");
190         } catch (UnknownHostException e) {
191             return;
192         }
193         InetAddress ip2 = null;
194         try {
195             ip2 = InetAddress.getByName("10.0.0.2");
196         } catch (UnknownHostException e) {
197             return;
198         }
199         try {
200             h1 = new HostNodeConnector(ip1);
201         } catch (ConstructionException e) {
202             return;
203         }
204         try {
205             h2 = new HostNodeConnector(ip2);
206         } catch (ConstructionException e) {
207             return;
208         }
209
210         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
211         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1l));
212         Assert.assertTrue(hsw1.hashCode() == hsw2.hashCode());
213     }
214
215     @Test
216     public void TestDiverseHashCodeHost() {
217         HostNodeConnector h1 = null;
218         HostNodeConnector h2 = null;
219         InetAddress ip1 = null;
220         try {
221             ip1 = InetAddress.getByName("10.0.0.1");
222         } catch (UnknownHostException e) {
223             return;
224         }
225         InetAddress ip2 = null;
226         try {
227             ip2 = InetAddress.getByName("10.0.0.2");
228         } catch (UnknownHostException e) {
229             return;
230         }
231         try {
232             h1 = new HostNodeConnector(ip1);
233         } catch (ConstructionException e) {
234             return;
235         }
236         try {
237             h2 = new HostNodeConnector(ip2);
238         } catch (ConstructionException e) {
239             return;
240         }
241         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
242         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1l));
243         Assert.assertTrue(hsw1.hashCode() != hsw2.hashCode());
244     }
245
246     @Test
247     public void TestDiverseHashCodeSwitch() {
248         HostNodeConnector h1 = null;
249         HostNodeConnector h2 = null;
250         InetAddress ip1 = null;
251         try {
252             ip1 = InetAddress.getByName("10.0.0.1");
253         } catch (UnknownHostException e) {
254             return;
255         }
256         InetAddress ip2 = null;
257         try {
258             ip2 = InetAddress.getByName("10.0.0.1");
259         } catch (UnknownHostException e) {
260             return;
261         }
262         try {
263             h1 = new HostNodeConnector(ip1);
264         } catch (ConstructionException e) {
265             return;
266         }
267         try {
268             h2 = new HostNodeConnector(ip2);
269         } catch (ConstructionException e) {
270             return;
271         }
272
273         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
274         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2l));
275         Assert.assertTrue(hsw1.hashCode() != hsw2.hashCode());
276     }
277
278     @Test
279     public void TestDiverseHashCodeAll() {
280         HostNodeConnector h1 = null;
281         HostNodeConnector h2 = null;
282         InetAddress ip1 = null;
283         try {
284             ip1 = InetAddress.getByName("10.0.0.1");
285         } catch (UnknownHostException e) {
286             return;
287         }
288         InetAddress ip2 = null;
289         try {
290             ip2 = InetAddress.getByName("10.0.0.3");
291         } catch (UnknownHostException e) {
292             return;
293         }
294         try {
295             h1 = new HostNodeConnector(ip1);
296         } catch (ConstructionException e) {
297             return;
298         }
299         try {
300             h2 = new HostNodeConnector(ip2);
301         } catch (ConstructionException e) {
302             return;
303         }
304
305         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
306         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2l));
307         Assert.assertTrue(hsw1.hashCode() != hsw2.hashCode());
308     }
309
310     @Test
311     public void TestUsageAsKey() {
312         HostNodeConnector h1 = null;
313         HostNodeConnector h2 = null;
314         InetAddress ip1 = null;
315         try {
316             ip1 = InetAddress.getByName("10.0.0.1");
317         } catch (UnknownHostException e) {
318             return;
319         }
320         InetAddress ip2 = null;
321         try {
322             ip2 = InetAddress.getByName("10.0.0.1");
323         } catch (UnknownHostException e) {
324             return;
325         }
326         try {
327             h1 = new HostNodeConnector(ip1);
328         } catch (ConstructionException e) {
329             return;
330         }
331         try {
332             h2 = new HostNodeConnector(ip2);
333         } catch (ConstructionException e) {
334             return;
335         }
336
337         HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
338         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1l));
339         HashMap<HostNodePair, Long> hm = new HashMap<HostNodePair, Long>();
340         hm.put(hsw1, new Long(10));
341         Assert.assertTrue(hm.get(hsw2) != null);
342         Assert.assertTrue(hm.get(hsw2).equals(new Long(10)));
343     }
344
345     @Test
346     public void TestUsageAsKeyChangingField() {
347         HostNodeConnector h1 = null;
348         HostNodeConnector h2 = null;
349         InetAddress ip1 = null;
350         try {
351             ip1 = InetAddress.getByName("10.0.0.1");
352         } catch (UnknownHostException e) {
353             return;
354         }
355         InetAddress ip2 = null;
356         try {
357             ip2 = InetAddress.getByName("10.0.0.1");
358         } catch (UnknownHostException e) {
359             return;
360         }
361         try {
362             h1 = new HostNodeConnector(ip1);
363         } catch (ConstructionException e) {
364             return;
365         }
366         try {
367             h2 = new HostNodeConnector(ip2);
368         } catch (ConstructionException e) {
369             return;
370         }
371         HostNodePair hsw1 = new HostNodePair(h1, null);
372         HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1l));
373         hsw1.setNode(NodeCreator.createOFNode(1l));
374         HashMap<HostNodePair, Long> hm = new HashMap<HostNodePair, Long>();
375         hm.put(hsw1, new Long(10));
376         Assert.assertTrue(hm.get(hsw2) != null);
377         Assert.assertTrue(hm.get(hsw2).equals(new Long(10)));
378     }
379 }