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