blob: 59505bcef5359a7df762e5e4659bfe1f32bdb10a [file] [log] [blame]
Vince Lehman4387e782014-06-19 16:57:45 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento22db5392017-04-14 00:56:43 -04003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08004 * 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 Lehman4387e782014-06-19 16:57:45 -050010 *
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 Pesavento22db5392017-04-14 00:56:43 -040026#ifndef NFD_TESTS_RIB_FIB_UPDATES_COMMON_HPP
27#define NFD_TESTS_RIB_FIB_UPDATES_COMMON_HPP
28
Vince Lehman76c751c2014-11-18 17:36:38 -060029#include "rib/fib-updater.hpp"
Junxiao Shi221b6fe2016-07-14 18:21:56 +000030#include "rib-test-common.hpp"
31#include "tests/identity-management-fixture.hpp"
Vince Lehman76c751c2014-11-18 17:36:38 -060032
33#include <ndn-cxx/util/dummy-client-face.hpp>
34
Vince Lehman4387e782014-06-19 16:57:45 -050035namespace nfd {
36namespace rib {
37namespace tests {
38
Vince Lehman4387e782014-06-19 16:57:45 -050039inline bool
Vince Lehman76c751c2014-11-18 17:36:38 -060040compareNameFaceIdCostAction(const FibUpdate& lhs, const FibUpdate& rhs)
Vince Lehman4387e782014-06-19 16:57:45 -050041{
Vince Lehman76c751c2014-11-18 17:36:38 -060042 if (lhs.name < rhs.name) {
43 return true;
44 }
45 else if (lhs.name == rhs.name) {
46 if (lhs.faceId < rhs.faceId) {
Vince Lehman4387e782014-06-19 16:57:45 -050047 return true;
48 }
Vince Lehman76c751c2014-11-18 17:36:38 -060049 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 Lehman4387e782014-06-19 16:57:45 -050056 }
Vince Lehman76c751c2014-11-18 17:36:38 -060057 }
Vince Lehman4387e782014-06-19 16:57:45 -050058
59 return false;
60}
61
Davide Pesavento97210d52016-10-14 15:45:48 +020062class FibUpdatesFixture : public nfd::tests::IdentityManagementFixture
Vince Lehman4387e782014-06-19 16:57:45 -050063{
64public:
Vince Lehman76c751c2014-11-18 17:36:38 -060065 FibUpdatesFixture()
Junxiao Shi221b6fe2016-07-14 18:21:56 +000066 : face(getGlobalIoService(), m_keyChain)
67 , controller(face, m_keyChain)
Vince Lehman76c751c2014-11-18 17:36:38 -060068 , fibUpdater(rib, controller)
69 {
70 }
71
Vince Lehman4387e782014-06-19 16:57:45 -050072 void
Davide Pesavento22db5392017-04-14 00:56:43 -040073 insertRoute(const Name& name, uint64_t faceId,
74 std::underlying_type<ndn::nfd::RouteOrigin>::type origin,
75 uint64_t cost, uint64_t flags)
Vince Lehman4387e782014-06-19 16:57:45 -050076 {
Vince Lehman76c751c2014-11-18 17:36:38 -060077 Route route = createRoute(faceId, origin, cost, flags);
Vince Lehman4387e782014-06-19 16:57:45 -050078
Vince Lehman76c751c2014-11-18 17:36:38 -060079 RibUpdate update;
80 update.setAction(RibUpdate::REGISTER)
81 .setName(name)
82 .setRoute(route);
83
84 simulateSuccessfulResponse(update);
Vince Lehman4387e782014-06-19 16:57:45 -050085 }
86
87 void
Davide Pesavento22db5392017-04-14 00:56:43 -040088 eraseRoute(const Name& name, uint64_t faceId,
89 std::underlying_type<ndn::nfd::RouteOrigin>::type origin)
Vince Lehman4387e782014-06-19 16:57:45 -050090 {
Vince Lehman76c751c2014-11-18 17:36:38 -060091 Route route = createRoute(faceId, origin, 0, 0);
Vince Lehman4387e782014-06-19 16:57:45 -050092
Vince Lehman76c751c2014-11-18 17:36:38 -060093 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 Lehman4387e782014-06-19 16:57:45 -0500154 }
155
156
Vince Lehman76c751c2014-11-18 17:36:38 -0600157 FibUpdater::FibUpdateList
Vince Lehman4387e782014-06-19 16:57:45 -0500158 getSortedFibUpdates()
159 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600160 FibUpdater::FibUpdateList updates = getFibUpdates();
Vince Lehman4387e782014-06-19 16:57:45 -0500161 updates.sort(&compareNameFaceIdCostAction);
162 return updates;
163 }
164
Vince Lehman76c751c2014-11-18 17:36:38 -0600165 void
166 clearFibUpdates()
167 {
168 fibUpdater.m_updatesForBatchFaceId.clear();
169 fibUpdater.m_updatesForNonBatchFaceId.clear();
170 }
171
Vince Lehman4387e782014-06-19 16:57:45 -0500172public:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000173 ndn::util::DummyClientFace face;
Vince Lehman76c751c2014-11-18 17:36:38 -0600174 ndn::nfd::Controller controller;
Vince Lehman76c751c2014-11-18 17:36:38 -0600175
Davide Pesavento97210d52016-10-14 15:45:48 +0200176 Rib rib;
177 FibUpdater fibUpdater;
Vince Lehman76c751c2014-11-18 17:36:38 -0600178 FibUpdater::FibUpdateList fibUpdates;
Vince Lehman4387e782014-06-19 16:57:45 -0500179};
180
181} // namespace tests
182} // namespace rib
183} // namespace nfd
Davide Pesavento22db5392017-04-14 00:56:43 -0400184
185#endif // NFD_TESTS_RIB_FIB_UPDATES_COMMON_HPP