blob: df41c3e004095940dc846cf37b2583242c8c3298 [file] [log] [blame]
Vince Lehman4387e782014-06-19 16:57:45 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi221b6fe2016-07-14 18:21:56 +00003 * Copyright (c) 2014-2016, 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
Vince Lehman76c751c2014-11-18 17:36:38 -060026#include "rib/fib-updater.hpp"
Junxiao Shi221b6fe2016-07-14 18:21:56 +000027#include "rib-test-common.hpp"
28#include "tests/identity-management-fixture.hpp"
Vince Lehman76c751c2014-11-18 17:36:38 -060029
30#include <ndn-cxx/util/dummy-client-face.hpp>
31
Vince Lehman4387e782014-06-19 16:57:45 -050032namespace nfd {
33namespace rib {
34namespace tests {
35
Vince Lehman4387e782014-06-19 16:57:45 -050036inline bool
Vince Lehman76c751c2014-11-18 17:36:38 -060037compareNameFaceIdCostAction(const FibUpdate& lhs, const FibUpdate& rhs)
Vince Lehman4387e782014-06-19 16:57:45 -050038{
Vince Lehman76c751c2014-11-18 17:36:38 -060039 if (lhs.name < rhs.name) {
40 return true;
41 }
42 else if (lhs.name == rhs.name) {
43 if (lhs.faceId < rhs.faceId) {
Vince Lehman4387e782014-06-19 16:57:45 -050044 return true;
45 }
Vince Lehman76c751c2014-11-18 17:36:38 -060046 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 Lehman4387e782014-06-19 16:57:45 -050053 }
Vince Lehman76c751c2014-11-18 17:36:38 -060054 }
Vince Lehman4387e782014-06-19 16:57:45 -050055
56 return false;
57}
58
Davide Pesavento97210d52016-10-14 15:45:48 +020059class FibUpdatesFixture : public nfd::tests::IdentityManagementFixture
Vince Lehman4387e782014-06-19 16:57:45 -050060{
61public:
Vince Lehman76c751c2014-11-18 17:36:38 -060062 FibUpdatesFixture()
Junxiao Shi221b6fe2016-07-14 18:21:56 +000063 : face(getGlobalIoService(), m_keyChain)
64 , controller(face, m_keyChain)
Vince Lehman76c751c2014-11-18 17:36:38 -060065 , fibUpdater(rib, controller)
66 {
67 }
68
Vince Lehman4387e782014-06-19 16:57:45 -050069 void
Vince Lehman218be0a2015-01-15 17:25:20 -060070 insertRoute(const Name& name, uint64_t faceId, uint64_t origin, uint64_t cost, uint64_t flags)
Vince Lehman4387e782014-06-19 16:57:45 -050071 {
Vince Lehman76c751c2014-11-18 17:36:38 -060072 Route route = createRoute(faceId, origin, cost, flags);
Vince Lehman4387e782014-06-19 16:57:45 -050073
Vince Lehman76c751c2014-11-18 17:36:38 -060074 RibUpdate update;
75 update.setAction(RibUpdate::REGISTER)
76 .setName(name)
77 .setRoute(route);
78
79 simulateSuccessfulResponse(update);
Vince Lehman4387e782014-06-19 16:57:45 -050080 }
81
82 void
Vince Lehman218be0a2015-01-15 17:25:20 -060083 eraseRoute(const Name& name, uint64_t faceId, uint64_t origin)
Vince Lehman4387e782014-06-19 16:57:45 -050084 {
Vince Lehman76c751c2014-11-18 17:36:38 -060085 Route route = createRoute(faceId, origin, 0, 0);
Vince Lehman4387e782014-06-19 16:57:45 -050086
Vince Lehman76c751c2014-11-18 17:36:38 -060087 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 Lehman4387e782014-06-19 16:57:45 -0500148 }
149
150
Vince Lehman76c751c2014-11-18 17:36:38 -0600151 FibUpdater::FibUpdateList
Vince Lehman4387e782014-06-19 16:57:45 -0500152 getSortedFibUpdates()
153 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600154 FibUpdater::FibUpdateList updates = getFibUpdates();
Vince Lehman4387e782014-06-19 16:57:45 -0500155 updates.sort(&compareNameFaceIdCostAction);
156 return updates;
157 }
158
Vince Lehman76c751c2014-11-18 17:36:38 -0600159 void
160 clearFibUpdates()
161 {
162 fibUpdater.m_updatesForBatchFaceId.clear();
163 fibUpdater.m_updatesForNonBatchFaceId.clear();
164 }
165
Vince Lehman4387e782014-06-19 16:57:45 -0500166public:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000167 ndn::util::DummyClientFace face;
Vince Lehman76c751c2014-11-18 17:36:38 -0600168 ndn::nfd::Controller controller;
Vince Lehman76c751c2014-11-18 17:36:38 -0600169
Davide Pesavento97210d52016-10-14 15:45:48 +0200170 Rib rib;
171 FibUpdater fibUpdater;
Vince Lehman76c751c2014-11-18 17:36:38 -0600172 FibUpdater::FibUpdateList fibUpdates;
Vince Lehman4387e782014-06-19 16:57:45 -0500173};
174
175} // namespace tests
176} // namespace rib
177} // namespace nfd