blob: 1b4628ef1e81d599f2b4b20f5e341f94b81e5735 [file] [log] [blame]
Yanbiao Li6704a4a2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi332783b2018-02-04 23:52:46 +00002/*
Davide Pesavento45c1f6a2025-01-01 19:30:30 -05003 * Copyright (c) 2014-2025, Regents of the University of California,
Yanbiao Li6704a4a2015-08-19 16:30:16 -07004 * 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.
10 *
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
26#include "strategy-choice-manager.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040027
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/logger.hpp"
Yanbiao Li6704a4a2015-08-19 16:30:16 -070029#include "table/strategy-choice.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040030
Junxiao Shi25c6ce42016-09-09 13:49:59 +000031#include <ndn-cxx/mgmt/nfd/strategy-choice.hpp>
Yanbiao Li6704a4a2015-08-19 16:30:16 -070032
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040033#include <boost/lexical_cast.hpp>
34
Yanbiao Li6704a4a2015-08-19 16:30:16 -070035namespace nfd {
36
Davide Pesaventoa3148082018-04-12 18:21:54 -040037NFD_LOG_INIT(StrategyChoiceManager);
Yanbiao Li6704a4a2015-08-19 16:30:16 -070038
39StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice,
40 Dispatcher& dispatcher,
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000041 CommandAuthenticator& authenticator)
Davide Pesavento78ddcab2019-02-28 22:00:03 -050042 : ManagerBase("strategy-choice", dispatcher, authenticator)
Junxiao Shiff10da62016-07-13 17:57:43 +000043 , m_table(strategyChoice)
Yanbiao Li6704a4a2015-08-19 16:30:16 -070044{
Davide Pesavento1db1bb62025-01-06 01:23:41 -050045 registerCommandHandler<ndn::nfd::StrategyChoiceSetCommand>([this] (auto&&, auto&&, auto&&... args) {
46 setStrategy(std::forward<decltype(args)>(args)...);
47 });
48 registerCommandHandler<ndn::nfd::StrategyChoiceUnsetCommand>([this] (auto&&, auto&&, auto&&... args) {
49 unsetStrategy(std::forward<decltype(args)>(args)...);
50 });
51 registerStatusDatasetHandler("list", [this] (auto&&, auto&&, auto&&... args) {
52 listChoices(std::forward<decltype(args)>(args)...);
53 });
Yanbiao Li6704a4a2015-08-19 16:30:16 -070054}
55
56void
Junxiao Shi65800d22016-12-25 21:05:45 +000057StrategyChoiceManager::setStrategy(ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050058 const CommandContinuation& done)
Yanbiao Li6704a4a2015-08-19 16:30:16 -070059{
60 const Name& prefix = parameters.getName();
Junxiao Shi65800d22016-12-25 21:05:45 +000061 const Name& strategy = parameters.getStrategy();
Yanbiao Li6704a4a2015-08-19 16:30:16 -070062
Junxiao Shi530cf002017-01-03 14:43:16 +000063 StrategyChoice::InsertResult res = m_table.insert(prefix, strategy);
64 if (!res) {
65 NFD_LOG_DEBUG("strategy-choice/set(" << prefix << "," << strategy << "): cannot-create " << res);
Junxiao Shi332783b2018-02-04 23:52:46 +000066 return done(ControlResponse(res.getStatusCode(), boost::lexical_cast<std::string>(res)));
Yanbiao Li6704a4a2015-08-19 16:30:16 -070067 }
68
Junxiao Shi65800d22016-12-25 21:05:45 +000069 NFD_LOG_DEBUG("strategy-choice/set(" << prefix << "," << strategy << "): OK");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040070 auto [hasEntry, instanceName] = m_table.get(prefix);
Junxiao Shi65800d22016-12-25 21:05:45 +000071 BOOST_ASSERT_MSG(hasEntry, "StrategyChoice entry must exist after StrategyChoice::insert");
72 parameters.setStrategy(instanceName);
73 return done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
Yanbiao Li6704a4a2015-08-19 16:30:16 -070074}
75
76void
Junxiao Shi65800d22016-12-25 21:05:45 +000077StrategyChoiceManager::unsetStrategy(ControlParameters parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050078 const CommandContinuation& done)
Yanbiao Li6704a4a2015-08-19 16:30:16 -070079{
Junxiao Shi65800d22016-12-25 21:05:45 +000080 const Name& prefix = parameters.getName();
81 // no need to test for ndn:/ , parameter validation takes care of that
82
Junxiao Shiff10da62016-07-13 17:57:43 +000083 m_table.erase(parameters.getName());
Yanbiao Li6704a4a2015-08-19 16:30:16 -070084
Junxiao Shi65800d22016-12-25 21:05:45 +000085 NFD_LOG_DEBUG("strategy-choice/unset(" << prefix << "): OK");
Yanbiao Li6704a4a2015-08-19 16:30:16 -070086 done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
87}
88
89void
Junxiao Shi65800d22016-12-25 21:05:45 +000090StrategyChoiceManager::listChoices(ndn::mgmt::StatusDatasetContext& context)
Yanbiao Li6704a4a2015-08-19 16:30:16 -070091{
Junxiao Shi4cb74312016-12-25 20:48:47 +000092 for (const auto& i : m_table) {
Yanbiao Li6704a4a2015-08-19 16:30:16 -070093 ndn::nfd::StrategyChoice entry;
Junxiao Shi4cb74312016-12-25 20:48:47 +000094 entry.setName(i.getPrefix())
95 .setStrategy(i.getStrategyInstanceName());
Yanbiao Li6704a4a2015-08-19 16:30:16 -070096 context.append(entry.wireEncode());
97 }
98 context.end();
99}
100
Yanbiao Lidf846e52016-01-30 21:53:47 -0800101} // namespace nfd