blob: 584c6b6e269fd899714cbc3569a8ffc634375fbe [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento7df36bd2023-10-28 19:32:43 -04003 * Copyright (c) 2014-2023, Regents of the University of California.
weijia yuan82cf9142018-10-21 12:25:02 -07004 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef REPO_HANDLES_COMMAND_BASE_HANDLE_HPP
21#define REPO_HANDLES_COMMAND_BASE_HANDLE_HPP
22
23#include "common.hpp"
24
25#include "storage/repo-storage.hpp"
26#include "repo-command-response.hpp"
27#include "repo-command-parameter.hpp"
28#include "repo-command.hpp"
29
30#include <ndn-cxx/mgmt/dispatcher.hpp>
Davide Pesavento11904062022-04-14 22:33:28 -040031#include <ndn-cxx/security/validator.hpp>
weijia yuan82cf9142018-10-21 12:25:02 -070032
33namespace repo {
34
35class CommandBaseHandle
36{
37public:
38 class Error : public std::runtime_error
39 {
40 public:
Davide Pesavento11904062022-04-14 22:33:28 -040041 using std::runtime_error::runtime_error;
weijia yuan82cf9142018-10-21 12:25:02 -070042 };
43
weijia yuan82cf9142018-10-21 12:25:02 -070044 ndn::mgmt::Authorization
45 makeAuthorization();
46
47 template<typename T>
48 bool
49 validateParameters(const ndn::mgmt::ControlParameters& parameters)
50 {
Davide Pesavento11904062022-04-14 22:33:28 -040051 const auto* castParams = dynamic_cast<const RepoCommandParameter*>(&parameters);
weijia yuan82cf9142018-10-21 12:25:02 -070052 BOOST_ASSERT(castParams != nullptr);
Davide Pesavento11904062022-04-14 22:33:28 -040053
weijia yuan82cf9142018-10-21 12:25:02 -070054 T command;
55 try {
56 command.validateRequest(*castParams);
57 }
Davide Pesavento11904062022-04-14 22:33:28 -040058 catch (const RepoCommand::ArgumentError&) {
weijia yuan82cf9142018-10-21 12:25:02 -070059 return false;
60 }
61 return true;
62 }
63
64protected:
Davide Pesavento7df36bd2023-10-28 19:32:43 -040065 CommandBaseHandle(Face& face, RepoStorage& storageHandle,
66 Scheduler& scheduler, ndn::security::Validator& validator);
67
68 ~CommandBaseHandle() = default;
69
70protected:
weijia yuan82cf9142018-10-21 12:25:02 -070071 Face& face;
72 RepoStorage& storageHandle;
73 Scheduler& scheduler;
74
75private:
Davide Pesavento11904062022-04-14 22:33:28 -040076 ndn::security::Validator& m_validator;
weijia yuan82cf9142018-10-21 12:25:02 -070077};
Davide Pesavento11904062022-04-14 22:33:28 -040078
weijia yuan82cf9142018-10-21 12:25:02 -070079} // namespace repo
80
Davide Pesavento11904062022-04-14 22:33:28 -040081#endif // REPO_HANDLES_COMMAND_BASE_HANDLE_HPP