blob: 0000fb05e63fd04f857859da18e800c205becb30 [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>
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)
Saurab Dulal427e0122019-11-28 11:58:02 -060038 , conf(face, m_keyChain)
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050039 , confProcessor(conf)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060040 , nlsr(face, m_keyChain, conf)
41 , lsdb(nlsr.m_lsdb)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060042 , hello(nlsr.m_helloProtocol)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060043 , collector(nlsr.m_statsCollector)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060044 {
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -060045 // Otherwise code coverage node fails with default 60 seconds lifetime
46 conf.setSyncInterestLifetime(1000);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060047
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050048 addIdentity(conf.getRouterPrefix());
49
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050050 this->advanceClocks(ndn::time::milliseconds(1), 10);
51 face.sentInterests.clear();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060052 }
53
54 /*!
55 * \brief Checks if lsa interest was received and data for interest was sent
56 *
57 * \param interestPrefix is an interest name prefix
58 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
59 * \param seqNo sequence number that will be appended to an interest name
60 * \param receivedInterestType is the specific Statisitcs::PacketType interest that is received
61 * \param sentDataType is the Statistics::PacketType data being sent upon interest process
62 *
63 * This is a general function that can be used for all three types of lsa. Calling processInterest()
64 * from lsdb will cause the statsCollector to increment the incoming interest type and increment the
65 * outgoing data type.
66 */
67 void
68 receiveInterestAndCheckSentStats(const std::string& interestPrefix,
69 const std::string& lsaType,
70 uint32_t seqNo,
71 Statistics::PacketType receivedInterestType,
72 Statistics::PacketType sentDataType)
73 {
74 size_t rcvBefore = collector.getStatistics().get(receivedInterestType);
75 size_t sentBefore = collector.getStatistics().get(sentDataType);
76
77 ndn::Name interestName = ndn::Name(ndn::Name(interestPrefix + lsaType).appendNumber(seqNo));
78 lsdb.processInterest(ndn::Name(), ndn::Interest(interestName));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050079 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060080
81 BOOST_CHECK_EQUAL(collector.getStatistics().get(receivedInterestType), rcvBefore + 1);
82 BOOST_CHECK_EQUAL(collector.getStatistics().get(sentDataType), sentBefore + 1);
83 }
84
85 /*!
86 * \brief Checks if statistics update after an lsa interest is sent
87 *
88 * \param prefix is an interest prefix
89 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
90 * \param seqNo is the sequence number
91 * \param statsType is a statistical PacketType
92 *
93 * The function is called to initiate an expressInterest call in lsdb and to check if the
94 * expected statistical packetType was incremented.
95 */
96 void
97 sendInterestAndCheckStats(const std::string& prefix,
98 const std::string& lsaType,
99 uint32_t seqNo,
100 Statistics::PacketType statsType)
101 {
102 size_t sentBefore = collector.getStatistics().get(statsType);
103
104 lsdb.expressInterest(ndn::Name(prefix + lsaType).appendNumber(seqNo), 0,
105 ndn::time::steady_clock::TimePoint::min());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500106 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600107
108 BOOST_CHECK_EQUAL(collector.getStatistics().get(statsType), sentBefore + 1);
109 }
110
111public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500112 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600113 ConfParameter conf;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500114 DummyConfFileProcessor confProcessor;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600115 Nlsr nlsr;
116
117 Lsdb& lsdb;
118 HelloProtocol& hello;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600119 StatsCollector& collector;
120};
121
122BOOST_FIXTURE_TEST_SUITE(TestStatistics, StatisticsFixture)
123
124
125// A statistical PacketType is directly incremented (without signals).
126BOOST_AUTO_TEST_CASE(StatsIncrement)
127{
128 Statistics stats;
129 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0);
130 stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST);
131 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 1);
132}
133
134/*
135 * After a PacketType has been incremented, the resetAll() function is called, which sets all
136 * statistical packetType counts to 0
137 */
138BOOST_AUTO_TEST_CASE(StatsReset)
139{
140 Statistics stats;
141 stats.increment(Statistics::PacketType::SENT_HELLO_INTEREST);
142 stats.resetAll();
143 BOOST_CHECK_EQUAL(stats.get(Statistics::PacketType::SENT_HELLO_INTEREST), 0);
144}
145
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600146/*
147 * This tests hello interests and hello data statistical collection by constructing an adjacency lsa
148 * and calling functions that trigger the sending and receiving hello of interests/data.
149 */
150BOOST_AUTO_TEST_CASE(SendHelloInterest)
151{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500152 Adjacent other("/ndn/router/other", ndn::FaceUri("udp4://other"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600153
154 // This router's Adjacency LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600155 conf.getAdjacencyList().insert(other);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600156
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500157 ndn::Name otherName(other.getName());
158 otherName.append("NLSR");
159 otherName.append("INFO");
160 otherName.append(conf.getRouterPrefix().wireEncode());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600161
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500162 hello.expressInterest(otherName, 1);
163 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600164
165 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_INTEREST), 1);
166
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500167 ndn::Name thisName(conf.getRouterPrefix());
168 thisName.append("NLSR");
169 thisName.append("INFO");
170 thisName.append(other.getName().wireEncode());
171
172 ndn::Interest interest(thisName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600173 hello.processInterest(ndn::Name(), interest);
174
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500175 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600176
177 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_INTEREST), 1);
178 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_DATA), 1);
179
180 // Receive Hello Data
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500181 ndn::Name dataName = otherName;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600182
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500183 ndn::Data data(dataName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600184 hello.onContentValidated(data);
185
186 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_HELLO_DATA), 1);
187}
188
189/*
190 * An interest is sent for each lsa type (name, adjacency, coordinate). The respective statistics are
191 * totaled and checked.
192 */
193BOOST_AUTO_TEST_CASE(LsdbSendLsaInterest)
194{
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600195 const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/router/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600196 uint32_t seqNo = 1;
197
198 // Adjacency LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800199 sendInterestAndCheckStats(interestPrefix, boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY),
200 seqNo, Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600201
202 // Coordinate LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800203 sendInterestAndCheckStats(interestPrefix, boost::lexical_cast<std::string>(Lsa::Type::COORDINATE),
204 seqNo, Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600205
206 // Name LSA
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800207 sendInterestAndCheckStats(interestPrefix, boost::lexical_cast<std::string>(Lsa::Type::NAME),
208 seqNo, Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600209
210 // 3 total lsa interests were sent
211 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_INTEREST), 3);
212}
213
214/*
Nick Gordon727d4832017-10-13 18:04:25 -0500215 * Tests the statistics collected upon processing incoming lsa
216 * interests and respective outgoing data. This process will trigger
217 * both an increment for received lsa interest and sent lsa data.
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600218 *
219 * /sa receiveInterestAndCheckSentStats
220 */
221BOOST_AUTO_TEST_CASE(LsdbReceiveInterestSendData)
222{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600223 // Adjacency LSA
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000224 lsdb.buildAndInstallOwnAdjLsa();
225
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700226 auto adjLsa = lsdb.findLsa<AdjLsa>(conf.getRouterPrefix());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800227 uint32_t seqNo = adjLsa->getSeqNo();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000228
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600229 Adjacent adjacency("adjacency");
230 adjacency.setStatus(Adjacent::STATUS_ACTIVE);
231
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000232 adjLsa->addAdjacent(adjacency);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600233
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700234 lsdb.installLsa(adjLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600235
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600236 const std::string interestPrefix("/localhop/ndn/nlsr/LSA/site/%C1.Router/this-router/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600237
238 // Receive Adjacency LSA Interest
239 receiveInterestAndCheckSentStats(interestPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800240 boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600241 seqNo,
242 Statistics::PacketType::RCV_ADJ_LSA_INTEREST,
243 Statistics::PacketType::SENT_ADJ_LSA_DATA);
244
245 // Name LSA
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700246 auto nameLsa = std::static_pointer_cast<NameLsa>(lsdb.findLsa(conf.getRouterPrefix(), Lsa::Type::NAME));
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500247 BOOST_ASSERT(nameLsa != nullptr);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000248
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800249 seqNo = nameLsa->getSeqNo();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000250
251 nameLsa->addName(ndn::Name("/ndn/name"));
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700252 lsdb.installLsa(nameLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600253
254 // Receive Name LSA Interest
255 receiveInterestAndCheckSentStats(interestPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800256 boost::lexical_cast<std::string>(Lsa::Type::NAME),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600257 seqNo,
258 Statistics::PacketType::RCV_NAME_LSA_INTEREST,
259 Statistics::PacketType::SENT_NAME_LSA_DATA);
260
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000261 // // Coordinate LSA
262 lsdb.buildAndInstallOwnCoordinateLsa();
263 ndn::Name coorLsaKey = conf.getRouterPrefix();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800264 coorLsaKey.append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE));
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000265
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700266 auto coorLsa = lsdb.findLsa<CoordinateLsa>(conf.getRouterPrefix());
267
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800268 seqNo = coorLsa->getSeqNo();
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000269 coorLsa->setCorTheta({20.0, 30.0});
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700270 lsdb.installLsa(coorLsa);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600271
272 // Receive Adjacency LSA Interest
273 receiveInterestAndCheckSentStats(interestPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800274 boost::lexical_cast<std::string>(Lsa::Type::COORDINATE),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600275 seqNo,
276 Statistics::PacketType::RCV_COORD_LSA_INTEREST,
277 Statistics::PacketType::SENT_COORD_LSA_DATA);
278
279 // 3 different lsa type interests should be received
280 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_INTEREST), 3);
281
282 // data should have been sent 3x, once per lsa type
283 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_DATA), 3);
284}
285
286/*
287 * Data for each lsa type (name, adjacency, coordinate) is sent to the lsdb and statistics are
288 * checked to verify the respective statistical PacketType has been received.
289 */
290BOOST_AUTO_TEST_CASE(LsdbReceiveData)
291{
292 ndn::Name routerName("/ndn/cs/%C1.Router/router1");
293 uint32_t seqNo = 1;
294 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
295
296 // adjacency lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600297 ndn::Name adjInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/ADJACENCY/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600298 adjInterest.appendNumber(seqNo);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600299 AdjLsa aLsa(routerName, seqNo, MAX_TIME, 1, conf.getAdjacencyList());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700300 lsdb.installLsa(std::make_shared<AdjLsa>(aLsa));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600301
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800302 lsdb.afterFetchLsa(aLsa.wireEncode().getBuffer(), adjInterest);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600303 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1);
304
305 // coordinate lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600306 ndn::Name coordInterest("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/COORDINATE/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600307 coordInterest.appendNumber(seqNo);
308 std::vector<double> angles = {20.0, 30.0};
309 CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700310 lsdb.installLsa(std::make_shared<CoordinateLsa>(cLsa));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600311
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800312 lsdb.afterFetchLsa(cLsa.wireEncode().getBuffer(), coordInterest);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600313 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1);
314
315 // name lsa
Ashlesh Gawande939b6f82018-12-09 16:51:09 -0600316 ndn::Name interestName("/localhop/ndn/nlsr/LSA/cs/%C1.Router/router1/NAME/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600317 interestName.appendNumber(seqNo);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800318 NameLsa nlsa(routerName, seqNo, MAX_TIME, conf.getNamePrefixList());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700319 lsdb.installLsa(std::make_shared<NameLsa>(nlsa));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600320
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800321 lsdb.afterFetchLsa(nlsa.wireEncode().getBuffer(), interestName);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600322 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_NAME_LSA_DATA), 1);
323
324 // 3 lsa data types should be received
325 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_DATA), 3);
326}
327
328BOOST_AUTO_TEST_SUITE_END()
329
330} // namespace test
331} // namespace nlsr