blob: 15e623f980aa3b5b05d802e75c534308f54badbc [file] [log] [blame]
Vince Lehman4387e782014-06-19 16:57:45 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi52009042018-09-10 12:33:56 +00002/*
Davide Pesaventoae430302023-05-11 01:42:46 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08004 * 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.
Vince Lehman4387e782014-06-19 16:57:45 -050010 *
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
Davide Pesavento1b077f62019-02-19 19:19:44 -050026#ifndef NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP
27#define NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP
Davide Pesavento22db5392017-04-14 00:56:43 -040028
Vince Lehman76c751c2014-11-18 17:36:38 -060029#include "rib/fib-updater.hpp"
Junxiao Shidf1dc652019-08-30 19:03:19 +000030#include "common/global.hpp"
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040031
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040032#include "tests/key-chain-fixture.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040033#include "tests/daemon/global-io-fixture.hpp"
Davide Pesavento1b077f62019-02-19 19:19:44 -050034#include "tests/daemon/rib/create-route.hpp"
Vince Lehman76c751c2014-11-18 17:36:38 -060035
Davide Pesavento5d642632023-10-03 00:36:08 -040036#include <boost/asio/defer.hpp>
37
Vince Lehman76c751c2014-11-18 17:36:38 -060038#include <ndn-cxx/util/dummy-client-face.hpp>
39
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040namespace nfd::tests {
Vince Lehman4387e782014-06-19 16:57:45 -050041
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040042using rib::FibUpdate;
43using rib::FibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -050044
Junxiao Shidf1dc652019-08-30 19:03:19 +000045class MockFibUpdater : public FibUpdater
46{
47public:
48 using FibUpdater::FibUpdater;
49
50 void
51 sortUpdates()
52 {
53 updates.sort([] (const auto& lhs, const auto& rhs) {
54 return std::tie(lhs.name, lhs.faceId, lhs.cost, lhs.action) <
55 std::tie(rhs.name, rhs.faceId, rhs.cost, rhs.action);
56 });
57 }
58
59private:
60 void
61 sendAddNextHopUpdate(const FibUpdate& update,
62 const FibUpdateSuccessCallback& onSuccess,
63 const FibUpdateFailureCallback& onFailure,
64 uint32_t nTimeouts) override
65 {
66 mockUpdate(update, onSuccess, onFailure, nTimeouts);
67 }
68
69 void
70 sendRemoveNextHopUpdate(const FibUpdate& update,
71 const FibUpdateSuccessCallback& onSuccess,
72 const FibUpdateFailureCallback& onFailure,
73 uint32_t nTimeouts) override
74 {
75 mockUpdate(update, onSuccess, onFailure, nTimeouts);
76 }
77
78 void
79 mockUpdate(const FibUpdate& update,
80 const FibUpdateSuccessCallback& onSuccess,
81 const FibUpdateFailureCallback& onFailure,
82 uint32_t nTimeouts)
83 {
84 updates.push_back(update);
Davide Pesavento5d642632023-10-03 00:36:08 -040085 boost::asio::defer(getGlobalIoService(), [=] {
Junxiao Shidf1dc652019-08-30 19:03:19 +000086 if (mockSuccess) {
87 onUpdateSuccess(update, onSuccess, onFailure);
88 }
89 else {
90 ndn::mgmt::ControlResponse resp(504, "mocked failure");
91 onUpdateError(update, onSuccess, onFailure, resp, nTimeouts);
92 }
93 });
94 }
95
96public:
97 FibUpdateList updates;
98 bool mockSuccess = true;
99};
100
Davide Pesavento14e71f02019-03-28 17:35:25 -0400101class FibUpdatesFixture : public GlobalIoFixture, public KeyChainFixture
Vince Lehman4387e782014-06-19 16:57:45 -0500102{
103public:
Vince Lehman76c751c2014-11-18 17:36:38 -0600104 FibUpdatesFixture()
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400105 : face(g_io, m_keyChain)
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000106 , controller(face, m_keyChain)
Vince Lehman76c751c2014-11-18 17:36:38 -0600107 , fibUpdater(rib, controller)
108 {
109 }
110
Vince Lehman4387e782014-06-19 16:57:45 -0500111 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400112 insertRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -0500113 std::underlying_type_t<ndn::nfd::RouteOrigin> origin,
114 uint64_t cost,
115 std::underlying_type_t<ndn::nfd::RouteFlags> flags)
Vince Lehman4387e782014-06-19 16:57:45 -0500116 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400117 auto route = createRoute(faceId, origin, cost, flags);
Vince Lehman4387e782014-06-19 16:57:45 -0500118
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400119 rib::RibUpdate update;
120 update.setAction(rib::RibUpdate::REGISTER)
Vince Lehman76c751c2014-11-18 17:36:38 -0600121 .setName(name)
122 .setRoute(route);
123
Junxiao Shi52009042018-09-10 12:33:56 +0000124 rib.beginApplyUpdate(update, nullptr, nullptr);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000125 pollIo();
Vince Lehman4387e782014-06-19 16:57:45 -0500126 }
127
128 void
Davide Pesavento22db5392017-04-14 00:56:43 -0400129 eraseRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -0500130 std::underlying_type_t<ndn::nfd::RouteOrigin> origin)
Vince Lehman4387e782014-06-19 16:57:45 -0500131 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400132 auto route = createRoute(faceId, origin, 0, 0);
Vince Lehman4387e782014-06-19 16:57:45 -0500133
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400134 rib::RibUpdate update;
135 update.setAction(rib::RibUpdate::UNREGISTER)
Vince Lehman76c751c2014-11-18 17:36:38 -0600136 .setName(name)
137 .setRoute(route);
138
Junxiao Shi52009042018-09-10 12:33:56 +0000139 rib.beginApplyUpdate(update, nullptr, nullptr);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000140 pollIo();
Vince Lehman76c751c2014-11-18 17:36:38 -0600141 }
142
143 void
144 destroyFace(uint64_t faceId)
145 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600146 rib.beginRemoveFace(faceId);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000147 pollIo();
Vince Lehman76c751c2014-11-18 17:36:38 -0600148 }
149
Vince Lehman76c751c2014-11-18 17:36:38 -0600150 const FibUpdater::FibUpdateList&
Junxiao Shidf1dc652019-08-30 19:03:19 +0000151 getFibUpdates() const
Vince Lehman76c751c2014-11-18 17:36:38 -0600152 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000153 return fibUpdater.updates;
Vince Lehman4387e782014-06-19 16:57:45 -0500154 }
155
Junxiao Shidf1dc652019-08-30 19:03:19 +0000156 const FibUpdater::FibUpdateList&
Vince Lehman4387e782014-06-19 16:57:45 -0500157 getSortedFibUpdates()
158 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000159 fibUpdater.sortUpdates();
160 return fibUpdater.updates;
Vince Lehman4387e782014-06-19 16:57:45 -0500161 }
162
Vince Lehman76c751c2014-11-18 17:36:38 -0600163 void
164 clearFibUpdates()
165 {
Junxiao Shidf1dc652019-08-30 19:03:19 +0000166 fibUpdater.updates.clear();
Junxiao Shi52009042018-09-10 12:33:56 +0000167 }
168
Vince Lehman4387e782014-06-19 16:57:45 -0500169public:
Davide Pesaventoae430302023-05-11 01:42:46 -0400170 ndn::DummyClientFace face;
Vince Lehman76c751c2014-11-18 17:36:38 -0600171 ndn::nfd::Controller controller;
Vince Lehman76c751c2014-11-18 17:36:38 -0600172
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400173 rib::Rib rib;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000174 MockFibUpdater fibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -0500175};
176
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400177} // namespace nfd::tests
Davide Pesavento22db5392017-04-14 00:56:43 -0400178
Davide Pesavento1b077f62019-02-19 19:19:44 -0500179#endif // NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP