blob: 4ce6f40602a96df05851e51d054b17ba351fae76 [file] [log] [blame]
Junxiao Shif3c07812014-03-11 21:48:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Teng Liang63086442018-02-25 20:39:42 -07002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Davide Pesaventob84bd3a2016-04-22 02:21:45 +02004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shif3c07812014-03-11 21:48:49 -070025
Junxiao Shi82e7f582014-09-07 15:15:40 -070026#ifndef NFD_TESTS_DAEMON_FW_DUMMY_STRATEGY_HPP
27#define NFD_TESTS_DAEMON_FW_DUMMY_STRATEGY_HPP
Junxiao Shif3c07812014-03-11 21:48:49 -070028
29#include "fw/strategy.hpp"
30
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050031#include <limits>
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
Junxiao Shif3c07812014-03-11 21:48:49 -070034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040036 * \brief Forwarding strategy for unit testing.
Junxiao Shif3c07812014-03-11 21:48:49 -070037 *
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038 * Triggers are recorded but do nothing.
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000039 *
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040 * DummyStrategy registers itself as `/dummy-strategy/<max-version>`, so that it can be instantiated
41 * with any version number. Aliases can be created with the registerAs() function.
Junxiao Shif3c07812014-03-11 21:48:49 -070042 */
43class DummyStrategy : public fw::Strategy
44{
45public:
Junxiao Shi18739c42016-12-22 08:03:00 +000046 static void
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000047 registerAs(const Name& strategyName);
Junxiao Shi18739c42016-12-22 08:03:00 +000048
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000049 static Name
50 getStrategyName(uint64_t version = std::numeric_limits<uint64_t>::max());
51
Davide Pesavento0498ce82021-06-14 02:02:21 -040052 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040053 * \brief Constructor.
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000054 *
Davide Pesavento0498ce82021-06-14 02:02:21 -040055 * \p name is recorded unchanged as getInstanceName(), and will not automatically
56 * gain a version number when instantiated without a version number.
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000057 */
58 explicit
59 DummyStrategy(Forwarder& forwarder, const Name& name = getStrategyName());
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070060
Davide Pesavento0498ce82021-06-14 02:02:21 -040061 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040062 * \brief After receive Interest trigger.
Junxiao Shi5e5e4452015-09-24 16:56:52 -070063 *
Davide Pesavento0498ce82021-06-14 02:02:21 -040064 * If interestOutFace is not null, \p interest is forwarded to that face;
65 * otherwise, rejectPendingInterest() is invoked.
Junxiao Shi5e5e4452015-09-24 16:56:52 -070066 */
Junxiao Shi18739c42016-12-22 08:03:00 +000067 void
Davide Pesavento0498ce82021-06-14 02:02:21 -040068 afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000069 const shared_ptr<pit::Entry>& pitEntry) override;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070070
Junxiao Shi18739c42016-12-22 08:03:00 +000071 void
Davide Pesavento0498ce82021-06-14 02:02:21 -040072 afterContentStoreHit(const Data& data, const FaceEndpoint& ingress,
73 const shared_ptr<pit::Entry>& pitEntry) override;
Junxiao Shif3c07812014-03-11 21:48:49 -070074
Junxiao Shi18739c42016-12-22 08:03:00 +000075 void
Davide Pesavento0498ce82021-06-14 02:02:21 -040076 beforeSatisfyInterest(const Data& data, const FaceEndpoint& ingress,
77 const shared_ptr<pit::Entry>& pitEntry) override;
Teng Liang85a36632018-03-21 05:59:34 -070078
79 void
Davide Pesavento0498ce82021-06-14 02:02:21 -040080 afterReceiveData(const Data& data, const FaceEndpoint& ingress,
81 const shared_ptr<pit::Entry>& pitEntry) override;
Teng Liang43bb2312018-03-26 04:16:42 -070082
83 void
Davide Pesavento0498ce82021-06-14 02:02:21 -040084 afterReceiveNack(const lp::Nack& nack, const FaceEndpoint& ingress,
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000085 const shared_ptr<pit::Entry>& pitEntry) override;
86
Ju Pan2feb4592019-09-16 20:56:38 +000087 void
88 afterNewNextHop(const fib::NextHop& nextHop, const shared_ptr<pit::Entry>& pitEntry) override;
89
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000090protected:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040091 /** \brief Register an alias.
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000092 * \tparam S subclass of DummyStrategy
93 */
94 template<typename S>
95 static void
96 registerAsImpl(const Name& strategyName)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070097 {
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000098 if (!fw::Strategy::canCreate(strategyName)) {
99 fw::Strategy::registerType<S>(strategyName);
100 }
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700101 }
102
Junxiao Shif3c07812014-03-11 21:48:49 -0700103public:
Davide Pesavento0498ce82021-06-14 02:02:21 -0400104 int afterReceiveInterest_count = 0;
105 int afterContentStoreHit_count = 0;
106 int beforeSatisfyInterest_count = 0;
107 int afterReceiveData_count = 0;
108 int afterReceiveNack_count = 0;
Junxiao Shi15e98b02016-08-12 11:21:44 +0000109
Ju Pan2feb4592019-09-16 20:56:38 +0000110 // a collection of names of PIT entries that afterNewNextHop() was called on
111 std::vector<Name> afterNewNextHopCalls;
112
Junxiao Shi15e98b02016-08-12 11:21:44 +0000113 shared_ptr<Face> interestOutFace;
Junxiao Shif3c07812014-03-11 21:48:49 -0700114};
115
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400116/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400117 * \brief DummyStrategy with specific version.
Junxiao Shi7f566dd2016-12-27 02:28:31 +0000118 */
119template<uint64_t VERSION>
120class VersionedDummyStrategy : public DummyStrategy
121{
122public:
123 static void
124 registerAs(const Name& strategyName)
125 {
126 DummyStrategy::registerAsImpl<VersionedDummyStrategy<VERSION>>(strategyName);
127 }
128
129 static Name
130 getStrategyName()
131 {
132 return DummyStrategy::getStrategyName(VERSION);
133 }
134
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400135 /** \brief Constructor.
Junxiao Shi7f566dd2016-12-27 02:28:31 +0000136 *
137 * The strategy instance name is taken from \p name ; if it does not contain a version component,
138 * \p VERSION will be appended.
139 */
140 explicit
141 VersionedDummyStrategy(Forwarder& forwarder, const Name& name = getStrategyName())
142 : DummyStrategy(forwarder, Strategy::makeInstanceName(name, getStrategyName()))
143 {
144 }
145};
146
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400147} // namespace nfd::tests
Junxiao Shif3c07812014-03-11 21:48:49 -0700148
Junxiao Shi82e7f582014-09-07 15:15:40 -0700149#endif // NFD_TESTS_DAEMON_FW_DUMMY_STRATEGY_HPP