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.internal.HostNodePair;
23 public class HostSwitchTest {
25 public void TestEquality() {
26 HostNodeConnector h1 = null;
27 HostNodeConnector h2 = null;
29 InetAddress ip1 = null;
31 ip1 = InetAddress.getByName("10.0.0.1");
32 } catch (UnknownHostException e) {
35 InetAddress ip2 = null;
37 ip2 = InetAddress.getByName("10.0.0.1");
38 } catch (UnknownHostException e) {
42 h1 = new HostNodeConnector(ip1);
43 } catch (ConstructionException e) {
47 h2 = new HostNodeConnector(ip2);
48 } catch (ConstructionException e) {
51 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
52 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1l));
53 Assert.assertTrue(hsw1.equals(hsw2));
57 public void TestDiversityHost() {
58 HostNodeConnector h1 = null;
59 HostNodeConnector h2 = null;
60 InetAddress ip1 = null;
62 ip1 = InetAddress.getByName("10.0.0.1");
63 } catch (UnknownHostException e) {
66 InetAddress ip2 = null;
68 ip2 = InetAddress.getByName("10.0.0.2");
69 } catch (UnknownHostException e) {
73 h1 = new HostNodeConnector(ip1);
74 } catch (ConstructionException e) {
78 h2 = new HostNodeConnector(ip2);
79 } catch (ConstructionException e) {
83 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
84 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(1l));
85 Assert.assertTrue(!hsw1.equals(hsw2));
89 public void TestDiversitySwitch() {
90 HostNodeConnector h1 = null;
91 HostNodeConnector h2 = null;
92 InetAddress ip1 = null;
94 ip1 = InetAddress.getByName("10.0.0.1");
95 } catch (UnknownHostException e) {
98 InetAddress ip2 = null;
100 ip2 = InetAddress.getByName("10.0.0.1");
101 } catch (UnknownHostException e) {
105 h1 = new HostNodeConnector(ip1);
106 } catch (ConstructionException e) {
110 h2 = new HostNodeConnector(ip2);
111 } catch (ConstructionException e) {
115 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
116 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2l));
117 Assert.assertTrue(!hsw1.equals(hsw2));
121 public void TestDiversityAll() {
122 HostNodeConnector h1 = null;
123 HostNodeConnector h2 = null;
124 InetAddress ip1 = null;
126 ip1 = InetAddress.getByName("10.0.0.1");
127 } catch (UnknownHostException e) {
130 InetAddress ip2 = null;
132 ip2 = InetAddress.getByName("10.0.0.2");
133 } catch (UnknownHostException e) {
137 h1 = new HostNodeConnector(ip1);
138 } catch (ConstructionException e) {
142 h2 = new HostNodeConnector(ip2);
143 } catch (ConstructionException e) {
147 HostNodePair hsw1 = new HostNodePair(h1, NodeCreator.createOFNode(1l));
148 HostNodePair hsw2 = new HostNodePair(h2, NodeCreator.createOFNode(2l));
149 Assert.assertTrue(!hsw1.equals(hsw2));
153 public void TestEqualHashCode1() {
154 HostNodeConnector h1 = null;
155 HostNodeConnector h2 = null;
156 InetAddress ip1 = null;
158 ip1 = InetAddress.getByName("10.0.0.1");
159 } catch (UnknownHostException e) {
162 InetAddress ip2 = null;
164 ip2 = InetAddress.getByName("10.0.0.1");
165 } catch (UnknownHostException e) {
169 h1 = new HostNodeConnector(ip1);
170 } catch (ConstructionException e) {
174 h2 = new HostNodeConnector(ip2);
175 } catch (ConstructionException e) {
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());
184 public void TestEqualHashCode2() {
185 HostNodeConnector h1 = null;
186 HostNodeConnector h2 = null;
187 InetAddress ip1 = null;
189 ip1 = InetAddress.getByName("10.0.0.2");
190 } catch (UnknownHostException e) {
193 InetAddress ip2 = null;
195 ip2 = InetAddress.getByName("10.0.0.2");
196 } catch (UnknownHostException e) {
200 h1 = new HostNodeConnector(ip1);
201 } catch (ConstructionException e) {
205 h2 = new HostNodeConnector(ip2);
206 } catch (ConstructionException e) {
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());
216 public void TestDiverseHashCodeHost() {
217 HostNodeConnector h1 = null;
218 HostNodeConnector h2 = null;
219 InetAddress ip1 = null;
221 ip1 = InetAddress.getByName("10.0.0.1");
222 } catch (UnknownHostException e) {
225 InetAddress ip2 = null;
227 ip2 = InetAddress.getByName("10.0.0.2");
228 } catch (UnknownHostException e) {
232 h1 = new HostNodeConnector(ip1);
233 } catch (ConstructionException e) {
237 h2 = new HostNodeConnector(ip2);
238 } catch (ConstructionException e) {
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());
247 public void TestDiverseHashCodeSwitch() {
248 HostNodeConnector h1 = null;
249 HostNodeConnector h2 = null;
250 InetAddress ip1 = null;
252 ip1 = InetAddress.getByName("10.0.0.1");
253 } catch (UnknownHostException e) {
256 InetAddress ip2 = null;
258 ip2 = InetAddress.getByName("10.0.0.1");
259 } catch (UnknownHostException e) {
263 h1 = new HostNodeConnector(ip1);
264 } catch (ConstructionException e) {
268 h2 = new HostNodeConnector(ip2);
269 } catch (ConstructionException e) {
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());
279 public void TestDiverseHashCodeAll() {
280 HostNodeConnector h1 = null;
281 HostNodeConnector h2 = null;
282 InetAddress ip1 = null;
284 ip1 = InetAddress.getByName("10.0.0.1");
285 } catch (UnknownHostException e) {
288 InetAddress ip2 = null;
290 ip2 = InetAddress.getByName("10.0.0.3");
291 } catch (UnknownHostException e) {
295 h1 = new HostNodeConnector(ip1);
296 } catch (ConstructionException e) {
300 h2 = new HostNodeConnector(ip2);
301 } catch (ConstructionException e) {
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());
311 public void TestUsageAsKey() {
312 HostNodeConnector h1 = null;
313 HostNodeConnector h2 = null;
314 InetAddress ip1 = null;
316 ip1 = InetAddress.getByName("10.0.0.1");
317 } catch (UnknownHostException e) {
320 InetAddress ip2 = null;
322 ip2 = InetAddress.getByName("10.0.0.1");
323 } catch (UnknownHostException e) {
327 h1 = new HostNodeConnector(ip1);
328 } catch (ConstructionException e) {
332 h2 = new HostNodeConnector(ip2);
333 } catch (ConstructionException e) {
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)));
346 public void TestUsageAsKeyChangingField() {
347 HostNodeConnector h1 = null;
348 HostNodeConnector h2 = null;
349 InetAddress ip1 = null;
351 ip1 = InetAddress.getByName("10.0.0.1");
352 } catch (UnknownHostException e) {
355 InetAddress ip2 = null;
357 ip2 = InetAddress.getByName("10.0.0.1");
358 } catch (UnknownHostException e) {
362 h1 = new HostNodeConnector(ip1);
363 } catch (ConstructionException e) {
367 h2 = new HostNodeConnector(ip2);
368 } catch (ConstructionException e) {
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)));