blob: 31c9abf30bcf7d3f1b26d791e939dd0186676755 [file] [log] [blame]
Vince Lehman41b173e2015-05-07 14:13:26 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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/>.
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
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)
45 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Vince Lehman41b173e2015-05-07 14:13:26 -050046 , 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 Chowdhuryf04f9892017-08-20 20:42:56 -050063 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 Lehman41b173e2015-05-07 14:13:26 -050066
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 Gawanded02c3882015-12-29 16:02:51 -060075 AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencyListA);
Vince Lehman41b173e2015-05-07 14:13:26 -050076 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 Gawanded02c3882015-12-29 16:02:51 -060086 AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB);
Vince Lehman41b173e2015-05-07 14:13:26 -050087 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 Gawanded02c3882015-12-29 16:02:51 -060097 AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC);
Vince Lehman41b173e2015-05-07 14:13:26 -050098 lsdb.installAdjLsa(adjC);
99
Nick Gordon22b5c952017-08-10 17:48:15 -0500100 map.createFromAdjLsdb(lsdb.getAdjLsdb().begin(), lsdb.getAdjLsdb().end());
Vince Lehman41b173e2015-05-07 14:13:26 -0500101 }
102
103public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500104 ndn::util::DummyClientFace face;
Vince Lehman41b173e2015-05-07 14:13:26 -0500105 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
124const ndn::Name LinkStateCalculatorFixture::ROUTER_A_NAME = "/ndn/router/a";
125const ndn::Name LinkStateCalculatorFixture::ROUTER_B_NAME = "/ndn/router/b";
126const ndn::Name LinkStateCalculatorFixture::ROUTER_C_NAME = "/ndn/router/c";
127
Nick Gordone9733ed2017-04-26 10:48:39 -0500128const std::string LinkStateCalculatorFixture::ROUTER_A_FACE = "udp4://10.0.0.1";
129const std::string LinkStateCalculatorFixture::ROUTER_B_FACE = "udp4://10.0.0.2";
130const std::string LinkStateCalculatorFixture::ROUTER_C_FACE = "udp4://10.0.0.3";
Vince Lehman41b173e2015-05-07 14:13:26 -0500131
132const double LinkStateCalculatorFixture::LINK_AB_COST = 5;
133const double LinkStateCalculatorFixture::LINK_AC_COST = 10;
134const double LinkStateCalculatorFixture::LINK_BC_COST = 17;
135
136BOOST_FIXTURE_TEST_SUITE(TestLinkStateRoutingCalculator, LinkStateCalculatorFixture)
137
138BOOST_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
175BOOST_AUTO_TEST_CASE(Asymmetric)
176{
177 // Asymmetric link cost between B and C
Nick Gordon727d4832017-10-13 18:04:25 -0500178 ndn::Name key = ndn::Name(ROUTER_B_NAME).append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehman41b173e2015-05-07 14:13:26 -0500179 AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key);
180 BOOST_REQUIRE(lsa != nullptr);
181
Nick Gordonc780a692017-04-27 18:03:02 -0500182 auto c = lsa->getAdl().findAdjacent(ROUTER_C_NAME);
183 BOOST_REQUIRE(c != nlsr.getAdjacencyList().end());
Vince Lehman41b173e2015-05-07 14:13:26 -0500184
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
224BOOST_AUTO_TEST_CASE(AsymmetricZeroCost)
225{
226 // Asymmetric link cost between B and C
Nick Gordon727d4832017-10-13 18:04:25 -0500227 ndn::Name key = ndn::Name(ROUTER_B_NAME).append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehman41b173e2015-05-07 14:13:26 -0500228 AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key);
229 BOOST_REQUIRE(lsa != nullptr);
230
Nick Gordonc780a692017-04-27 18:03:02 -0500231 auto c = lsa->getAdl().findAdjacent(ROUTER_C_NAME);
232 BOOST_REQUIRE(c != nlsr.getAdjacencyList().end());
Vince Lehman41b173e2015-05-07 14:13:26 -0500233
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 Lehmanef21d8e2015-04-01 15:59:39 -0500247 const NextHop& nextHopForB = *(bHopList.getNextHops().begin());
Vince Lehman41b173e2015-05-07 14:13:26 -0500248
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 Lehmanef21d8e2015-04-01 15:59:39 -0500259 const NextHop& nextHopForC = *(cHopList.getNextHops().begin());
Vince Lehman41b173e2015-05-07 14:13:26 -0500260
261 BOOST_CHECK(nextHopForC.getConnectingFaceUri() == ROUTER_C_FACE &&
262 nextHopForC.getRouteCostAsAdjustedInteger() == LINK_AC_COST);
263}
264
265BOOST_AUTO_TEST_SUITE_END()
266
Nick Gordonfad8e252016-08-11 14:21:38 -0500267} // namespace test
268} // namespace nlsr