Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 1 | /* -*- 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 | |
| 37 | namespace nlsr { |
| 38 | namespace test { |
| 39 | |
| 40 | using ndn::DummyFace; |
| 41 | using ndn::shared_ptr; |
| 42 | using ndn::time::system_clock; |
| 43 | static const system_clock::TimePoint MAX_TIME = system_clock::TimePoint::max(); |
| 44 | |
| 45 | class HyperbolicCalculatorFixture : public BaseFixture |
| 46 | { |
| 47 | public: |
| 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 | |
| 71 | AdjLsa adjA(a.getName(), "adjacency", 1, MAX_TIME, 2, adjacencies); |
| 72 | lsdb.installAdjLsa(adjA); |
| 73 | |
| 74 | CoordinateLsa coordA(adjA.getOrigRouter(), "coordinate", 1, MAX_TIME, 16.23, 2.97); |
| 75 | 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 | |
| 85 | AdjLsa adjB(b.getName(), "adjacency", 1, MAX_TIME, 2, adjacencyListB); |
| 86 | lsdb.installAdjLsa(adjB); |
| 87 | |
| 88 | CoordinateLsa coordB(adjB.getOrigRouter(), "coordinate", 1, MAX_TIME, 16.59, 3.0); |
| 89 | 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 | |
| 99 | AdjLsa adjC(c.getName(), "adjacency", 1, MAX_TIME, 2, adjacencyListC); |
| 100 | lsdb.installAdjLsa(adjC); |
| 101 | |
| 102 | CoordinateLsa coordC(adjC.getOrigRouter(), "coordinate", 1, MAX_TIME, 14.11, 2.99); |
| 103 | lsdb.installCoordinateLsa(coordC); |
| 104 | |
| 105 | map.createFromAdjLsdb(nlsr); |
| 106 | } |
| 107 | |
| 108 | public: |
| 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 | |
| 126 | const ndn::Name HyperbolicCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a"; |
| 127 | const ndn::Name HyperbolicCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b"; |
| 128 | const ndn::Name HyperbolicCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c"; |
| 129 | |
| 130 | const std::string HyperbolicCalculatorFixture::ROUTER_A_FACE = "face-a"; |
| 131 | const std::string HyperbolicCalculatorFixture::ROUTER_B_FACE = "face-b"; |
| 132 | const std::string HyperbolicCalculatorFixture::ROUTER_C_FACE = "face-c"; |
| 133 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame^] | 134 | uint64_t |
| 135 | applyHyperbolicFactorAndRound(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 Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 143 | BOOST_FIXTURE_TEST_SUITE(TestHyperbolicRoutingCalculator, HyperbolicCalculatorFixture) |
| 144 | |
| 145 | BOOST_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 Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame^] | 152 | // Router A should be able to get to B through B with cost 0 and to B through C |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 153 | 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 Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame^] | 161 | (faceUri == ROUTER_C_FACE && cost == applyHyperbolicFactorAndRound(20.103356956))); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME); |
| 165 | |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame^] | 166 | // Router A should be able to get to C through C with cost 0 and to C through B |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 167 | 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 Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame^] | 174 | BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == applyHyperbolicFactorAndRound(20.103356956)) || |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 175 | (faceUri == ROUTER_C_FACE && cost == 0)); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | BOOST_AUTO_TEST_SUITE_END() |
| 180 | |
| 181 | } //namespace test |
| 182 | } //namespace nlsr |