blob: 7d502989469de99014a5dfe49272757dbda9c171 [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/*
Junxiao Shib8752932024-01-07 15:18:46 +00003 * Copyright (c) 2014-2024, 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"
Junxiao Shi4eb4eae2024-02-14 12:36:57 +000027#include "route/name-map.hpp"
Vince Lehman9a709032014-09-13 16:28:07 -050028#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
Davide Pesavento288141a2024-02-13 17:30:35 -050033namespace nlsr::tests {
Vince Lehman9a709032014-09-13 16:28:07 -050034
Davide Pesavento288141a2024-02-13 17:30:35 -050035constexpr time::system_clock::time_point MAX_TIME = time::system_clock::time_point::max();
Junxiao Shi6593a432023-08-21 10:50:28 +000036static const ndn::Name ROUTER_A_NAME = "/ndn/router/a";
37static const ndn::Name ROUTER_B_NAME = "/ndn/router/b";
38static const ndn::Name ROUTER_C_NAME = "/ndn/router/c";
39static const ndn::FaceUri ROUTER_A_FACE("udp4://10.0.0.1:6363");
40static const ndn::FaceUri ROUTER_B_FACE("udp4://10.0.0.2:6363");
41static const ndn::FaceUri ROUTER_C_FACE("udp4://10.0.0.3:6363");
42
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040043class HyperbolicCalculatorFixture : public IoKeyChainFixture
Vince Lehman9a709032014-09-13 16:28:07 -050044{
45public:
46 HyperbolicCalculatorFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040047 : face(m_io, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060048 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060049 , nlsr(face, m_keyChain, conf)
50 , routingTable(nlsr.m_routingTable)
51 , adjacencies(conf.getAdjacencyList())
52 , lsdb(nlsr.m_lsdb)
Vince Lehman9a709032014-09-13 16:28:07 -050053 {
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 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050060 Adjacent a(ROUTER_A_NAME, ndn::FaceUri(ROUTER_A_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
61 Adjacent b(ROUTER_B_NAME, ndn::FaceUri(ROUTER_B_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
62 Adjacent c(ROUTER_C_NAME, ndn::FaceUri(ROUTER_C_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehman9a709032014-09-13 16:28:07 -050063
64 // Router A
65 adjacencies.insert(b);
66 adjacencies.insert(c);
67
Junxiao Shib8752932024-01-07 15:18:46 +000068 AdjLsa adjA(a.getName(), 1, MAX_TIME, adjacencies);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070069 lsdb.installLsa(std::make_shared<AdjLsa>(adjA));
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060070
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080071 CoordinateLsa coordA(adjA.getOriginRouter(), 1, MAX_TIME, 16.23, anglesA);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070072 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordA));
Vince Lehman9a709032014-09-13 16:28:07 -050073
74 // Router B
75 a.setFaceId(1);
76 c.setFaceId(2);
77
78 AdjacencyList adjacencyListB;
79 adjacencyListB.insert(a);
80 adjacencyListB.insert(c);
81
Junxiao Shib8752932024-01-07 15:18:46 +000082 AdjLsa adjB(b.getName(), 1, MAX_TIME, adjacencyListB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070083 lsdb.installLsa(std::make_shared<AdjLsa>(adjB));
Vince Lehman9a709032014-09-13 16:28:07 -050084
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080085 CoordinateLsa coordB(adjB.getOriginRouter(), 1, MAX_TIME, 16.59, anglesB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070086 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordB));
Vince Lehman9a709032014-09-13 16:28:07 -050087
88 // Router C
89 a.setFaceId(1);
90 b.setFaceId(2);
91
92 AdjacencyList adjacencyListC;
93 adjacencyListC.insert(a);
94 adjacencyListC.insert(b);
95
Junxiao Shib8752932024-01-07 15:18:46 +000096 AdjLsa adjC(c.getName(), 1, MAX_TIME, adjacencyListC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070097 lsdb.installLsa(std::make_shared<AdjLsa>(adjC));
Vince Lehman9a709032014-09-13 16:28:07 -050098
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080099 CoordinateLsa coordC(adjC.getOriginRouter(), 1, MAX_TIME, 14.11, anglesC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700100 lsdb.installLsa(std::make_shared<CoordinateLsa>(coordC));
Vince Lehman9a709032014-09-13 16:28:07 -0500101
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700102 auto lsaRange = lsdb.getLsdbIterator<CoordinateLsa>();
Junxiao Shi4eb4eae2024-02-14 12:36:57 +0000103 map = NameMap::createFromCoordinateLsdb(lsaRange.first, lsaRange.second);
Vince Lehman9a709032014-09-13 16:28:07 -0500104 }
105
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600106 void runTest(const double& expectedCost)
107 {
Junxiao Shid6922b52024-01-14 19:50:34 +0000108 HyperbolicRoutingCalculator calculator(map.size(), false, ROUTER_A_NAME);
Saurab Dulal72b2b252019-01-22 16:58:08 -0600109 calculator.calculatePath(map, routingTable, lsdb, adjacencies);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600110
111 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
112
113 // Router A should be able to get to B through B with cost 0 and to B through C
114 NexthopList& bHopList = entryB->getNexthopList();
115 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
116
Junxiao Shi6593a432023-08-21 10:50:28 +0000117 for (auto it = bHopList.begin(); it != bHopList.end(); ++it) {
118 auto faceUri = it->getConnectingFaceUri();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600119 uint64_t cost = it->getRouteCostAsAdjustedInteger();
120
121 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == 0) ||
122 (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)));
123 }
124
125 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
126
127 // Router A should be able to get to C through C with cost 0 and to C through B
128 NexthopList& cHopList = entryC->getNexthopList();
129 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
130
Junxiao Shi6593a432023-08-21 10:50:28 +0000131 for (auto it = cHopList.begin(); it != cHopList.end(); ++it) {
132 auto faceUri = it->getConnectingFaceUri();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600133 uint64_t cost = it->getRouteCostAsAdjustedInteger();
134
135 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)) ||
136 (faceUri == ROUTER_C_FACE && cost == 0));
137 }
138 }
139
140 uint64_t
141 applyHyperbolicFactorAndRound(double d)
142 {
143 // Hyperbolic costs in the tests were calculated with 1*10^-9 precision.
144 // A factor larger than 1*10^9 will cause the tests to fail.
145 BOOST_REQUIRE(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR <= 1000000000);
146 return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d);
147 }
148
Vince Lehman9a709032014-09-13 16:28:07 -0500149public:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000150 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600151 ConfParameter conf;
Vince Lehman9a709032014-09-13 16:28:07 -0500152 Nlsr nlsr;
Junxiao Shi4eb4eae2024-02-14 12:36:57 +0000153 NameMap map;
Vince Lehman9a709032014-09-13 16:28:07 -0500154
155 RoutingTable& routingTable;
156 AdjacencyList& adjacencies;
157 Lsdb& lsdb;
Vince Lehman9a709032014-09-13 16:28:07 -0500158};
159
Vince Lehman9a709032014-09-13 16:28:07 -0500160BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture)
161
162BOOST_AUTO_TEST_CASE(Basic)
163{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600164 std::vector<double> anglesA = {2.97},
165 anglesB = {3.0},
166 anglesC = {2.99};
167 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500168
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600169 runTest(20.103356956);
170}
Vince Lehman9a709032014-09-13 16:28:07 -0500171
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600172BOOST_AUTO_TEST_CASE(BasicMultipleAngles)
173{
174 std::vector<double> anglesA = {2.97,1.22},
175 anglesB = {3.0, 0.09},
176 anglesC = {321, 2.99};
177 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500178
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600179 runTest(30.655296361);
Vince Lehman9a709032014-09-13 16:28:07 -0500180}
181
182BOOST_AUTO_TEST_SUITE_END()
183
Davide Pesavento288141a2024-02-13 17:30:35 -0500184} // namespace nlsr::tests