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