blob: 4668af54dce3dfcbea6ee41b966f55b26423dfe0 [file] [log] [blame]
Vince Lehman4387e782014-06-19 16:57:45 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi52009042018-09-10 12:33:56 +00002/*
Davide Pesavento1b077f62019-02-19 19:19:44 -05003 * Copyright (c) 2014-2019, 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 Pesavento1b077f62019-02-19 19:19:44 -050026#ifndef NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP
27#define NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP
Davide Pesavento22db5392017-04-14 00:56:43 -040028
Vince Lehman76c751c2014-11-18 17:36:38 -060029#include "rib/fib-updater.hpp"
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040030
Junxiao Shi221b6fe2016-07-14 18:21:56 +000031#include "tests/identity-management-fixture.hpp"
Davide Pesavento1b077f62019-02-19 19:19:44 -050032#include "tests/daemon/rib/create-route.hpp"
Vince Lehman76c751c2014-11-18 17:36:38 -060033
34#include <ndn-cxx/util/dummy-client-face.hpp>
35
Vince Lehman4387e782014-06-19 16:57:45 -050036namespace nfd {
37namespace rib {
38namespace tests {
39
Vince Lehman4387e782014-06-19 16:57:45 -050040inline bool
Vince Lehman76c751c2014-11-18 17:36:38 -060041compareNameFaceIdCostAction(const FibUpdate& lhs, const FibUpdate& rhs)
Vince Lehman4387e782014-06-19 16:57:45 -050042{
Vince Lehman76c751c2014-11-18 17:36:38 -060043 if (lhs.name < rhs.name) {
44 return true;
45 }
46 else if (lhs.name == rhs.name) {
47 if (lhs.faceId < rhs.faceId) {
Vince Lehman4387e782014-06-19 16:57:45 -050048 return true;
49 }
Vince Lehman76c751c2014-11-18 17:36:38 -060050 else if (lhs.faceId == rhs.faceId) {
51 if (lhs.cost < rhs.cost) {
52 return true;
53 }
54 else if (lhs.cost == rhs.cost) {
55 return lhs.action < rhs.action;
56 }
Vince Lehman4387e782014-06-19 16:57:45 -050057 }
Vince Lehman76c751c2014-11-18 17:36:38 -060058 }
Vince Lehman4387e782014-06-19 16:57:45 -050059
60 return false;
61}
62
Davide Pesavento97210d52016-10-14 15:45:48 +020063class FibUpdatesFixture : public nfd::tests::IdentityManagementFixture
Vince Lehman4387e782014-06-19 16:57:45 -050064{
65public:
Vince Lehman76c751c2014-11-18 17:36:38 -060066 FibUpdatesFixture()
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040067 : face(g_io, m_keyChain)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000068 , controller(face, m_keyChain)
Vince Lehman76c751c2014-11-18 17:36:38 -060069 , fibUpdater(rib, controller)
70 {
71 }
72
Vince Lehman4387e782014-06-19 16:57:45 -050073 void
Davide Pesavento22db5392017-04-14 00:56:43 -040074 insertRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -050075 std::underlying_type_t<ndn::nfd::RouteOrigin> origin,
76 uint64_t cost,
77 std::underlying_type_t<ndn::nfd::RouteFlags> flags)
Vince Lehman4387e782014-06-19 16:57:45 -050078 {
Vince Lehman76c751c2014-11-18 17:36:38 -060079 Route route = createRoute(faceId, origin, cost, flags);
Vince Lehman4387e782014-06-19 16:57:45 -050080
Vince Lehman76c751c2014-11-18 17:36:38 -060081 RibUpdate update;
82 update.setAction(RibUpdate::REGISTER)
83 .setName(name)
84 .setRoute(route);
85
Junxiao Shi52009042018-09-10 12:33:56 +000086 simulateSuccessfulResponse();
87 rib.beginApplyUpdate(update, nullptr, nullptr);
Vince Lehman4387e782014-06-19 16:57:45 -050088 }
89
90 void
Davide Pesavento22db5392017-04-14 00:56:43 -040091 eraseRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -050092 std::underlying_type_t<ndn::nfd::RouteOrigin> origin)
Vince Lehman4387e782014-06-19 16:57:45 -050093 {
Vince Lehman76c751c2014-11-18 17:36:38 -060094 Route route = createRoute(faceId, origin, 0, 0);
Vince Lehman4387e782014-06-19 16:57:45 -050095
Vince Lehman76c751c2014-11-18 17:36:38 -060096 RibUpdate update;
97 update.setAction(RibUpdate::UNREGISTER)
98 .setName(name)
99 .setRoute(route);
100
Junxiao Shi52009042018-09-10 12:33:56 +0000101 simulateSuccessfulResponse();
102 rib.beginApplyUpdate(update, nullptr, nullptr);
Vince Lehman76c751c2014-11-18 17:36:38 -0600103 }
104
105 void
106 destroyFace(uint64_t faceId)
107 {
Junxiao Shi52009042018-09-10 12:33:56 +0000108 simulateSuccessfulResponse();
Vince Lehman76c751c2014-11-18 17:36:38 -0600109 rib.beginRemoveFace(faceId);
110 }
111
Vince Lehman76c751c2014-11-18 17:36:38 -0600112 const FibUpdater::FibUpdateList&
113 getFibUpdates()
114 {
115 fibUpdates.clear();
116 fibUpdates = fibUpdater.m_updatesForBatchFaceId;
117 fibUpdates.insert(fibUpdates.end(), fibUpdater.m_updatesForNonBatchFaceId.begin(),
118 fibUpdater.m_updatesForNonBatchFaceId.end());
119
120 return fibUpdates;
Vince Lehman4387e782014-06-19 16:57:45 -0500121 }
122
Vince Lehman76c751c2014-11-18 17:36:38 -0600123 FibUpdater::FibUpdateList
Vince Lehman4387e782014-06-19 16:57:45 -0500124 getSortedFibUpdates()
125 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600126 FibUpdater::FibUpdateList updates = getFibUpdates();
Vince Lehman4387e782014-06-19 16:57:45 -0500127 updates.sort(&compareNameFaceIdCostAction);
128 return updates;
129 }
130
Vince Lehman76c751c2014-11-18 17:36:38 -0600131 void
132 clearFibUpdates()
133 {
134 fibUpdater.m_updatesForBatchFaceId.clear();
135 fibUpdater.m_updatesForNonBatchFaceId.clear();
136 }
137
Junxiao Shi52009042018-09-10 12:33:56 +0000138private:
139 void
140 simulateSuccessfulResponse()
141 {
142 rib.mockFibResponse = [] (const RibUpdateBatch&) { return true; };
143 rib.wantMockFibResponseOnce = true;
144 }
145
Vince Lehman4387e782014-06-19 16:57:45 -0500146public:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000147 ndn::util::DummyClientFace face;
Vince Lehman76c751c2014-11-18 17:36:38 -0600148 ndn::nfd::Controller controller;
Vince Lehman76c751c2014-11-18 17:36:38 -0600149
Davide Pesavento97210d52016-10-14 15:45:48 +0200150 Rib rib;
151 FibUpdater fibUpdater;
Vince Lehman76c751c2014-11-18 17:36:38 -0600152 FibUpdater::FibUpdateList fibUpdates;
Vince Lehman4387e782014-06-19 16:57:45 -0500153};
154
155} // namespace tests
156} // namespace rib
157} // namespace nfd
Davide Pesavento22db5392017-04-14 00:56:43 -0400158
Davide Pesavento1b077f62019-02-19 19:19:44 -0500159#endif // NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP