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 import static junit.framework.Assert.fail;
27 public class StaticRouteTest {
30 public void testStaticRouteGetSet() {
31 StaticRoute staticRoute = new StaticRoute();
32 InetAddress networkAddress = null;
33 InetAddress mask = null;
34 InetAddress nextHopAddress = null;
36 networkAddress = InetAddress.getByName("10.1.1.0");
37 mask = InetAddress.getByName("255.255.255.0");
38 nextHopAddress = InetAddress.getByName("200.0.0.1");
40 } catch (UnknownHostException e) {
41 fail("Failed due to unknown host " + e.getMessage());
43 staticRoute.setNetworkAddress(networkAddress);
44 Assert.assertEquals(networkAddress.getHostAddress(), staticRoute.getNetworkAddress().getHostAddress());
45 staticRoute.setMask(mask);
46 Assert.assertEquals(mask.getHostAddress(), staticRoute.getMask().getHostAddress());
47 staticRoute.setType(NextHopType.IPADDRESS);
48 Assert.assertEquals("nexthop-ip", staticRoute.getType().toString());
49 staticRoute.setNextHopAddress(nextHopAddress);
50 Assert.assertEquals(nextHopAddress.getHostAddress(), staticRoute.getNextHopAddress().getHostAddress());
51 Node node = NodeCreator.createOFNode(((long)10));
52 staticRoute.setNode(node);
53 Assert.assertEquals(node, staticRoute.getNode());
54 NodeConnector nc0 = NodeConnectorCreator.createOFNodeConnector((short)20, node);
55 staticRoute.setPort(nc0);
56 Assert.assertEquals(nc0, staticRoute.getPort());
57 InetAddress ip1 = null;
58 HostNodeConnector h1 = null;
60 ip1 = InetAddress.getByName("192.1.1.1");
61 } catch (UnknownHostException e) {
62 fail("Failed due to unknown host " + e.getMessage());
65 h1 = new HostNodeConnector(ip1);
66 } catch (ConstructionException e) {
67 fail("Failed to construct host " + e.getMessage());
69 staticRoute.setHost(h1);
70 Assert.assertEquals(h1, staticRoute.getHost());
74 public void testStaticRouteComparison() {
75 StaticRouteConfig staticRouteConfig1 = new StaticRouteConfig("route1", "10.1.1.0/24", "100.1.1.1");
76 StaticRouteConfig staticRouteConfig2 = new StaticRouteConfig("route2", "10.1.1.0/24", "100.2.1.1");
77 StaticRouteConfig staticRouteConfig3 = new StaticRouteConfig("route3", "10.2.1.0/24", "100.3.1.1");
78 StaticRouteConfig staticRouteConfig4 = new StaticRouteConfig("route4", "10.1.1.0/31", "");
79 StaticRoute staticRoute1 = new StaticRoute(staticRouteConfig1);
80 StaticRoute staticRoute2 = new StaticRoute(staticRouteConfig2);
81 StaticRoute staticRoute3 = new StaticRoute(staticRouteConfig3);
82 StaticRoute staticRoute4 = new StaticRoute(staticRouteConfig4);
84 Assert.assertTrue(staticRoute1.equals(staticRoute2));
85 Assert.assertFalse(staticRoute1.equals(staticRoute3));
86 Assert.assertFalse(staticRoute1.equals(staticRoute4));
88 Assert.assertTrue(staticRoute1.compareTo(staticRoute2) == 0);
89 Assert.assertFalse(staticRoute1.compareTo(staticRoute3) == 0);
90 Assert.assertTrue(staticRoute1.compareTo(staticRoute4) == 0);
95 public void testLongestPrefixMatch() {
96 StaticRouteConfig staticRouteConfig1 = new StaticRouteConfig("route1", "10.1.1.254/24", "100.1.1.1");
97 StaticRoute staticRoute1 = new StaticRoute(staticRouteConfig1);
98 InetAddress ip1 = null;
99 InetAddress ip2 = null;
101 ip1 = InetAddress.getByName("10.1.0.2");
102 ip2 = InetAddress.getByName("10.1.1.2");
103 } catch (UnknownHostException e) {
104 fail("Test failed due to unknown host " + e.getMessage());
106 InetAddress rxdIp1 = staticRoute1.longestPrefixMatch(ip1);
107 InetAddress rxdIp2 = staticRoute1.longestPrefixMatch(ip2);
108 Assert.assertNull(rxdIp1);
109 Assert.assertEquals("10.1.1.0", rxdIp2.getHostAddress());