Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, 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) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 45 | , conf(face) |
| 46 | , confProcessor(conf) |
| 47 | , nlsr(face, m_keyChain, conf) |
| 48 | , routingTable(nlsr.m_routingTable) |
| 49 | , lsdb(nlsr.m_lsdb) |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 50 | { |
| 51 | setUpTopology(); |
| 52 | } |
| 53 | |
| 54 | // Triangle topology with routers A, B, C connected |
| 55 | void setUpTopology() |
| 56 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 57 | Adjacent a(ROUTER_A_NAME, ndn::FaceUri(ROUTER_A_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 58 | Adjacent b(ROUTER_B_NAME, ndn::FaceUri(ROUTER_B_FACE), 0, Adjacent::STATUS_ACTIVE, 0, 0); |
| 59 | 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] | 60 | |
| 61 | // Router A |
| 62 | b.setLinkCost(LINK_AB_COST); |
| 63 | c.setLinkCost(LINK_AC_COST); |
| 64 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 65 | AdjacencyList& adjacencyListA = conf.getAdjacencyList(); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 66 | adjacencyListA.insert(b); |
| 67 | adjacencyListA.insert(c); |
| 68 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 69 | AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencyListA); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 70 | lsdb.installAdjLsa(adjA); |
| 71 | |
| 72 | // Router B |
| 73 | a.setLinkCost(LINK_AB_COST); |
| 74 | c.setLinkCost(LINK_BC_COST); |
| 75 | |
| 76 | AdjacencyList adjacencyListB; |
| 77 | adjacencyListB.insert(a); |
| 78 | adjacencyListB.insert(c); |
| 79 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 80 | AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 81 | lsdb.installAdjLsa(adjB); |
| 82 | |
| 83 | // Router C |
| 84 | a.setLinkCost(LINK_AC_COST); |
| 85 | b.setLinkCost(LINK_BC_COST); |
| 86 | |
| 87 | AdjacencyList adjacencyListC; |
| 88 | adjacencyListC.insert(a); |
| 89 | adjacencyListC.insert(b); |
| 90 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 91 | AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 92 | lsdb.installAdjLsa(adjC); |
| 93 | |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 94 | map.createFromAdjLsdb(lsdb.getAdjLsdb().begin(), lsdb.getAdjLsdb().end()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | public: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 98 | ndn::util::DummyClientFace face; |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 99 | ConfParameter conf; |
| 100 | DummyConfFileProcessor confProcessor; |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 101 | Nlsr nlsr; |
| 102 | Map map; |
| 103 | |
| 104 | RoutingTable& routingTable; |
| 105 | Lsdb& lsdb; |
| 106 | |
| 107 | static const ndn::Name ROUTER_A_NAME; |
| 108 | static const ndn::Name ROUTER_B_NAME; |
| 109 | static const ndn::Name ROUTER_C_NAME; |
| 110 | |
| 111 | static const std::string ROUTER_A_FACE; |
| 112 | static const std::string ROUTER_B_FACE; |
| 113 | static const std::string ROUTER_C_FACE; |
| 114 | |
| 115 | static const double LINK_AB_COST; |
| 116 | static const double LINK_AC_COST; |
| 117 | static const double LINK_BC_COST; |
| 118 | }; |
| 119 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 120 | const ndn::Name LinkStateCalculatorFixture::ROUTER_A_NAME = "/ndn/site/%C1.Router/this-router"; |
| 121 | const ndn::Name LinkStateCalculatorFixture::ROUTER_B_NAME = "/ndn/site/%C1.Router/b"; |
| 122 | const ndn::Name LinkStateCalculatorFixture::ROUTER_C_NAME = "/ndn/site/%C1.Router/c"; |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 123 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 124 | const std::string LinkStateCalculatorFixture::ROUTER_A_FACE = "udp4://10.0.0.1"; |
| 125 | const std::string LinkStateCalculatorFixture::ROUTER_B_FACE = "udp4://10.0.0.2"; |
| 126 | const std::string LinkStateCalculatorFixture::ROUTER_C_FACE = "udp4://10.0.0.3"; |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 127 | |
| 128 | const double LinkStateCalculatorFixture::LINK_AB_COST = 5; |
| 129 | const double LinkStateCalculatorFixture::LINK_AC_COST = 10; |
| 130 | const double LinkStateCalculatorFixture::LINK_BC_COST = 17; |
| 131 | |
| 132 | BOOST_FIXTURE_TEST_SUITE(TestLinkStateRoutingCalculator, LinkStateCalculatorFixture) |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(Basic) |
| 135 | { |
| 136 | LinkStateRoutingTableCalculator calculator(map.getMapSize()); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 137 | calculator.calculatePath(map, routingTable, conf, lsdb.getAdjLsdb()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 138 | |
| 139 | RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME); |
| 140 | BOOST_REQUIRE(entryB != nullptr); |
| 141 | |
| 142 | // Router A should be able to get to B through B and to B through C |
| 143 | NexthopList& bHopList = entryB->getNexthopList(); |
| 144 | BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2); |
| 145 | |
| 146 | for (const NextHop& hop : bHopList) { |
| 147 | std::string faceUri = hop.getConnectingFaceUri(); |
| 148 | uint64_t cost = hop.getRouteCostAsAdjustedInteger(); |
| 149 | |
| 150 | BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == LINK_AB_COST) || |
| 151 | (faceUri == ROUTER_C_FACE && cost == LINK_AC_COST + LINK_BC_COST)); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME); |
| 156 | BOOST_REQUIRE(entryC != nullptr); |
| 157 | |
| 158 | // Router A should be able to get to C through C and to C through B |
| 159 | NexthopList& cHopList = entryC->getNexthopList(); |
| 160 | BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2); |
| 161 | |
| 162 | for (const NextHop& hop : cHopList) { |
| 163 | std::string faceUri = hop.getConnectingFaceUri(); |
| 164 | uint64_t cost = hop.getRouteCostAsAdjustedInteger(); |
| 165 | |
| 166 | BOOST_CHECK((faceUri == ROUTER_C_FACE && cost == LINK_AC_COST) || |
| 167 | (faceUri == ROUTER_B_FACE && cost == LINK_AB_COST + LINK_BC_COST)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | BOOST_AUTO_TEST_CASE(Asymmetric) |
| 172 | { |
| 173 | // Asymmetric link cost between B and C |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 174 | ndn::Name key = ndn::Name(ROUTER_B_NAME).append(std::to_string(Lsa::Type::ADJACENCY)); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 175 | AdjLsa* lsa = nlsr.m_lsdb.findAdjLsa(key); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 176 | BOOST_REQUIRE(lsa != nullptr); |
| 177 | |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 178 | auto c = lsa->getAdl().findAdjacent(ROUTER_C_NAME); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 179 | BOOST_REQUIRE(c != conf.getAdjacencyList().end()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 180 | |
| 181 | double higherLinkCost = LINK_BC_COST + 1; |
| 182 | c->setLinkCost(higherLinkCost); |
| 183 | |
| 184 | // Calculation should consider the link between B and C as having cost = higherLinkCost |
| 185 | LinkStateRoutingTableCalculator calculator(map.getMapSize()); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 186 | calculator.calculatePath(map, routingTable, conf, lsdb.getAdjLsdb()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 187 | |
| 188 | RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME); |
| 189 | BOOST_REQUIRE(entryB != nullptr); |
| 190 | |
| 191 | // Router A should be able to get to B through B and to B through C |
| 192 | NexthopList& bHopList = entryB->getNexthopList(); |
| 193 | BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2); |
| 194 | |
| 195 | for (const NextHop& hop : bHopList) { |
| 196 | std::string faceUri = hop.getConnectingFaceUri(); |
| 197 | uint64_t cost = hop.getRouteCostAsAdjustedInteger(); |
| 198 | |
| 199 | BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == LINK_AB_COST) || |
| 200 | (faceUri == ROUTER_C_FACE && cost == LINK_AC_COST + higherLinkCost)); |
| 201 | |
| 202 | } |
| 203 | |
| 204 | RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME); |
| 205 | BOOST_REQUIRE(entryC != nullptr); |
| 206 | |
| 207 | // Router A should be able to get to C through C and to C through B |
| 208 | NexthopList& cHopList = entryC->getNexthopList(); |
| 209 | BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2); |
| 210 | |
| 211 | for (const NextHop& hop : cHopList) { |
| 212 | std::string faceUri = hop.getConnectingFaceUri(); |
| 213 | uint64_t cost = hop.getRouteCostAsAdjustedInteger(); |
| 214 | |
| 215 | BOOST_CHECK((faceUri == ROUTER_C_FACE && cost == LINK_AC_COST) || |
| 216 | (faceUri == ROUTER_B_FACE && cost == LINK_AB_COST + higherLinkCost)); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | BOOST_AUTO_TEST_CASE(AsymmetricZeroCost) |
| 221 | { |
| 222 | // Asymmetric link cost between B and C |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 223 | ndn::Name key = ndn::Name(ROUTER_B_NAME).append(std::to_string(Lsa::Type::ADJACENCY)); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 224 | AdjLsa* lsa = nlsr.m_lsdb.findAdjLsa(key); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 225 | BOOST_REQUIRE(lsa != nullptr); |
| 226 | |
Nick Gordon | c780a69 | 2017-04-27 18:03:02 -0500 | [diff] [blame] | 227 | auto c = lsa->getAdl().findAdjacent(ROUTER_C_NAME); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 228 | BOOST_REQUIRE(c != conf.getAdjacencyList().end()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 229 | |
| 230 | c->setLinkCost(0); |
| 231 | |
| 232 | // Calculation should consider the link between B and C as down |
| 233 | LinkStateRoutingTableCalculator calculator(map.getMapSize()); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 234 | calculator.calculatePath(map, routingTable, conf, lsdb.getAdjLsdb()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 235 | |
| 236 | // Router A should be able to get to B through B but not through C |
| 237 | RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME); |
| 238 | BOOST_REQUIRE(entryB != nullptr); |
| 239 | |
| 240 | NexthopList& bHopList = entryB->getNexthopList(); |
| 241 | BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 1); |
| 242 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 243 | const NextHop& nextHopForB = *(bHopList.getNextHops().begin()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 244 | |
| 245 | BOOST_CHECK(nextHopForB.getConnectingFaceUri() == ROUTER_B_FACE && |
| 246 | nextHopForB.getRouteCostAsAdjustedInteger() == LINK_AB_COST); |
| 247 | |
| 248 | // Router A should be able to get to C through C but not through B |
| 249 | RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME); |
| 250 | BOOST_REQUIRE(entryC != nullptr); |
| 251 | |
| 252 | NexthopList& cHopList = entryC->getNexthopList(); |
| 253 | BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 1); |
| 254 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 255 | const NextHop& nextHopForC = *(cHopList.getNextHops().begin()); |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 256 | |
| 257 | BOOST_CHECK(nextHopForC.getConnectingFaceUri() == ROUTER_C_FACE && |
| 258 | nextHopForC.getRouteCostAsAdjustedInteger() == LINK_AC_COST); |
| 259 | } |
| 260 | |
| 261 | BOOST_AUTO_TEST_SUITE_END() |
| 262 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 263 | } // namespace test |
| 264 | } // namespace nlsr |