Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 26 | #include "rib/fib-updater.hpp" |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 27 | #include "rib-test-common.hpp" |
| 28 | #include "tests/identity-management-fixture.hpp" |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 29 | |
| 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 31 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 32 | namespace nfd { |
| 33 | namespace rib { |
| 34 | namespace tests { |
| 35 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 36 | inline bool |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 37 | compareNameFaceIdCostAction(const FibUpdate& lhs, const FibUpdate& rhs) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 38 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 39 | if (lhs.name < rhs.name) { |
| 40 | return true; |
| 41 | } |
| 42 | else if (lhs.name == rhs.name) { |
| 43 | if (lhs.faceId < rhs.faceId) { |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 44 | return true; |
| 45 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 46 | else if (lhs.faceId == rhs.faceId) { |
| 47 | if (lhs.cost < rhs.cost) { |
| 48 | return true; |
| 49 | } |
| 50 | else if (lhs.cost == rhs.cost) { |
| 51 | return lhs.action < rhs.action; |
| 52 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 53 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 54 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 59 | class FibUpdatesFixture : public nfd::tests::IdentityManagementFixture |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 60 | { |
| 61 | public: |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 62 | FibUpdatesFixture() |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 63 | : face(getGlobalIoService(), m_keyChain) |
| 64 | , controller(face, m_keyChain) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 65 | , fibUpdater(rib, controller) |
| 66 | { |
| 67 | } |
| 68 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 69 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 70 | insertRoute(const Name& name, uint64_t faceId, uint64_t origin, uint64_t cost, uint64_t flags) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 71 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 72 | Route route = createRoute(faceId, origin, cost, flags); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 73 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 74 | RibUpdate update; |
| 75 | update.setAction(RibUpdate::REGISTER) |
| 76 | .setName(name) |
| 77 | .setRoute(route); |
| 78 | |
| 79 | simulateSuccessfulResponse(update); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 83 | eraseRoute(const Name& name, uint64_t faceId, uint64_t origin) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 84 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 85 | Route route = createRoute(faceId, origin, 0, 0); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 86 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 87 | RibUpdate update; |
| 88 | update.setAction(RibUpdate::UNREGISTER) |
| 89 | .setName(name) |
| 90 | .setRoute(route); |
| 91 | |
| 92 | simulateSuccessfulResponse(update); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | onSendBatchFromQueue(const RibUpdateBatch& batch) |
| 97 | { |
| 98 | Rib::UpdateSuccessCallback managerCallback = bind(&FibUpdatesFixture::onSuccess, this); |
| 99 | |
| 100 | // Only receive callback after the first send |
| 101 | rib.m_onSendBatchFromQueue = nullptr; |
| 102 | |
| 103 | rib.onFibUpdateSuccess(batch, fibUpdater.m_inheritedRoutes, managerCallback); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | destroyFace(uint64_t faceId) |
| 108 | { |
| 109 | rib.m_onSendBatchFromQueue = bind(&FibUpdatesFixture::onSendBatchFromQueue, this, _1); |
| 110 | |
| 111 | rib.beginRemoveFace(faceId); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | simulateSuccessfulResponse(const RibUpdate& update) |
| 116 | { |
| 117 | Rib::UpdateSuccessCallback managerCallback = bind(&FibUpdatesFixture::onSuccess, this); |
| 118 | |
| 119 | rib.beginApplyUpdate(update, managerCallback, nullptr); |
| 120 | |
| 121 | RibUpdateBatch batch(update.getRoute().faceId); |
| 122 | batch.add(update); |
| 123 | |
| 124 | // Simulate a successful response from NFD |
| 125 | rib.onFibUpdateSuccess(batch, fibUpdater.m_inheritedRoutes, managerCallback); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | onSuccess() |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | onFailure() |
| 135 | { |
| 136 | BOOST_FAIL("FibUpdate failed"); |
| 137 | } |
| 138 | |
| 139 | const FibUpdater::FibUpdateList& |
| 140 | getFibUpdates() |
| 141 | { |
| 142 | fibUpdates.clear(); |
| 143 | fibUpdates = fibUpdater.m_updatesForBatchFaceId; |
| 144 | fibUpdates.insert(fibUpdates.end(), fibUpdater.m_updatesForNonBatchFaceId.begin(), |
| 145 | fibUpdater.m_updatesForNonBatchFaceId.end()); |
| 146 | |
| 147 | return fibUpdates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 151 | FibUpdater::FibUpdateList |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 152 | getSortedFibUpdates() |
| 153 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 154 | FibUpdater::FibUpdateList updates = getFibUpdates(); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 155 | updates.sort(&compareNameFaceIdCostAction); |
| 156 | return updates; |
| 157 | } |
| 158 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 159 | void |
| 160 | clearFibUpdates() |
| 161 | { |
| 162 | fibUpdater.m_updatesForBatchFaceId.clear(); |
| 163 | fibUpdater.m_updatesForNonBatchFaceId.clear(); |
| 164 | } |
| 165 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 166 | public: |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 167 | ndn::util::DummyClientFace face; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 168 | ndn::nfd::Controller controller; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 169 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 170 | Rib rib; |
| 171 | FibUpdater fibUpdater; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 172 | FibUpdater::FibUpdateList fibUpdates; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } // namespace tests |
| 176 | } // namespace rib |
| 177 | } // namespace nfd |