blob: 46e34b64a891ee0933780a37bc47b209ff9fe3c2 [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 DiBenedetto7564d972014-03-24 14:28:46 -060015#include <ndn-cpp-dev/management/nfd-control-response.hpp>
16#include <ndn-cpp-dev/management/nfd-control-parameters.hpp>
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070017
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070018namespace nfd {
19
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080020using ndn::nfd::ControlResponse;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060021using ndn::nfd::ControlParameters;
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080022
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070023class InternalFace;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070024
25class ManagerBase
26{
27public:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070028
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080029 struct Error : public std::runtime_error
30 {
31 Error(const std::string& what) : std::runtime_error(what) {}
32 };
33
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070034 ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070035
36 virtual
37 ~ManagerBase();
38
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070039 void
40 onCommandValidationFailed(const shared_ptr<const Interest>& command,
41 const std::string& error);
42
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070043protected:
44
Steve DiBenedetto7564d972014-03-24 14:28:46 -060045 static bool
46 extractParameters(const Name::Component& parameterComponent,
47 ControlParameters& extractedParameters);
48
Steve DiBenedetto3970c892014-01-31 23:31:13 -070049 void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080050 setResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070051 uint32_t code,
52 const std::string& text);
Steve DiBenedetto2693db92014-02-10 15:58:36 -070053 void
54 setResponse(ControlResponse& response,
55 uint32_t code,
56 const std::string& text,
57 const Block& body);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070058
59 void
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070060 sendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080061 const ControlResponse& response);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070062
63 void
64 sendResponse(const Name& name,
65 uint32_t code,
66 const std::string& text);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070067
Steve DiBenedetto7564d972014-03-24 14:28:46 -060068 void
69 sendResponse(const Name& name,
70 uint32_t code,
71 const std::string& text,
72 const Block& body);
73
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070074PUBLIC_WITH_TESTS_ELSE_PROTECTED:
75 void
76 addInterestRule(const std::string& regex,
77 const ndn::IdentityCertificate& certificate);
78
79 void
80 addInterestRule(const std::string& regex,
81 const Name& keyName,
82 const ndn::PublicKey& publicKey);
83
84 void
85 validate(const Interest& interest,
86 const ndn::OnInterestValidated& onValidated,
87 const ndn::OnInterestValidationFailed& onValidationFailed);
88
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070089protected:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070090 shared_ptr<InternalFace> m_face;
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070091};
92
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070093inline void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080094ManagerBase::setResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070095 uint32_t code,
96 const std::string& text)
97{
98 response.setCode(code);
99 response.setText(text);
100}
101
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700102inline void
103ManagerBase::setResponse(ControlResponse& response,
104 uint32_t code,
105 const std::string& text,
106 const Block& body)
107{
108 setResponse(response, code, text);
109 response.setBody(body);
110}
111
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700112inline void
113ManagerBase::addInterestRule(const std::string& regex,
114 const ndn::IdentityCertificate& certificate)
115{
116 m_face->getValidator().addInterestRule(regex, certificate);
117}
118
119inline void
120ManagerBase::addInterestRule(const std::string& regex,
121 const Name& keyName,
122 const ndn::PublicKey& publicKey)
123{
124 m_face->getValidator().addInterestRule(regex, keyName, publicKey);
125}
126
127inline void
128ManagerBase::validate(const Interest& interest,
129 const ndn::OnInterestValidated& onValidated,
130 const ndn::OnInterestValidationFailed& onValidationFailed)
131{
132 m_face->getValidator().validate(interest, onValidated, onValidationFailed);
133}
134
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700135
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700136} // namespace nfd
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700137
138#endif // NFD_MGMT_MANAGER_BASE_HPP
139