blob: f830c94f16d0f05b965d29f2213e22964218be63 [file] [log] [blame]
Vince Lehman9a709032014-09-13 16:28:07 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesavento8de8a8b2022-05-12 01:26:43 -04003 * Copyright (c) 2014-2022, 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/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070020 */
Vince Lehman9a709032014-09-13 16:28:07 -050021
Vince Lehman9a709032014-09-13 16:28:07 -050022#include "route/routing-table-calculator.hpp"
23
24#include "adjacency-list.hpp"
Vince Lehman9a709032014-09-13 16:28:07 -050025#include "lsdb.hpp"
26#include "nlsr.hpp"
27#include "route/map.hpp"
28#include "route/routing-table.hpp"
29
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040030#include "tests/io-key-chain-fixture.hpp"
31#include "tests/test-common.hpp"
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060032
Vince Lehman9a709032014-09-13 16:28:07 -050033namespace nlsr {
34namespace test {
35
dmcoomes9f936662017-03-02 10:33:09 -060036using std::shared_ptr;
Vince Lehman9a709032014-09-13 16:28:07 -050037using ndn::time::system_clock;
38static const system_clock::TimePoint MAX_TIME = system_clock::TimePoint::max();
39
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040040class HyperbolicCalculatorFixture : public IoKeyChainFixture
Vince Lehman9a709032014-09-13 16:28:07 -050041{
42public:
43 HyperbolicCalculatorFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040044 : face(m_io, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060045 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060046 , nlsr(face, m_keyChain, conf)
47 , routingTable(nlsr.m_routingTable)
48 , adjacencies(conf.getAdjacencyList())
49 , lsdb(nlsr.m_lsdb)
Vince Lehman9a709032014-09-13 16:28:07 -050050 {
Vince Lehman9a709032014-09-13 16:28:07 -050051 }
52
53 // Triangle topology with routers A, B, C connected
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060054 void setUpTopology(std::vector<double> anglesA, std::vector<double> anglesB,
55 std::vector<double> anglesC)
Vince Lehman9a709032014-09-13 16:28:07 -050056 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050057 Adjacent a(ROUTER_A_NAME, ndn::FaceUri(ROUTER_A_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
58 Adjacent b(ROUTER_B_NAME, ndn::FaceUri(ROUTER_B_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
59 Adjacent c(ROUTER_C_NAME, ndn::FaceUri(ROUTER_C_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehman9a709032014-09-13 16:28:07 -050060
61 // Router A
62 adjacencies.insert(b);
63 adjacencies.insert(c);
64
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060065 AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencies);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070066 lsdb.installLsa(std::make_shared<AdjLsa>(adjA));
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060067
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080068 CoordinateLsa coordA(adjA.getOriginRouter(), 1, MAX_TIME, 16.23, anglesA);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070069 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordA));
Vince Lehman9a709032014-09-13 16:28:07 -050070
71 // Router B
72 a.setFaceId(1);
73 c.setFaceId(2);
74
75 AdjacencyList adjacencyListB;
76 adjacencyListB.insert(a);
77 adjacencyListB.insert(c);
78
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060079 AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070080 lsdb.installLsa(std::make_shared<AdjLsa>(adjB));
Vince Lehman9a709032014-09-13 16:28:07 -050081
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080082 CoordinateLsa coordB(adjB.getOriginRouter(), 1, MAX_TIME, 16.59, anglesB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070083 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordB));
Vince Lehman9a709032014-09-13 16:28:07 -050084
85 // Router C
86 a.setFaceId(1);
87 b.setFaceId(2);
88
89 AdjacencyList adjacencyListC;
90 adjacencyListC.insert(a);
91 adjacencyListC.insert(b);
92
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060093 AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070094 lsdb.installLsa(std::make_shared<AdjLsa>(adjC));
Vince Lehman9a709032014-09-13 16:28:07 -050095
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080096 CoordinateLsa coordC(adjC.getOriginRouter(), 1, MAX_TIME, 14.11, anglesC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070097 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordC));
Vince Lehman9a709032014-09-13 16:28:07 -050098
Ashlesh Gawande57a87172020-05-09 19:47:06 -070099 auto lsaRange = lsdb.getLsdbIterator<CoordinateLsa>();
100 map.createFromCoordinateLsdb(lsaRange.first, lsaRange.second);
Vince Lehman9a709032014-09-13 16:28:07 -0500101 }
102
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600103 void runTest(const double& expectedCost)
104 {
105 HyperbolicRoutingCalculator calculator(map.getMapSize(), false, ROUTER_A_NAME);
Saurab Dulal72b2b252019-01-22 16:58:08 -0600106 calculator.calculatePath(map, routingTable, lsdb, adjacencies);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600107
108 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
109
110 // Router A should be able to get to B through B with cost 0 and to B through C
111 NexthopList& bHopList = entryB->getNexthopList();
112 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
113
114 for (std::set<NextHop, NextHopComparator>::iterator it = bHopList.begin(); it != bHopList.end(); ++it) {
115 std::string faceUri = it->getConnectingFaceUri();
116 uint64_t cost = it->getRouteCostAsAdjustedInteger();
117
118 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == 0) ||
119 (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)));
120 }
121
122 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
123
124 // Router A should be able to get to C through C with cost 0 and to C through B
125 NexthopList& cHopList = entryC->getNexthopList();
126 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
127
128 for (std::set<NextHop, NextHopComparator>::iterator it = cHopList.begin(); it != cHopList.end(); ++it) {
129 std::string faceUri = it->getConnectingFaceUri();
130 uint64_t cost = it->getRouteCostAsAdjustedInteger();
131
132 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)) ||
133 (faceUri == ROUTER_C_FACE && cost == 0));
134 }
135 }
136
137 uint64_t
138 applyHyperbolicFactorAndRound(double d)
139 {
140 // Hyperbolic costs in the tests were calculated with 1*10^-9 precision.
141 // A factor larger than 1*10^9 will cause the tests to fail.
142 BOOST_REQUIRE(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR <= 1000000000);
143 return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d);
144 }
145
Vince Lehman9a709032014-09-13 16:28:07 -0500146public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500147 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600148 ConfParameter conf;
Vince Lehman9a709032014-09-13 16:28:07 -0500149 Nlsr nlsr;
150 Map map;
151
152 RoutingTable& routingTable;
153 AdjacencyList& adjacencies;
154 Lsdb& lsdb;
155
156 static const ndn::Name ROUTER_A_NAME;
157 static const ndn::Name ROUTER_B_NAME;
158 static const ndn::Name ROUTER_C_NAME;
159
160 static const std::string ROUTER_A_FACE;
161 static const std::string ROUTER_B_FACE;
162 static const std::string ROUTER_C_FACE;
163};
164
165const ndn::Name HyperbolicCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a";
166const ndn::Name HyperbolicCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b";
167const ndn::Name HyperbolicCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c";
168
Nick Gordone9733ed2017-04-26 10:48:39 -0500169const std::string HyperbolicCalculatorFixture::ROUTER_A_FACE = "udp4://10.0.0.1";
170const std::string HyperbolicCalculatorFixture::ROUTER_B_FACE = "udp4://10.0.0.2";
171const std::string HyperbolicCalculatorFixture::ROUTER_C_FACE = "udp4://10.0.0.3";
Vince Lehman9a709032014-09-13 16:28:07 -0500172
173BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture)
174
175BOOST_AUTO_TEST_CASE(Basic)
176{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600177 std::vector<double> anglesA = {2.97},
178 anglesB = {3.0},
179 anglesC = {2.99};
180 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500181
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600182 runTest(20.103356956);
183}
Vince Lehman9a709032014-09-13 16:28:07 -0500184
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600185BOOST_AUTO_TEST_CASE(BasicMultipleAngles)
186{
187 std::vector<double> anglesA = {2.97,1.22},
188 anglesB = {3.0, 0.09},
189 anglesC = {321, 2.99};
190 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500191
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600192 runTest(30.655296361);
Vince Lehman9a709032014-09-13 16:28:07 -0500193}
194
195BOOST_AUTO_TEST_SUITE_END()
196
Nick Gordonfad8e252016-08-11 14:21:38 -0500197} // namespace test
198} // namespace nlsr