Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 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/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "route/routing-table-calculator.hpp" |
| 23 | |
| 24 | #include "adjacency-list.hpp" |
| 25 | #include "lsa.hpp" |
| 26 | #include "lsdb.hpp" |
| 27 | #include "nlsr.hpp" |
| 28 | #include "test-common.hpp" |
| 29 | #include "route/map.hpp" |
| 30 | #include "route/routing-table.hpp" |
| 31 | |
| 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 33 | |
| 34 | namespace nlsr { |
| 35 | namespace test { |
| 36 | |
| 37 | static const ndn::time::system_clock::TimePoint MAX_TIME = |
| 38 | ndn::time::system_clock::TimePoint::max(); |
| 39 | |
| 40 | class LinkStateCalculatorFixture : public BaseFixture |
| 41 | { |
| 42 | public: |
| 43 | LinkStateCalculatorFixture() |
| 44 | : face(ndn::util::makeDummyClientFace(g_ioService)) |
| 45 | , nlsr(g_ioService, g_scheduler, ndn::ref(*face)) |
| 46 | , routingTable(nlsr.getRoutingTable()) |
| 47 | , lsdb(nlsr.getLsdb()) |
| 48 | { |
| 49 | setUpTopology(); |
| 50 | } |
| 51 | |
| 52 | // Triangle topology with routers A, B, C connected |
| 53 | void setUpTopology() |
| 54 | { |
| 55 | INIT_LOGGERS("/tmp", "TRACE"); |
| 56 | |
| 57 | ConfParameter& conf = nlsr.getConfParameter(); |
| 58 | conf.setNetwork("/ndn"); |
| 59 | conf.setSiteName("/router"); |
| 60 | conf.setRouterName("/a"); |
| 61 | conf.buildRouterPrefix(); |
| 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 | b.setLinkCost(LINK_AB_COST); |
| 69 | c.setLinkCost(LINK_AC_COST); |
| 70 | |
| 71 | AdjacencyList& adjacencyListA = nlsr.getAdjacencyList(); |
| 72 | adjacencyListA.insert(b); |
| 73 | adjacencyListA.insert(c); |
| 74 | |
| 75 | AdjLsa adjA(a.getName(), AdjLsa::TYPE_STRING, 1, MAX_TIME, 2, adjacencyListA); |
| 76 | lsdb.installAdjLsa(adjA); |
| 77 | |
| 78 | // Router B |
| 79 | a.setLinkCost(LINK_AB_COST); |
| 80 | c.setLinkCost(LINK_BC_COST); |
| 81 | |
| 82 | AdjacencyList adjacencyListB; |
| 83 | adjacencyListB.insert(a); |
| 84 | adjacencyListB.insert(c); |
| 85 | |
| 86 | AdjLsa adjB(b.getName(), AdjLsa::TYPE_STRING, 1, MAX_TIME, 2, adjacencyListB); |
| 87 | lsdb.installAdjLsa(adjB); |
| 88 | |
| 89 | // Router C |
| 90 | a.setLinkCost(LINK_AC_COST); |
| 91 | b.setLinkCost(LINK_BC_COST); |
| 92 | |
| 93 | AdjacencyList adjacencyListC; |
| 94 | adjacencyListC.insert(a); |
| 95 | adjacencyListC.insert(b); |
| 96 | |
| 97 | AdjLsa adjC(c.getName(), AdjLsa::TYPE_STRING, 1, MAX_TIME, 2, adjacencyListC); |
| 98 | lsdb.installAdjLsa(adjC); |
| 99 | |
| 100 | map.createFromAdjLsdb(nlsr); |
| 101 | } |
| 102 | |
| 103 | public: |
| 104 | shared_ptr<ndn::util::DummyClientFace> face; |
| 105 | Nlsr nlsr; |
| 106 | Map map; |
| 107 | |
| 108 | RoutingTable& routingTable; |
| 109 | Lsdb& lsdb; |
| 110 | |
| 111 | static const ndn::Name ROUTER_A_NAME; |
| 112 | static const ndn::Name ROUTER_B_NAME; |
| 113 | static const ndn::Name ROUTER_C_NAME; |
| 114 | |
| 115 | static const std::string ROUTER_A_FACE; |
| 116 | static const std::string ROUTER_B_FACE; |
| 117 | static const std::string ROUTER_C_FACE; |
| 118 | |
| 119 | static const double LINK_AB_COST; |
| 120 | static const double LINK_AC_COST; |
| 121 | static const double LINK_BC_COST; |
| 122 | }; |
| 123 | |
| 124 | const ndn::Name LinkStateCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a"; |
| 125 | const ndn::Name LinkStateCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b"; |
| 126 | const ndn::Name LinkStateCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c"; |
| 127 | |
| 128 | const std::string LinkStateCalculatorFixture::ROUTER_A_FACE = "face-a"; |
| 129 | const std::string LinkStateCalculatorFixture::ROUTER_B_FACE = "face-b"; |
| 130 | const std::string LinkStateCalculatorFixture::ROUTER_C_FACE = "face-c"; |
| 131 | |
| 132 | const double LinkStateCalculatorFixture::LINK_AB_COST = 5; |
| 133 | const double LinkStateCalculatorFixture::LINK_AC_COST = 10; |
| 134 | const double LinkStateCalculatorFixture::LINK_BC_COST = 17; |
| 135 | |
| 136 | BOOST_FIXTURE_TEST_SUITE(TestLinkStateRoutingCalculator, LinkStateCalculatorFixture) |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(Basic) |
| 139 | { |
| 140 | LinkStateRoutingTableCalculator calculator(map.getMapSize()); |
| 141 | calculator.calculatePath(map, routingTable, nlsr); |
| 142 | |
| 143 | RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME); |
| 144 | BOOST_REQUIRE(entryB != nullptr); |
| 145 | |
| 146 | // Router A should be able to get to B through B and to B through C |
| 147 | NexthopList& bHopList = entryB->getNexthopList(); |
| 148 | BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2); |
| 149 | |
| 150 | for (const NextHop& hop : bHopList) { |
| 151 | std::string faceUri = hop.getConnectingFaceUri(); |
| 152 | uint64_t cost = hop.getRouteCostAsAdjustedInteger(); |
| 153 | |
| 154 | BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == LINK_AB_COST) || |
| 155 | (faceUri == ROUTER_C_FACE && cost == LINK_AC_COST + LINK_BC_COST)); |
| 156 | |
| 157 | } |
| 158 | |
| 159 | RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME); |
| 160 | BOOST_REQUIRE(entryC != nullptr); |
| 161 | |
| 162 | // Router A should be able to get to C through C and to C through B |
| 163 | NexthopList& cHopList = entryC->getNexthopList(); |
| 164 | BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2); |
| 165 | |
| 166 | for (const NextHop& hop : cHopList) { |
| 167 | std::string faceUri = hop.getConnectingFaceUri(); |
| 168 | uint64_t cost = hop.getRouteCostAsAdjustedInteger(); |
| 169 | |
| 170 | BOOST_CHECK((faceUri == ROUTER_C_FACE && cost == LINK_AC_COST) || |
| 171 | (faceUri == ROUTER_B_FACE && cost == LINK_AB_COST + LINK_BC_COST)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(Asymmetric) |
| 176 | { |
| 177 | // Asymmetric link cost between B and C |
| 178 | ndn::Name key = ndn::Name(ROUTER_B_NAME).append(AdjLsa::TYPE_STRING); |
| 179 | AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key); |
| 180 | BOOST_REQUIRE(lsa != nullptr); |
| 181 | |
| 182 | Adjacent* c = lsa->getAdl().findAdjacent(ROUTER_C_NAME); |
| 183 | BOOST_REQUIRE(c != nullptr); |
| 184 | |
| 185 | double higherLinkCost = LINK_BC_COST + 1; |
| 186 | c->setLinkCost(higherLinkCost); |
| 187 | |
| 188 | // Calculation should consider the link between B and C as having cost = higherLinkCost |
| 189 | LinkStateRoutingTableCalculator calculator(map.getMapSize()); |
| 190 | calculator.calculatePath(map, routingTable, nlsr); |
| 191 | |
| 192 | RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME); |
| 193 | BOOST_REQUIRE(entryB != nullptr); |
| 194 | |
| 195 | // Router A should be able to get to B through B and to B through C |
| 196 | NexthopList& bHopList = entryB->getNexthopList(); |
| 197 | BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2); |
| 198 | |
| 199 | for (const NextHop& hop : bHopList) { |
| 200 | std::string faceUri = hop.getConnectingFaceUri(); |
| 201 | uint64_t cost = hop.getRouteCostAsAdjustedInteger(); |
| 202 | |
| 203 | BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == LINK_AB_COST) || |
| 204 | (faceUri == ROUTER_C_FACE && cost == LINK_AC_COST + higherLinkCost)); |
| 205 | |
| 206 | } |
| 207 | |
| 208 | RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME); |
| 209 | BOOST_REQUIRE(entryC != nullptr); |
| 210 | |
| 211 | // Router A should be able to get to C through C and to C through B |
| 212 | NexthopList& cHopList = entryC->getNexthopList(); |
| 213 | BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2); |
| 214 | |
| 215 | for (const NextHop& hop : cHopList) { |
| 216 | std::string faceUri = hop.getConnectingFaceUri(); |
| 217 | uint64_t cost = hop.getRouteCostAsAdjustedInteger(); |
| 218 | |
| 219 | BOOST_CHECK((faceUri == ROUTER_C_FACE && cost == LINK_AC_COST) || |
| 220 | (faceUri == ROUTER_B_FACE && cost == LINK_AB_COST + higherLinkCost)); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | BOOST_AUTO_TEST_CASE(AsymmetricZeroCost) |
| 225 | { |
| 226 | // Asymmetric link cost between B and C |
| 227 | ndn::Name key = ndn::Name(ROUTER_B_NAME).append(AdjLsa::TYPE_STRING); |
| 228 | AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key); |
| 229 | BOOST_REQUIRE(lsa != nullptr); |
| 230 | |
| 231 | Adjacent* c = lsa->getAdl().findAdjacent(ROUTER_C_NAME); |
| 232 | BOOST_REQUIRE(c != nullptr); |
| 233 | |
| 234 | c->setLinkCost(0); |
| 235 | |
| 236 | // Calculation should consider the link between B and C as down |
| 237 | LinkStateRoutingTableCalculator calculator(map.getMapSize()); |
| 238 | calculator.calculatePath(map, routingTable, nlsr); |
| 239 | |
| 240 | // Router A should be able to get to B through B but not through C |
| 241 | RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME); |
| 242 | BOOST_REQUIRE(entryB != nullptr); |
| 243 | |
| 244 | NexthopList& bHopList = entryB->getNexthopList(); |
| 245 | BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 1); |
| 246 | |
| 247 | NextHop& nextHopForB = bHopList.getNextHops().front(); |
| 248 | |
| 249 | BOOST_CHECK(nextHopForB.getConnectingFaceUri() == ROUTER_B_FACE && |
| 250 | nextHopForB.getRouteCostAsAdjustedInteger() == LINK_AB_COST); |
| 251 | |
| 252 | // Router A should be able to get to C through C but not through B |
| 253 | RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME); |
| 254 | BOOST_REQUIRE(entryC != nullptr); |
| 255 | |
| 256 | NexthopList& cHopList = entryC->getNexthopList(); |
| 257 | BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 1); |
| 258 | |
| 259 | NextHop& nextHopForC = cHopList.getNextHops().front(); |
| 260 | |
| 261 | BOOST_CHECK(nextHopForC.getConnectingFaceUri() == ROUTER_C_FACE && |
| 262 | nextHopForC.getRouteCostAsAdjustedInteger() == LINK_AC_COST); |
| 263 | } |
| 264 | |
| 265 | BOOST_AUTO_TEST_SUITE_END() |
| 266 | |
| 267 | } //namespace test |
| 268 | } //namespace nlsr |