blob: 5d19b2afae7177821cd9593cc0c9e5c604219d55 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#ifndef NFD_DAEMON_MGMT_MANAGER_BASE_HPP
26#define NFD_DAEMON_MGMT_MANAGER_BASE_HPP
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070027
28#include "common.hpp"
29
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070030#include "mgmt/command-validator.hpp"
31#include "mgmt/internal-face.hpp"
32
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060033#include <ndn-cpp-dev/management/nfd-control-command.hpp>
Steve DiBenedetto7564d972014-03-24 14:28:46 -060034#include <ndn-cpp-dev/management/nfd-control-response.hpp>
35#include <ndn-cpp-dev/management/nfd-control-parameters.hpp>
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070036
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070037namespace nfd {
38
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060039using ndn::nfd::ControlCommand;
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080040using ndn::nfd::ControlResponse;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060041using ndn::nfd::ControlParameters;
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080042
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070043class InternalFace;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070044
45class ManagerBase
46{
47public:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070048
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080049 struct Error : public std::runtime_error
50 {
51 Error(const std::string& what) : std::runtime_error(what) {}
52 };
53
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070054 ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070055
56 virtual
57 ~ManagerBase();
58
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070059 void
60 onCommandValidationFailed(const shared_ptr<const Interest>& command,
61 const std::string& error);
62
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070063protected:
64
Steve DiBenedetto7564d972014-03-24 14:28:46 -060065 static bool
66 extractParameters(const Name::Component& parameterComponent,
67 ControlParameters& extractedParameters);
68
Steve DiBenedetto3970c892014-01-31 23:31:13 -070069 void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080070 setResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070071 uint32_t code,
72 const std::string& text);
Steve DiBenedetto2693db92014-02-10 15:58:36 -070073 void
74 setResponse(ControlResponse& response,
75 uint32_t code,
76 const std::string& text,
77 const Block& body);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070078
79 void
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070080 sendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080081 const ControlResponse& response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070082
83 void
84 sendResponse(const Name& name,
85 uint32_t code,
86 const std::string& text);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070087
Steve DiBenedetto7564d972014-03-24 14:28:46 -060088 void
89 sendResponse(const Name& name,
90 uint32_t code,
91 const std::string& text,
92 const Block& body);
93
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060094 virtual bool
95 validateParameters(const ControlCommand& command,
96 ControlParameters& parameters);
97
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070098PUBLIC_WITH_TESTS_ELSE_PROTECTED:
99 void
100 addInterestRule(const std::string& regex,
101 const ndn::IdentityCertificate& certificate);
102
103 void
104 addInterestRule(const std::string& regex,
105 const Name& keyName,
106 const ndn::PublicKey& publicKey);
107
108 void
109 validate(const Interest& interest,
110 const ndn::OnInterestValidated& onValidated,
111 const ndn::OnInterestValidationFailed& onValidationFailed);
112
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700113protected:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700114 shared_ptr<InternalFace> m_face;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700115};
116
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700117inline void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800118ManagerBase::setResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700119 uint32_t code,
120 const std::string& text)
121{
122 response.setCode(code);
123 response.setText(text);
124}
125
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700126inline void
127ManagerBase::setResponse(ControlResponse& response,
128 uint32_t code,
129 const std::string& text,
130 const Block& body)
131{
132 setResponse(response, code, text);
133 response.setBody(body);
134}
135
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700136inline void
137ManagerBase::addInterestRule(const std::string& regex,
138 const ndn::IdentityCertificate& certificate)
139{
140 m_face->getValidator().addInterestRule(regex, certificate);
141}
142
143inline void
144ManagerBase::addInterestRule(const std::string& regex,
145 const Name& keyName,
146 const ndn::PublicKey& publicKey)
147{
148 m_face->getValidator().addInterestRule(regex, keyName, publicKey);
149}
150
151inline void
152ManagerBase::validate(const Interest& interest,
153 const ndn::OnInterestValidated& onValidated,
154 const ndn::OnInterestValidationFailed& onValidationFailed)
155{
156 m_face->getValidator().validate(interest, onValidated, onValidationFailed);
157}
158
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700159
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700160} // namespace nfd
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700161
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700162#endif // NFD_DAEMON_MGMT_MANAGER_BASE_HPP