blob: 2c02bb7194d838dd758705ddba277e1cb9a505e9 [file] [log] [blame]
Ashlesh Gawande57a87172020-05-09 19:47:06 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -07003 * Copyright (c) 2014-2021, 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/>.
Ashlesh Gawande57a87172020-05-09 19:47:06 -070020 */
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060021
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>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070029#include <boost/lexical_cast.hpp>
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060030
31namespace nlsr {
32namespace test {
33
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050034class StatisticsFixture : public UnitTestTimeFixture
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060035{
36public:
37 StatisticsFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050038 : face(m_ioService, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060039 , conf(face, m_keyChain)
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050040 , confProcessor(conf)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060041 , nlsr(face, m_keyChain, conf)
42 , lsdb(nlsr.m_lsdb)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060043 , hello(nlsr.m_helloProtocol)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060044 , collector(nlsr.m_statsCollector)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060045 {
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -060046 // Otherwise code coverage node fails with default 60 seconds lifetime
47 conf.setSyncInterestLifetime(1000);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060048
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050049 addIdentity(conf.getRouterPrefix());
50
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050051 this->advanceClocks(ndn::time::milliseconds(1), 10);
52 face.sentInterests.clear();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060053 }
54
55 /*!
56 * \brief Checks if lsa interest was received and data for interest was sent
57 *
58 * \param interestPrefix is an interest name prefix
59 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
60 * \param seqNo sequence number that will be appended to an interest name
61 * \param receivedInterestType is the specific Statisitcs::PacketType interest that is received
62 * \param sentDataType is the Statistics::PacketType data being sent upon interest process
63 *
64 * This is a general function that can be used for all three types of lsa. Calling processInterest()
65 * from lsdb will cause the statsCollector to increment the incoming interest type and increment the
66 * outgoing data type.
67 */
68 void
69 receiveInterestAndCheckSentStats(const std::string& interestPrefix,
70 const std::string& lsaType,
71 uint32_t seqNo,
72 Statistics::PacketType receivedInterestType,
73 Statistics::PacketType sentDataType)
74 {
75 size_t rcvBefore = collector.getStatistics().get(receivedInterestType);
76 size_t sentBefore = collector.getStatistics().get(sentDataType);
77
78 ndn::Name interestName = ndn::Name(ndn::Name(interestPrefix + lsaType).appendNumber(seqNo));
79 lsdb.processInterest(ndn::Name(), ndn::Interest(interestName));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050080 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060081
82 BOOST_CHECK_EQUAL(collector.getStatistics().get(receivedInterestType), rcvBefore + 1);
83 BOOST_CHECK_EQUAL(collector.getStatistics().get(sentDataType), sentBefore + 1);
84 }
85
86 /*!
87 * \brief Checks if statistics update after an lsa interest is sent
88 *
89 * \param prefix is an interest prefix
90 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
91 * \param seqNo is the sequence number
92 * \param statsType is a statistical PacketType
93 *
94 * The function is called to initiate an expressInterest call in lsdb and to check if the
95 * expected statistical packetType was incremented.
96 */
97 void
98 sendInterestAndCheckStats(const std::string& prefix,
99 const std::string& lsaType,
100 uint32_t seqNo,
101 Statistics::PacketType statsType)
102 {
103 size_t sentBefore = collector.getStatistics().get(statsType);
104
105 lsdb.expressInterest(ndn::Name(prefix + lsaType).appendNumber(seqNo), 0,
106 ndn::time::steady_clock::TimePoint::min());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500107 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600108
109 BOOST_CHECK_EQUAL(collector.getStatistics().get(statsType), sentBefore + 1);
110 }
111
112public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500113 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600114 ConfParameter conf;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500115 DummyConfFileProcessor confProcessor;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600116 Nlsr nlsr;
117
118 Lsdb& lsdb;
119 HelloProtocol& hello;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600120 StatsCollector& collector;
121};
122
123BOOST_FIXTURE_TEST_SUITE(TestStatistics, StatisticsFixture)
124
125
126// A statistical PacketType is directly incremented (without signals).
127BOOST_AUTO_TEST_CASE(StatsIncrement)
128{
129 Statistics stats;
130 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0);
131 stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST);
132 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 1);
133}
134
135/*
136 * After a PacketType has been incremented, the resetAll() function is called, which sets all
137 * statistical packetType counts to 0
138 */
139BOOST_AUTO_TEST_CASE(StatsReset)
140{
141 Statistics stats;
142 stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST);
143 stats.resetAll();
144 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0);
145}
146
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600147/*
148 * This tests hello interests and hello data statistical collection by constructing an adjacency lsa
149 * and calling functions that trigger the sending and receiving hello of interests/data.
150 */
151BOOST_AUTO_TEST_CASE(SendHelloInterest)
152{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500153 Adjacent other("/ndn/router/other", ndn::FaceUri("udp4://other"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600154
155 // This router's Adjacency LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600156 conf.getAdjacencyList().insert(other);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600157
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500158 ndn::Name otherName(other.getName());
159 otherName.append("NLSR");
160 otherName.append("INFO");
161 otherName.append(conf.getRouterPrefix().wireEncode());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600162
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500163 hello.expressInterest(otherName, 1);
164 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600165
166 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_INTEREST), 1);
167
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500168 ndn::Name thisName(conf.getRouterPrefix());
169 thisName.append("NLSR");
170 thisName.append("INFO");
171 thisName.append(other.getName().wireEncode());
172
173 ndn::Interest interest(thisName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600174 hello.processInterest(ndn::Name(), interest);
175
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500176 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600177
178 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_INTEREST), 1);
179 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_DATA), 1);
180
181 // Receive Hello Data
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500182 ndn::Name dataName = otherName;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600183
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500184 ndn::Data data(dataName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600185 hello.onContentValidated(data);
186
187 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_DATA), 1);
188}
189
190/*
191 * An interest is sent for each lsa type (name, adjacency, coordinate). The respective statistics are
192 * totaled and checked.
193 */
194BOOST_AUTO_TEST_CASE(LsdbSendLsaInterest)
195{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600196 const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/router/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600197 uint32_t seqNo = 1;
198
199 // Adjacency LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800200 sendInterestAndCheckStats(interestPrefix, boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY),
201 seqNo, Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600202
203 // Coordinate LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800204 sendInterestAndCheckStats(interestPrefix, boost::lexical_cast<std::string>(Lsa::Type::COORDINATE),
205 seqNo, Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600206
207 // Name LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800208 sendInterestAndCheckStats(interestPrefix, boost::lexical_cast<std::string>(Lsa::Type::NAME),
209 seqNo, Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600210
211 // 3 total lsa interests were sent
212 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_INTEREST), 3);
213}
214
215/*
Nick Gordon727d4832017-10-13 18:04:25 -0500216 * Tests the statistics collected upon processing incoming lsa
217 * interests and respective outgoing data. This process will trigger
218 * both an increment for received lsa interest and sent lsa data.
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600219 *
220 * /sa receiveInterestAndCheckSentStats
221 */
222BOOST_AUTO_TEST_CASE(LsdbReceiveInterestSendData)
223{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600224 // Adjacency LSA
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000225 lsdb.buildAndInstallOwnAdjLsa();
226
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700227 auto adjLsa = lsdb.findLsa<AdjLsa>(conf.getRouterPrefix());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800228 uint32_t seqNo = adjLsa->getSeqNo();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000229
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600230 Adjacent adjacency("adjacency");
231 adjacency.setStatus(Adjacent::STATUS_ACTIVE);
232
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000233 adjLsa->addAdjacent(adjacency);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600234
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700235 lsdb.installLsa(adjLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600236
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600237 const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600238
239 // Receive Adjacency LSA Interest
240 receiveInterestAndCheckSentStats(interestPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800241 boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600242 seqNo,
243 Statistics::PacketType::RCV_ADJ_LSA_INTEREST,
244 Statistics::PacketType::SENT_ADJ_LSA_DATA);
245
246 // Name LSA
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700247 auto nameLsa = std::static_pointer_cast<NameLsa>(lsdb.findLsa(conf.getRouterPrefix(), Lsa::Type::NAME));
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500248 BOOST_ASSERT(nameLsa != nullptr);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000249
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800250 seqNo = nameLsa->getSeqNo();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000251
252 nameLsa->addName(ndn::Name("/ndn/name"));
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700253 lsdb.installLsa(nameLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600254
255 // Receive Name LSA Interest
256 receiveInterestAndCheckSentStats(interestPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800257 boost::lexical_cast<std::string>(Lsa::Type::NAME),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600258 seqNo,
259 Statistics::PacketType::RCV_NAME_LSA_INTEREST,
260 Statistics::PacketType::SENT_NAME_LSA_DATA);
261
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000262 // // Coordinate LSA
263 lsdb.buildAndInstallOwnCoordinateLsa();
264 ndn::Name coorLsaKey = conf.getRouterPrefix();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800265 coorLsaKey.append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE));
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000266
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700267 auto coorLsa = lsdb.findLsa<CoordinateLsa>(conf.getRouterPrefix());
268
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800269 seqNo = coorLsa->getSeqNo();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000270 coorLsa->setCorTheta({20.0, 30.0});
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700271 lsdb.installLsa(coorLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600272
273 // Receive Adjacency LSA Interest
274 receiveInterestAndCheckSentStats(interestPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800275 boost::lexical_cast<std::string>(Lsa::Type::COORDINATE),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600276 seqNo,
277 Statistics::PacketType::RCV_COORD_LSA_INTEREST,
278 Statistics::PacketType::SENT_COORD_LSA_DATA);
279
280 // 3 different lsa type interests should be received
281 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_INTEREST), 3);
282
283 // data should have been sent 3x, once per lsa type
284 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_DATA), 3);
285}
286
287/*
288 * Data for each lsa type (name, adjacency, coordinate) is sent to the lsdb and statistics are
289 * checked to verify the respective statistical PacketType has been received.
290 */
291BOOST_AUTO_TEST_CASE(LsdbReceiveData)
292{
293 ndn::Name routerName("/ndn/cs/%C1.Router/router1");
294 uint32_t seqNo = 1;
295 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
296
297 // adjacency lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600298 ndn::Name adjInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/ADJACENCY/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600299 adjInterest.appendNumber(seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600300 AdjLsa aLsa(routerName, seqNo, MAX_TIME, 1, conf.getAdjacencyList());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700301 lsdb.installLsa(std::make_shared<AdjLsa>(aLsa));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600302
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800303 lsdb.afterFetchLsa(aLsa.wireEncode().getBuffer(), adjInterest);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600304 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1);
305
306 // coordinate lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600307 ndn::Name coordInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/COORDINATE/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600308 coordInterest.appendNumber(seqNo);
309 std::vector<double> angles = {20.0, 30.0};
310 CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700311 lsdb.installLsa(std::make_shared<CoordinateLsa>(cLsa));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600312
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800313 lsdb.afterFetchLsa(cLsa.wireEncode().getBuffer(), coordInterest);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600314 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1);
315
316 // name lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600317 ndn::Name interestName("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/NAME/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600318 interestName.appendNumber(seqNo);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800319 NameLsa nlsa(routerName, seqNo, MAX_TIME, conf.getNamePrefixList());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700320 lsdb.installLsa(std::make_shared<NameLsa>(nlsa));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600321
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800322 lsdb.afterFetchLsa(nlsa.wireEncode().getBuffer(), interestName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600323 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_NAME_LSA_DATA), 1);
324
325 // 3 lsa data types should be received
326 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_DATA), 3);
327}
328
329BOOST_AUTO_TEST_SUITE_END()
330
331} // namespace test
332} // namespace nlsr