blob: 776b447d58a028f5f558377ec1da260243aa3fd1 [file] [log] [blame]
Vince Lehmancae33b62015-06-05 09:21:30 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, 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()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050035 : face(m_ioService, m_keyChain)
36 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Vince Lehmancae33b62015-06-05 09:21:30 -050037 , lsdb(nlsr.getLsdb())
38 , npt(nlsr.getNamePrefixTable())
39 {
Vince Lehmancae33b62015-06-05 09:21:30 -050040 }
41
42public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050043 ndn::util::DummyClientFace face;
Vince Lehmancae33b62015-06-05 09:21:30 -050044 Nlsr nlsr;
45
46 Lsdb& lsdb;
47 NamePrefixTable& npt;
48};
49
50BOOST_AUTO_TEST_SUITE(TestNamePrefixTable)
51
52BOOST_FIXTURE_TEST_CASE(Bupt, NamePrefixTableFixture)
53{
54 ConfParameter& conf = nlsr.getConfParameter();
55 conf.setNetwork("/ndn");
56 conf.setSiteName("/router");
57 conf.setRouterName("/a");
58 conf.buildRouterPrefix();
59
60 RoutingTable& routingTable = nlsr.getRoutingTable();
61 routingTable.setRoutingCalcInterval(0);
62
63 NamePrefixTable& npt = nlsr.getNamePrefixTable();
64
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050065 Adjacent thisRouter(conf.getRouterPrefix(), ndn::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehmancae33b62015-06-05 09:21:30 -050066
67 ndn::Name buptRouterName("/ndn/cn/edu/bupt/%C1.Router/bupthub");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050068 Adjacent bupt(buptRouterName, ndn::FaceUri("udp4://10.0.0.2"), 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehmancae33b62015-06-05 09:21:30 -050069
70 // This router's Adjacency LSA
71 nlsr.getAdjacencyList().insert(bupt);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060072 AdjLsa thisRouterAdjLsa(thisRouter.getName(), 1,
Vince Lehmancae33b62015-06-05 09:21:30 -050073 ndn::time::system_clock::now() + ndn::time::seconds::max(),
74 2,
75 nlsr.getAdjacencyList());
76
77 lsdb.installAdjLsa(thisRouterAdjLsa);
78
79 // BUPT Adjacency LSA
80 AdjacencyList buptAdjacencies;
81 buptAdjacencies.insert(thisRouter);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060082 AdjLsa buptAdjLsa(buptRouterName, 1,
Vince Lehmancae33b62015-06-05 09:21:30 -050083 ndn::time::system_clock::now() + ndn::time::seconds(5),
84 0 , buptAdjacencies);
85
86 lsdb.installAdjLsa(buptAdjLsa);
87
88 // BUPT Name LSA
89 ndn::Name buptAdvertisedName("/ndn/cn/edu/bupt");
90
Nick Gordon96861ca2017-10-17 18:25:21 -050091 NamePrefixList buptNames{buptAdvertisedName};
Vince Lehmancae33b62015-06-05 09:21:30 -050092
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060093 NameLsa buptNameLsa(buptRouterName, 1, ndn::time::system_clock::now(),
Vince Lehmancae33b62015-06-05 09:21:30 -050094 buptNames);
95
96 lsdb.installNameLsa(buptNameLsa);
97
98 // Advance clocks to expire LSAs
99 this->advanceClocks(ndn::time::seconds(15));
100
101 // LSA expirations should cause NPT entries to be completely removed
102 NamePrefixTable::const_iterator it = npt.begin();
103 BOOST_REQUIRE(it == npt.end());
104
105 // Install new name LSA
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600106 NameLsa buptNewNameLsa(buptRouterName, 12,
Vince Lehmancae33b62015-06-05 09:21:30 -0500107 ndn::time::system_clock::now() + ndn::time::seconds(3600),
108 buptNames);
109
110 lsdb.installNameLsa(buptNewNameLsa);
111
112 this->advanceClocks(ndn::time::seconds(1));
113
114 // Install new adjacency LSA
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600115 AdjLsa buptNewAdjLsa(buptRouterName, 12,
Vince Lehmancae33b62015-06-05 09:21:30 -0500116 ndn::time::system_clock::now() + ndn::time::seconds(3600),
117 0, buptAdjacencies);
118 lsdb.installAdjLsa(buptNewAdjLsa);
119
120 this->advanceClocks(ndn::time::seconds(1));
121
122 // Each NPT entry should have a destination router
123 it = npt.begin();
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500124 BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptRouterName);
125 BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1);
126 BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName);
Vince Lehmancae33b62015-06-05 09:21:30 -0500127
128 ++it;
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500129 BOOST_REQUIRE_EQUAL((*it)->getNamePrefix(), buptAdvertisedName);
130 BOOST_REQUIRE_EQUAL((*it)->getRteList().size(), 1);
131 BOOST_CHECK_EQUAL((*(*it)->getRteList().begin())->getDestination(), buptRouterName);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500132}
133
134BOOST_FIXTURE_TEST_CASE(AddEntryToPool, NamePrefixTableFixture)
135{
136 NamePrefixTable& npt = nlsr.getNamePrefixTable();
137 RoutingTablePoolEntry rtpe1("router1");
138
139 npt.addRtpeToPool(rtpe1);
140
141 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
142 BOOST_CHECK_EQUAL(*(npt.m_rtpool.find("router1")->second), rtpe1);
143}
144
145BOOST_FIXTURE_TEST_CASE(RemoveEntryFromPool, NamePrefixTableFixture)
146{
147 NamePrefixTable& npt = nlsr.getNamePrefixTable();
148 RoutingTablePoolEntry rtpe1("router1", 0);
dmcoomes9f936662017-03-02 10:33:09 -0600149 std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500150
151 npt.addRtpeToPool(rtpe1);
152
153 npt.deleteRtpeFromPool(rtpePtr);
154
155 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 0);
156 BOOST_CHECK_EQUAL(npt.m_rtpool.count("router1"), 0);
157}
158
159BOOST_FIXTURE_TEST_CASE(AddRoutingEntryToNptEntry, NamePrefixTableFixture)
160{
161 NamePrefixTable& npt = nlsr.getNamePrefixTable();
162 RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
dmcoomes9f936662017-03-02 10:33:09 -0600163 std::shared_ptr<RoutingTablePoolEntry> rtpePtr = npt.addRtpeToPool(rtpe1);
Nick Gordonb50e51b2016-07-22 16:05:57 -0500164 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
165
166 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
167
168 NamePrefixTable::NptEntryList::iterator nItr =
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500169 std::find_if(npt.m_table.begin(),
170 npt.m_table.end(),
171 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
172 return entry->getNamePrefix() == npte1.getNamePrefix();
173 });
Nick Gordonb50e51b2016-07-22 16:05:57 -0500174
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500175 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
dmcoomes9f936662017-03-02 10:33:09 -0600176 std::list<std::shared_ptr<RoutingTablePoolEntry>>::iterator rItr =
Nick Gordonb50e51b2016-07-22 16:05:57 -0500177 std::find(rtpeList.begin(),
178 rtpeList.end(),
179 rtpePtr);
180 BOOST_CHECK_EQUAL(**rItr, *rtpePtr);
181}
182
183BOOST_FIXTURE_TEST_CASE(RemoveRoutingEntryFromNptEntry, NamePrefixTableFixture)
184{
185 NamePrefixTable& npt = nlsr.getNamePrefixTable();
186 RoutingTablePoolEntry rtpe1("/ndn/memphis/rtr1", 0);
187
188 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500189 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
Nick Gordonb50e51b2016-07-22 16:05:57 -0500190
191 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
192 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/altrtr");
193
194 npt.removeEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
195
196 NamePrefixTable::NptEntryList::iterator nItr =
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500197 std::find_if(npt.m_table.begin(),
198 npt.m_table.end(),
199 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
200 return entry->getNamePrefix() == npte1.getNamePrefix();
201 });
Nick Gordonb50e51b2016-07-22 16:05:57 -0500202
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500203 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
Nick Gordonb50e51b2016-07-22 16:05:57 -0500204
205 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
206 BOOST_CHECK_EQUAL(npt.m_rtpool.size(), 1);
Vince Lehmancae33b62015-06-05 09:21:30 -0500207}
208
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500209BOOST_FIXTURE_TEST_CASE(AddNptEntryPtrToRoutingEntry, NamePrefixTableFixture)
210{
211 NamePrefixTable& npt = nlsr.getNamePrefixTable();
212 NamePrefixTableEntry npte1("/ndn/memphis/rtr2");
213 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
214
215 npt.addEntry("/ndn/memphis/rtr2", "/ndn/memphis/rtr1");
216
217 NamePrefixTable::NptEntryList::iterator nItr =
218 std::find_if(npt.m_table.begin(),
219 npt.m_table.end(),
220 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
221 return entry->getNamePrefix() == npte1.getNamePrefix();
222 });
223
224 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
225
226 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
227
228 auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries;
229
230 auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix());
231 BOOST_REQUIRE(nptIterator != namePrefixPtrs.end());
232 auto nptSharedPtr = nptIterator->second.lock();
233 BOOST_CHECK_EQUAL(*nptSharedPtr, npte1);
234}
235
236BOOST_FIXTURE_TEST_CASE(RemoveNptEntryPtrFromRoutingEntry, NamePrefixTableFixture)
237{
238 NamePrefixTable& npt = nlsr.getNamePrefixTable();
239 NamePrefixTableEntry npte1("/ndn/memphis/rtr1");
240 NamePrefixTableEntry npte2("/ndn/memphis/rtr2");
241 RoutingTableEntry rte1("/ndn/memphis/destination1");
242 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte1));
243 npt.m_table.push_back(make_shared<NamePrefixTableEntry>(npte2));
244
245 npt.addEntry(npte1.getNamePrefix(), rte1.getDestination());
246 // We have to add two entries, otherwise the routing pool entry will be deleted.
247 npt.addEntry(npte2.getNamePrefix(), rte1.getDestination());
248 npt.removeEntry(npte2.getNamePrefix(), rte1.getDestination());
249
250 NamePrefixTable::NptEntryList::iterator nItr =
251 std::find_if(npt.m_table.begin(),
252 npt.m_table.end(),
253 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
254 return entry->getNamePrefix() == npte1.getNamePrefix();
255 });
256
257 std::list<std::shared_ptr<RoutingTablePoolEntry>> rtpeList = (*nItr)->getRteList();
258
259 BOOST_CHECK_EQUAL(rtpeList.size(), 1);
260
261 auto& namePrefixPtrs = rtpeList.front()->namePrefixTableEntries;
262
263 // We should have removed the second one
264 BOOST_CHECK_EQUAL(namePrefixPtrs.size(), 1);
265
266 auto nptIterator = namePrefixPtrs.find(npte1.getNamePrefix());
267
268 BOOST_REQUIRE(nptIterator != namePrefixPtrs.end());
269 auto nptSharedPtr = nptIterator->second.lock();
270 BOOST_CHECK_EQUAL(*nptSharedPtr, npte1);
271}
272
273BOOST_FIXTURE_TEST_CASE(RoutingTableUpdate, NamePrefixTableFixture)
274{
275 NamePrefixTable& namePrefixTable = nlsr.getNamePrefixTable();
276 RoutingTable& routingTable = nlsr.getRoutingTable();
277 const ndn::Name destination = ndn::Name{"/ndn/destination1"};
278 NextHop hop1{"upd4://10.0.0.1", 0};
279 NextHop hop2{"udp4://10.0.0.2", 1};
280 NextHop hop3{"udp4://10.0.0.3", 2};
281 const NamePrefixTableEntry entry1{"/ndn/router1"};
282 namePrefixTable.addEntry(entry1.getNamePrefix(), destination);
283
284 routingTable.addNextHop(destination, hop1);
285 routingTable.addNextHop(destination, hop2);
286
Nick Gordonb7b58392017-08-17 16:29:21 -0500287 namePrefixTable.updateWithNewRoute(routingTable.m_rTable);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500288
289 // At this point the NamePrefixTableEntry should have two NextHops.
290 auto nameIterator = std::find_if(namePrefixTable.begin(), namePrefixTable.end(),
291 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
292 return entry1.getNamePrefix() == entry->getNamePrefix();
293 });
294 BOOST_REQUIRE(nameIterator != namePrefixTable.end());
295
296 auto iterator = namePrefixTable.m_rtpool.find(destination);
297 BOOST_REQUIRE(iterator != namePrefixTable.m_rtpool.end());
298 auto nextHops = (iterator->second)->getNexthopList();
Nick Gordonff9a6272017-10-12 13:38:29 -0500299 BOOST_CHECK_EQUAL(nextHops.size(), 2);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500300
301 // Add the other NextHop
302 routingTable.addNextHop(destination, hop3);
Nick Gordonb7b58392017-08-17 16:29:21 -0500303 namePrefixTable.updateWithNewRoute(routingTable.m_rTable);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500304
305 // At this point the NamePrefixTableEntry should have three NextHops.
306 nameIterator = std::find_if(namePrefixTable.begin(), namePrefixTable.end(),
307 [&] (const std::shared_ptr<NamePrefixTableEntry>& entry) {
308 return entry1.getNamePrefix() == entry->getNamePrefix();
309 });
310 BOOST_REQUIRE(nameIterator != namePrefixTable.end());
311 iterator = namePrefixTable.m_rtpool.find(destination);
312 BOOST_REQUIRE(iterator != namePrefixTable.m_rtpool.end());
313 nextHops = (iterator->second)->getNexthopList();
Nick Gordonff9a6272017-10-12 13:38:29 -0500314 BOOST_CHECK_EQUAL(nextHops.size(), 3);
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500315}
316
Vince Lehmancae33b62015-06-05 09:21:30 -0500317BOOST_AUTO_TEST_SUITE_END()
318
319} // namespace test
320} // namespace nlsr