blob: 15c19ee6f00cc17337a7b452126782d079f11494 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman5144f822014-07-23 15:12:56 -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 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Vince Lehman5144f822014-07-23 15:12:56 -070024 */
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_MGMT_MANAGER_BASE_HPP
27#define NFD_DAEMON_MGMT_MANAGER_BASE_HPP
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070028
29#include "common.hpp"
30
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070031#include "mgmt/command-validator.hpp"
32#include "mgmt/internal-face.hpp"
33
Alexander Afanasyev4a771362014-04-24 21:29:33 -070034#include <ndn-cxx/management/nfd-control-command.hpp>
35#include <ndn-cxx/management/nfd-control-response.hpp>
36#include <ndn-cxx/management/nfd-control-parameters.hpp>
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070037
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070038namespace nfd {
39
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060040using ndn::nfd::ControlCommand;
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080041using ndn::nfd::ControlResponse;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060042using ndn::nfd::ControlParameters;
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080043
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070044class InternalFace;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070045
46class ManagerBase
47{
48public:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070049
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080050 struct Error : public std::runtime_error
51 {
52 Error(const std::string& what) : std::runtime_error(what) {}
53 };
54
Vince Lehman5144f822014-07-23 15:12:56 -070055 ManagerBase(shared_ptr<InternalFace> face,
56 const std::string& privilege,
57 ndn::KeyChain& keyChain);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070058
59 virtual
60 ~ManagerBase();
61
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070062 void
63 onCommandValidationFailed(const shared_ptr<const Interest>& command,
64 const std::string& error);
65
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070066protected:
67
Steve DiBenedetto7564d972014-03-24 14:28:46 -060068 static bool
69 extractParameters(const Name::Component& parameterComponent,
70 ControlParameters& extractedParameters);
71
Steve DiBenedetto3970c892014-01-31 23:31:13 -070072 void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080073 setResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070074 uint32_t code,
75 const std::string& text);
Steve DiBenedetto2693db92014-02-10 15:58:36 -070076 void
77 setResponse(ControlResponse& response,
78 uint32_t code,
79 const std::string& text,
80 const Block& body);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070081
82 void
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070083 sendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080084 const ControlResponse& response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070085
86 void
87 sendResponse(const Name& name,
88 uint32_t code,
89 const std::string& text);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070090
Steve DiBenedetto7564d972014-03-24 14:28:46 -060091 void
92 sendResponse(const Name& name,
93 uint32_t code,
94 const std::string& text,
95 const Block& body);
96
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060097 virtual bool
98 validateParameters(const ControlCommand& command,
99 ControlParameters& parameters);
100
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700101PUBLIC_WITH_TESTS_ELSE_PROTECTED:
102 void
103 addInterestRule(const std::string& regex,
104 const ndn::IdentityCertificate& certificate);
105
106 void
107 addInterestRule(const std::string& regex,
108 const Name& keyName,
109 const ndn::PublicKey& publicKey);
110
111 void
112 validate(const Interest& interest,
113 const ndn::OnInterestValidated& onValidated,
114 const ndn::OnInterestValidationFailed& onValidationFailed);
115
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700116protected:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700117 shared_ptr<InternalFace> m_face;
Vince Lehman5144f822014-07-23 15:12:56 -0700118 ndn::KeyChain& m_keyChain;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700119};
120
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700121inline void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800122ManagerBase::setResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700123 uint32_t code,
124 const std::string& text)
125{
126 response.setCode(code);
127 response.setText(text);
128}
129
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700130inline void
131ManagerBase::setResponse(ControlResponse& response,
132 uint32_t code,
133 const std::string& text,
134 const Block& body)
135{
136 setResponse(response, code, text);
137 response.setBody(body);
138}
139
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700140inline void
141ManagerBase::addInterestRule(const std::string& regex,
142 const ndn::IdentityCertificate& certificate)
143{
144 m_face->getValidator().addInterestRule(regex, certificate);
145}
146
147inline void
148ManagerBase::addInterestRule(const std::string& regex,
149 const Name& keyName,
150 const ndn::PublicKey& publicKey)
151{
152 m_face->getValidator().addInterestRule(regex, keyName, publicKey);
153}
154
155inline void
156ManagerBase::validate(const Interest& interest,
157 const ndn::OnInterestValidated& onValidated,
158 const ndn::OnInterestValidationFailed& onValidationFailed)
159{
160 m_face->getValidator().validate(interest, onValidated, onValidationFailed);
161}
162
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700163
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700164} // namespace nfd
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700165
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700166#endif // NFD_DAEMON_MGMT_MANAGER_BASE_HPP