blob: 0011ce9b4b52d6dea06f433d0a03dd18e9724b7f [file] [log] [blame]
Vince Lehman9a709032014-09-13 16:28:07 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, 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
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060033#include <ndn-cxx/util/dummy-client-face.hpp>
34
Vince Lehman9a709032014-09-13 16:28:07 -050035namespace nlsr {
36namespace test {
37
dmcoomes9f936662017-03-02 10:33:09 -060038using std::shared_ptr;
Vince Lehman9a709032014-09-13 16:28:07 -050039using ndn::time::system_clock;
40static const system_clock::TimePoint MAX_TIME = system_clock::TimePoint::max();
41
42class HyperbolicCalculatorFixture : public BaseFixture
43{
44public:
45 HyperbolicCalculatorFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050046 : face(m_ioService, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060047 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060048 , nlsr(face, m_keyChain, conf)
49 , routingTable(nlsr.m_routingTable)
50 , adjacencies(conf.getAdjacencyList())
51 , lsdb(nlsr.m_lsdb)
Vince Lehman9a709032014-09-13 16:28:07 -050052 {
Vince Lehman9a709032014-09-13 16:28:07 -050053 }
54
55 // Triangle topology with routers A, B, C connected
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060056 void setUpTopology(std::vector<double> anglesA, std::vector<double> anglesB,
57 std::vector<double> anglesC)
Vince Lehman9a709032014-09-13 16:28:07 -050058 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050059 Adjacent a(ROUTER_A_NAME, ndn::FaceUri(ROUTER_A_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
60 Adjacent b(ROUTER_B_NAME, ndn::FaceUri(ROUTER_B_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
61 Adjacent c(ROUTER_C_NAME, ndn::FaceUri(ROUTER_C_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehman9a709032014-09-13 16:28:07 -050062
63 // Router A
64 adjacencies.insert(b);
65 adjacencies.insert(c);
66
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060067 AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencies);
Vince Lehman9a709032014-09-13 16:28:07 -050068 lsdb.installAdjLsa(adjA);
69
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060070
71 CoordinateLsa coordA(adjA.getOrigRouter(), 1, MAX_TIME, 16.23, anglesA);
Vince Lehman9a709032014-09-13 16:28:07 -050072 lsdb.installCoordinateLsa(coordA);
73
74 // Router B
75 a.setFaceId(1);
76 c.setFaceId(2);
77
78 AdjacencyList adjacencyListB;
79 adjacencyListB.insert(a);
80 adjacencyListB.insert(c);
81
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060082 AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB);
Vince Lehman9a709032014-09-13 16:28:07 -050083 lsdb.installAdjLsa(adjB);
84
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060085 CoordinateLsa coordB(adjB.getOrigRouter(), 1, MAX_TIME, 16.59, anglesB);
Vince Lehman9a709032014-09-13 16:28:07 -050086 lsdb.installCoordinateLsa(coordB);
87
88 // Router C
89 a.setFaceId(1);
90 b.setFaceId(2);
91
92 AdjacencyList adjacencyListC;
93 adjacencyListC.insert(a);
94 adjacencyListC.insert(b);
95
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060096 AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC);
Vince Lehman9a709032014-09-13 16:28:07 -050097 lsdb.installAdjLsa(adjC);
98
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060099 CoordinateLsa coordC(adjC.getOrigRouter(), 1, MAX_TIME, 14.11, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500100 lsdb.installCoordinateLsa(coordC);
101
Nick Gordon22b5c952017-08-10 17:48:15 -0500102 map.createFromAdjLsdb(lsdb.getAdjLsdb().begin(), lsdb.getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500103 }
104
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600105 void runTest(const double& expectedCost)
106 {
107 HyperbolicRoutingCalculator calculator(map.getMapSize(), false, ROUTER_A_NAME);
Saurab Dulal72b2b252019-01-22 16:58:08 -0600108 calculator.calculatePath(map, routingTable, lsdb, adjacencies);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600109
110 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
111
112 // Router A should be able to get to B through B with cost 0 and to B through C
113 NexthopList& bHopList = entryB->getNexthopList();
114 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
115
116 for (std::set<NextHop, NextHopComparator>::iterator it = bHopList.begin(); it != bHopList.end(); ++it) {
117 std::string faceUri = it->getConnectingFaceUri();
118 uint64_t cost = it->getRouteCostAsAdjustedInteger();
119
120 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == 0) ||
121 (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)));
122 }
123
124 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
125
126 // Router A should be able to get to C through C with cost 0 and to C through B
127 NexthopList& cHopList = entryC->getNexthopList();
128 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
129
130 for (std::set<NextHop, NextHopComparator>::iterator it = cHopList.begin(); it != cHopList.end(); ++it) {
131 std::string faceUri = it->getConnectingFaceUri();
132 uint64_t cost = it->getRouteCostAsAdjustedInteger();
133
134 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(expectedCost)) ||
135 (faceUri == ROUTER_C_FACE && cost == 0));
136 }
137 }
138
139 uint64_t
140 applyHyperbolicFactorAndRound(double d)
141 {
142 // Hyperbolic costs in the tests were calculated with 1*10^-9 precision.
143 // A factor larger than 1*10^9 will cause the tests to fail.
144 BOOST_REQUIRE(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR <= 1000000000);
145 return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d);
146 }
147
Vince Lehman9a709032014-09-13 16:28:07 -0500148public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500149 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600150 ConfParameter conf;
Vince Lehman9a709032014-09-13 16:28:07 -0500151 Nlsr nlsr;
152 Map map;
153
154 RoutingTable& routingTable;
155 AdjacencyList& adjacencies;
156 Lsdb& lsdb;
157
158 static const ndn::Name ROUTER_A_NAME;
159 static const ndn::Name ROUTER_B_NAME;
160 static const ndn::Name ROUTER_C_NAME;
161
162 static const std::string ROUTER_A_FACE;
163 static const std::string ROUTER_B_FACE;
164 static const std::string ROUTER_C_FACE;
165};
166
167const ndn::Name HyperbolicCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a";
168const ndn::Name HyperbolicCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b";
169const ndn::Name HyperbolicCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c";
170
Nick Gordone9733ed2017-04-26 10:48:39 -0500171const std::string HyperbolicCalculatorFixture::ROUTER_A_FACE = "udp4://10.0.0.1";
172const std::string HyperbolicCalculatorFixture::ROUTER_B_FACE = "udp4://10.0.0.2";
173const std::string HyperbolicCalculatorFixture::ROUTER_C_FACE = "udp4://10.0.0.3";
Vince Lehman9a709032014-09-13 16:28:07 -0500174
175BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture)
176
177BOOST_AUTO_TEST_CASE(Basic)
178{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600179 std::vector<double> anglesA = {2.97},
180 anglesB = {3.0},
181 anglesC = {2.99};
182 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500183
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600184 runTest(20.103356956);
185}
Vince Lehman9a709032014-09-13 16:28:07 -0500186
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600187BOOST_AUTO_TEST_CASE(BasicMultipleAngles)
188{
189 std::vector<double> anglesA = {2.97,1.22},
190 anglesB = {3.0, 0.09},
191 anglesC = {321, 2.99};
192 setUpTopology(anglesA, anglesB, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500193
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600194 runTest(30.655296361);
Vince Lehman9a709032014-09-13 16:28:07 -0500195}
196
197BOOST_AUTO_TEST_SUITE_END()
198
Nick Gordonfad8e252016-08-11 14:21:38 -0500199} // namespace test
200} // namespace nlsr