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 | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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" |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 30 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 31 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 32 | #include "tests/daemon/global-io-fixture.hpp" |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 33 | #include "tests/daemon/rib/create-route.hpp" |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 34 | |
| 35 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 36 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 37 | namespace nfd { |
| 38 | namespace rib { |
| 39 | namespace tests { |
| 40 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 41 | inline bool |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 42 | compareNameFaceIdCostAction(const FibUpdate& lhs, const FibUpdate& rhs) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 43 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 44 | if (lhs.name < rhs.name) { |
| 45 | return true; |
| 46 | } |
| 47 | else if (lhs.name == rhs.name) { |
| 48 | if (lhs.faceId < rhs.faceId) { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 49 | return true; |
| 50 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 51 | else if (lhs.faceId == rhs.faceId) { |
| 52 | if (lhs.cost < rhs.cost) { |
| 53 | return true; |
| 54 | } |
| 55 | else if (lhs.cost == rhs.cost) { |
| 56 | return lhs.action < rhs.action; |
| 57 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 58 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 59 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 64 | class FibUpdatesFixture : public nfd::tests::GlobalIoFixture, public nfd::tests::KeyChainFixture |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 65 | { |
| 66 | public: |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 67 | FibUpdatesFixture() |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 68 | : face(g_io, m_keyChain) |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 69 | , controller(face, m_keyChain) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 70 | , fibUpdater(rib, controller) |
| 71 | { |
| 72 | } |
| 73 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 74 | void |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 75 | insertRoute(const Name& name, uint64_t faceId, |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 76 | std::underlying_type_t<ndn::nfd::RouteOrigin> origin, |
| 77 | uint64_t cost, |
| 78 | std::underlying_type_t<ndn::nfd::RouteFlags> flags) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 79 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 80 | Route route = createRoute(faceId, origin, cost, flags); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 81 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 82 | RibUpdate update; |
| 83 | update.setAction(RibUpdate::REGISTER) |
| 84 | .setName(name) |
| 85 | .setRoute(route); |
| 86 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 87 | simulateSuccessfulResponse(); |
| 88 | rib.beginApplyUpdate(update, nullptr, nullptr); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 92 | eraseRoute(const Name& name, uint64_t faceId, |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 93 | std::underlying_type_t<ndn::nfd::RouteOrigin> origin) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 94 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 95 | Route route = createRoute(faceId, origin, 0, 0); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 96 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 97 | RibUpdate update; |
| 98 | update.setAction(RibUpdate::UNREGISTER) |
| 99 | .setName(name) |
| 100 | .setRoute(route); |
| 101 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 102 | simulateSuccessfulResponse(); |
| 103 | rib.beginApplyUpdate(update, nullptr, nullptr); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void |
| 107 | destroyFace(uint64_t faceId) |
| 108 | { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 109 | simulateSuccessfulResponse(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 110 | rib.beginRemoveFace(faceId); |
| 111 | } |
| 112 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 113 | const FibUpdater::FibUpdateList& |
| 114 | getFibUpdates() |
| 115 | { |
| 116 | fibUpdates.clear(); |
| 117 | fibUpdates = fibUpdater.m_updatesForBatchFaceId; |
| 118 | fibUpdates.insert(fibUpdates.end(), fibUpdater.m_updatesForNonBatchFaceId.begin(), |
| 119 | fibUpdater.m_updatesForNonBatchFaceId.end()); |
| 120 | |
| 121 | return fibUpdates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 124 | FibUpdater::FibUpdateList |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 125 | getSortedFibUpdates() |
| 126 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 127 | FibUpdater::FibUpdateList updates = getFibUpdates(); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 128 | updates.sort(&compareNameFaceIdCostAction); |
| 129 | return updates; |
| 130 | } |
| 131 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 132 | void |
| 133 | clearFibUpdates() |
| 134 | { |
| 135 | fibUpdater.m_updatesForBatchFaceId.clear(); |
| 136 | fibUpdater.m_updatesForNonBatchFaceId.clear(); |
| 137 | } |
| 138 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 139 | private: |
| 140 | void |
| 141 | simulateSuccessfulResponse() |
| 142 | { |
| 143 | rib.mockFibResponse = [] (const RibUpdateBatch&) { return true; }; |
| 144 | rib.wantMockFibResponseOnce = true; |
| 145 | } |
| 146 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 147 | public: |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 148 | ndn::util::DummyClientFace face; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 149 | ndn::nfd::Controller controller; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 150 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 151 | Rib rib; |
| 152 | FibUpdater fibUpdater; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 153 | FibUpdater::FibUpdateList fibUpdates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | } // namespace tests |
| 157 | } // namespace rib |
| 158 | } // namespace nfd |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 159 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 160 | #endif // NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP |