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.forwarding.staticrouting;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.controller.forwarding.staticrouting.StaticRoute.NextHopType;
18 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
19 import org.opendaylight.controller.sal.core.ConstructionException;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
23 import org.opendaylight.controller.sal.utils.NodeCreator;
25 public class StaticRouteTest {
28 public void testStaticRouteGetSet() {
29 StaticRoute staticRoute = new StaticRoute();
30 InetAddress networkAddress = null;
31 InetAddress mask = null;
32 InetAddress nextHopAddress = null;
34 networkAddress = InetAddress.getByName("10.1.1.0");
35 mask = InetAddress.getByName("255.255.255.0");
36 nextHopAddress = InetAddress.getByName("200.0.0.1");
38 } catch (UnknownHostException e) {
39 Assert.assertTrue(false);
41 staticRoute.setNetworkAddress(networkAddress);
42 Assert.assertEquals(networkAddress.getHostAddress(), staticRoute.getNetworkAddress().getHostAddress());
43 staticRoute.setMask(mask);
44 Assert.assertEquals(mask.getHostAddress(), staticRoute.getMask().getHostAddress());
45 staticRoute.setType(NextHopType.IPADDRESS);
46 Assert.assertEquals("nexthop-ip", staticRoute.getType().toString());
47 staticRoute.setNextHopAddress(nextHopAddress);
48 Assert.assertEquals(nextHopAddress.getHostAddress(), staticRoute.getNextHopAddress().getHostAddress());
49 Node node = NodeCreator.createOFNode(((long)10));
50 staticRoute.setNode(node);
51 Assert.assertEquals(node, staticRoute.getNode());
52 NodeConnector nc0 = NodeConnectorCreator.createOFNodeConnector((short)20, node);
53 staticRoute.setPort(nc0);
54 Assert.assertEquals(nc0, staticRoute.getPort());
55 InetAddress ip1 = null;
56 HostNodeConnector h1 = null;
58 ip1 = InetAddress.getByName("192.1.1.1");
59 } catch (UnknownHostException e) {
60 Assert.assertTrue(false);
63 h1 = new HostNodeConnector(ip1);
64 } catch (ConstructionException e) {
65 Assert.assertTrue(false);
67 staticRoute.setHost(h1);
68 Assert.assertEquals(h1, staticRoute.getHost());
72 public void testStaticRouteComparison() {
73 StaticRouteConfig staticRouteConfig1 = new StaticRouteConfig("route1", "10.1.1.0/24", "100.1.1.1");
74 StaticRouteConfig staticRouteConfig2 = new StaticRouteConfig("route2", "10.1.1.0/24", "100.2.1.1");
75 StaticRouteConfig staticRouteConfig3 = new StaticRouteConfig("route3", "10.2.1.0/24", "100.3.1.1");
76 StaticRouteConfig staticRouteConfig4 = new StaticRouteConfig("route4", "10.1.1.0/31", "");
77 StaticRoute staticRoute1 = new StaticRoute(staticRouteConfig1);
78 StaticRoute staticRoute2 = new StaticRoute(staticRouteConfig2);
79 StaticRoute staticRoute3 = new StaticRoute(staticRouteConfig3);
80 StaticRoute staticRoute4 = new StaticRoute(staticRouteConfig4);
82 Assert.assertTrue(staticRoute1.equals(staticRoute2));
83 Assert.assertFalse(staticRoute1.equals(staticRoute3));
84 Assert.assertFalse(staticRoute1.equals(staticRoute4));
86 Assert.assertTrue(staticRoute1.compareTo(staticRoute2) == 0 ? true : false);
87 Assert.assertFalse(staticRoute1.compareTo(staticRoute3) == 0 ? true : false);
88 Assert.assertTrue(staticRoute1.compareTo(staticRoute4) == 0 ? true : false);
93 public void testLongestPrefixMatch() {
94 StaticRouteConfig staticRouteConfig1 = new StaticRouteConfig("route1", "10.1.1.254/24", "100.1.1.1");
95 StaticRoute staticRoute1 = new StaticRoute(staticRouteConfig1);
96 InetAddress ip1 = null;
97 InetAddress ip2 = null;
99 ip1 = InetAddress.getByName("10.1.0.2");
100 ip2 = InetAddress.getByName("10.1.1.2");
101 } catch (UnknownHostException e) {
102 Assert.assertTrue(false);
104 InetAddress rxdIp1 = staticRoute1.longestPrefixMatch(ip1);
105 InetAddress rxdIp2 = staticRoute1.longestPrefixMatch(ip2);
106 Assert.assertEquals(null, rxdIp1);
107 Assert.assertEquals("10.1.1.0", rxdIp2.getHostAddress());