3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.samples.simpleforwarding.internal;
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;
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.HostNodePair;
24 public class HostSwitchTest {
26 public void TestEquality() {
27 HostNodeConnector h1 = null;
28 HostNodeConnector h2 = null;
30 InetAddress ip1 = null;
32 ip1 = InetAddress.getByName("10.0.0.1");
33 } catch (UnknownHostException e) {
36 InetAddress ip2 = null;
38 ip2 = InetAddress.getByName("10.0.0.1");
39 } catch (UnknownHostException e) {
43 h1 = new HostNodeConnector(ip1);
44 } catch (ConstructionException e) {
48 h2 = new HostNodeConnector(ip2);
49 } catch (ConstructionException e) {
52 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
53 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1L));
54 Assert.assertTrue(hsw1.equals(hsw2));
58 public void TestDiversityHost() {
59 HostNodeConnector h1 = null;
60 HostNodeConnector h2 = null;
61 InetAddress ip1 = null;
63 ip1 = InetAddress.getByName("10.0.0.1");
64 } catch (UnknownHostException e) {
67 InetAddress ip2 = null;
69 ip2 = InetAddress.getByName("10.0.0.2");
70 } catch (UnknownHostException e) {
74 h1 = new HostNodeConnector(ip1);
75 } catch (ConstructionException e) {
79 h2 = new HostNodeConnector(ip2);
80 } catch (ConstructionException e) {
84 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
85 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1L));
86 Assert.assertTrue(!hsw1.equals(hsw2));
90 public void TestDiversitySwitch() {
91 HostNodeConnector h1 = null;
92 HostNodeConnector h2 = null;
93 InetAddress ip1 = null;
95 ip1 = InetAddress.getByName("10.0.0.1");
96 } catch (UnknownHostException e) {
99 InetAddress ip2 = null;
101 ip2 = InetAddress.getByName("10.0.0.1");
102 } catch (UnknownHostException e) {
106 h1 = new HostNodeConnector(ip1);
107 } catch (ConstructionException e) {
111 h2 = new HostNodeConnector(ip2);
112 } catch (ConstructionException e) {
116 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
117 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2L));
118 Assert.assertTrue(!hsw1.equals(hsw2));
122 public void TestDiversityAll() {
123 HostNodeConnector h1 = null;
124 HostNodeConnector h2 = null;
125 InetAddress ip1 = null;
127 ip1 = InetAddress.getByName("10.0.0.1");
128 } catch (UnknownHostException e) {
131 InetAddress ip2 = null;
133 ip2 = InetAddress.getByName("10.0.0.2");
134 } catch (UnknownHostException e) {
138 h1 = new HostNodeConnector(ip1);
139 } catch (ConstructionException e) {
143 h2 = new HostNodeConnector(ip2);
144 } catch (ConstructionException e) {
148 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
149 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2L));
150 Assert.assertTrue(!hsw1.equals(hsw2));
154 public void TestEqualHashCode1() {
155 HostNodeConnector h1 = null;
156 HostNodeConnector h2 = null;
157 InetAddress ip1 = null;
159 ip1 = InetAddress.getByName("10.0.0.1");
160 } catch (UnknownHostException e) {
163 InetAddress ip2 = null;
165 ip2 = InetAddress.getByName("10.0.0.1");
166 } catch (UnknownHostException e) {
170 h1 = new HostNodeConnector(ip1);
171 } catch (ConstructionException e) {
175 h2 = new HostNodeConnector(ip2);
176 } catch (ConstructionException e) {
179 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
180 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1L));
181 Assert.assertTrue(hsw1.hashCode() == hsw2.hashCode());
185 public void TestEqualHashCode2() {
186 HostNodeConnector h1 = null;
187 HostNodeConnector h2 = null;
188 InetAddress ip1 = null;
190 ip1 = InetAddress.getByName("10.0.0.2");
191 } catch (UnknownHostException e) {
194 InetAddress ip2 = null;
196 ip2 = InetAddress.getByName("10.0.0.2");
197 } catch (UnknownHostException e) {
201 h1 = new HostNodeConnector(ip1);
202 } catch (ConstructionException e) {
206 h2 = new HostNodeConnector(ip2);
207 } catch (ConstructionException e) {
211 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
212 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1L));
213 Assert.assertTrue(hsw1.hashCode() == hsw2.hashCode());
217 public void TestDiverseHashCodeHost() {
218 HostNodeConnector h1 = null;
219 HostNodeConnector h2 = null;
220 InetAddress ip1 = null;
222 ip1 = InetAddress.getByName("10.0.0.1");
223 } catch (UnknownHostException e) {
226 InetAddress ip2 = null;
228 ip2 = InetAddress.getByName("10.0.0.2");
229 } catch (UnknownHostException e) {
233 h1 = new HostNodeConnector(ip1);
234 } catch (ConstructionException e) {
238 h2 = new HostNodeConnector(ip2);
239 } catch (ConstructionException e) {
242 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
243 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1L));
244 Assert.assertTrue(hsw1.hashCode() != hsw2.hashCode());
248 public void TestDiverseHashCodeSwitch() {
249 HostNodeConnector h1 = null;
250 HostNodeConnector h2 = null;
251 InetAddress ip1 = null;
253 ip1 = InetAddress.getByName("10.0.0.1");
254 } catch (UnknownHostException e) {
257 InetAddress ip2 = null;
259 ip2 = InetAddress.getByName("10.0.0.1");
260 } catch (UnknownHostException e) {
264 h1 = new HostNodeConnector(ip1);
265 } catch (ConstructionException e) {
269 h2 = new HostNodeConnector(ip2);
270 } catch (ConstructionException e) {
274 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
275 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2L));
276 Assert.assertTrue(hsw1.hashCode() != hsw2.hashCode());
280 public void TestDiverseHashCodeAll() {
281 HostNodeConnector h1 = null;
282 HostNodeConnector h2 = null;
283 InetAddress ip1 = null;
285 ip1 = InetAddress.getByName("10.0.0.1");
286 } catch (UnknownHostException e) {
289 InetAddress ip2 = null;
291 ip2 = InetAddress.getByName("10.0.0.3");
292 } catch (UnknownHostException e) {
296 h1 = new HostNodeConnector(ip1);
297 } catch (ConstructionException e) {
301 h2 = new HostNodeConnector(ip2);
302 } catch (ConstructionException e) {
306 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
307 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2L));
308 Assert.assertTrue(hsw1.hashCode() != hsw2.hashCode());
312 public void TestUsageAsKey() {
313 HostNodeConnector h1 = null;
314 HostNodeConnector h2 = null;
315 InetAddress ip1 = null;
317 ip1 = InetAddress.getByName("10.0.0.1");
318 } catch (UnknownHostException e) {
321 InetAddress ip2 = null;
323 ip2 = InetAddress.getByName("10.0.0.1");
324 } catch (UnknownHostException e) {
328 h1 = new HostNodeConnector(ip1);
329 } catch (ConstructionException e) {
333 h2 = new HostNodeConnector(ip2);
334 } catch (ConstructionException e) {
338 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1L));
339 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1L));
340 HashMap<HostNodePair, Long> hm = new HashMap<HostNodePair, Long>();
341 hm.put(hsw1, new Long(10));
342 Assert.assertTrue(hm.get(hsw2) != null);
343 Assert.assertTrue(hm.get(hsw2).equals(new Long(10)));
347 public void TestUsageAsKeyChangingField() {
348 HostNodeConnector h1 = null;
349 HostNodeConnector h2 = null;
350 InetAddress ip1 = null;
352 ip1 = InetAddress.getByName("10.0.0.1");
353 } catch (UnknownHostException e) {
356 InetAddress ip2 = null;
358 ip2 = InetAddress.getByName("10.0.0.1");
359 } catch (UnknownHostException e) {
363 h1 = new HostNodeConnector(ip1);
364 } catch (ConstructionException e) {
368 h2 = new HostNodeConnector(ip2);
369 } catch (ConstructionException e) {
372 HostNodePair hsw1 = new HostNodePair(h1, null);
373 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1L));
374 hsw1.setNode(NodeCreator.createOFNode(1L));
375 HashMap<HostNodePair, Long> hm = new HashMap<HostNodePair, Long>();
376 hm.put(hsw1, new Long(10));
377 Assert.assertTrue(hm.get(hsw2) != null);
378 Assert.assertTrue(hm.get(hsw2).equals(new Long(10)));