blob: 59267012f28338b7657384c64f87a22ce1202320 [file] [log] [blame]
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06002/**
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, The University of Memphis,
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06004 * 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 "statistics.hpp"
23#include "test-common.hpp"
24#include "hello-protocol.hpp"
25#include "lsdb.hpp"
26#include "nlsr.hpp"
27
28#include <ndn-cxx/util/dummy-client-face.hpp>
29
30namespace nlsr {
31namespace test {
32
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050033class StatisticsFixture : public UnitTestTimeFixture
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060034{
35public:
36 StatisticsFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050037 : face(m_ioService, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060038 , conf(face)
39 , nlsr(face, m_keyChain, conf)
40 , lsdb(nlsr.m_lsdb)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060041 , hello(nlsr.m_helloProtocol)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042 , collector(nlsr.m_statsCollector)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060043 {
44 conf.setNetwork("/ndn");
45 conf.setSiteName("/site");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050046 conf.setRouterName("/%C1.Router/this-router");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060047 conf.buildRouterPrefix();
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -060048 // Otherwise code coverage node fails with default 60 seconds lifetime
49 conf.setSyncInterestLifetime(1000);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060050
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050051 addIdentity(conf.getRouterPrefix());
52
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060053 nlsr.initialize();
54
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050055 this->advanceClocks(ndn::time::milliseconds(1), 10);
56 face.sentInterests.clear();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060057 }
58
59 /*!
60 * \brief Checks if lsa interest was received and data for interest was sent
61 *
62 * \param interestPrefix is an interest name prefix
63 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
64 * \param seqNo sequence number that will be appended to an interest name
65 * \param receivedInterestType is the specific Statisitcs::PacketType interest that is received
66 * \param sentDataType is the Statistics::PacketType data being sent upon interest process
67 *
68 * This is a general function that can be used for all three types of lsa. Calling processInterest()
69 * from lsdb will cause the statsCollector to increment the incoming interest type and increment the
70 * outgoing data type.
71 */
72 void
73 receiveInterestAndCheckSentStats(const std::string& interestPrefix,
74 const std::string& lsaType,
75 uint32_t seqNo,
76 Statistics::PacketType receivedInterestType,
77 Statistics::PacketType sentDataType)
78 {
79 size_t rcvBefore = collector.getStatistics().get(receivedInterestType);
80 size_t sentBefore = collector.getStatistics().get(sentDataType);
81
82 ndn::Name interestName = ndn::Name(ndn::Name(interestPrefix + lsaType).appendNumber(seqNo));
83 lsdb.processInterest(ndn::Name(), ndn::Interest(interestName));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050084 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060085
86 BOOST_CHECK_EQUAL(collector.getStatistics().get(receivedInterestType), rcvBefore + 1);
87 BOOST_CHECK_EQUAL(collector.getStatistics().get(sentDataType), sentBefore + 1);
88 }
89
90 /*!
91 * \brief Checks if statistics update after an lsa interest is sent
92 *
93 * \param prefix is an interest prefix
94 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
95 * \param seqNo is the sequence number
96 * \param statsType is a statistical PacketType
97 *
98 * The function is called to initiate an expressInterest call in lsdb and to check if the
99 * expected statistical packetType was incremented.
100 */
101 void
102 sendInterestAndCheckStats(const std::string& prefix,
103 const std::string& lsaType,
104 uint32_t seqNo,
105 Statistics::PacketType statsType)
106 {
107 size_t sentBefore = collector.getStatistics().get(statsType);
108
109 lsdb.expressInterest(ndn::Name(prefix + lsaType).appendNumber(seqNo), 0,
110 ndn::time::steady_clock::TimePoint::min());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500111 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600112
113 BOOST_CHECK_EQUAL(collector.getStatistics().get(statsType), sentBefore + 1);
114 }
115
116public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500117 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600118 ConfParameter conf;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600119 Nlsr nlsr;
120
121 Lsdb& lsdb;
122 HelloProtocol& hello;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600123 StatsCollector& collector;
124};
125
126BOOST_FIXTURE_TEST_SUITE(TestStatistics, StatisticsFixture)
127
128
129// A statistical PacketType is directly incremented (without signals).
130BOOST_AUTO_TEST_CASE(StatsIncrement)
131{
132 Statistics stats;
133 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0);
134 stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST);
135 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 1);
136}
137
138/*
139 * After a PacketType has been incremented, the resetAll() function is called, which sets all
140 * statistical packetType counts to 0
141 */
142BOOST_AUTO_TEST_CASE(StatsReset)
143{
144 Statistics stats;
145 stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST);
146 stats.resetAll();
147 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0);
148}
149
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600150/*
151 * This tests hello interests and hello data statistical collection by constructing an adjacency lsa
152 * and calling functions that trigger the sending and receiving hello of interests/data.
153 */
154BOOST_AUTO_TEST_CASE(SendHelloInterest)
155{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500156 Adjacent other("/ndn/router/other", ndn::FaceUri("udp4://other"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600157
158 // This router's Adjacency LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600159 conf.getAdjacencyList().insert(other);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600160
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500161 ndn::Name otherName(other.getName());
162 otherName.append("NLSR");
163 otherName.append("INFO");
164 otherName.append(conf.getRouterPrefix().wireEncode());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600165
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500166 hello.expressInterest(otherName, 1);
167 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600168
169 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_INTEREST), 1);
170
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500171 ndn::Name thisName(conf.getRouterPrefix());
172 thisName.append("NLSR");
173 thisName.append("INFO");
174 thisName.append(other.getName().wireEncode());
175
176 ndn::Interest interest(thisName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600177 hello.processInterest(ndn::Name(), interest);
178
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500179 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600180
181 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_INTEREST), 1);
182 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_DATA), 1);
183
184 // Receive Hello Data
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500185 ndn::Name dataName = otherName;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600186
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500187 ndn::Data data(dataName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600188 hello.onContentValidated(data);
189
190 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_DATA), 1);
191}
192
193/*
194 * An interest is sent for each lsa type (name, adjacency, coordinate). The respective statistics are
195 * totaled and checked.
196 */
197BOOST_AUTO_TEST_CASE(LsdbSendLsaInterest)
198{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600199 const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/router/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600200 uint32_t seqNo = 1;
201
202 // Adjacency LSA
Nick Gordon727d4832017-10-13 18:04:25 -0500203 sendInterestAndCheckStats(interestPrefix, std::to_string(Lsa::Type::ADJACENCY), seqNo,
204 Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600205
206 // Coordinate LSA
Nick Gordon727d4832017-10-13 18:04:25 -0500207 sendInterestAndCheckStats(interestPrefix, std::to_string(Lsa::Type::COORDINATE), seqNo,
208 Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600209
210 // Name LSA
Nick Gordon727d4832017-10-13 18:04:25 -0500211 sendInterestAndCheckStats(interestPrefix, std::to_string(Lsa::Type::NAME), seqNo,
212 Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600213
214 // 3 total lsa interests were sent
215 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_INTEREST), 3);
216}
217
218/*
Nick Gordon727d4832017-10-13 18:04:25 -0500219 * Tests the statistics collected upon processing incoming lsa
220 * interests and respective outgoing data. This process will trigger
221 * both an increment for received lsa interest and sent lsa data.
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600222 *
223 * /sa receiveInterestAndCheckSentStats
224 */
225BOOST_AUTO_TEST_CASE(LsdbReceiveInterestSendData)
226{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600227 // Adjacency LSA
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000228 lsdb.buildAndInstallOwnAdjLsa();
229
230 ndn::Name adjLsaKey = conf.getRouterPrefix();
231 adjLsaKey.append(std::to_string(Lsa::Type::ADJACENCY));
232
233 AdjLsa* adjLsa = lsdb.findAdjLsa(adjLsaKey);
234 uint32_t seqNo = adjLsa->getLsSeqNo();
235
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600236 Adjacent adjacency("adjacency");
237 adjacency.setStatus(Adjacent::STATUS_ACTIVE);
238
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000239 adjLsa->addAdjacent(adjacency);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600240
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000241 lsdb.installAdjLsa(*adjLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600242
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600243 const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600244
245 // Receive Adjacency LSA Interest
246 receiveInterestAndCheckSentStats(interestPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500247 std::to_string(Lsa::Type::ADJACENCY),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600248 seqNo,
249 Statistics::PacketType::RCV_ADJ_LSA_INTEREST,
250 Statistics::PacketType::SENT_ADJ_LSA_DATA);
251
252 // Name LSA
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000253 ndn::Name nameLsaKey = conf.getRouterPrefix();
254 nameLsaKey.append(std::to_string(Lsa::Type::NAME));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600255
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000256 NameLsa* nameLsa = lsdb.findNameLsa(nameLsaKey);
257
258 seqNo = nameLsa->getLsSeqNo();
259
260 nameLsa->addName(ndn::Name("/ndn/name"));
261 lsdb.installNameLsa(*nameLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600262
263 // Receive Name LSA Interest
264 receiveInterestAndCheckSentStats(interestPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500265 std::to_string(Lsa::Type::NAME),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600266 seqNo,
267 Statistics::PacketType::RCV_NAME_LSA_INTEREST,
268 Statistics::PacketType::SENT_NAME_LSA_DATA);
269
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000270 // // Coordinate LSA
271 lsdb.buildAndInstallOwnCoordinateLsa();
272 ndn::Name coorLsaKey = conf.getRouterPrefix();
273 coorLsaKey.append(std::to_string(Lsa::Type::COORDINATE));
274
275 CoordinateLsa* coorLsa = lsdb.findCoordinateLsa(coorLsaKey);
276 seqNo = coorLsa->getLsSeqNo();
277 coorLsa->setCorTheta({20.0, 30.0});
278 lsdb.installCoordinateLsa(*coorLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600279
280 // Receive Adjacency LSA Interest
281 receiveInterestAndCheckSentStats(interestPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500282 std::to_string(Lsa::Type::COORDINATE),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600283 seqNo,
284 Statistics::PacketType::RCV_COORD_LSA_INTEREST,
285 Statistics::PacketType::SENT_COORD_LSA_DATA);
286
287 // 3 different lsa type interests should be received
288 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_INTEREST), 3);
289
290 // data should have been sent 3x, once per lsa type
291 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_DATA), 3);
292}
293
294/*
295 * Data for each lsa type (name, adjacency, coordinate) is sent to the lsdb and statistics are
296 * checked to verify the respective statistical PacketType has been received.
297 */
298BOOST_AUTO_TEST_CASE(LsdbReceiveData)
299{
300 ndn::Name routerName("/ndn/cs/%C1.Router/router1");
301 uint32_t seqNo = 1;
302 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
303
304 // adjacency lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600305 ndn::Name adjInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/ADJACENCY/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600306 adjInterest.appendNumber(seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600307 AdjLsa aLsa(routerName, seqNo, MAX_TIME, 1, conf.getAdjacencyList());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600308 lsdb.installAdjLsa(aLsa);
309
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600310 ndn::Block block = ndn::encoding::makeStringBlock(ndn::tlv::Content, aLsa.serialize());
311
312 lsdb.afterFetchLsa(block.getBuffer(), adjInterest);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600313 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1);
314
315 // coordinate lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600316 ndn::Name coordInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/COORDINATE/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600317 coordInterest.appendNumber(seqNo);
318 std::vector<double> angles = {20.0, 30.0};
319 CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
320 lsdb.installCoordinateLsa(cLsa);
321
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600322 block = ndn::encoding::makeStringBlock(ndn::tlv::Content, cLsa.serialize());
323
324 lsdb.afterFetchLsa(block.getBuffer(), coordInterest);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600325 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1);
326
327 // name lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600328 ndn::Name interestName("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/NAME/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600329 interestName.appendNumber(seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600330 NameLsa nLsa(routerName, seqNo, MAX_TIME, conf.getNamePrefixList());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600331 lsdb.installNameLsa(nLsa);
332
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600333 block = ndn::encoding::makeStringBlock(ndn::tlv::Content, nLsa.serialize());
334
335 lsdb.afterFetchLsa(block.getBuffer(), interestName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600336 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_NAME_LSA_DATA), 1);
337
338 // 3 lsa data types should be received
339 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_DATA), 3);
340}
341
342BOOST_AUTO_TEST_SUITE_END()
343
344} // namespace test
345} // namespace nlsr