blob: 0d4dd5e406ecd904a39c373b3dd389f7950bd933 [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 Pesavento658fd852023-05-10 22:15:03 -04003 * Copyright (c) 2014-2023, 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
Vince Lehman9a709032014-09-13 16:28:07 -050036using ndn::time::system_clock;
Davide Pesavento658fd852023-05-10 22:15:03 -040037
Junxiao Shi6593a432023-08-21 10:50:28 +000038static const ndn::Name ROUTER_A_NAME = "/ndn/router/a";
39static const ndn::Name ROUTER_B_NAME = "/ndn/router/b";
40static const ndn::Name ROUTER_C_NAME = "/ndn/router/c";
41static const ndn::FaceUri ROUTER_A_FACE("udp4://10.0.0.1:6363");
42static const ndn::FaceUri ROUTER_B_FACE("udp4://10.0.0.2:6363");
43static const ndn::FaceUri ROUTER_C_FACE("udp4://10.0.0.3:6363");
44
Davide Pesavento658fd852023-05-10 22:15:03 -040045constexpr system_clock::time_point MAX_TIME = system_clock::time_point::max();
Vince Lehman9a709032014-09-13 16:28:07 -050046
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040047class HyperbolicCalculatorFixture : public IoKeyChainFixture
Vince Lehman9a709032014-09-13 16:28:07 -050048{
49public:
50 HyperbolicCalculatorFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040051 : face(m_io, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060052 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060053 , nlsr(face, m_keyChain, conf)
54 , routingTable(nlsr.m_routingTable)
55 , adjacencies(conf.getAdjacencyList())
56 , lsdb(nlsr.m_lsdb)
Vince Lehman9a709032014-09-13 16:28:07 -050057 {
Vince Lehman9a709032014-09-13 16:28:07 -050058 }
59
60 // Triangle topology with routers A, B, C connected
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060061 void setUpTopology(std::vector<double> anglesA, std::vector<double> anglesB,
62 std::vector<double> anglesC)
Vince Lehman9a709032014-09-13 16:28:07 -050063 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050064 Adjacent a(ROUTER_A_NAME, ndn::FaceUri(ROUTER_A_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
65 Adjacent b(ROUTER_B_NAME, ndn::FaceUri(ROUTER_B_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
66 Adjacent c(ROUTER_C_NAME, ndn::FaceUri(ROUTER_C_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehman9a709032014-09-13 16:28:07 -050067
68 // Router A
69 adjacencies.insert(b);
70 adjacencies.insert(c);
71
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060072 AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencies);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070073 lsdb.installLsa(std::make_shared<AdjLsa>(adjA));
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060074
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080075 CoordinateLsa coordA(adjA.getOriginRouter(), 1, MAX_TIME, 16.23, anglesA);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070076 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordA));
Vince Lehman9a709032014-09-13 16:28:07 -050077
78 // Router B
79 a.setFaceId(1);
80 c.setFaceId(2);
81
82 AdjacencyList adjacencyListB;
83 adjacencyListB.insert(a);
84 adjacencyListB.insert(c);
85
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060086 AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070087 lsdb.installLsa(std::make_shared<AdjLsa>(adjB));
Vince Lehman9a709032014-09-13 16:28:07 -050088
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080089 CoordinateLsa coordB(adjB.getOriginRouter(), 1, MAX_TIME, 16.59, anglesB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070090 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordB));
Vince Lehman9a709032014-09-13 16:28:07 -050091
92 // Router C
93 a.setFaceId(1);
94 b.setFaceId(2);
95
96 AdjacencyList adjacencyListC;
97 adjacencyListC.insert(a);
98 adjacencyListC.insert(b);
99
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600100 AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700101 lsdb.installLsa(std::make_shared<AdjLsa>(adjC));
Vince Lehman9a709032014-09-13 16:28:07 -0500102
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800103 CoordinateLsa coordC(adjC.getOriginRouter(), 1, MAX_TIME, 14.11, anglesC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700104 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordC));
Vince Lehman9a709032014-09-13 16:28:07 -0500105
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700106 auto lsaRange = lsdb.getLsdbIterator<CoordinateLsa>();
107 map.createFromCoordinateLsdb(lsaRange.first, lsaRange.second);
Vince Lehman9a709032014-09-13 16:28:07 -0500108 }
109
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600110 void runTest(const double& expectedCost)
111 {
112 HyperbolicRoutingCalculator calculator(map.getMapSize(), false, ROUTER_A_NAME);
Saurab Dulal72b2b252019-01-22 16:58:08 -0600113 calculator.calculatePath(map, routingTable, lsdb, adjacencies);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600114
115 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
116
117 // Router A should be able to get to B through B with cost 0 and to B through C
118 NexthopList& bHopList = entryB->getNexthopList();
119 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
120
Junxiao Shi6593a432023-08-21 10:50:28 +0000121 for (auto it = bHopList.begin(); it != bHopList.end(); ++it) {
122 auto faceUri = it->getConnectingFaceUri();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600123 uint64_t cost = it->getRouteCostAsAdjustedInteger();
124
125 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == 0) ||
126 (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)));
127 }
128
129 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
130
131 // Router A should be able to get to C through C with cost 0 and to C through B
132 NexthopList& cHopList = entryC->getNexthopList();
133 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
134
Junxiao Shi6593a432023-08-21 10:50:28 +0000135 for (auto it = cHopList.begin(); it != cHopList.end(); ++it) {
136 auto faceUri = it->getConnectingFaceUri();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600137 uint64_t cost = it->getRouteCostAsAdjustedInteger();
138
139 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)) ||
140 (faceUri == ROUTER_C_FACE && cost == 0));
141 }
142 }
143
144 uint64_t
145 applyHyperbolicFactorAndRound(double d)
146 {
147 // Hyperbolic costs in the tests were calculated with 1*10^-9 precision.
148 // A factor larger than 1*10^9 will cause the tests to fail.
149 BOOST_REQUIRE(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR <= 1000000000);
150 return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d);
151 }
152
Vince Lehman9a709032014-09-13 16:28:07 -0500153public:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000154 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600155 ConfParameter conf;
Vince Lehman9a709032014-09-13 16:28:07 -0500156 Nlsr nlsr;
157 Map map;
158
159 RoutingTable& routingTable;
160 AdjacencyList& adjacencies;
161 Lsdb& lsdb;
Vince Lehman9a709032014-09-13 16:28:07 -0500162};
163
Vince Lehman9a709032014-09-13 16:28:07 -0500164BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture)
165
166BOOST_AUTO_TEST_CASE(Basic)
167{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600168 std::vector<double> anglesA = {2.97},
169 anglesB = {3.0},
170 anglesC = {2.99};
171 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500172
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600173 runTest(20.103356956);
174}
Vince Lehman9a709032014-09-13 16:28:07 -0500175
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600176BOOST_AUTO_TEST_CASE(BasicMultipleAngles)
177{
178 std::vector<double> anglesA = {2.97,1.22},
179 anglesB = {3.0, 0.09},
180 anglesC = {321, 2.99};
181 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500182
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600183 runTest(30.655296361);
Vince Lehman9a709032014-09-13 16:28:07 -0500184}
185
186BOOST_AUTO_TEST_SUITE_END()
187
Nick Gordonfad8e252016-08-11 14:21:38 -0500188} // namespace test
189} // namespace nlsr