blob: bcfc29ca0635f85e00017417e7ba84a1e602ddcd [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
203 sendInterestAndCheckStats(interestPrefix, AdjLsa::TYPE_STRING, seqNo, Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
204
205 // Coordinate LSA
206 sendInterestAndCheckStats(interestPrefix, CoordinateLsa::TYPE_STRING, seqNo, Statistics::PacketType::SENT_COORD_LSA_INTEREST);
207
208 // Name LSA
209 sendInterestAndCheckStats(interestPrefix, NameLsa::TYPE_STRING, seqNo, Statistics::PacketType::SENT_NAME_LSA_INTEREST);
210
211 // 3 total lsa interests were sent
212 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_INTEREST), 3);
213}
214
215/*
216 * Tests the statistics collected upon processing incoming lsa interests and respective outgoing data.
217 * This process will trigger both an increment for received lsa interest and sent lsa data.
218 *
219 * /sa receiveInterestAndCheckSentStats
220 */
221BOOST_AUTO_TEST_CASE(LsdbReceiveInterestSendData)
222{
223 std::string routerName("/ndn/site/%C1.Router/router");
224 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
225 uint32_t seqNo = 1;
226
227 // Adjacency LSA
228 Adjacent adjacency("adjacency");
229 adjacency.setStatus(Adjacent::STATUS_ACTIVE);
230
231 AdjacencyList adjacencies;
232 adjacencies.insert(adjacency);
233
234 AdjLsa adjLsa(routerName, seqNo, MAX_TIME, 1, adjacencies);
235 lsdb.installAdjLsa(adjLsa);
236
237 const std::string interestPrefix("/ndn/NLSR/LSA/site/%C1.Router/router/");
238
239 // Receive Adjacency LSA Interest
240 receiveInterestAndCheckSentStats(interestPrefix,
241 AdjLsa::TYPE_STRING,
242 seqNo,
243 Statistics::PacketType::RCV_ADJ_LSA_INTEREST,
244 Statistics::PacketType::SENT_ADJ_LSA_DATA);
245
246 // Name LSA
247 NamePrefixList prefixes;
248 prefixes.insert("/ndn/name");
249
250 NameLsa nameLsa(routerName, seqNo, MAX_TIME, prefixes);
251 lsdb.installNameLsa(nameLsa);
252
253 // Receive Name LSA Interest
254 receiveInterestAndCheckSentStats(interestPrefix,
255 NameLsa::TYPE_STRING,
256 seqNo,
257 Statistics::PacketType::RCV_NAME_LSA_INTEREST,
258 Statistics::PacketType::SENT_NAME_LSA_DATA);
259
260 // Coordinate LSA
261 std::vector<double> angles = {20.0, 30.0};
262 CoordinateLsa coordLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
263 lsdb.installCoordinateLsa(coordLsa);
264
265 // Receive Adjacency LSA Interest
266 receiveInterestAndCheckSentStats(interestPrefix,
267 CoordinateLsa::TYPE_STRING,
268 seqNo,
269 Statistics::PacketType::RCV_COORD_LSA_INTEREST,
270 Statistics::PacketType::SENT_COORD_LSA_DATA);
271
272 // 3 different lsa type interests should be received
273 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_INTEREST), 3);
274
275 // data should have been sent 3x, once per lsa type
276 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::SENT_LSA_DATA), 3);
277}
278
279/*
280 * Data for each lsa type (name, adjacency, coordinate) is sent to the lsdb and statistics are
281 * checked to verify the respective statistical PacketType has been received.
282 */
283BOOST_AUTO_TEST_CASE(LsdbReceiveData)
284{
285 ndn::Name routerName("/ndn/cs/%C1.Router/router1");
286 uint32_t seqNo = 1;
287 ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max();
288
289 // adjacency lsa
290 ndn::Name adjInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/adjacency/");
291 adjInterest.appendNumber(seqNo);
292 AdjLsa aLsa(routerName, seqNo, MAX_TIME, 1, nlsr.getAdjacencyList());
293 lsdb.installAdjLsa(aLsa);
294
295 const ndn::ConstBufferPtr aBuffer = std::make_shared<ndn::Buffer>(aLsa.getData().c_str(),
296 aLsa.getData().size());
297 lsdb.afterFetchLsa(aBuffer, adjInterest);
298 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1);
299
300 // coordinate lsa
301 ndn::Name coordInterest("/ndn/NLSR/LSA/cs/%C1.Router/router1/coordinate/");
302 coordInterest.appendNumber(seqNo);
303 std::vector<double> angles = {20.0, 30.0};
304 CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
305 lsdb.installCoordinateLsa(cLsa);
306
307 const ndn::ConstBufferPtr cBuffer = std::make_shared<ndn::Buffer>(cLsa.getData().c_str(),
308 cLsa.getData().size());
309 lsdb.afterFetchLsa(cBuffer, coordInterest);
310 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1);
311
312 // name lsa
313 ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/name/");
314 interestName.appendNumber(seqNo);
315 NameLsa nLsa(routerName, seqNo, MAX_TIME, nlsr.getNamePrefixList());
316 lsdb.installNameLsa(nLsa);
317
318 const ndn::ConstBufferPtr nBuffer = std::make_shared<ndn::Buffer>(nLsa.getData().c_str(),
319 nLsa.getData().size());
320 lsdb.afterFetchLsa(nBuffer, interestName);
321 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_NAME_LSA_DATA), 1);
322
323 // 3 lsa data types should be received
324 BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_LSA_DATA), 3);
325}
326
327BOOST_AUTO_TEST_SUITE_END()
328
329} // namespace test
330} // namespace nlsr