blob: 057b1c8126ddf25e6a7ce977c98f7d41a732710f [file] [log] [blame]
Vince Lehman41b173e2015-05-07 14:13:26 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, The University of Memphis,
Vince Lehman41b173e2015-05-07 14:13:26 -05004 * 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/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070020 */
Vince Lehman41b173e2015-05-07 14:13:26 -050021
22#include "route/routing-table-calculator.hpp"
23
24#include "adjacency-list.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050025#include "lsdb.hpp"
26#include "nlsr.hpp"
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070027#include "../test-common.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050028#include "route/map.hpp"
29#include "route/routing-table.hpp"
dulalsaurabd0816a32019-07-26 13:11:24 +000030#include "adjacent.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050031
32#include <ndn-cxx/util/dummy-client-face.hpp>
33
34namespace nlsr {
35namespace test {
36
37static const ndn::time::system_clock::TimePoint MAX_TIME =
38 ndn::time::system_clock::TimePoint::max();
39
40class LinkStateCalculatorFixture : public BaseFixture
41{
42public:
43 LinkStateCalculatorFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050044 : face(m_ioService, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060045 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060046 , confProcessor(conf)
47 , nlsr(face, m_keyChain, conf)
48 , routingTable(nlsr.m_routingTable)
49 , lsdb(nlsr.m_lsdb)
Vince Lehman41b173e2015-05-07 14:13:26 -050050 {
51 setUpTopology();
52 }
53
54 // Triangle topology with routers A, B, C connected
55 void setUpTopology()
56 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050057 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 Lehman41b173e2015-05-07 14:13:26 -050060
61 // Router A
62 b.setLinkCost(LINK_AB_COST);
63 c.setLinkCost(LINK_AC_COST);
64
Ashlesh Gawande85998a12017-12-07 22:22:13 -060065 AdjacencyList& adjacencyListA = conf.getAdjacencyList();
Vince Lehman41b173e2015-05-07 14:13:26 -050066 adjacencyListA.insert(b);
67 adjacencyListA.insert(c);
68
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060069 AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencyListA);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070070 lsdb.installLsa(std::make_shared<AdjLsa>(adjA));
Vince Lehman41b173e2015-05-07 14:13:26 -050071
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 Gawanded02c3882015-12-29 16:02:51 -060080 AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070081 lsdb.installLsa(std::make_shared<AdjLsa>(adjB));
Vince Lehman41b173e2015-05-07 14:13:26 -050082
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 Gawanded02c3882015-12-29 16:02:51 -060091 AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070092 lsdb.installLsa(std::make_shared<AdjLsa>(adjC));
Vince Lehman41b173e2015-05-07 14:13:26 -050093
Ashlesh Gawande57a87172020-05-09 19:47:06 -070094 auto lsaRange = lsdb.getLsdbIterator<AdjLsa>();
95 map.createFromAdjLsdb(lsaRange.first, lsaRange.second);
Vince Lehman41b173e2015-05-07 14:13:26 -050096 }
97
98public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050099 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600100 ConfParameter conf;
101 DummyConfFileProcessor confProcessor;
Vince Lehman41b173e2015-05-07 14:13:26 -0500102 Nlsr nlsr;
103 Map map;
104
105 RoutingTable& routingTable;
106 Lsdb& lsdb;
107
108 static const ndn::Name ROUTER_A_NAME;
109 static const ndn::Name ROUTER_B_NAME;
110 static const ndn::Name ROUTER_C_NAME;
111
112 static const std::string ROUTER_A_FACE;
113 static const std::string ROUTER_B_FACE;
114 static const std::string ROUTER_C_FACE;
115
116 static const double LINK_AB_COST;
117 static const double LINK_AC_COST;
118 static const double LINK_BC_COST;
119};
120
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600121const ndn::Name LinkStateCalculatorFixture::ROUTER_A_NAME = "/ndn/site/%C1.Router/this-router";
122const ndn::Name LinkStateCalculatorFixture::ROUTER_B_NAME = "/ndn/site/%C1.Router/b";
123const ndn::Name LinkStateCalculatorFixture::ROUTER_C_NAME = "/ndn/site/%C1.Router/c";
Vince Lehman41b173e2015-05-07 14:13:26 -0500124
Nick Gordone9733ed2017-04-26 10:48:39 -0500125const std::string LinkStateCalculatorFixture::ROUTER_A_FACE = "udp4://10.0.0.1";
126const std::string LinkStateCalculatorFixture::ROUTER_B_FACE = "udp4://10.0.0.2";
127const std::string LinkStateCalculatorFixture::ROUTER_C_FACE = "udp4://10.0.0.3";
Vince Lehman41b173e2015-05-07 14:13:26 -0500128
129const double LinkStateCalculatorFixture::LINK_AB_COST = 5;
130const double LinkStateCalculatorFixture::LINK_AC_COST = 10;
131const double LinkStateCalculatorFixture::LINK_BC_COST = 17;
132
133BOOST_FIXTURE_TEST_SUITE(TestLinkStateRoutingCalculator, LinkStateCalculatorFixture)
134
135BOOST_AUTO_TEST_CASE(Basic)
136{
137 LinkStateRoutingTableCalculator calculator(map.getMapSize());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700138 calculator.calculatePath(map, routingTable, conf, lsdb);
Vince Lehman41b173e2015-05-07 14:13:26 -0500139
140 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
141 BOOST_REQUIRE(entryB != nullptr);
142
143 // Router A should be able to get to B through B and to B through C
144 NexthopList& bHopList = entryB->getNexthopList();
145 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
146
147 for (const NextHop& hop : bHopList) {
148 std::string faceUri = hop.getConnectingFaceUri();
149 uint64_t cost = hop.getRouteCostAsAdjustedInteger();
150
151 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == LINK_AB_COST) ||
152 (faceUri == ROUTER_C_FACE && cost == LINK_AC_COST + LINK_BC_COST));
153
154 }
155
156 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
157 BOOST_REQUIRE(entryC != nullptr);
158
159 // Router A should be able to get to C through C and to C through B
160 NexthopList& cHopList = entryC->getNexthopList();
161 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
162
163 for (const NextHop& hop : cHopList) {
164 std::string faceUri = hop.getConnectingFaceUri();
165 uint64_t cost = hop.getRouteCostAsAdjustedInteger();
166
167 BOOST_CHECK((faceUri == ROUTER_C_FACE && cost == LINK_AC_COST) ||
168 (faceUri == ROUTER_B_FACE && cost == LINK_AB_COST + LINK_BC_COST));
169 }
170}
171
172BOOST_AUTO_TEST_CASE(Asymmetric)
173{
174 // Asymmetric link cost between B and C
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700175 auto lsa = nlsr.m_lsdb.findLsa<AdjLsa>(ndn::Name(ROUTER_B_NAME));
Vince Lehman41b173e2015-05-07 14:13:26 -0500176 BOOST_REQUIRE(lsa != nullptr);
177
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800178 auto c = lsa->m_adl.findAdjacent(ROUTER_C_NAME);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600179 BOOST_REQUIRE(c != conf.getAdjacencyList().end());
Vince Lehman41b173e2015-05-07 14:13:26 -0500180
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 Gawande57a87172020-05-09 19:47:06 -0700186 calculator.calculatePath(map, routingTable, conf, lsdb);
Vince Lehman41b173e2015-05-07 14:13:26 -0500187
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
dulalsaurabd0816a32019-07-26 13:11:24 +0000220BOOST_AUTO_TEST_CASE(NonAdjacentCost)
Vince Lehman41b173e2015-05-07 14:13:26 -0500221{
222 // Asymmetric link cost between B and C
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700223 auto lsa = nlsr.m_lsdb.findLsa<AdjLsa>(ROUTER_B_NAME);
Vince Lehman41b173e2015-05-07 14:13:26 -0500224 BOOST_REQUIRE(lsa != nullptr);
225
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800226 auto c = lsa->m_adl.findAdjacent(ROUTER_C_NAME);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600227 BOOST_REQUIRE(c != conf.getAdjacencyList().end());
Vince Lehman41b173e2015-05-07 14:13:26 -0500228
dulalsaurabd0816a32019-07-26 13:11:24 +0000229 // Break the link between B - C by setting it to a NON_ADJACENT_COST.
230 c->setLinkCost(Adjacent::NON_ADJACENT_COST);
Vince Lehman41b173e2015-05-07 14:13:26 -0500231
232 // Calculation should consider the link between B and C as down
233 LinkStateRoutingTableCalculator calculator(map.getMapSize());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700234 calculator.calculatePath(map, routingTable, conf, lsdb);
Vince Lehman41b173e2015-05-07 14:13:26 -0500235
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
dulalsaurabd0816a32019-07-26 13:11:24 +0000240 auto bHopList = entryB->getNexthopList();
Vince Lehman41b173e2015-05-07 14:13:26 -0500241 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 1);
242
dulalsaurabd0816a32019-07-26 13:11:24 +0000243 const auto nextHopForB = bHopList.getNextHops().begin();
Vince Lehman41b173e2015-05-07 14:13:26 -0500244
dulalsaurabd0816a32019-07-26 13:11:24 +0000245 BOOST_CHECK(nextHopForB->getConnectingFaceUri() == ROUTER_B_FACE &&
246 nextHopForB->getRouteCostAsAdjustedInteger() == LINK_AB_COST);
Vince Lehman41b173e2015-05-07 14:13:26 -0500247
248 // Router A should be able to get to C through C but not through B
dulalsaurabd0816a32019-07-26 13:11:24 +0000249 auto entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
Vince Lehman41b173e2015-05-07 14:13:26 -0500250 BOOST_REQUIRE(entryC != nullptr);
251
252 NexthopList& cHopList = entryC->getNexthopList();
253 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 1);
254
dulalsaurabd0816a32019-07-26 13:11:24 +0000255 const auto nextHopForC = cHopList.getNextHops().begin();
Vince Lehman41b173e2015-05-07 14:13:26 -0500256
dulalsaurabd0816a32019-07-26 13:11:24 +0000257 BOOST_CHECK(nextHopForC->getConnectingFaceUri() == ROUTER_C_FACE &&
258 nextHopForC->getRouteCostAsAdjustedInteger() == LINK_AC_COST);
259}
260
261BOOST_AUTO_TEST_CASE(AsymmetricZeroCostLink)
262{
263 // Asymmetric and zero link cost between B - C, and B - A.
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700264 auto lsaB = nlsr.m_lsdb.findLsa<AdjLsa>(ROUTER_B_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000265 BOOST_REQUIRE(lsaB != nullptr);
266
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800267 auto c = lsaB->m_adl.findAdjacent(ROUTER_C_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000268 BOOST_REQUIRE(c != conf.getAdjacencyList().end());
269 // Re-adjust link cost to 0 from B-C. However, this should not set B-C cost 0 because C-B
270 // cost is greater that 0 i.e. 17
271 c->setLinkCost(0);
272
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800273 auto a = lsaB->m_adl.findAdjacent(ROUTER_A_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000274 BOOST_REQUIRE(a != conf.getAdjacencyList().end());
275
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700276 auto lsaA = nlsr.m_lsdb.findLsa<AdjLsa>(ROUTER_A_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000277 BOOST_REQUIRE(lsaA != nullptr);
278
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800279 auto b = lsaA->m_adl.findAdjacent(ROUTER_B_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000280 BOOST_REQUIRE(b != conf.getAdjacencyList().end());
281
282 // Re-adjust link cost to 0 from both the direction i.e B-A and A-B
283 a->setLinkCost(0);
284 b->setLinkCost(0);
285
286 // Calculation should consider 0 link-cost between B and C
287 LinkStateRoutingTableCalculator calculator(map.getMapSize());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700288 calculator.calculatePath(map, routingTable, conf, lsdb);
dulalsaurabd0816a32019-07-26 13:11:24 +0000289
290 // Router A should be able to get to B through B and C
291 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
292 BOOST_REQUIRE(entryB != nullptr);
293
294 // Node can have neighbors with zero cost, so the nexthop count should be 2
295 NexthopList& bHopList = entryB->getNexthopList();
296 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
297
298 const auto nextHopForB = bHopList.getNextHops().begin();
299 // Check if the next hop via B is through A or not after the cost adjustment
300 BOOST_CHECK(nextHopForB->getConnectingFaceUri() == ROUTER_B_FACE &&
301 nextHopForB->getRouteCostAsAdjustedInteger() == 0);
302
303 // Router A should be able to get to C through C and B
304 auto entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
305 BOOST_REQUIRE(entryC != nullptr);
306
307 NexthopList& cHopList = entryC->getNexthopList();
308 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
309
310 const auto nextHopForC = cHopList.getNextHops().begin();
311 // Check if the nextHop from C is via A or not
312 BOOST_CHECK(nextHopForC->getConnectingFaceUri() == ROUTER_C_FACE &&
313 nextHopForC->getRouteCostAsAdjustedInteger() == LINK_AC_COST);
314
Vince Lehman41b173e2015-05-07 14:13:26 -0500315}
316
317BOOST_AUTO_TEST_SUITE_END()
318
Nick Gordonfad8e252016-08-11 14:21:38 -0500319} // namespace test
320} // namespace nlsr