blob: 5322cb71d1da46a9129b623c61e0465539214786 [file] [log] [blame]
Vince Lehmancae33b62015-06-05 09:21:30 -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 Lehmancae33b62015-06-05 09:21:30 -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
Nick Gordonc0c6bcf2017-08-15 18:11:21 -050022#include "route/name-prefix-table.hpp"
Vince Lehmancae33b62015-06-05 09:21:30 -050023#include "nlsr.hpp"
24#include "test-common.hpp"
25
26#include <ndn-cxx/util/dummy-client-face.hpp>
27
28namespace nlsr {
29namespace test {
30
31class NamePrefixTableFixture : public UnitTestTimeFixture
32{
33public:
34 NamePrefixTableFixture()
dmcoomes9f936662017-03-02 10:33:09 -060035 : face(std::make_shared<ndn::util::DummyClientFace>(g_ioService))
36 , nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain)
Vince Lehmancae33b62015-06-05 09:21:30 -050037 , lsdb(nlsr.getLsdb())
38 , npt(nlsr.getNamePrefixTable())
39 {
40 INIT_LOGGERS("/tmp", "DEBUG");
41 }
42
43public:
dmcoomes9f936662017-03-02 10:33:09 -060044 std::shared_ptr<ndn::util::DummyClientFace> face;
Vince Lehmancae33b62015-06-05 09:21:30 -050045 Nlsr nlsr;
46
47 Lsdb& lsdb;
48 NamePrefixTable& npt;
49};
50
51BOOST_AUTO_TEST_SUITE(TestNamePrefixTable)
52
53BOOST_FIXTURE_TEST_CASE(Bupt, NamePrefixTableFixture)
54{
55 ConfParameter& conf = nlsr.getConfParameter();
56 conf.setNetwork("/ndn");
57 conf.setSiteName("/router");
58 conf.setRouterName("/a");
59 conf.buildRouterPrefix();
60
61 RoutingTable& routingTable = nlsr.getRoutingTable();
62 routingTable.setRoutingCalcInterval(0);
63
64 NamePrefixTable& npt = nlsr.getNamePrefixTable();
65
Nick Gordone9733ed2017-04-26 10:48:39 -050066 Adjacent thisRouter(conf.getRouterPrefix(), ndn::util::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehmancae33b62015-06-05 09:21:30 -050067
68 ndn::Name buptRouterName("/ndn/cn/edu/bupt/%C1.Router/bupthub");
Nick Gordone9733ed2017-04-26 10:48:39 -050069 Adjacent bupt(buptRouterName, ndn::util::FaceUri("udp4://10.0.0.2"), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehmancae33b62015-06-05 09:21:30 -050070
71 // This router's Adjacency LSA
72 nlsr.getAdjacencyList().insert(bupt);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060073 AdjLsa thisRouterAdjLsa(thisRouter.getName(), 1,
Vince Lehmancae33b62015-06-05 09:21:30 -050074 ndn::time::system_clock::now() + ndn::time::seconds::max(),
75 2,
76 nlsr.getAdjacencyList());
77
78 lsdb.installAdjLsa(thisRouterAdjLsa);
79
80 // BUPT Adjacency LSA
81 AdjacencyList buptAdjacencies;
82 buptAdjacencies.insert(thisRouter);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060083 AdjLsa buptAdjLsa(buptRouterName, 1,
Vince Lehmancae33b62015-06-05 09:21:30 -050084 ndn::time::system_clock::now() + ndn::time::seconds(5),
85 0 , buptAdjacencies);
86
87 lsdb.installAdjLsa(buptAdjLsa);
88
89 // BUPT Name LSA
90 ndn::Name buptAdvertisedName("/ndn/cn/edu/bupt");
91
Nick Gordon96861ca2017-10-17 18:25:21 -050092 NamePrefixList buptNames{buptAdvertisedName};
Vince Lehmancae33b62015-06-05 09:21:30 -050093
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060094 NameLsa buptNameLsa(buptRouterName, 1, ndn::time::system_clock::now(),
Vince Lehmancae33b62015-06-05 09:21:30 -050095 buptNames);
96
97 lsdb.installNameLsa(buptNameLsa);
98
99 // Advance clocks to expire LSAs
100 this->advanceClocks(ndn::time::seconds(15));
101
102 // LSA expirations should cause NPT entries to be completely removed
103 NamePrefixTable::const_iterator it = npt.begin();
104 BOOST_REQUIRE(it == npt.end());
105
106 // Install new name LSA
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600107 NameLsa buptNewNameLsa(buptRouterName, 12,
Vince Lehmancae33b62015-06-05 09:21:30 -0500108 ndn::time::system_clock::now() + ndn::time::seconds(3600),
109 buptNames);
110
111 lsdb.installNameLsa(buptNewNameLsa);
112
113 this->advanceClocks(ndn::time::seconds(1));
114
115 // Install new adjacency LSA
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600116 AdjLsa buptNewAdjLsa(buptRouterName, 12,
Vince Lehmancae33b62015-06-05 09:21:30 -0500117 ndn::time::system_clock::now() + ndn::time::seconds(3600),
118 0, buptAdjacencies);
119 lsdb.installAdjLsa(buptNewAdjLsa);
120
121 this->advanceClocks(ndn::time::seconds(1));
122
123 // Each NPT entry should have a destination router
124 it = npt.begin();
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500125 BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptRouterName);
126 BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1);
127 BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName);
Vince Lehmancae33b62015-06-05 09:21:30 -0500128
129 ++it;
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500130 BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptAdvertisedName);
131 BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1);
132 BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500133}
134
135BOOST_FIXTURE_TEST_CASE(AddEntryToPool, NamePrefixTableFixture)
136{
137 NamePrefixTable& npt = nlsr.getNamePrefixTable();
138 RoutingTablePoolEntry rtpe1("router1");
139
140 npt.addRtpeToPool(rtpe1);
141
142 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
143 BOOST_CHECK_EQUAL(*(npt.m_rtpool.find("router1")->second), rtpe1);
144}
145
146BOOST_FIXTURE_TEST_CASE(RemoveEntryFromPool, NamePrefixTableFixture)
147{
148 NamePrefixTable& npt = nlsr.getNamePrefixTable();
149 RoutingTablePoolEntry rtpe1("router1", 0);
dmcoomes9f936662017-03-02 10:33:09 -0600150 std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500151
152 npt.addRtpeToPool(rtpe1);
153
154 npt.deleteRtpeFromPool(rtpePtr);
155
156 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 0);
157 BOOST_CHECK_EQUAL(npt.m_rtpool.count("router1"), 0);
158}
159
160BOOST_FIXTURE_TEST_CASE(AddRoutingEntryToNptEntry, NamePrefixTableFixture)
161{
162 NamePrefixTable& npt = nlsr.getNamePrefixTable();
163 RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
dmcoomes9f936662017-03-02 10:33:09 -0600164 std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500165 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
166
167 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
168
169 NamePrefixTable::NptEntryList::iterator nItr =
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500170 std::find_if(npt.m_table.begin(),
171 npt.m_table.end(),
172 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
173 return entry->getNamePrefix() == npte1.getNamePrefix();
174 });
Nick Gordonb50e51b2016-07-22 16:05:57 -0500175
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500176 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
dmcoomes9f936662017-03-02 10:33:09 -0600177 std::list<std::shared_ptr<RoutingTablePoolEntry>>::iterator rItr =
Nick Gordonb50e51b2016-07-22 16:05:57 -0500178 std::find(rtpeList.begin(),
179 rtpeList.end(),
180 rtpePtr);
181 BOOST_CHECK_EQUAL(**rItr, *rtpePtr);
182}
183
184BOOST_FIXTURE_TEST_CASE(RemoveRoutingEntryFromNptEntry, NamePrefixTableFixture)
185{
186 NamePrefixTable& npt = nlsr.getNamePrefixTable();
187 RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
188
189 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500190 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
Nick Gordonb50e51b2016-07-22 16:05:57 -0500191
192 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
193 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/altrtr");
194
195 npt.removeEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
196
197 NamePrefixTable::NptEntryList::iterator nItr =
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500198 std::find_if(npt.m_table.begin(),
199 npt.m_table.end(),
200 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
201 return entry->getNamePrefix() == npte1.getNamePrefix();
202 });
Nick Gordonb50e51b2016-07-22 16:05:57 -0500203
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500204 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
Nick Gordonb50e51b2016-07-22 16:05:57 -0500205
206 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
207 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
Vince Lehmancae33b62015-06-05 09:21:30 -0500208}
209
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500210BOOST_FIXTURE_TEST_CASE(AddNptEntryPtrToRoutingEntry, NamePrefixTableFixture)
211{
212 NamePrefixTable& npt = nlsr.getNamePrefixTable();
213 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
214 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
215
216 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
217
218 NamePrefixTable::NptEntryList::iterator nItr =
219 std::find_if(npt.m_table.begin(),
220 npt.m_table.end(),
221 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
222 return entry->getNamePrefix() == npte1.getNamePrefix();
223 });
224
225 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
226
227 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
228
229 auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries;
230
231 auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix());
232 BOOST_REQUIRE(nptIterator != namePrefixPtrs.end());
233 auto nptSharedPtr = nptIterator->second.lock();
234 BOOST_CHECK_EQUAL(*nptSharedPtr, npte1);
235}
236
237BOOST_FIXTURE_TEST_CASE(RemoveNptEntryPtrFromRoutingEntry, NamePrefixTableFixture)
238{
239 NamePrefixTable& npt = nlsr.getNamePrefixTable();
240 NamePrefixTableEntry npte1("/ndn/memphis/rtr1");
241 NamePrefixTableEntry npte2("/ndn/memphis/rtr2");
242 RoutingTableEntry rte1("/ndn/memphis/destination1");
243 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
244 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte2));
245
246 npt.addEntry(npte1.getNamePrefix(), rte1.getDestination());
247 // We have to add two entries, otherwise the routing pool entry will be deleted.
248 npt.addEntry(npte2.getNamePrefix(), rte1.getDestination());
249 npt.removeEntry(npte2.getNamePrefix(), rte1.getDestination());
250
251 NamePrefixTable::NptEntryList::iterator nItr =
252 std::find_if(npt.m_table.begin(),
253 npt.m_table.end(),
254 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
255 return entry->getNamePrefix() == npte1.getNamePrefix();
256 });
257
258 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
259
260 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
261
262 auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries;
263
264 // We should have removed the second one
265 BOOST_CHECK_EQUAL(namePrefixPtrs.size(), 1);
266
267 auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix());
268
269 BOOST_REQUIRE(nptIterator != namePrefixPtrs.end());
270 auto nptSharedPtr = nptIterator->second.lock();
271 BOOST_CHECK_EQUAL(*nptSharedPtr, npte1);
272}
273
274BOOST_FIXTURE_TEST_CASE(RoutingTableUpdate, NamePrefixTableFixture)
275{
276 NamePrefixTable& namePrefixTable = nlsr.getNamePrefixTable();
277 RoutingTable& routingTable = nlsr.getRoutingTable();
278 const ndn::Name destination = ndn::Name{"/ndn/destination1"};
279 NextHop hop1{"upd4://10.0.0.1", 0};
280 NextHop hop2{"udp4://10.0.0.2", 1};
281 NextHop hop3{"udp4://10.0.0.3", 2};
282 const NamePrefixTableEntry entry1{"/ndn/router1"};
283 namePrefixTable.addEntry(entry1.getNamePrefix(), destination);
284
285 routingTable.addNextHop(destination, hop1);
286 routingTable.addNextHop(destination, hop2);
287
Nick Gordonb7b58392017-08-17 16:29:21 -0500288 namePrefixTable.updateWithNewRoute(routingTable.m_rTable);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500289
290 // At this point the NamePrefixTableEntry should have two NextHops.
291 auto nameIterator = std::find_if(namePrefixTable.begin(), namePrefixTable.end(),
292 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
293 return entry1.getNamePrefix() == entry->getNamePrefix();
294 });
295 BOOST_REQUIRE(nameIterator != namePrefixTable.end());
296
297 auto iterator = namePrefixTable.m_rtpool.find(destination);
298 BOOST_REQUIRE(iterator != namePrefixTable.m_rtpool.end());
299 auto nextHops = (iterator->second)->getNexthopList();
Nick Gordonff9a6272017-10-12 13:38:29 -0500300 BOOST_CHECK_EQUAL(nextHops.size(), 2);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500301
302 // Add the other NextHop
303 routingTable.addNextHop(destination, hop3);
Nick Gordonb7b58392017-08-17 16:29:21 -0500304 namePrefixTable.updateWithNewRoute(routingTable.m_rTable);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500305
306 // At this point the NamePrefixTableEntry should have three NextHops.
307 nameIterator = std::find_if(namePrefixTable.begin(), namePrefixTable.end(),
308 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
309 return entry1.getNamePrefix() == entry->getNamePrefix();
310 });
311 BOOST_REQUIRE(nameIterator != namePrefixTable.end());
312 iterator = namePrefixTable.m_rtpool.find(destination);
313 BOOST_REQUIRE(iterator != namePrefixTable.m_rtpool.end());
314 nextHops = (iterator->second)->getNexthopList();
Nick Gordonff9a6272017-10-12 13:38:29 -0500315 BOOST_CHECK_EQUAL(nextHops.size(), 3);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500316}
317
Vince Lehmancae33b62015-06-05 09:21:30 -0500318BOOST_AUTO_TEST_SUITE_END()
319
320} // namespace test
321} // namespace nlsr