blob: 520b7d0326d2818bf8c1565e0b51a571aba75db4 [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento11904062022-04-14 22:33:28 -04003 * Copyright (c) 2014-2022, 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
44public:
45 CommandBaseHandle(Face& face, RepoStorage& storageHandle,
Davide Pesavento11904062022-04-14 22:33:28 -040046 Scheduler& scheduler, ndn::security::Validator& validator);
weijia yuan82cf9142018-10-21 12:25:02 -070047
48 virtual
49 ~CommandBaseHandle() = default;
50
51 ndn::mgmt::Authorization
52 makeAuthorization();
53
54 template<typename T>
55 bool
56 validateParameters(const ndn::mgmt::ControlParameters& parameters)
57 {
Davide Pesavento11904062022-04-14 22:33:28 -040058 const auto* castParams = dynamic_cast<const RepoCommandParameter*>(&parameters);
weijia yuan82cf9142018-10-21 12:25:02 -070059 BOOST_ASSERT(castParams != nullptr);
Davide Pesavento11904062022-04-14 22:33:28 -040060
weijia yuan82cf9142018-10-21 12:25:02 -070061 T command;
62 try {
63 command.validateRequest(*castParams);
64 }
Davide Pesavento11904062022-04-14 22:33:28 -040065 catch (const RepoCommand::ArgumentError&) {
weijia yuan82cf9142018-10-21 12:25:02 -070066 return false;
67 }
68 return true;
69 }
70
71protected:
72 Face& face;
73 RepoStorage& storageHandle;
74 Scheduler& scheduler;
75
76private:
Davide Pesavento11904062022-04-14 22:33:28 -040077 ndn::security::Validator& m_validator;
weijia yuan82cf9142018-10-21 12:25:02 -070078};
Davide Pesavento11904062022-04-14 22:33:28 -040079
weijia yuan82cf9142018-10-21 12:25:02 -070080} // namespace repo
81
Davide Pesavento11904062022-04-14 22:33:28 -040082#endif // REPO_HANDLES_COMMAND_BASE_HANDLE_HPP