blob: 5387ffd01c2c143c566b8b68cad3ebb16087700a [file] [log] [blame]
Vince Lehman9a709032014-09-13 16:28:07 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 **/
22
23#include "test-common.hpp"
24#include "dummy-face.hpp"
25
26#include "route/routing-table-calculator.hpp"
27
28#include "adjacency-list.hpp"
29#include "lsa.hpp"
30#include "lsdb.hpp"
31#include "nlsr.hpp"
32#include "route/map.hpp"
33#include "route/routing-table.hpp"
34
35#include <boost/test/unit_test.hpp>
36
37namespace nlsr {
38namespace test {
39
40using ndn::DummyFace;
41using ndn::shared_ptr;
42using ndn::time::system_clock;
43static const system_clock::TimePoint MAX_TIME = system_clock::TimePoint::max();
44
45class HyperbolicCalculatorFixture : public BaseFixture
46{
47public:
48 HyperbolicCalculatorFixture()
49 : face(ndn::makeDummyFace())
50 , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
51 , routingTable(nlsr.getRoutingTable())
52 , adjacencies(nlsr.getAdjacencyList())
53 , lsdb(nlsr.getLsdb())
54 {
55 setUpTopology();
56 }
57
58 // Triangle topology with routers A, B, C connected
59 void setUpTopology()
60 {
61 INIT_LOGGERS("/tmp", "TRACE");
62
63 Adjacent a(ROUTER_A_NAME, ROUTER_A_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0);
64 Adjacent b(ROUTER_B_NAME, ROUTER_B_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0);
65 Adjacent c(ROUTER_C_NAME, ROUTER_C_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0);
66
67 // Router A
68 adjacencies.insert(b);
69 adjacencies.insert(c);
70
alvy49b1c0c2014-12-19 13:57:46 -060071 AdjLsa adjA(a.getName(), AdjLsa::TYPE_STRING, 1, MAX_TIME, 2, adjacencies);
Vince Lehman9a709032014-09-13 16:28:07 -050072 lsdb.installAdjLsa(adjA);
73
alvy49b1c0c2014-12-19 13:57:46 -060074 CoordinateLsa coordA(adjA.getOrigRouter(), CoordinateLsa::TYPE_STRING, 1, MAX_TIME, 16.23, 2.97);
Vince Lehman9a709032014-09-13 16:28:07 -050075 lsdb.installCoordinateLsa(coordA);
76
77 // Router B
78 a.setFaceId(1);
79 c.setFaceId(2);
80
81 AdjacencyList adjacencyListB;
82 adjacencyListB.insert(a);
83 adjacencyListB.insert(c);
84
alvy49b1c0c2014-12-19 13:57:46 -060085 AdjLsa adjB(b.getName(), AdjLsa::TYPE_STRING, 1, MAX_TIME, 2, adjacencyListB);
Vince Lehman9a709032014-09-13 16:28:07 -050086 lsdb.installAdjLsa(adjB);
87
alvy49b1c0c2014-12-19 13:57:46 -060088 CoordinateLsa coordB(adjB.getOrigRouter(), CoordinateLsa::TYPE_STRING, 1, MAX_TIME, 16.59, 3.0);
Vince Lehman9a709032014-09-13 16:28:07 -050089 lsdb.installCoordinateLsa(coordB);
90
91 // Router C
92 a.setFaceId(1);
93 b.setFaceId(2);
94
95 AdjacencyList adjacencyListC;
96 adjacencyListC.insert(a);
97 adjacencyListC.insert(b);
98
alvy49b1c0c2014-12-19 13:57:46 -060099 AdjLsa adjC(c.getName(), AdjLsa::TYPE_STRING, 1, MAX_TIME, 2, adjacencyListC);
Vince Lehman9a709032014-09-13 16:28:07 -0500100 lsdb.installAdjLsa(adjC);
101
alvy49b1c0c2014-12-19 13:57:46 -0600102 CoordinateLsa coordC(adjC.getOrigRouter(), CoordinateLsa::TYPE_STRING, 1, MAX_TIME, 14.11, 2.99);
Vince Lehman9a709032014-09-13 16:28:07 -0500103 lsdb.installCoordinateLsa(coordC);
104
105 map.createFromAdjLsdb(nlsr);
106 }
107
108public:
109 shared_ptr<DummyFace> face;
110 Nlsr nlsr;
111 Map map;
112
113 RoutingTable& routingTable;
114 AdjacencyList& adjacencies;
115 Lsdb& lsdb;
116
117 static const ndn::Name ROUTER_A_NAME;
118 static const ndn::Name ROUTER_B_NAME;
119 static const ndn::Name ROUTER_C_NAME;
120
121 static const std::string ROUTER_A_FACE;
122 static const std::string ROUTER_B_FACE;
123 static const std::string ROUTER_C_FACE;
124};
125
126const ndn::Name HyperbolicCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a";
127const ndn::Name HyperbolicCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b";
128const ndn::Name HyperbolicCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c";
129
130const std::string HyperbolicCalculatorFixture::ROUTER_A_FACE = "face-a";
131const std::string HyperbolicCalculatorFixture::ROUTER_B_FACE = "face-b";
132const std::string HyperbolicCalculatorFixture::ROUTER_C_FACE = "face-c";
133
Vince Lehman20fe4a92014-09-09 15:57:59 -0500134uint64_t
135applyHyperbolicFactorAndRound(double d)
136{
137 // Hyperbolic costs in the tests were calculated with 1*10^-9 precision.
138 // A factor larger than 1*10^9 will cause the tests to fail.
139 BOOST_REQUIRE(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR <= 1000000000);
140 return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR*d);
141}
142
Vince Lehman9a709032014-09-13 16:28:07 -0500143BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture)
144
145BOOST_AUTO_TEST_CASE(Basic)
146{
147 HyperbolicRoutingCalculator calculator(map.getMapSize(), false, ROUTER_A_NAME);
148 calculator.calculatePaths(map, routingTable, lsdb, adjacencies);
149
150 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
151
Vince Lehman20fe4a92014-09-09 15:57:59 -0500152 // Router A should be able to get to B through B with cost 0 and to B through C
Vince Lehman9a709032014-09-13 16:28:07 -0500153 NexthopList& bHopList = entryB->getNexthopList();
154 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
155
156 for (std::list<NextHop>::iterator it = bHopList.begin(); it != bHopList.end(); ++it) {
157 std::string faceUri = it->getConnectingFaceUri();
158 uint64_t cost = it->getRouteCostAsAdjustedInteger();
159
160 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == 0) ||
Vince Lehman20fe4a92014-09-09 15:57:59 -0500161 (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(20.103356956)));
Vince Lehman9a709032014-09-13 16:28:07 -0500162 }
163
164 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
165
Vince Lehman20fe4a92014-09-09 15:57:59 -0500166 // Router A should be able to get to C through C with cost 0 and to C through B
Vince Lehman9a709032014-09-13 16:28:07 -0500167 NexthopList& cHopList = entryC->getNexthopList();
168 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
169
170 for (std::list<NextHop>::iterator it = cHopList.begin(); it != cHopList.end(); ++it) {
171 std::string faceUri = it->getConnectingFaceUri();
172 uint64_t cost = it->getRouteCostAsAdjustedInteger();
173
Vince Lehman20fe4a92014-09-09 15:57:59 -0500174 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(20.103356956)) ||
Vince Lehman9a709032014-09-13 16:28:07 -0500175 (faceUri == ROUTER_C_FACE && cost == 0));
176 }
177}
178
179BOOST_AUTO_TEST_SUITE_END()
180
181} //namespace test
182} //namespace nlsr