blob: 69d3c9e117b194e5bcc6c7a72781793cd218124a [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento11904062022-04-14 22:33:28 -04002/*
3 * 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#include "repo-command.hpp"
21
22namespace repo {
23
24void
Davide Pesavento11904062022-04-14 22:33:28 -040025RepoCommand::validateRequest(const RepoCommandParameter& parameters)
26{
weijia yuan82cf9142018-10-21 12:25:02 -070027 m_requestValidator.validate(parameters);
28 check(parameters);
29}
30
31void
32RepoCommand::FieldValidator::validate(const RepoCommandParameter& parameters) const
33{
34 const std::vector<bool>& presentFields = parameters.getPresentFields();
35
36 for (size_t i = 0; i < REPO_PARAMETER_UBOUND; i++) {
37 bool isPresent = presentFields[i];
38 if (m_required[i]) {
39 if (!isPresent) {
Davide Pesavento11904062022-04-14 22:33:28 -040040 NDN_THROW(ArgumentError(REPO_PARAMETER_FIELD[i] + " is required but missing"));
weijia yuan82cf9142018-10-21 12:25:02 -070041 }
42 }
43 else if (isPresent && !m_optional[i]) {
Davide Pesavento11904062022-04-14 22:33:28 -040044 NDN_THROW(ArgumentError(REPO_PARAMETER_FIELD[i] + " is forbidden but present"));
weijia yuan82cf9142018-10-21 12:25:02 -070045 }
46 }
47}
48
49RepoCommand::FieldValidator::FieldValidator()
50 : m_required(REPO_PARAMETER_UBOUND)
51 , m_optional(REPO_PARAMETER_UBOUND)
52{
53}
54
55InsertCommand::InsertCommand()
Davide Pesavento11904062022-04-14 22:33:28 -040056 : RepoCommand()
weijia yuan82cf9142018-10-21 12:25:02 -070057{
58 m_requestValidator
59 .required(REPO_PARAMETER_NAME)
60 .required(REPO_PARAMETER_START_BLOCK_ID)
61 .required(REPO_PARAMETER_END_BLOCK_ID);
62}
63
64InsertCheckCommand::InsertCheckCommand()
65{
66 m_requestValidator
67 .required(REPO_PARAMETER_NAME)
68 .required(REPO_PARAMETER_PROCESS_ID);
69}
70
weijia yuan82cf9142018-10-21 12:25:02 -070071DeleteCommand::DeleteCommand()
72{
73 m_requestValidator
74 .required(REPO_PARAMETER_NAME)
75 .required(REPO_PARAMETER_START_BLOCK_ID)
76 .required(REPO_PARAMETER_END_BLOCK_ID)
77 .required(REPO_PARAMETER_PROCESS_ID);
78}
weijia yuan82cf9142018-10-21 12:25:02 -070079
80void
81DeleteCommand::check(const RepoCommandParameter& parameters) const
82{
83 if (parameters.hasStartBlockId() || parameters.hasEndBlockId()) {
84 if (parameters.hasEndBlockId()) {
85 SegmentNo startBlockId = parameters.getStartBlockId();
86 SegmentNo endBlockId = parameters.getEndBlockId();
weijia yuan82cf9142018-10-21 12:25:02 -070087 if (startBlockId > endBlockId) {
Davide Pesavento11904062022-04-14 22:33:28 -040088 NDN_THROW(ArgumentError("Start block Id is bigger than end block Id"));
weijia yuan82cf9142018-10-21 12:25:02 -070089 }
90 }
91 else {
Davide Pesavento11904062022-04-14 22:33:28 -040092 NDN_THROW(ArgumentError("Segmented deletion without EndBlockId, not implemented"));
weijia yuan82cf9142018-10-21 12:25:02 -070093 }
94 }
95}
Davide Pesavento11904062022-04-14 22:33:28 -040096
97} // namespace repo