blob: 77348be22ac765251c72c58b9d480f6ce051eb45 [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
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040031#include "tests/key-chain-fixture.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040032#include "tests/daemon/global-io-fixture.hpp"
Davide Pesavento1b077f62019-02-19 19:19:44 -050033#include "tests/daemon/rib/create-route.hpp"
Vince Lehman76c751c2014-11-18 17:36:38 -060034
35#include <ndn-cxx/util/dummy-client-face.hpp>
36
Vince Lehman4387e782014-06-19 16:57:45 -050037namespace nfd {
38namespace rib {
39namespace tests {
40
Vince Lehman4387e782014-06-19 16:57:45 -050041inline bool
Vince Lehman76c751c2014-11-18 17:36:38 -060042compareNameFaceIdCostAction(const FibUpdate& lhs, const FibUpdate& rhs)
Vince Lehman4387e782014-06-19 16:57:45 -050043{
Vince Lehman76c751c2014-11-18 17:36:38 -060044 if (lhs.name < rhs.name) {
45 return true;
46 }
47 else if (lhs.name == rhs.name) {
48 if (lhs.faceId < rhs.faceId) {
Vince Lehman4387e782014-06-19 16:57:45 -050049 return true;
50 }
Vince Lehman76c751c2014-11-18 17:36:38 -060051 else if (lhs.faceId == rhs.faceId) {
52 if (lhs.cost < rhs.cost) {
53 return true;
54 }
55 else if (lhs.cost == rhs.cost) {
56 return lhs.action < rhs.action;
57 }
Vince Lehman4387e782014-06-19 16:57:45 -050058 }
Vince Lehman76c751c2014-11-18 17:36:38 -060059 }
Vince Lehman4387e782014-06-19 16:57:45 -050060
61 return false;
62}
63
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040064class FibUpdatesFixture : public nfd::tests::GlobalIoFixture, public nfd::tests::KeyChainFixture
Vince Lehman4387e782014-06-19 16:57:45 -050065{
66public:
Vince Lehman76c751c2014-11-18 17:36:38 -060067 FibUpdatesFixture()
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040068 : face(g_io, m_keyChain)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000069 , controller(face, m_keyChain)
Vince Lehman76c751c2014-11-18 17:36:38 -060070 , fibUpdater(rib, controller)
71 {
72 }
73
Vince Lehman4387e782014-06-19 16:57:45 -050074 void
Davide Pesavento22db5392017-04-14 00:56:43 -040075 insertRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -050076 std::underlying_type_t<ndn::nfd::RouteOrigin> origin,
77 uint64_t cost,
78 std::underlying_type_t<ndn::nfd::RouteFlags> flags)
Vince Lehman4387e782014-06-19 16:57:45 -050079 {
Vince Lehman76c751c2014-11-18 17:36:38 -060080 Route route = createRoute(faceId, origin, cost, flags);
Vince Lehman4387e782014-06-19 16:57:45 -050081
Vince Lehman76c751c2014-11-18 17:36:38 -060082 RibUpdate update;
83 update.setAction(RibUpdate::REGISTER)
84 .setName(name)
85 .setRoute(route);
86
Junxiao Shi52009042018-09-10 12:33:56 +000087 simulateSuccessfulResponse();
88 rib.beginApplyUpdate(update, nullptr, nullptr);
Vince Lehman4387e782014-06-19 16:57:45 -050089 }
90
91 void
Davide Pesavento22db5392017-04-14 00:56:43 -040092 eraseRoute(const Name& name, uint64_t faceId,
Davide Pesavento1b077f62019-02-19 19:19:44 -050093 std::underlying_type_t<ndn::nfd::RouteOrigin> origin)
Vince Lehman4387e782014-06-19 16:57:45 -050094 {
Vince Lehman76c751c2014-11-18 17:36:38 -060095 Route route = createRoute(faceId, origin, 0, 0);
Vince Lehman4387e782014-06-19 16:57:45 -050096
Vince Lehman76c751c2014-11-18 17:36:38 -060097 RibUpdate update;
98 update.setAction(RibUpdate::UNREGISTER)
99 .setName(name)
100 .setRoute(route);
101
Junxiao Shi52009042018-09-10 12:33:56 +0000102 simulateSuccessfulResponse();
103 rib.beginApplyUpdate(update, nullptr, nullptr);
Vince Lehman76c751c2014-11-18 17:36:38 -0600104 }
105
106 void
107 destroyFace(uint64_t faceId)
108 {
Junxiao Shi52009042018-09-10 12:33:56 +0000109 simulateSuccessfulResponse();
Vince Lehman76c751c2014-11-18 17:36:38 -0600110 rib.beginRemoveFace(faceId);
111 }
112
Vince Lehman76c751c2014-11-18 17:36:38 -0600113 const FibUpdater::FibUpdateList&
114 getFibUpdates()
115 {
116 fibUpdates.clear();
117 fibUpdates = fibUpdater.m_updatesForBatchFaceId;
118 fibUpdates.insert(fibUpdates.end(), fibUpdater.m_updatesForNonBatchFaceId.begin(),
119 fibUpdater.m_updatesForNonBatchFaceId.end());
120
121 return fibUpdates;
Vince Lehman4387e782014-06-19 16:57:45 -0500122 }
123
Vince Lehman76c751c2014-11-18 17:36:38 -0600124 FibUpdater::FibUpdateList
Vince Lehman4387e782014-06-19 16:57:45 -0500125 getSortedFibUpdates()
126 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600127 FibUpdater::FibUpdateList updates = getFibUpdates();
Vince Lehman4387e782014-06-19 16:57:45 -0500128 updates.sort(&compareNameFaceIdCostAction);
129 return updates;
130 }
131
Vince Lehman76c751c2014-11-18 17:36:38 -0600132 void
133 clearFibUpdates()
134 {
135 fibUpdater.m_updatesForBatchFaceId.clear();
136 fibUpdater.m_updatesForNonBatchFaceId.clear();
137 }
138
Junxiao Shi52009042018-09-10 12:33:56 +0000139private:
140 void
141 simulateSuccessfulResponse()
142 {
143 rib.mockFibResponse = [] (const RibUpdateBatch&) { return true; };
144 rib.wantMockFibResponseOnce = true;
145 }
146
Vince Lehman4387e782014-06-19 16:57:45 -0500147public:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000148 ndn::util::DummyClientFace face;
Vince Lehman76c751c2014-11-18 17:36:38 -0600149 ndn::nfd::Controller controller;
Vince Lehman76c751c2014-11-18 17:36:38 -0600150
Davide Pesavento97210d52016-10-14 15:45:48 +0200151 Rib rib;
152 FibUpdater fibUpdater;
Vince Lehman76c751c2014-11-18 17:36:38 -0600153 FibUpdater::FibUpdateList fibUpdates;
Vince Lehman4387e782014-06-19 16:57:45 -0500154};
155
156} // namespace tests
157} // namespace rib
158} // namespace nfd
Davide Pesavento22db5392017-04-14 00:56:43 -0400159
Davide Pesavento1b077f62019-02-19 19:19:44 -0500160#endif // NFD_TESTS_DAEMON_RIB_FIB_UPDATES_COMMON_HPP