blob: 90a2a67d1295563ad3a5d6ace75601a11209172b [file] [log] [blame]
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, The University of Memphis,
4 * 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)
38 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060039 , lsdb(nlsr.getLsdb())
40 , hello(nlsr.m_helloProtocol)
41 , conf(nlsr.getConfParameter())
42 , collector(nlsr.getStatsCollector())
43 {
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();
48
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050049 addIdentity(conf.getRouterPrefix());
50
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060051 nlsr.initialize();
52
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050053 this->advanceClocks(ndn::time::milliseconds(1), 10);
54 face.sentInterests.clear();
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060055 }
56
57 /*!
58 * \brief Checks if lsa interest was received and data for interest was sent
59 *
60 * \param interestPrefix is an interest name prefix
61 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
62 * \param seqNo sequence number that will be appended to an interest name
63 * \param receivedInterestType is the specific Statisitcs::PacketType interest that is received
64 * \param sentDataType is the Statistics::PacketType data being sent upon interest process
65 *
66 * This is a general function that can be used for all three types of lsa. Calling processInterest()
67 * from lsdb will cause the statsCollector to increment the incoming interest type and increment the
68 * outgoing data type.
69 */
70 void
71 receiveInterestAndCheckSentStats(const std::string& interestPrefix,
72 const std::string& lsaType,
73 uint32_t seqNo,
74 Statistics::PacketType receivedInterestType,
75 Statistics::PacketType sentDataType)
76 {
77 size_t rcvBefore = collector.getStatistics().get(receivedInterestType);
78 size_t sentBefore = collector.getStatistics().get(sentDataType);
79
80 ndn::Name interestName = ndn::Name(ndn::Name(interestPrefix + lsaType).appendNumber(seqNo));
81 lsdb.processInterest(ndn::Name(), ndn::Interest(interestName));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050082 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060083
84 BOOST_CHECK_EQUAL(collector.getStatistics().get(receivedInterestType), rcvBefore + 1);
85 BOOST_CHECK_EQUAL(collector.getStatistics().get(sentDataType), sentBefore + 1);
86 }
87
88 /*!
89 * \brief Checks if statistics update after an lsa interest is sent
90 *
91 * \param prefix is an interest prefix
92 * \param lsaType indicates whether the lsa is a name, adjacency, or coordinate
93 * \param seqNo is the sequence number
94 * \param statsType is a statistical PacketType
95 *
96 * The function is called to initiate an expressInterest call in lsdb and to check if the
97 * expected statistical packetType was incremented.
98 */
99 void
100 sendInterestAndCheckStats(const std::string& prefix,
101 const std::string& lsaType,
102 uint32_t seqNo,
103 Statistics::PacketType statsType)
104 {
105 size_t sentBefore = collector.getStatistics().get(statsType);
106
107 lsdb.expressInterest(ndn::Name(prefix + lsaType).appendNumber(seqNo), 0,
108 ndn::time::steady_clock::TimePoint::min());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500109 this->advanceClocks(ndn::time::milliseconds(1), 10);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600110
111 BOOST_CHECK_EQUAL(collector.getStatistics().get(statsType), sentBefore + 1);
112 }
113
114public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500115 ndn::util::DummyClientFace face;
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600116 Nlsr nlsr;
117
118 Lsdb& lsdb;
119 HelloProtocol& hello;
120 ConfParameter& conf;
121 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
157 nlsr.getAdjacencyList().insert(other);
158
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500159 ndn::Name otherName(other.getName());
160 otherName.append("NLSR");
161 otherName.append("INFO");
162 otherName.append(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");
172 thisName.append(other.getName().wireEncode());
173
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{
197 const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/router/");
198 uint32_t seqNo = 1;
199
200 // Adjacency LSA
Nick Gordon727d4832017-10-13 18:04:25 -0500201 sendInterestAndCheckStats(interestPrefix, std::to_string(Lsa::Type::ADJACENCY), seqNo,
202 Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600203
204 // Coordinate LSA
Nick Gordon727d4832017-10-13 18:04:25 -0500205 sendInterestAndCheckStats(interestPrefix, std::to_string(Lsa::Type::COORDINATE), seqNo,
206 Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600207
208 // Name LSA
Nick Gordon727d4832017-10-13 18:04:25 -0500209 sendInterestAndCheckStats(interestPrefix, std::to_string(Lsa::Type::NAME), seqNo,
210 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{
225 std::string routerName("/ndn/site/%C1.Router/router");
226 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
227 uint32_t seqNo = 1;
228
229 // Adjacency LSA
230 Adjacent adjacency("adjacency");
231 adjacency.setStatus(Adjacent::STATUS_ACTIVE);
232
233 AdjacencyList adjacencies;
234 adjacencies.insert(adjacency);
235
236 AdjLsa adjLsa(routerName, seqNo, MAX_TIME, 1, adjacencies);
237 lsdb.installAdjLsa(adjLsa);
238
239 const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/router/");
240
241 // Receive Adjacency LSA Interest
242 receiveInterestAndCheckSentStats(interestPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500243 std::to_string(Lsa::Type::ADJACENCY),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600244 seqNo,
245 Statistics::PacketType::RCV_ADJ_LSA_INTEREST,
246 Statistics::PacketType::SENT_ADJ_LSA_DATA);
247
248 // Name LSA
Nick Gordon96861ca2017-10-17 18:25:21 -0500249 NamePrefixList prefixes{ndn::Name{"/ndn/name"}};
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600250
251 NameLsa nameLsa(routerName, seqNo, MAX_TIME, prefixes);
252 lsdb.installNameLsa(nameLsa);
253
254 // Receive Name LSA Interest
255 receiveInterestAndCheckSentStats(interestPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500256 std::to_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
261 // Coordinate LSA
262 std::vector<double> angles = {20.0, 30.0};
263 CoordinateLsa coordLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
264 lsdb.installCoordinateLsa(coordLsa);
265
266 // Receive Adjacency LSA Interest
267 receiveInterestAndCheckSentStats(interestPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500268 std::to_string(Lsa::Type::COORDINATE),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600269 seqNo,
270 Statistics::PacketType::RCV_COORD_LSA_INTEREST,
271 Statistics::PacketType::SENT_COORD_LSA_DATA);
272
273 // 3 different lsa type interests should be received
274 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_INTEREST), 3);
275
276 // data should have been sent 3x, once per lsa type
277 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_DATA), 3);
278}
279
280/*
281 * Data for each lsa type (name, adjacency, coordinate) is sent to the lsdb and statistics are
282 * checked to verify the respective statistical PacketType has been received.
283 */
284BOOST_AUTO_TEST_CASE(LsdbReceiveData)
285{
286 ndn::Name routerName("/ndn/cs/%C1.Router/router1");
287 uint32_t seqNo = 1;
288 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
289
290 // adjacency lsa
Nick Gordon727d4832017-10-13 18:04:25 -0500291 ndn::Name adjInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/ADJACENCY/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600292 adjInterest.appendNumber(seqNo);
293 AdjLsa aLsa(routerName, seqNo, MAX_TIME, 1, nlsr.getAdjacencyList());
294 lsdb.installAdjLsa(aLsa);
295
Nick Gordonfaf49f42017-10-23 12:36:28 -0500296 const ndn::ConstBufferPtr aBuffer = std::make_shared<ndn::Buffer>(aLsa.serialize().c_str(),
297 aLsa.serialize().size());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600298 lsdb.afterFetchLsa(aBuffer, adjInterest);
299 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1);
300
301 // coordinate lsa
Nick Gordon727d4832017-10-13 18:04:25 -0500302 ndn::Name coordInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/COORDINATE/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600303 coordInterest.appendNumber(seqNo);
304 std::vector<double> angles = {20.0, 30.0};
305 CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
306 lsdb.installCoordinateLsa(cLsa);
307
Nick Gordonfaf49f42017-10-23 12:36:28 -0500308 const ndn::ConstBufferPtr cBuffer = std::make_shared<ndn::Buffer>(cLsa.serialize().c_str(),
309 cLsa.serialize().size());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600310 lsdb.afterFetchLsa(cBuffer, coordInterest);
311 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1);
312
313 // name lsa
Nick Gordon727d4832017-10-13 18:04:25 -0500314 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600315 interestName.appendNumber(seqNo);
316 NameLsa nLsa(routerName, seqNo, MAX_TIME, nlsr.getNamePrefixList());
317 lsdb.installNameLsa(nLsa);
318
Nick Gordonfaf49f42017-10-23 12:36:28 -0500319 const ndn::ConstBufferPtr nBuffer = std::make_shared<ndn::Buffer>(nLsa.serialize().c_str(),
320 nLsa.serialize().size());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600321 lsdb.afterFetchLsa(nBuffer, interestName);
322 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