Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 84 | simulateSuccessfulResponse(update); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 88 | eraseRoute(const Name& name, uint64_t faceId, |
| 89 | std::underlying_type<ndn::nfd::RouteOrigin>::type origin) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 90 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 91 | Route route = createRoute(faceId, origin, 0, 0); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 92 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 93 | RibUpdate update; |
| 94 | update.setAction(RibUpdate::UNREGISTER) |
| 95 | .setName(name) |
| 96 | .setRoute(route); |
| 97 | |
| 98 | simulateSuccessfulResponse(update); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | onSendBatchFromQueue(const RibUpdateBatch& batch) |
| 103 | { |
| 104 | Rib::UpdateSuccessCallback managerCallback = bind(&FibUpdatesFixture::onSuccess, this); |
| 105 | |
| 106 | // Only receive callback after the first send |
| 107 | rib.m_onSendBatchFromQueue = nullptr; |
| 108 | |
| 109 | rib.onFibUpdateSuccess(batch, fibUpdater.m_inheritedRoutes, managerCallback); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | destroyFace(uint64_t faceId) |
| 114 | { |
| 115 | rib.m_onSendBatchFromQueue = bind(&FibUpdatesFixture::onSendBatchFromQueue, this, _1); |
| 116 | |
| 117 | rib.beginRemoveFace(faceId); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | simulateSuccessfulResponse(const RibUpdate& update) |
| 122 | { |
| 123 | Rib::UpdateSuccessCallback managerCallback = bind(&FibUpdatesFixture::onSuccess, this); |
| 124 | |
| 125 | rib.beginApplyUpdate(update, managerCallback, nullptr); |
| 126 | |
| 127 | RibUpdateBatch batch(update.getRoute().faceId); |
| 128 | batch.add(update); |
| 129 | |
| 130 | // Simulate a successful response from NFD |
| 131 | rib.onFibUpdateSuccess(batch, fibUpdater.m_inheritedRoutes, managerCallback); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | onSuccess() |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | onFailure() |
| 141 | { |
| 142 | BOOST_FAIL("FibUpdate failed"); |
| 143 | } |
| 144 | |
| 145 | const FibUpdater::FibUpdateList& |
| 146 | getFibUpdates() |
| 147 | { |
| 148 | fibUpdates.clear(); |
| 149 | fibUpdates = fibUpdater.m_updatesForBatchFaceId; |
| 150 | fibUpdates.insert(fibUpdates.end(), fibUpdater.m_updatesForNonBatchFaceId.begin(), |
| 151 | fibUpdater.m_updatesForNonBatchFaceId.end()); |
| 152 | |
| 153 | return fibUpdates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 157 | FibUpdater::FibUpdateList |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 158 | getSortedFibUpdates() |
| 159 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 160 | FibUpdater::FibUpdateList updates = getFibUpdates(); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 161 | updates.sort(&compareNameFaceIdCostAction); |
| 162 | return updates; |
| 163 | } |
| 164 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 165 | void |
| 166 | clearFibUpdates() |
| 167 | { |
| 168 | fibUpdater.m_updatesForBatchFaceId.clear(); |
| 169 | fibUpdater.m_updatesForNonBatchFaceId.clear(); |
| 170 | } |
| 171 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 172 | public: |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 173 | ndn::util::DummyClientFace face; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 174 | ndn::nfd::Controller controller; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 175 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 176 | Rib rib; |
| 177 | FibUpdater fibUpdater; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 178 | FibUpdater::FibUpdateList fibUpdates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | } // namespace tests |
| 182 | } // namespace rib |
| 183 | } // namespace nfd |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 184 | |
| 185 | #endif // NFD_TESTS_RIB_FIB_UPDATES_COMMON_HPP |