blob: 3aeec6f6e3e7e97605753cf90a40ddc0fead1697 [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Lidf846e52016-01-30 21:53:47 -08003 * Copyright (c) 2014-2016, 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
33ManagerBase::ManagerBase(Dispatcher& dispatcher,
Yanbiao Li698f4fe2015-08-19 16:30:16 -070034 const std::string& module)
35 : m_dispatcher(dispatcher)
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000036 , m_module(module)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070037{
Yanbiao Li698f4fe2015-08-19 16:30:16 -070038}
39
40void
41ManagerBase::registerStatusDatasetHandler(const std::string& verb,
42 const ndn::mgmt::StatusDatasetHandler& handler)
43{
44 m_dispatcher.addStatusDataset(makeRelPrefix(verb),
45 ndn::mgmt::makeAcceptAllAuthorization(),
46 handler);
47}
48
49ndn::mgmt::PostNotification
50ManagerBase::registerNotificationStream(const std::string& verb)
51{
52 return m_dispatcher.addNotificationStream(makeRelPrefix(verb));
53}
54
55void
Yanbiao Li698f4fe2015-08-19 16:30:16 -070056ManagerBase::extractRequester(const Interest& interest,
57 ndn::mgmt::AcceptContinuation accept)
58{
59 const Name& interestName = interest.getName();
60
61 try {
62 ndn::SignatureInfo sigInfo(interestName.at(ndn::signed_interest::POS_SIG_INFO).blockFromValue());
63 if (!sigInfo.hasKeyLocator() ||
64 sigInfo.getKeyLocator().getType() != ndn::KeyLocator::KeyLocator_Name) {
65 return accept("");
66 }
67
68 accept(sigInfo.getKeyLocator().getName().toUri());
69 }
70 catch (const tlv::Error&) {
71 accept("");
72 }
73}
74
75bool
76ManagerBase::validateParameters(const nfd::ControlCommand& command, const ndn::mgmt::ControlParameters& parameters)
77{
78 BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&parameters) != nullptr);
79
80 try {
81 command.validateRequest(static_cast<const ControlParameters&>(parameters));
82 }
83 catch (const ControlCommand::ArgumentError&) {
84 return false;
85 }
86 return true;
87}
88
89void
90ManagerBase::handleCommand(shared_ptr<nfd::ControlCommand> command,
91 const ControlCommandHandler& handler,
92 const Name& prefix, const Interest& interest,
93 const ndn::mgmt::ControlParameters& params,
94 ndn::mgmt::CommandContinuation done)
95{
96 BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&params) != nullptr);
97 ControlParameters parameters = static_cast<const ControlParameters&>(params);
98 command->applyDefaultsToRequest(parameters);
99 handler(*command, prefix, interest, parameters, done);
100}
101
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700102} // namespace nfd