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