blob: 34ae41d004cfc5cbc55c0fe1f076918efa135df6 [file] [log] [blame]
Vince Lehman4387e782014-06-19 16:57:45 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08003 * 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 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-test-common.hpp"
27#include "rib/fib-updater.hpp"
28
29#include <ndn-cxx/util/dummy-client-face.hpp>
30
Vince Lehman4387e782014-06-19 16:57:45 -050031namespace nfd {
32namespace rib {
33namespace tests {
34
Vince Lehman4387e782014-06-19 16:57:45 -050035inline bool
Vince Lehman76c751c2014-11-18 17:36:38 -060036compareNameFaceIdCostAction(const FibUpdate& lhs, const FibUpdate& rhs)
Vince Lehman4387e782014-06-19 16:57:45 -050037{
Vince Lehman76c751c2014-11-18 17:36:38 -060038 if (lhs.name < rhs.name) {
39 return true;
40 }
41 else if (lhs.name == rhs.name) {
42 if (lhs.faceId < rhs.faceId) {
Vince Lehman4387e782014-06-19 16:57:45 -050043 return true;
44 }
Vince Lehman76c751c2014-11-18 17:36:38 -060045 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 Lehman4387e782014-06-19 16:57:45 -050052 }
Vince Lehman76c751c2014-11-18 17:36:38 -060053 }
Vince Lehman4387e782014-06-19 16:57:45 -050054
55 return false;
56}
57
58class FibUpdatesFixture : public nfd::tests::BaseFixture
59{
60public:
Vince Lehman76c751c2014-11-18 17:36:38 -060061 FibUpdatesFixture()
62 : face(ndn::util::makeDummyClientFace())
63 , controller(*face, keyChain)
64 , fibUpdater(rib, controller)
65 {
66 }
67
Vince Lehman4387e782014-06-19 16:57:45 -050068 void
Vince Lehman218be0a2015-01-15 17:25:20 -060069 insertRoute(const Name& name, uint64_t faceId, uint64_t origin, uint64_t cost, uint64_t flags)
Vince Lehman4387e782014-06-19 16:57:45 -050070 {
Vince Lehman76c751c2014-11-18 17:36:38 -060071 Route route = createRoute(faceId, origin, cost, flags);
Vince Lehman4387e782014-06-19 16:57:45 -050072
Vince Lehman76c751c2014-11-18 17:36:38 -060073 RibUpdate update;
74 update.setAction(RibUpdate::REGISTER)
75 .setName(name)
76 .setRoute(route);
77
78 simulateSuccessfulResponse(update);
Vince Lehman4387e782014-06-19 16:57:45 -050079 }
80
81 void
Vince Lehman218be0a2015-01-15 17:25:20 -060082 eraseRoute(const Name& name, uint64_t faceId, uint64_t origin)
Vince Lehman4387e782014-06-19 16:57:45 -050083 {
Vince Lehman76c751c2014-11-18 17:36:38 -060084 Route route = createRoute(faceId, origin, 0, 0);
Vince Lehman4387e782014-06-19 16:57:45 -050085
Vince Lehman76c751c2014-11-18 17:36:38 -060086 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 Lehman4387e782014-06-19 16:57:45 -0500147 }
148
149
Vince Lehman76c751c2014-11-18 17:36:38 -0600150 FibUpdater::FibUpdateList
Vince Lehman4387e782014-06-19 16:57:45 -0500151 getSortedFibUpdates()
152 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600153 FibUpdater::FibUpdateList updates = getFibUpdates();
Vince Lehman4387e782014-06-19 16:57:45 -0500154 updates.sort(&compareNameFaceIdCostAction);
155 return updates;
156 }
157
Vince Lehman76c751c2014-11-18 17:36:38 -0600158 void
159 clearFibUpdates()
160 {
161 fibUpdater.m_updatesForBatchFaceId.clear();
162 fibUpdater.m_updatesForNonBatchFaceId.clear();
163 }
164
Vince Lehman4387e782014-06-19 16:57:45 -0500165public:
Vince Lehman76c751c2014-11-18 17:36:38 -0600166 shared_ptr<ndn::util::DummyClientFace> face;
167 ndn::KeyChain keyChain;
168 ndn::nfd::Controller controller;
169 rib::FibUpdater fibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -0500170 rib::Rib rib;
Vince Lehman76c751c2014-11-18 17:36:38 -0600171
172 FibUpdater::FibUpdateList fibUpdates;
Vince Lehman4387e782014-06-19 16:57:45 -0500173};
174
175} // namespace tests
176} // namespace rib
177} // namespace nfd