blob: ed8224a20f8eac059473af086b693c48435b9428 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_MGMT_MANAGER_BASE_HPP
8#define NFD_MGMT_MANAGER_BASE_HPP
9
10#include "common.hpp"
11
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070012#include "mgmt/command-validator.hpp"
13#include "mgmt/internal-face.hpp"
14
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060015#include <ndn-cpp-dev/management/nfd-control-command.hpp>
Steve DiBenedetto7564d972014-03-24 14:28:46 -060016#include <ndn-cpp-dev/management/nfd-control-response.hpp>
17#include <ndn-cpp-dev/management/nfd-control-parameters.hpp>
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070018
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070019namespace nfd {
20
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060021using ndn::nfd::ControlCommand;
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080022using ndn::nfd::ControlResponse;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060023using ndn::nfd::ControlParameters;
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080024
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070025class InternalFace;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070026
27class ManagerBase
28{
29public:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070030
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080031 struct Error : public std::runtime_error
32 {
33 Error(const std::string& what) : std::runtime_error(what) {}
34 };
35
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070036 ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070037
38 virtual
39 ~ManagerBase();
40
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070041 void
42 onCommandValidationFailed(const shared_ptr<const Interest>& command,
43 const std::string& error);
44
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070045protected:
46
Steve DiBenedetto7564d972014-03-24 14:28:46 -060047 static bool
48 extractParameters(const Name::Component& parameterComponent,
49 ControlParameters& extractedParameters);
50
Steve DiBenedetto3970c892014-01-31 23:31:13 -070051 void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080052 setResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070053 uint32_t code,
54 const std::string& text);
Steve DiBenedetto2693db92014-02-10 15:58:36 -070055 void
56 setResponse(ControlResponse& response,
57 uint32_t code,
58 const std::string& text,
59 const Block& body);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070060
61 void
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070062 sendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080063 const ControlResponse& response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070064
65 void
66 sendResponse(const Name& name,
67 uint32_t code,
68 const std::string& text);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070069
Steve DiBenedetto7564d972014-03-24 14:28:46 -060070 void
71 sendResponse(const Name& name,
72 uint32_t code,
73 const std::string& text,
74 const Block& body);
75
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060076 virtual bool
77 validateParameters(const ControlCommand& command,
78 ControlParameters& parameters);
79
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070080PUBLIC_WITH_TESTS_ELSE_PROTECTED:
81 void
82 addInterestRule(const std::string& regex,
83 const ndn::IdentityCertificate& certificate);
84
85 void
86 addInterestRule(const std::string& regex,
87 const Name& keyName,
88 const ndn::PublicKey& publicKey);
89
90 void
91 validate(const Interest& interest,
92 const ndn::OnInterestValidated& onValidated,
93 const ndn::OnInterestValidationFailed& onValidationFailed);
94
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070095protected:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070096 shared_ptr<InternalFace> m_face;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070097};
98
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070099inline void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800100ManagerBase::setResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700101 uint32_t code,
102 const std::string& text)
103{
104 response.setCode(code);
105 response.setText(text);
106}
107
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700108inline void
109ManagerBase::setResponse(ControlResponse& response,
110 uint32_t code,
111 const std::string& text,
112 const Block& body)
113{
114 setResponse(response, code, text);
115 response.setBody(body);
116}
117
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700118inline void
119ManagerBase::addInterestRule(const std::string& regex,
120 const ndn::IdentityCertificate& certificate)
121{
122 m_face->getValidator().addInterestRule(regex, certificate);
123}
124
125inline void
126ManagerBase::addInterestRule(const std::string& regex,
127 const Name& keyName,
128 const ndn::PublicKey& publicKey)
129{
130 m_face->getValidator().addInterestRule(regex, keyName, publicKey);
131}
132
133inline void
134ManagerBase::validate(const Interest& interest,
135 const ndn::OnInterestValidated& onValidated,
136 const ndn::OnInterestValidationFailed& onValidationFailed)
137{
138 m_face->getValidator().validate(interest, onValidated, onValidationFailed);
139}
140
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700141
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700142} // namespace nfd
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700143
144#endif // NFD_MGMT_MANAGER_BASE_HPP
145