Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 44 | : face(m_ioService, m_keyChain) |
| 45 | , nlsr(m_ioService, m_scheduler, face, m_keyChain) |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 63 | Adjacent a(ROUTER_A_NAME, ndn::FaceUri(ROUTER_A_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 64 | Adjacent b(ROUTER_B_NAME, ndn::FaceUri(ROUTER_B_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 65 | Adjacent c(ROUTER_C_NAME, ndn::FaceUri(ROUTER_C_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 75 | AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencyListA); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 86 | AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 97 | AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 98 | lsdb.installAdjLsa(adjC); |
| 99 | |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 100 | map.createFromAdjLsdb(lsdb.getAdjLsdb().begin(), lsdb.getAdjLsdb().end()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | public: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 104 | ndn::util::DummyClientFace face; |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 128 | const std::string LinkStateCalculatorFixture::ROUTER_A_FACE = "udp4://10.0.0.1"; |
| 129 | const std::string LinkStateCalculatorFixture::ROUTER_B_FACE = "udp4://10.0.0.2"; |
| 130 | const std::string LinkStateCalculatorFixture::ROUTER_C_FACE = "udp4://10.0.0.3"; |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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 |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 178 | ndn::Name key = ndn::Name(ROUTER_B_NAME).append(std::to_string(Lsa::Type::ADJACENCY)); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 179 | AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key); |
| 180 | BOOST_REQUIRE(lsa != nullptr); |
| 181 | |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 182 | auto c = lsa->getAdl().findAdjacent(ROUTER_C_NAME); |
| 183 | BOOST_REQUIRE(c != nlsr.getAdjacencyList().end()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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 |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 227 | ndn::Name key = ndn::Name(ROUTER_B_NAME).append(std::to_string(Lsa::Type::ADJACENCY)); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 228 | AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key); |
| 229 | BOOST_REQUIRE(lsa != nullptr); |
| 230 | |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 231 | auto c = lsa->getAdl().findAdjacent(ROUTER_C_NAME); |
| 232 | BOOST_REQUIRE(c != nlsr.getAdjacencyList().end()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 247 | const NextHop& nextHopForB = *(bHopList.getNextHops().begin()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 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 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 259 | const NextHop& nextHopForC = *(cHopList.getNextHops().begin()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 260 | |
| 261 | BOOST_CHECK(nextHopForC.getConnectingFaceUri() == ROUTER_C_FACE && |
| 262 | nextHopForC.getRouteCostAsAdjustedInteger() == LINK_AC_COST); |
| 263 | } |
| 264 | |
| 265 | BOOST_AUTO_TEST_SUITE_END() |
| 266 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 267 | } // namespace test |
| 268 | } // namespace nlsr |