Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame] | 4 | * 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 Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 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 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 26 | #ifndef NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP |
| 27 | #define NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 28 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 29 | #include "rib/fib-updater.hpp" |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 30 | #include "common/global.hpp" |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 31 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 32 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 33 | #include "tests/daemon/global-io-fixture.hpp" |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 34 | #include "tests/daemon/rib/create-route.hpp" |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 35 | |
| 36 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 37 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 38 | namespace nfd::tests { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 39 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 40 | using rib::FibUpdate; |
| 41 | using rib::FibUpdater; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 42 | |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 43 | class MockFibUpdater : public FibUpdater |
| 44 | { |
| 45 | public: |
| 46 | using FibUpdater::FibUpdater; |
| 47 | |
| 48 | void |
| 49 | sortUpdates() |
| 50 | { |
| 51 | updates.sort([] (const auto& lhs, const auto& rhs) { |
| 52 | return std::tie(lhs.name, lhs.faceId, lhs.cost, lhs.action) < |
| 53 | std::tie(rhs.name, rhs.faceId, rhs.cost, rhs.action); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | void |
| 59 | sendAddNextHopUpdate(const FibUpdate& update, |
| 60 | const FibUpdateSuccessCallback& onSuccess, |
| 61 | const FibUpdateFailureCallback& onFailure, |
| 62 | uint32_t nTimeouts) override |
| 63 | { |
| 64 | mockUpdate(update, onSuccess, onFailure, nTimeouts); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | sendRemoveNextHopUpdate(const FibUpdate& update, |
| 69 | const FibUpdateSuccessCallback& onSuccess, |
| 70 | const FibUpdateFailureCallback& onFailure, |
| 71 | uint32_t nTimeouts) override |
| 72 | { |
| 73 | mockUpdate(update, onSuccess, onFailure, nTimeouts); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | mockUpdate(const FibUpdate& update, |
| 78 | const FibUpdateSuccessCallback& onSuccess, |
| 79 | const FibUpdateFailureCallback& onFailure, |
| 80 | uint32_t nTimeouts) |
| 81 | { |
| 82 | updates.push_back(update); |
| 83 | getGlobalIoService().post([=] { |
| 84 | if (mockSuccess) { |
| 85 | onUpdateSuccess(update, onSuccess, onFailure); |
| 86 | } |
| 87 | else { |
| 88 | ndn::mgmt::ControlResponse resp(504, "mocked failure"); |
| 89 | onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | public: |
| 95 | FibUpdateList updates; |
| 96 | bool mockSuccess = true; |
| 97 | }; |
| 98 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 99 | class FibUpdatesFixture : public GlobalIoFixture, public KeyChainFixture |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 100 | { |
| 101 | public: |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 102 | FibUpdatesFixture() |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 103 | : face(g_io, m_keyChain) |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 104 | , controller(face, m_keyChain) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 105 | , fibUpdater(rib, controller) |
| 106 | { |
| 107 | } |
| 108 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 109 | void |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 110 | insertRoute(const Name& name, uint64_t faceId, |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 111 | std::underlying_type_t<ndn::nfd::RouteOrigin> origin, |
| 112 | uint64_t cost, |
| 113 | std::underlying_type_t<ndn::nfd::RouteFlags> flags) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 114 | { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 115 | auto route = createRoute(faceId, origin, cost, flags); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 116 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 117 | rib::RibUpdate update; |
| 118 | update.setAction(rib::RibUpdate::REGISTER) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 119 | .setName(name) |
| 120 | .setRoute(route); |
| 121 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 122 | rib.beginApplyUpdate(update, nullptr, nullptr); |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 123 | pollIo(); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 127 | eraseRoute(const Name& name, uint64_t faceId, |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 128 | std::underlying_type_t<ndn::nfd::RouteOrigin> origin) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 129 | { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 130 | auto route = createRoute(faceId, origin, 0, 0); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 131 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 132 | rib::RibUpdate update; |
| 133 | update.setAction(rib::RibUpdate::UNREGISTER) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 134 | .setName(name) |
| 135 | .setRoute(route); |
| 136 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 137 | rib.beginApplyUpdate(update, nullptr, nullptr); |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 138 | pollIo(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
| 142 | destroyFace(uint64_t faceId) |
| 143 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 144 | rib.beginRemoveFace(faceId); |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 145 | pollIo(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 146 | } |
| 147 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 148 | const FibUpdater::FibUpdateList& |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 149 | getFibUpdates() const |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 150 | { |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 151 | return fibUpdater.updates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 154 | const FibUpdater::FibUpdateList& |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 155 | getSortedFibUpdates() |
| 156 | { |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 157 | fibUpdater.sortUpdates(); |
| 158 | return fibUpdater.updates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 161 | void |
| 162 | clearFibUpdates() |
| 163 | { |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 164 | fibUpdater.updates.clear(); |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 167 | public: |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 168 | ndn::DummyClientFace face; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 169 | ndn::nfd::Controller controller; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 170 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 171 | rib::Rib rib; |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 172 | MockFibUpdater fibUpdater; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 173 | }; |
| 174 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 175 | } // namespace nfd::tests |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 176 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 177 | #endif // NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP |