blob: 43bbf70620023498ff0d0b5ce98c8fcace16339b [file] [log] [blame]
Ashlesh Gawande57a87172020-05-09 19:47:06 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shib8752932024-01-07 15:18:46 +00003 * Copyright (c) 2014-2024, 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"
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060023#include "hello-protocol.hpp"
24#include "lsdb.hpp"
25#include "nlsr.hpp"
26
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040027#include "tests/io-key-chain-fixture.hpp"
28#include "tests/test-common.hpp"
Davide Pesaventoe28d8752022-03-19 03:55:25 -040029
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070030#include <boost/lexical_cast.hpp>
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060031
32namespace nlsr {
33namespace test {
34
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040035class StatisticsFixture : public IoKeyChainFixture
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060036{
37public:
38 StatisticsFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040039 : face(m_io, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060040 , conf(face, m_keyChain)
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050041 , confProcessor(conf)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042 , nlsr(face, m_keyChain, conf)
43 , lsdb(nlsr.m_lsdb)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060044 , hello(nlsr.m_helloProtocol)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060045 , collector(nlsr.m_statsCollector)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060046 {
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -060047 // Otherwise code coverage node fails with default 60 seconds lifetime
48 conf.setSyncInterestLifetime(1000);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060049
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040050 m_keyChain.createIdentity(conf.getRouterPrefix());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050051
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050052 this->advanceClocks(ndn::time::milliseconds(1), 10);
53 face.sentInterests.clear();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060054 }
55
56 /*!
57 * \brief Checks if lsa interest was received and data for interest was sent
58 *
59 * \param interestPrefix is an interest name prefix
60 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
61 * \param seqNo sequence number that will be appended to an interest name
62 * \param receivedInterestType is the specific Statisitcs::PacketType interest that is received
63 * \param sentDataType is the Statistics::PacketType data being sent upon interest process
64 *
65 * This is a general function that can be used for all three types of lsa. Calling processInterest()
66 * from lsdb will cause the statsCollector to increment the incoming interest type and increment the
67 * outgoing data type.
68 */
69 void
70 receiveInterestAndCheckSentStats(const std::string& interestPrefix,
71 const std::string& lsaType,
72 uint32_t seqNo,
73 Statistics::PacketType receivedInterestType,
74 Statistics::PacketType sentDataType)
75 {
76 size_t rcvBefore = collector.getStatistics().get(receivedInterestType);
77 size_t sentBefore = collector.getStatistics().get(sentDataType);
78
79 ndn::Name interestName = ndn::Name(ndn::Name(interestPrefix + lsaType).appendNumber(seqNo));
80 lsdb.processInterest(ndn::Name(), ndn::Interest(interestName));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050081 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060082
83 BOOST_CHECK_EQUAL(collector.getStatistics().get(receivedInterestType), rcvBefore + 1);
84 BOOST_CHECK_EQUAL(collector.getStatistics().get(sentDataType), sentBefore + 1);
85 }
86
87 /*!
88 * \brief Checks if statistics update after an lsa interest is sent
89 *
90 * \param prefix is an interest prefix
91 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
92 * \param seqNo is the sequence number
93 * \param statsType is a statistical PacketType
94 *
95 * The function is called to initiate an expressInterest call in lsdb and to check if the
96 * expected statistical packetType was incremented.
97 */
98 void
99 sendInterestAndCheckStats(const std::string& prefix,
100 const std::string& lsaType,
101 uint32_t seqNo,
102 Statistics::PacketType statsType)
103 {
104 size_t sentBefore = collector.getStatistics().get(statsType);
105
Alexander Afanasyev135288c2022-04-23 23:06:56 -0400106 lsdb.expressInterest(ndn::Name(prefix + lsaType).appendNumber(seqNo), 0, 0,
Davide Pesavento658fd852023-05-10 22:15:03 -0400107 ndn::time::steady_clock::time_point::min());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500108 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600109
110 BOOST_CHECK_EQUAL(collector.getStatistics().get(statsType), sentBefore + 1);
111 }
112
113public:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000114 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600115 ConfParameter conf;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500116 DummyConfFileProcessor confProcessor;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600117 Nlsr nlsr;
118
119 Lsdb& lsdb;
120 HelloProtocol& hello;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600121 StatsCollector& collector;
122};
123
124BOOST_FIXTURE_TEST_SUITE(TestStatistics, StatisticsFixture)
125
126
127// A statistical PacketType is directly incremented (without signals).
128BOOST_AUTO_TEST_CASE(StatsIncrement)
129{
130 Statistics stats;
131 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0);
132 stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST);
133 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 1);
134}
135
136/*
137 * After a PacketType has been incremented, the resetAll() function is called, which sets all
138 * statistical packetType counts to 0
139 */
140BOOST_AUTO_TEST_CASE(StatsReset)
141{
142 Statistics stats;
143 stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST);
144 stats.resetAll();
145 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0);
146}
147
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600148/*
149 * This tests hello interests and hello data statistical collection by constructing an adjacency lsa
150 * and calling functions that trigger the sending and receiving hello of interests/data.
151 */
152BOOST_AUTO_TEST_CASE(SendHelloInterest)
153{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500154 Adjacent other("/ndn/router/other", ndn::FaceUri("udp4://other"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600155
156 // This router's Adjacency LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600157 conf.getAdjacencyList().insert(other);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600158
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500159 ndn::Name otherName(other.getName());
160 otherName.append("NLSR");
161 otherName.append("INFO");
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400162 otherName.append(ndn::tlv::GenericNameComponent, conf.getRouterPrefix().wireEncode());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600163
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500164 hello.expressInterest(otherName, 1);
165 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600166
167 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_INTEREST), 1);
168
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500169 ndn::Name thisName(conf.getRouterPrefix());
170 thisName.append("NLSR");
171 thisName.append("INFO");
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400172 thisName.append(ndn::tlv::GenericNameComponent, other.getName().wireEncode());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500173
174 ndn::Interest interest(thisName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600175 hello.processInterest(ndn::Name(), interest);
176
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500177 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600178
179 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_INTEREST), 1);
180 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_DATA), 1);
181
182 // Receive Hello Data
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500183 ndn::Name dataName = otherName;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600184
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500185 ndn::Data data(dataName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600186 hello.onContentValidated(data);
187
188 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_DATA), 1);
189}
190
191/*
192 * An interest is sent for each lsa type (name, adjacency, coordinate). The respective statistics are
193 * totaled and checked.
194 */
195BOOST_AUTO_TEST_CASE(LsdbSendLsaInterest)
196{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600197 const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/router/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600198 uint32_t seqNo = 1;
199
200 // Adjacency LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800201 sendInterestAndCheckStats(interestPrefix, boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY),
202 seqNo, Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600203
204 // Coordinate LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800205 sendInterestAndCheckStats(interestPrefix, boost::lexical_cast<std::string>(Lsa::Type::COORDINATE),
206 seqNo, Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600207
208 // Name LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800209 sendInterestAndCheckStats(interestPrefix, boost::lexical_cast<std::string>(Lsa::Type::NAME),
210 seqNo, Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600211
212 // 3 total lsa interests were sent
213 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_INTEREST), 3);
214}
215
216/*
Nick Gordon727d4832017-10-13 18:04:25 -0500217 * Tests the statistics collected upon processing incoming lsa
218 * interests and respective outgoing data. This process will trigger
219 * both an increment for received lsa interest and sent lsa data.
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600220 *
221 * /sa receiveInterestAndCheckSentStats
222 */
223BOOST_AUTO_TEST_CASE(LsdbReceiveInterestSendData)
224{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600225 // Adjacency LSA
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000226 lsdb.buildAndInstallOwnAdjLsa();
227
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700228 auto adjLsa = lsdb.findLsa<AdjLsa>(conf.getRouterPrefix());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800229 uint32_t seqNo = adjLsa->getSeqNo();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000230
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600231 Adjacent adjacency("adjacency");
232 adjacency.setStatus(Adjacent::STATUS_ACTIVE);
233
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000234 adjLsa->addAdjacent(adjacency);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600235
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700236 lsdb.installLsa(adjLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600237
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600238 const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600239
240 // Receive Adjacency LSA Interest
241 receiveInterestAndCheckSentStats(interestPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800242 boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600243 seqNo,
244 Statistics::PacketType::RCV_ADJ_LSA_INTEREST,
245 Statistics::PacketType::SENT_ADJ_LSA_DATA);
246
247 // Name LSA
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700248 auto nameLsa = std::static_pointer_cast<NameLsa>(lsdb.findLsa(conf.getRouterPrefix(), Lsa::Type::NAME));
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500249 BOOST_ASSERT(nameLsa != nullptr);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000250
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800251 seqNo = nameLsa->getSeqNo();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000252
253 nameLsa->addName(ndn::Name("/ndn/name"));
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700254 lsdb.installLsa(nameLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600255
256 // Receive Name LSA Interest
257 receiveInterestAndCheckSentStats(interestPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800258 boost::lexical_cast<std::string>(Lsa::Type::NAME),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600259 seqNo,
260 Statistics::PacketType::RCV_NAME_LSA_INTEREST,
261 Statistics::PacketType::SENT_NAME_LSA_DATA);
262
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000263 // // Coordinate LSA
264 lsdb.buildAndInstallOwnCoordinateLsa();
265 ndn::Name coorLsaKey = conf.getRouterPrefix();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800266 coorLsaKey.append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE));
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000267
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700268 auto coorLsa = lsdb.findLsa<CoordinateLsa>(conf.getRouterPrefix());
269
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800270 seqNo = coorLsa->getSeqNo();
Junxiao Shib5734842024-01-09 21:14:53 +0000271 coorLsa->setTheta({20.0, 30.0});
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700272 lsdb.installLsa(coorLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600273
274 // Receive Adjacency LSA Interest
275 receiveInterestAndCheckSentStats(interestPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800276 boost::lexical_cast<std::string>(Lsa::Type::COORDINATE),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600277 seqNo,
278 Statistics::PacketType::RCV_COORD_LSA_INTEREST,
279 Statistics::PacketType::SENT_COORD_LSA_DATA);
280
281 // 3 different lsa type interests should be received
282 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_INTEREST), 3);
283
284 // data should have been sent 3x, once per lsa type
285 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_DATA), 3);
286}
287
288/*
289 * Data for each lsa type (name, adjacency, coordinate) is sent to the lsdb and statistics are
290 * checked to verify the respective statistical PacketType has been received.
291 */
292BOOST_AUTO_TEST_CASE(LsdbReceiveData)
293{
294 ndn::Name routerName("/ndn/cs/%C1.Router/router1");
295 uint32_t seqNo = 1;
Davide Pesavento658fd852023-05-10 22:15:03 -0400296 const auto MAX_TIME = ndn::time::system_clock::time_point::max();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600297
298 // adjacency lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600299 ndn::Name adjInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/ADJACENCY/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600300 adjInterest.appendNumber(seqNo);
Junxiao Shib8752932024-01-07 15:18:46 +0000301 AdjLsa aLsa(routerName, seqNo, MAX_TIME, conf.getAdjacencyList());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700302 lsdb.installLsa(std::make_shared<AdjLsa>(aLsa));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600303
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800304 lsdb.afterFetchLsa(aLsa.wireEncode().getBuffer(), adjInterest);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600305 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1);
306
307 // coordinate lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600308 ndn::Name coordInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/COORDINATE/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600309 coordInterest.appendNumber(seqNo);
310 std::vector<double> angles = {20.0, 30.0};
311 CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700312 lsdb.installLsa(std::make_shared<CoordinateLsa>(cLsa));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600313
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800314 lsdb.afterFetchLsa(cLsa.wireEncode().getBuffer(), coordInterest);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600315 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1);
316
317 // name lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600318 ndn::Name interestName("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/NAME/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600319 interestName.appendNumber(seqNo);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800320 NameLsa nlsa(routerName, seqNo, MAX_TIME, conf.getNamePrefixList());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700321 lsdb.installLsa(std::make_shared<NameLsa>(nlsa));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600322
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800323 lsdb.afterFetchLsa(nlsa.wireEncode().getBuffer(), interestName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600324 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_NAME_LSA_DATA), 1);
325
326 // 3 lsa data types should be received
327 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_DATA), 3);
328}
329
330BOOST_AUTO_TEST_SUITE_END()
331
332} // namespace test
333} // namespace nlsr