blob: c4f39e257e37906536d892b6bcde1e3a60f32024 [file] [log] [blame]
Vince Lehman9a709032014-09-13 16:28:07 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal72b2b252019-01-22 16:58:08 -06003 * Copyright (c) 2014-2019, 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 {
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
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060068 AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencies);
Vince Lehman9a709032014-09-13 16:28:07 -050069 lsdb.installAdjLsa(adjA);
70
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060071
72 CoordinateLsa coordA(adjA.getOrigRouter(), 1, MAX_TIME, 16.23, anglesA);
Vince Lehman9a709032014-09-13 16:28:07 -050073 lsdb.installCoordinateLsa(coordA);
74
75 // Router B
76 a.setFaceId(1);
77 c.setFaceId(2);
78
79 AdjacencyList adjacencyListB;
80 adjacencyListB.insert(a);
81 adjacencyListB.insert(c);
82
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060083 AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB);
Vince Lehman9a709032014-09-13 16:28:07 -050084 lsdb.installAdjLsa(adjB);
85
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060086 CoordinateLsa coordB(adjB.getOrigRouter(), 1, MAX_TIME, 16.59, anglesB);
Vince Lehman9a709032014-09-13 16:28:07 -050087 lsdb.installCoordinateLsa(coordB);
88
89 // Router C
90 a.setFaceId(1);
91 b.setFaceId(2);
92
93 AdjacencyList adjacencyListC;
94 adjacencyListC.insert(a);
95 adjacencyListC.insert(b);
96
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060097 AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC);
Vince Lehman9a709032014-09-13 16:28:07 -050098 lsdb.installAdjLsa(adjC);
99
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600100 CoordinateLsa coordC(adjC.getOrigRouter(), 1, MAX_TIME, 14.11, anglesC);
Vince Lehman9a709032014-09-13 16:28:07 -0500101 lsdb.installCoordinateLsa(coordC);
102
Nick Gordon22b5c952017-08-10 17:48:15 -0500103 map.createFromAdjLsdb(lsdb.getAdjLsdb().begin(), lsdb.getAdjLsdb().end());
Vince Lehman9a709032014-09-13 16:28:07 -0500104 }
105
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600106 void runTest(const double& expectedCost)
107 {
108 HyperbolicRoutingCalculator calculator(map.getMapSize(), 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
117 for (std::set<NextHop, NextHopComparator>::iterator it = bHopList.begin(); it != bHopList.end(); ++it) {
118 std::string faceUri = it->getConnectingFaceUri();
119 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
131 for (std::set<NextHop, NextHopComparator>::iterator it = cHopList.begin(); it != cHopList.end(); ++it) {
132 std::string faceUri = it->getConnectingFaceUri();
133 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:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500150 ndn::util::DummyClientFace face;
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