blob: 737c1c6f308b85fcd5205039c0949674662a29c3 [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventod396b612017-02-20 22:11:50 -05003 * Copyright (c) 2014-2017, Regents of the University of California,
Yanbiao Li698f4fe2015-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 "manager-base.hpp"
27
28namespace nfd {
29
30using ndn::mgmt::ValidateParameters;
31using ndn::mgmt::Authorization;
32
Davide Pesaventod396b612017-02-20 22:11:50 -050033ManagerBase::ManagerBase(Dispatcher& dispatcher, const std::string& module)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070034 : m_dispatcher(dispatcher)
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000035 , m_module(module)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070036{
Yanbiao Li698f4fe2015-08-19 16:30:16 -070037}
38
Davide Pesaventod396b612017-02-20 22:11:50 -050039ManagerBase::~ManagerBase() = default;
40
Yanbiao Li698f4fe2015-08-19 16:30:16 -070041void
42ManagerBase::registerStatusDatasetHandler(const std::string& verb,
43 const ndn::mgmt::StatusDatasetHandler& handler)
44{
45 m_dispatcher.addStatusDataset(makeRelPrefix(verb),
46 ndn::mgmt::makeAcceptAllAuthorization(),
47 handler);
48}
49
50ndn::mgmt::PostNotification
51ManagerBase::registerNotificationStream(const std::string& verb)
52{
53 return m_dispatcher.addNotificationStream(makeRelPrefix(verb));
54}
55
56void
Yanbiao Li698f4fe2015-08-19 16:30:16 -070057ManagerBase::extractRequester(const Interest& interest,
58 ndn::mgmt::AcceptContinuation accept)
59{
60 const Name& interestName = interest.getName();
61
62 try {
63 ndn::SignatureInfo sigInfo(interestName.at(ndn::signed_interest::POS_SIG_INFO).blockFromValue());
64 if (!sigInfo.hasKeyLocator() ||
65 sigInfo.getKeyLocator().getType() != ndn::KeyLocator::KeyLocator_Name) {
66 return accept("");
67 }
68
69 accept(sigInfo.getKeyLocator().getName().toUri());
70 }
71 catch (const tlv::Error&) {
72 accept("");
73 }
74}
75
76bool
77ManagerBase::validateParameters(const nfd::ControlCommand& command, const ndn::mgmt::ControlParameters& parameters)
78{
79 BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&parameters) != nullptr);
80
81 try {
82 command.validateRequest(static_cast<const ControlParameters&>(parameters));
83 }
84 catch (const ControlCommand::ArgumentError&) {
85 return false;
86 }
87 return true;
88}
89
90void
91ManagerBase::handleCommand(shared_ptr<nfd::ControlCommand> command,
92 const ControlCommandHandler& handler,
93 const Name& prefix, const Interest& interest,
94 const ndn::mgmt::ControlParameters& params,
95 ndn::mgmt::CommandContinuation done)
96{
97 BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&params) != nullptr);
98 ControlParameters parameters = static_cast<const ControlParameters&>(params);
99 command->applyDefaultsToRequest(parameters);
100 handler(*command, prefix, interest, parameters, done);
101}
102
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700103} // namespace nfd