blob: bc029e1e2594cee8de7794c0bb0ecf502a0414cc [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
33class StatisticsFixture : public BaseFixture
34{
35public:
36 StatisticsFixture()
37 : face(std::make_shared<ndn::util::DummyClientFace>(g_ioService))
38 , nlsr(g_ioService, g_scheduler, std::ref(*face), g_keyChain)
39 , 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");
46 conf.setRouterName("/%C1.router/this-router");
47 conf.buildRouterPrefix();
48
49 nlsr.initialize();
50
51 face->processEvents(ndn::time::milliseconds(1));
52 face->sentInterests.clear();
53 }
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));
80 face->processEvents(ndn::time::milliseconds(1));
81
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());
107 face->processEvents(ndn::time::milliseconds(1));
108
109 BOOST_CHECK_EQUAL(collector.getStatistics().get(statsType), sentBefore + 1);
110 }
111
112public:
113 std::shared_ptr<ndn::util::DummyClientFace> face;
114 Nlsr nlsr;
115
116 Lsdb& lsdb;
117 HelloProtocol& hello;
118 ConfParameter& conf;
119 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
146
147/*
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{
153 nlsr.initialize();
154
155 face->processEvents(ndn::time::milliseconds(1));
156 face->sentInterests.clear();
157
158 Adjacent other("/ndn/router/other", ndn::util::FaceUri("udp4://other"), 25, Adjacent::STATUS_INACTIVE, 0, 0);
159
160 // This router's Adjacency LSA
161 nlsr.getAdjacencyList().insert(other);
162
163 ndn::Name name(conf.getRouterPrefix());
164 name.append("NLSR");
165 name.append("INFO");
166 name.append(other.getName().wireEncode());
167
168 hello.expressInterest(name, 1);
169 face->processEvents(ndn::time::milliseconds(1));
170
171 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_HELLO_INTEREST), 1);
172
173 ndn::Interest interest(name);
174 hello.processInterest(ndn::Name(), interest);
175
176 face->processEvents(ndn::time::milliseconds(1));
177
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
182 ndn::Name dataName = other.getName();
183 dataName.append("NLSR");
184 dataName.append("INFO");
185 dataName.append(conf.getRouterPrefix().wireEncode());
186
187 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(dataName);
188 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{
199 const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/router/");
200 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{
227 std::string routerName("/ndn/site/%C1.Router/router");
228 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
229 uint32_t seqNo = 1;
230
231 // Adjacency LSA
232 Adjacent adjacency("adjacency");
233 adjacency.setStatus(Adjacent::STATUS_ACTIVE);
234
235 AdjacencyList adjacencies;
236 adjacencies.insert(adjacency);
237
238 AdjLsa adjLsa(routerName, seqNo, MAX_TIME, 1, adjacencies);
239 lsdb.installAdjLsa(adjLsa);
240
241 const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/router/");
242
243 // Receive Adjacency LSA Interest
244 receiveInterestAndCheckSentStats(interestPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500245 std::to_string(Lsa::Type::ADJACENCY),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600246 seqNo,
247 Statistics::PacketType::RCV_ADJ_LSA_INTEREST,
248 Statistics::PacketType::SENT_ADJ_LSA_DATA);
249
250 // Name LSA
251 NamePrefixList prefixes;
252 prefixes.insert("/ndn/name");
253
254 NameLsa nameLsa(routerName, seqNo, MAX_TIME, prefixes);
255 lsdb.installNameLsa(nameLsa);
256
257 // Receive Name LSA Interest
258 receiveInterestAndCheckSentStats(interestPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500259 std::to_string(Lsa::Type::NAME),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600260 seqNo,
261 Statistics::PacketType::RCV_NAME_LSA_INTEREST,
262 Statistics::PacketType::SENT_NAME_LSA_DATA);
263
264 // Coordinate LSA
265 std::vector<double> angles = {20.0, 30.0};
266 CoordinateLsa coordLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
267 lsdb.installCoordinateLsa(coordLsa);
268
269 // Receive Adjacency LSA Interest
270 receiveInterestAndCheckSentStats(interestPrefix,
Nick Gordon727d4832017-10-13 18:04:25 -0500271 std::to_string(Lsa::Type::COORDINATE),
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600272 seqNo,
273 Statistics::PacketType::RCV_COORD_LSA_INTEREST,
274 Statistics::PacketType::SENT_COORD_LSA_DATA);
275
276 // 3 different lsa type interests should be received
277 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_INTEREST), 3);
278
279 // data should have been sent 3x, once per lsa type
280 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_DATA), 3);
281}
282
283/*
284 * Data for each lsa type (name, adjacency, coordinate) is sent to the lsdb and statistics are
285 * checked to verify the respective statistical PacketType has been received.
286 */
287BOOST_AUTO_TEST_CASE(LsdbReceiveData)
288{
289 ndn::Name routerName("/ndn/cs/%C1.Router/router1");
290 uint32_t seqNo = 1;
291 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
292
293 // adjacency lsa
Nick Gordon727d4832017-10-13 18:04:25 -0500294 ndn::Name adjInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/ADJACENCY/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600295 adjInterest.appendNumber(seqNo);
296 AdjLsa aLsa(routerName, seqNo, MAX_TIME, 1, nlsr.getAdjacencyList());
297 lsdb.installAdjLsa(aLsa);
298
Nick Gordonfaf49f42017-10-23 12:36:28 -0500299 const ndn::ConstBufferPtr aBuffer = std::make_shared<ndn::Buffer>(aLsa.serialize().c_str(),
300 aLsa.serialize().size());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600301 lsdb.afterFetchLsa(aBuffer, adjInterest);
302 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1);
303
304 // coordinate lsa
Nick Gordon727d4832017-10-13 18:04:25 -0500305 ndn::Name coordInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/COORDINATE/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600306 coordInterest.appendNumber(seqNo);
307 std::vector<double> angles = {20.0, 30.0};
308 CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
309 lsdb.installCoordinateLsa(cLsa);
310
Nick Gordonfaf49f42017-10-23 12:36:28 -0500311 const ndn::ConstBufferPtr cBuffer = std::make_shared<ndn::Buffer>(cLsa.serialize().c_str(),
312 cLsa.serialize().size());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600313 lsdb.afterFetchLsa(cBuffer, coordInterest);
314 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1);
315
316 // name lsa
Nick Gordon727d4832017-10-13 18:04:25 -0500317 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600318 interestName.appendNumber(seqNo);
319 NameLsa nLsa(routerName, seqNo, MAX_TIME, nlsr.getNamePrefixList());
320 lsdb.installNameLsa(nLsa);
321
Nick Gordonfaf49f42017-10-23 12:36:28 -0500322 const ndn::ConstBufferPtr nBuffer = std::make_shared<ndn::Buffer>(nLsa.serialize().c_str(),
323 nLsa.serialize().size());
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600324 lsdb.afterFetchLsa(nBuffer, interestName);
325 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_NAME_LSA_DATA), 1);
326
327 // 3 lsa data types should be received
328 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_DATA), 3);
329}
330
331BOOST_AUTO_TEST_SUITE_END()
332
333} // namespace test
334} // namespace nlsr