blob: de1c86a659e10f839829eec2f9a68f26cb42b6b0 [file] [log] [blame]
Vince Lehman9a709032014-09-13 16:28:07 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
Vince Lehman9a709032014-09-13 16:28:07 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Vince Lehman9a709032014-09-13 16:28:07 -050020 **/
21
22#include "test-common.hpp"
Vince Lehman9a709032014-09-13 16:28:07 -050023
24#include "route/routing-table-calculator.hpp"
25
26#include "adjacency-list.hpp"
27#include "lsa.hpp"
28#include "lsdb.hpp"
29#include "nlsr.hpp"
30#include "route/map.hpp"
31#include "route/routing-table.hpp"
32
33#include <boost/test/unit_test.hpp>
34
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060035#include <ndn-cxx/util/dummy-client-face.hpp>
36
Vince Lehman9a709032014-09-13 16:28:07 -050037namespace nlsr {
38namespace test {
39
Vince Lehman9a709032014-09-13 16:28:07 -050040using ndn::shared_ptr;
41using ndn::time::system_clock;
42static const system_clock::TimePoint MAX_TIME = system_clock::TimePoint::max();
43
44class HyperbolicCalculatorFixture : public BaseFixture
45{
46public:
47 HyperbolicCalculatorFixture()
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060048 : face(make_shared<ndn::util::DummyClientFace>())
Vince Lehman9a709032014-09-13 16:28:07 -050049 , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
50 , routingTable(nlsr.getRoutingTable())
51 , adjacencies(nlsr.getAdjacencyList())
52 , lsdb(nlsr.getLsdb())
53 {
54 setUpTopology();
55 }
56
57 // Triangle topology with routers A, B, C connected
58 void setUpTopology()
59 {
60 INIT_LOGGERS("/tmp", "TRACE");
61
62 Adjacent a(ROUTER_A_NAME, ROUTER_A_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0);
63 Adjacent b(ROUTER_B_NAME, ROUTER_B_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0);
64 Adjacent c(ROUTER_C_NAME, ROUTER_C_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0);
65
66 // Router A
67 adjacencies.insert(b);
68 adjacencies.insert(c);
69
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060070 AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencies);
Vince Lehman9a709032014-09-13 16:28:07 -050071 lsdb.installAdjLsa(adjA);
72
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060073 CoordinateLsa coordA(adjA.getOrigRouter(), 1, MAX_TIME, 16.23, 2.97);
Vince Lehman9a709032014-09-13 16:28:07 -050074 lsdb.installCoordinateLsa(coordA);
75
76 // Router B
77 a.setFaceId(1);
78 c.setFaceId(2);
79
80 AdjacencyList adjacencyListB;
81 adjacencyListB.insert(a);
82 adjacencyListB.insert(c);
83
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060084 AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB);
Vince Lehman9a709032014-09-13 16:28:07 -050085 lsdb.installAdjLsa(adjB);
86
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060087 CoordinateLsa coordB(adjB.getOrigRouter(), 1, MAX_TIME, 16.59, 3.0);
Vince Lehman9a709032014-09-13 16:28:07 -050088 lsdb.installCoordinateLsa(coordB);
89
90 // Router C
91 a.setFaceId(1);
92 b.setFaceId(2);
93
94 AdjacencyList adjacencyListC;
95 adjacencyListC.insert(a);
96 adjacencyListC.insert(b);
97
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060098 AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC);
Vince Lehman9a709032014-09-13 16:28:07 -050099 lsdb.installAdjLsa(adjC);
100
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600101 CoordinateLsa coordC(adjC.getOrigRouter(), 1, MAX_TIME, 14.11, 2.99);
Vince Lehman9a709032014-09-13 16:28:07 -0500102 lsdb.installCoordinateLsa(coordC);
103
104 map.createFromAdjLsdb(nlsr);
105 }
106
107public:
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -0600108 shared_ptr<ndn::util::DummyClientFace> face;
Vince Lehman9a709032014-09-13 16:28:07 -0500109 Nlsr nlsr;
110 Map map;
111
112 RoutingTable& routingTable;
113 AdjacencyList& adjacencies;
114 Lsdb& lsdb;
115
116 static const ndn::Name ROUTER_A_NAME;
117 static const ndn::Name ROUTER_B_NAME;
118 static const ndn::Name ROUTER_C_NAME;
119
120 static const std::string ROUTER_A_FACE;
121 static const std::string ROUTER_B_FACE;
122 static const std::string ROUTER_C_FACE;
123};
124
125const ndn::Name HyperbolicCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a";
126const ndn::Name HyperbolicCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b";
127const ndn::Name HyperbolicCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c";
128
129const std::string HyperbolicCalculatorFixture::ROUTER_A_FACE = "face-a";
130const std::string HyperbolicCalculatorFixture::ROUTER_B_FACE = "face-b";
131const std::string HyperbolicCalculatorFixture::ROUTER_C_FACE = "face-c";
132
Vince Lehman20fe4a92014-09-09 15:57:59 -0500133uint64_t
134applyHyperbolicFactorAndRound(double d)
135{
136 // Hyperbolic costs in the tests were calculated with 1*10^-9 precision.
137 // A factor larger than 1*10^9 will cause the tests to fail.
138 BOOST_REQUIRE(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR <= 1000000000);
139 return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d);
140}
141
Vince Lehman9a709032014-09-13 16:28:07 -0500142BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture)
143
144BOOST_AUTO_TEST_CASE(Basic)
145{
146 HyperbolicRoutingCalculator calculator(map.getMapSize(), false, ROUTER_A_NAME);
147 calculator.calculatePaths(map, routingTable, lsdb, adjacencies);
148
149 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
150
Vince Lehman20fe4a92014-09-09 15:57:59 -0500151 // Router A should be able to get to B through B with cost 0 and to B through C
Vince Lehman9a709032014-09-13 16:28:07 -0500152 NexthopList& bHopList = entryB->getNexthopList();
153 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
154
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500155 for (std::set<NextHop, NextHopComparator>::iterator it = bHopList.begin(); it != bHopList.end(); ++it) {
Vince Lehman9a709032014-09-13 16:28:07 -0500156 std::string faceUri = it->getConnectingFaceUri();
157 uint64_t cost = it->getRouteCostAsAdjustedInteger();
158
159 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == 0) ||
Vince Lehman20fe4a92014-09-09 15:57:59 -0500160 (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(20.103356956)));
Vince Lehman9a709032014-09-13 16:28:07 -0500161 }
162
163 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
164
Vince Lehman20fe4a92014-09-09 15:57:59 -0500165 // Router A should be able to get to C through C with cost 0 and to C through B
Vince Lehman9a709032014-09-13 16:28:07 -0500166 NexthopList& cHopList = entryC->getNexthopList();
167 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
168
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500169 for (std::set<NextHop, NextHopComparator>::iterator it = cHopList.begin(); it != cHopList.end(); ++it) {
Vince Lehman9a709032014-09-13 16:28:07 -0500170 std::string faceUri = it->getConnectingFaceUri();
171 uint64_t cost = it->getRouteCostAsAdjustedInteger();
172
Vince Lehman20fe4a92014-09-09 15:57:59 -0500173 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(20.103356956)) ||
Vince Lehman9a709032014-09-13 16:28:07 -0500174 (faceUri == ROUTER_C_FACE && cost == 0));
175 }
176}
177
178BOOST_AUTO_TEST_SUITE_END()
179
Nick Gordonfad8e252016-08-11 14:21:38 -0500180} // namespace test
181} // namespace nlsr