blob: abb053d34050eb5829e43153d03a25d8430e348b [file] [log] [blame]
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Hila Ben Abraham39a79be2016-03-30 22:00:55 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26/** \file
27 * \brief allows testing forwarding in a network topology
28 */
29
30#ifndef NFD_TESTS_NFD_FW_TOPOLOGY_TESTER_HPP
31#define NFD_TESTS_NFD_FW_TOPOLOGY_TESTER_HPP
32
Junxiao Shi5e5e4452015-09-24 16:56:52 -070033#include <ndn-cxx/face.hpp>
Junxiao Shi6535f1e2015-10-08 13:02:18 -070034#include "face/internal-transport.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070035#include "face/face.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070036#include "fw/strategy.hpp"
37#include "tests/test-common.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070038
39namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080040namespace fw {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070041namespace tests {
42
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080043using namespace nfd::tests;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070044
45/** \brief identifies a node (forwarder) in the topology
46 */
47typedef size_t TopologyNode;
48
Junxiao Shi6535f1e2015-10-08 13:02:18 -070049/** \brief represents a network link in the topology which connects two or more nodes
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070050 */
Junxiao Shi6535f1e2015-10-08 13:02:18 -070051class TopologyLink : noncopyable
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070052{
53public:
Junxiao Shi6535f1e2015-10-08 13:02:18 -070054 explicit
55 TopologyLink(const time::nanoseconds& delay);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070056
57 /** \brief fail the link, cause packets to be dropped silently
58 */
59 void
60 fail()
61 {
62 m_isUp = false;
63 }
64
65 /** \brief recover the link from a failure
66 */
67 void
68 recover()
69 {
70 m_isUp = true;
71 }
72
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070073 /** \brief change the link delay
74 * \param delay link delay, must be positive
75 */
76 void
77 setDelay(const time::nanoseconds& delay);
78
Junxiao Shicde37ad2015-12-24 01:02:05 -070079 /** \brief attach a face to the link
80 * \param i forwarder index
81 * \param face a Face with InternalForwarderTransport
82 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -070083 void
Junxiao Shicde37ad2015-12-24 01:02:05 -070084 addFace(TopologyNode i, shared_ptr<Face> face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -070085
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070086 /** \return a face of forwarder \p i which is attached to this link
87 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -070088 Face&
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070089 getFace(TopologyNode i)
90 {
Junxiao Shi5e5e4452015-09-24 16:56:52 -070091 return *m_faces.at(i);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070092 }
93
Junxiao Shi6535f1e2015-10-08 13:02:18 -070094protected:
95 /** \brief attach a Transport onto this link
96 */
97 void
98 attachTransport(TopologyNode i, face::InternalTransportBase* transport);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070099
100private:
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700101 void
102 transmit(TopologyNode i, const Block& packet);
103
104 void
105 scheduleReceive(face::InternalTransportBase* recipient, const Block& packet);
106
107private:
108 bool m_isUp;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700109 time::nanoseconds m_delay;
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700110 std::unordered_map<TopologyNode, face::InternalTransportBase*> m_transports;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700111 std::unordered_map<TopologyNode, shared_ptr<Face>> m_faces;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700112};
113
114/** \brief represents a link to a local application
115 */
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700116class TopologyAppLink : noncopyable
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700117{
118public:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700119 /** \brief constructor
120 * \param forwarderFace a Face with InternalForwarderTransport
121 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700122 explicit
Junxiao Shicde37ad2015-12-24 01:02:05 -0700123 TopologyAppLink(shared_ptr<Face> forwarderFace);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700124
125 /** \brief fail the link, cause packets to be dropped silently
126 */
127 void
128 fail();
129
130 /** \brief recover the link from a failure
131 */
132 void
133 recover();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700134
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700135 /** \return face on forwarder side
136 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700137 Face&
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700138 getForwarderFace()
139 {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700140 return *m_face;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700141 }
142
143 /** \return face on application side
144 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700145 ndn::Face&
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700146 getClientFace()
147 {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700148 return *m_client;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700149 }
150
151private:
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700152 shared_ptr<Face> m_face;
153 face::InternalForwarderTransport* m_forwarderTransport;
154 shared_ptr<face::InternalClientTransport> m_clientTransport;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700155 shared_ptr<ndn::Face> m_client;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700156};
157
158/** \brief builds a topology for forwarding tests
159 */
160class TopologyTester : noncopyable
161{
162public:
163 /** \brief creates a forwarder
164 * \return index of new forwarder
165 */
166 TopologyNode
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700167 addForwarder(const std::string& label);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700168
169 /** \return forwarder instance \p i
170 */
171 Forwarder&
172 getForwarder(TopologyNode i)
173 {
174 return *m_forwarders.at(i);
175 }
176
177 /** \brief sets strategy on forwarder \p i
178 * \tparam the strategy type
179 * \note Test scenario can also access StrategyChoice table directly.
180 */
181 template<typename S>
182 void
183 setStrategy(TopologyNode i, Name prefix = Name("ndn:/"))
184 {
185 Forwarder& forwarder = this->getForwarder(i);
186 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
187 shared_ptr<S> strategy = make_shared<S>(ref(forwarder));
188 strategyChoice.install(strategy);
189 strategyChoice.insert(prefix, strategy->getName());
190 }
191
192 /** \brief makes a link that interconnects two or more forwarders
193 *
194 * A face is created on each of \p forwarders .
195 * When a packet is sent onto one of the faces on this link,
196 * this packet will be received by all other faces on this link after \p delay .
197 */
198 shared_ptr<TopologyLink>
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700199 addLink(const std::string& label, const time::nanoseconds& delay,
200 std::initializer_list<TopologyNode> forwarders,
201 bool forceMultiAccessFace = false);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700202
203 /** \brief makes a link to local application
204 */
205 shared_ptr<TopologyAppLink>
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700206 addAppFace(const std::string& label, TopologyNode i);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700207
208 /** \brief makes a link to local application, and register a prefix
209 */
210 shared_ptr<TopologyAppLink>
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700211 addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost = 0);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700212
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700213 /** \brief registers a prefix on a forwarder face
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700214 */
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700215 void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700216 registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost = 0);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700217
218 /** \brief creates a producer application that answers every Interest with Data of same Name
219 */
220 void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700221 addEchoProducer(ndn::Face& face, const Name& prefix = "/");
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700222
223 /** \brief creates a consumer application that sends \p n Interests under \p prefix
224 * at \p interval fixed rate.
225 */
226 void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700227 addIntervalConsumer(ndn::Face& face, const Name& prefix,
228 const time::nanoseconds& interval, size_t n);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700229
230private:
231 std::vector<unique_ptr<Forwarder>> m_forwarders;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700232 std::vector<std::string> m_forwarderLabels;
233 std::vector<shared_ptr<TopologyLink>> m_links;
234 std::vector<shared_ptr<TopologyAppLink>> m_appLinks;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700235};
236
237} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800238} // namespace fw
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700239} // namespace nfd
240
241#endif // NFD_TESTS_NFD_FW_TOPOLOGY_TESTER_HPP