blob: feaa54a88686fb2927bd7d569bf7a6301efdd51f [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
Chengyu Fan320d2332014-10-29 16:40:33 -060097 //void
98 //sendNack(const Name& name);
99
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600100 virtual bool
101 validateParameters(const ControlCommand& command,
102 ControlParameters& parameters);
103
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700104PUBLIC_WITH_TESTS_ELSE_PROTECTED:
105 void
106 addInterestRule(const std::string& regex,
107 const ndn::IdentityCertificate& certificate);
108
109 void
110 addInterestRule(const std::string& regex,
111 const Name& keyName,
112 const ndn::PublicKey& publicKey);
113
114 void
115 validate(const Interest& interest,
116 const ndn::OnInterestValidated& onValidated,
117 const ndn::OnInterestValidationFailed& onValidationFailed);
118
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700119protected:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700120 shared_ptr<InternalFace> m_face;
Vince Lehman5144f822014-07-23 15:12:56 -0700121 ndn::KeyChain& m_keyChain;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700122};
123
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700124inline void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800125ManagerBase::setResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700126 uint32_t code,
127 const std::string& text)
128{
129 response.setCode(code);
130 response.setText(text);
131}
132
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700133inline void
134ManagerBase::setResponse(ControlResponse& response,
135 uint32_t code,
136 const std::string& text,
137 const Block& body)
138{
139 setResponse(response, code, text);
140 response.setBody(body);
141}
142
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700143inline void
144ManagerBase::addInterestRule(const std::string& regex,
145 const ndn::IdentityCertificate& certificate)
146{
147 m_face->getValidator().addInterestRule(regex, certificate);
148}
149
150inline void
151ManagerBase::addInterestRule(const std::string& regex,
152 const Name& keyName,
153 const ndn::PublicKey& publicKey)
154{
155 m_face->getValidator().addInterestRule(regex, keyName, publicKey);
156}
157
158inline void
159ManagerBase::validate(const Interest& interest,
160 const ndn::OnInterestValidated& onValidated,
161 const ndn::OnInterestValidationFailed& onValidationFailed)
162{
163 m_face->getValidator().validate(interest, onValidated, onValidationFailed);
164}
165
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700166
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700167} // namespace nfd
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700168
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700169#endif // NFD_DAEMON_MGMT_MANAGER_BASE_HPP