blob: b8200d5332d5b2e32a0c771562226905e7f775c7 [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2018, Regents of the University of California.
4 *
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
25RepoCommand::validateRequest(const RepoCommandParameter& parameters) {
26 m_requestValidator.validate(parameters);
27 check(parameters);
28}
29
30void
31RepoCommand::FieldValidator::validate(const RepoCommandParameter& parameters) const
32{
33 const std::vector<bool>& presentFields = parameters.getPresentFields();
34
35 for (size_t i = 0; i < REPO_PARAMETER_UBOUND; i++) {
36 bool isPresent = presentFields[i];
37 if (m_required[i]) {
38 if (!isPresent) {
39 BOOST_THROW_EXCEPTION(ArgumentError(REPO_PARAMETER_FIELD[i] + " is required but missing"));
40 }
41 }
42 else if (isPresent && !m_optional[i]) {
43 BOOST_THROW_EXCEPTION(ArgumentError(REPO_PARAMETER_FIELD[i] + " is forbidden but present"));
44 }
45 }
46}
47
48RepoCommand::FieldValidator::FieldValidator()
49 : m_required(REPO_PARAMETER_UBOUND)
50 , m_optional(REPO_PARAMETER_UBOUND)
51{
52}
53
54InsertCommand::InsertCommand()
55: RepoCommand()
56{
57 m_requestValidator
58 .required(REPO_PARAMETER_NAME)
59 .required(REPO_PARAMETER_START_BLOCK_ID)
60 .required(REPO_PARAMETER_END_BLOCK_ID);
61}
62
63InsertCheckCommand::InsertCheckCommand()
64{
65 m_requestValidator
66 .required(REPO_PARAMETER_NAME)
67 .required(REPO_PARAMETER_PROCESS_ID);
68}
69
70WatchStartCommand::WatchStartCommand()
71{
72 m_requestValidator
73 .required(REPO_PARAMETER_NAME)
74 .optional(REPO_PARAMETER_INTEREST_LIFETIME)
75 .optional(REPO_PARAMETER_MAX_INTEREST_NUM)
76 .optional(REPO_PARAMETER_WATCH_TIME_OUT);
77}
78
79WatchCheckCommand::WatchCheckCommand()
80{
81 m_requestValidator
82 .required(REPO_PARAMETER_NAME);
83}
84
85WatchStopCommand::WatchStopCommand()
86{
87 m_requestValidator
88 .required(REPO_PARAMETER_NAME);
89}
90
91DeleteCommand::DeleteCommand()
92{
93 m_requestValidator
94 .required(REPO_PARAMETER_NAME)
95 .required(REPO_PARAMETER_START_BLOCK_ID)
96 .required(REPO_PARAMETER_END_BLOCK_ID)
97 .required(REPO_PARAMETER_PROCESS_ID);
98}
99void
100InsertCommand::check(const RepoCommandParameter& parameters) const
101{
102 if (parameters.hasStartBlockId() || parameters.hasEndBlockId()) {
103 if (parameters.hasSelectors()) {
104 BOOST_THROW_EXCEPTION(ArgumentError("BlockId present. BlockId is not supported in this protocol"));
105 return;
106 }
107 }
108}
109
110void
111DeleteCommand::check(const RepoCommandParameter& parameters) const
112{
113 if (parameters.hasStartBlockId() || parameters.hasEndBlockId()) {
114 if (parameters.hasEndBlockId()) {
115 SegmentNo startBlockId = parameters.getStartBlockId();
116 SegmentNo endBlockId = parameters.getEndBlockId();
117
118 if (startBlockId > endBlockId) {
119 BOOST_THROW_EXCEPTION(ArgumentError("start block Id is bigger than end block Id"));
120 }
121 }
122 else {
123 BOOST_THROW_EXCEPTION(ArgumentError("Segmented deletion without EndBlockId, not implemented"));
124 }
125 }
126}
127} // namespace repo