blob: dc0f5f10e5837628e2cf7370787623aa3cf437bf [file] [log] [blame]
Shuo Chen09f09bb2014-03-18 15:37:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc0e26582017-08-13 21:16:49 -04002/*
Davide Pesavento0c139512018-11-03 18:23:38 -04003 * Copyright (c) 2014-2018, Regents of the University of California.
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -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/>.
Shuo Chen09f09bb2014-03-18 15:37:11 -070018 */
19
20#include "delete-handle.hpp"
21
22namespace repo {
23
weijia yuan82cf9142018-10-21 12:25:02 -070024DeleteHandle::DeleteHandle(Face& face, RepoStorage& storageHandle,
25 ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler,
Junxiao Shi047a6fb2017-06-08 16:16:05 +000026 Validator& validator)
weijia yuan82cf9142018-10-21 12:25:02 -070027 : CommandBaseHandle(face, storageHandle, scheduler, validator)
Shuo Chen09f09bb2014-03-18 15:37:11 -070028{
weijia yuan82cf9142018-10-21 12:25:02 -070029 dispatcher.addControlCommand<RepoCommandParameter>(ndn::PartialName("delete"),
30 makeAuthorization(),
31 std::bind(&DeleteHandle::validateParameters<DeleteCommand>, this, _1),
32 std::bind(&DeleteHandle::handleDeleteCommand, this, _1, _2, _3, _4));
33
Shuo Chen09f09bb2014-03-18 15:37:11 -070034}
35
36void
weijia yuan82cf9142018-10-21 12:25:02 -070037DeleteHandle::handleDeleteCommand(const Name& prefix, const Interest& interest,
38 const ndn::mgmt::ControlParameters& parameter,
39 const ndn::mgmt::CommandContinuation& done)
Shuo Chen09f09bb2014-03-18 15:37:11 -070040{
weijia yuan82cf9142018-10-21 12:25:02 -070041 const RepoCommandParameter& repoParameter = dynamic_cast<const RepoCommandParameter&>(parameter);
Shuo Chen09f09bb2014-03-18 15:37:11 -070042
weijia yuan82cf9142018-10-21 12:25:02 -070043 if (repoParameter.hasSelectors()) {
Shuo Chen09f09bb2014-03-18 15:37:11 -070044 //choose data with selector and delete it
weijia yuan82cf9142018-10-21 12:25:02 -070045 processSelectorDeleteCommand(interest, repoParameter, done);
Shuo Chen09f09bb2014-03-18 15:37:11 -070046 return;
47 }
48
weijia yuan82cf9142018-10-21 12:25:02 -070049 if (!repoParameter.hasStartBlockId() && !repoParameter.hasEndBlockId()) {
50 processSingleDeleteCommand(interest, repoParameter, done);
Shuo Chen09f09bb2014-03-18 15:37:11 -070051 return;
52 }
53
weijia yuan82cf9142018-10-21 12:25:02 -070054 processSegmentDeleteCommand(interest, repoParameter, done);
Shuo Chen09f09bb2014-03-18 15:37:11 -070055}
56
weijia yuan82cf9142018-10-21 12:25:02 -070057RepoCommandResponse
Shuo Chen09f09bb2014-03-18 15:37:11 -070058DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter,
weijia yuan82cf9142018-10-21 12:25:02 -070059 uint64_t statusCode, uint64_t nDeletedData) const
Shuo Chen09f09bb2014-03-18 15:37:11 -070060{
weijia yuan82cf9142018-10-21 12:25:02 -070061 RepoCommandResponse response(statusCode, "Deletion Successful");
62
Shuo Chen09f09bb2014-03-18 15:37:11 -070063 if (parameter.hasProcessId()) {
64 response.setProcessId(parameter.getProcessId());
weijia yuan82cf9142018-10-21 12:25:02 -070065 response.setDeleteNum(nDeletedData);
66 response.setBody(response.wireEncode());
Shuo Chen09f09bb2014-03-18 15:37:11 -070067 }
68 else {
weijia yuan82cf9142018-10-21 12:25:02 -070069 response.setCode(403);
70 response.setText("Malformed Command");
71 response.setBody(response.wireEncode());
Shuo Chen09f09bb2014-03-18 15:37:11 -070072 }
weijia yuan82cf9142018-10-21 12:25:02 -070073 return response;
74}
75
76RepoCommandResponse
77DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode, std::string text) const
78{
79 RepoCommandResponse response(statusCode, text);
80 response.setBody(response.wireEncode());
81 return response;
Shuo Chen09f09bb2014-03-18 15:37:11 -070082}
83
84void
weijia yuan82cf9142018-10-21 12:25:02 -070085DeleteHandle::processSingleDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
86 const ndn::mgmt::CommandContinuation& done) const
Shuo Chen09f09bb2014-03-18 15:37:11 -070087{
weijia yuan82cf9142018-10-21 12:25:02 -070088 int64_t nDeletedData = storageHandle.deleteData(parameter.getName());
89 if (nDeletedData == -1) {
Weiqi Shif0330d52014-07-09 10:54:27 -070090 std::cerr << "Deletion Failed!" <<std::endl;
weijia yuan82cf9142018-10-21 12:25:02 -070091 done(negativeReply(interest, 405, "Deletion Failed"));
Shuo Chen09f09bb2014-03-18 15:37:11 -070092 }
Weiqi Shif0330d52014-07-09 10:54:27 -070093 else
weijia yuan82cf9142018-10-21 12:25:02 -070094 done(positiveReply(interest, parameter, 200, nDeletedData));
Shuo Chen09f09bb2014-03-18 15:37:11 -070095}
96
97void
weijia yuan82cf9142018-10-21 12:25:02 -070098DeleteHandle::processSelectorDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
99 const ndn::mgmt::CommandContinuation& done) const
Shuo Chen09f09bb2014-03-18 15:37:11 -0700100{
weijia yuan82cf9142018-10-21 12:25:02 -0700101 int64_t nDeletedData = storageHandle.deleteData(Interest(parameter.getName())
102 .setSelectors(parameter.getSelectors()));
103 if (nDeletedData == -1) {
Weiqi Shif0330d52014-07-09 10:54:27 -0700104 std::cerr << "Deletion Failed!" <<std::endl;
weijia yuan82cf9142018-10-21 12:25:02 -0700105 done(negativeReply(interest, 405, "Deletion Failed"));
Shuo Chen09f09bb2014-03-18 15:37:11 -0700106 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700107 else
weijia yuan82cf9142018-10-21 12:25:02 -0700108 done(positiveReply(interest, parameter, 200, nDeletedData));
Shuo Chen09f09bb2014-03-18 15:37:11 -0700109}
110
111void
weijia yuan82cf9142018-10-21 12:25:02 -0700112DeleteHandle::processSegmentDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
113 const ndn::mgmt::CommandContinuation& done) const
Shuo Chen09f09bb2014-03-18 15:37:11 -0700114{
weijia yuan82cf9142018-10-21 12:25:02 -0700115 SegmentNo startBlockId = parameter.hasStartBlockId() ? parameter.getStartBlockId() : 0;
116 SegmentNo endBlockId = parameter.getEndBlockId();
Shuo Chen09f09bb2014-03-18 15:37:11 -0700117
weijia yuan82cf9142018-10-21 12:25:02 -0700118 Name prefix = parameter.getName();
119 uint64_t nDeletedData = 0;
120 for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
121 Name name = prefix;
122 name.appendSegment(i);
123 if (storageHandle.deleteData(name)) {
124 nDeletedData++;
Shuo Chen09f09bb2014-03-18 15:37:11 -0700125 }
weijia yuan82cf9142018-10-21 12:25:02 -0700126 }
127 //All the data deleted, return 200
128 done(positiveReply(interest, parameter, 200, nDeletedData));
Shuo Chen09f09bb2014-03-18 15:37:11 -0700129
Shuo Chen09f09bb2014-03-18 15:37:11 -0700130}
131
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800132} // namespace repo