blob: 1f293c6066cb96216eb11f3a10c1a81624cfd03b [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
Junxiao Shi221b6fe2016-07-14 18:21:56 +000036using namespace nfd::tests;
37
Vince Lehman4387e782014-06-19 16:57:45 -050038inline bool
Vince Lehman76c751c2014-11-18 17:36:38 -060039compareNameFaceIdCostAction(const FibUpdate& lhs, const FibUpdate& rhs)
Vince Lehman4387e782014-06-19 16:57:45 -050040{
Vince Lehman76c751c2014-11-18 17:36:38 -060041 if (lhs.name < rhs.name) {
42 return true;
43 }
44 else if (lhs.name == rhs.name) {
45 if (lhs.faceId < rhs.faceId) {
Vince Lehman4387e782014-06-19 16:57:45 -050046 return true;
47 }
Vince Lehman76c751c2014-11-18 17:36:38 -060048 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 Lehman4387e782014-06-19 16:57:45 -050055 }
Vince Lehman76c751c2014-11-18 17:36:38 -060056 }
Vince Lehman4387e782014-06-19 16:57:45 -050057
58 return false;
59}
60
Junxiao Shi221b6fe2016-07-14 18:21:56 +000061class FibUpdatesFixture : public IdentityManagementFixture
Vince Lehman4387e782014-06-19 16:57:45 -050062{
63public:
Vince Lehman76c751c2014-11-18 17:36:38 -060064 FibUpdatesFixture()
Junxiao Shi221b6fe2016-07-14 18:21:56 +000065 : face(getGlobalIoService(), m_keyChain)
66 , controller(face, m_keyChain)
Vince Lehman76c751c2014-11-18 17:36:38 -060067 , fibUpdater(rib, controller)
68 {
69 }
70
Vince Lehman4387e782014-06-19 16:57:45 -050071 void
Vince Lehman218be0a2015-01-15 17:25:20 -060072 insertRoute(const Name& name, uint64_t faceId, uint64_t origin, uint64_t cost, uint64_t flags)
Vince Lehman4387e782014-06-19 16:57:45 -050073 {
Vince Lehman76c751c2014-11-18 17:36:38 -060074 Route route = createRoute(faceId, origin, cost, flags);
Vince Lehman4387e782014-06-19 16:57:45 -050075
Vince Lehman76c751c2014-11-18 17:36:38 -060076 RibUpdate update;
77 update.setAction(RibUpdate::REGISTER)
78 .setName(name)
79 .setRoute(route);
80
81 simulateSuccessfulResponse(update);
Vince Lehman4387e782014-06-19 16:57:45 -050082 }
83
84 void
Vince Lehman218be0a2015-01-15 17:25:20 -060085 eraseRoute(const Name& name, uint64_t faceId, uint64_t origin)
Vince Lehman4387e782014-06-19 16:57:45 -050086 {
Vince Lehman76c751c2014-11-18 17:36:38 -060087 Route route = createRoute(faceId, origin, 0, 0);
Vince Lehman4387e782014-06-19 16:57:45 -050088
Vince Lehman76c751c2014-11-18 17:36:38 -060089 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 Lehman4387e782014-06-19 16:57:45 -0500150 }
151
152
Vince Lehman76c751c2014-11-18 17:36:38 -0600153 FibUpdater::FibUpdateList
Vince Lehman4387e782014-06-19 16:57:45 -0500154 getSortedFibUpdates()
155 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600156 FibUpdater::FibUpdateList updates = getFibUpdates();
Vince Lehman4387e782014-06-19 16:57:45 -0500157 updates.sort(&compareNameFaceIdCostAction);
158 return updates;
159 }
160
Vince Lehman76c751c2014-11-18 17:36:38 -0600161 void
162 clearFibUpdates()
163 {
164 fibUpdater.m_updatesForBatchFaceId.clear();
165 fibUpdater.m_updatesForNonBatchFaceId.clear();
166 }
167
Vince Lehman4387e782014-06-19 16:57:45 -0500168public:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000169 ndn::util::DummyClientFace face;
Vince Lehman76c751c2014-11-18 17:36:38 -0600170 ndn::nfd::Controller controller;
171 rib::FibUpdater fibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -0500172 rib::Rib rib;
Vince Lehman76c751c2014-11-18 17:36:38 -0600173
174 FibUpdater::FibUpdateList fibUpdates;
Vince Lehman4387e782014-06-19 16:57:45 -0500175};
176
177} // namespace tests
178} // namespace rib
179} // namespace nfd