blob: dc0cd187d69f843f5f8b939e7d5c8f0ae9e784f2 [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
dmcoomes9f936662017-03-02 10:33:09 -060040using std::shared_ptr;
Vince Lehman9a709032014-09-13 16:28:07 -050041using 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 Chowdhuryf04f9892017-08-20 20:42:56 -050048 : face(m_ioService, m_keyChain)
49 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Vince Lehman9a709032014-09-13 16:28:07 -050050 , routingTable(nlsr.getRoutingTable())
51 , adjacencies(nlsr.getAdjacencyList())
52 , lsdb(nlsr.getLsdb())
53 {
Vince Lehman9a709032014-09-13 16:28:07 -050054 }
55
56 // Triangle topology with routers A, B, C connected
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060057 void setUpTopology(std::vector<double> anglesA, std::vector<double> anglesB,
58 std::vector<double> anglesC)
Vince Lehman9a709032014-09-13 16:28:07 -050059 {
60 INIT_LOGGERS("/tmp", "TRACE");
61
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050062 Adjacent a(ROUTER_A_NAME, ndn::FaceUri(ROUTER_A_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
63 Adjacent b(ROUTER_B_NAME, ndn::FaceUri(ROUTER_B_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
64 Adjacent c(ROUTER_C_NAME, ndn::FaceUri(ROUTER_C_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehman9a709032014-09-13 16:28:07 -050065
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
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060073
74 CoordinateLsa coordA(adjA.getOrigRouter(), 1, MAX_TIME, 16.23, anglesA);
Vince Lehman9a709032014-09-13 16:28:07 -050075 lsdb.installCoordinateLsa(coordA);
76
77 // Router B
78 a.setFaceId(1);
79 c.setFaceId(2);
80
81 AdjacencyList adjacencyListB;
82 adjacencyListB.insert(a);
83 adjacencyListB.insert(c);
84
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060085 AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB);
Vince Lehman9a709032014-09-13 16:28:07 -050086 lsdb.installAdjLsa(adjB);
87
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060088 CoordinateLsa coordB(adjB.getOrigRouter(), 1, MAX_TIME, 16.59, anglesB);
Vince Lehman9a709032014-09-13 16:28:07 -050089 lsdb.installCoordinateLsa(coordB);
90
91 // Router C
92 a.setFaceId(1);
93 b.setFaceId(2);
94
95 AdjacencyList adjacencyListC;
96 adjacencyListC.insert(a);
97 adjacencyListC.insert(b);
98
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060099 AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC);
Vince Lehman9a709032014-09-13 16:28:07 -0500100 lsdb.installAdjLsa(adjC);
101
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600102 CoordinateLsa coordC(adjC.getOrigRouter(), 1, MAX_TIME, 14.11, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500103 lsdb.installCoordinateLsa(coordC);
104
Nick Gordon22b5c952017-08-10 17:48:15 -0500105 map.createFromAdjLsdb(lsdb.getAdjLsdb().begin(), lsdb.getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500106 }
107
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600108 void runTest(const double& expectedCost)
109 {
110 HyperbolicRoutingCalculator calculator(map.getMapSize(), false, ROUTER_A_NAME);
111 calculator.calculatePaths(map, routingTable, lsdb, adjacencies);
112
113 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
114
115 // Router A should be able to get to B through B with cost 0 and to B through C
116 NexthopList& bHopList = entryB->getNexthopList();
117 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
118
119 for (std::set<NextHop, NextHopComparator>::iterator it = bHopList.begin(); it != bHopList.end(); ++it) {
120 std::string faceUri = it->getConnectingFaceUri();
121 uint64_t cost = it->getRouteCostAsAdjustedInteger();
122
123 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == 0) ||
124 (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)));
125 }
126
127 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
128
129 // Router A should be able to get to C through C with cost 0 and to C through B
130 NexthopList& cHopList = entryC->getNexthopList();
131 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
132
133 for (std::set<NextHop, NextHopComparator>::iterator it = cHopList.begin(); it != cHopList.end(); ++it) {
134 std::string faceUri = it->getConnectingFaceUri();
135 uint64_t cost = it->getRouteCostAsAdjustedInteger();
136
137 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)) ||
138 (faceUri == ROUTER_C_FACE && cost == 0));
139 }
140 }
141
142 uint64_t
143 applyHyperbolicFactorAndRound(double d)
144 {
145 // Hyperbolic costs in the tests were calculated with 1*10^-9 precision.
146 // A factor larger than 1*10^9 will cause the tests to fail.
147 BOOST_REQUIRE(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR <= 1000000000);
148 return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d);
149 }
150
Vince Lehman9a709032014-09-13 16:28:07 -0500151public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500152 ndn::util::DummyClientFace face;
Vince Lehman9a709032014-09-13 16:28:07 -0500153 Nlsr nlsr;
154 Map map;
155
156 RoutingTable& routingTable;
157 AdjacencyList& adjacencies;
158 Lsdb& lsdb;
159
160 static const ndn::Name ROUTER_A_NAME;
161 static const ndn::Name ROUTER_B_NAME;
162 static const ndn::Name ROUTER_C_NAME;
163
164 static const std::string ROUTER_A_FACE;
165 static const std::string ROUTER_B_FACE;
166 static const std::string ROUTER_C_FACE;
167};
168
169const ndn::Name HyperbolicCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a";
170const ndn::Name HyperbolicCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b";
171const ndn::Name HyperbolicCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c";
172
Nick Gordone9733ed2017-04-26 10:48:39 -0500173const std::string HyperbolicCalculatorFixture::ROUTER_A_FACE = "udp4://10.0.0.1";
174const std::string HyperbolicCalculatorFixture::ROUTER_B_FACE = "udp4://10.0.0.2";
175const std::string HyperbolicCalculatorFixture::ROUTER_C_FACE = "udp4://10.0.0.3";
Vince Lehman9a709032014-09-13 16:28:07 -0500176
177BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture)
178
179BOOST_AUTO_TEST_CASE(Basic)
180{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600181 std::vector<double> anglesA = {2.97},
182 anglesB = {3.0},
183 anglesC = {2.99};
184 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500185
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600186 runTest(20.103356956);
187}
Vince Lehman9a709032014-09-13 16:28:07 -0500188
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600189BOOST_AUTO_TEST_CASE(BasicMultipleAngles)
190{
191 std::vector<double> anglesA = {2.97,1.22},
192 anglesB = {3.0, 0.09},
193 anglesC = {321, 2.99};
194 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500195
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600196 runTest(30.655296361);
Vince Lehman9a709032014-09-13 16:28:07 -0500197}
198
199BOOST_AUTO_TEST_SUITE_END()
200
Nick Gordonfad8e252016-08-11 14:21:38 -0500201} // namespace test
202} // namespace nlsr