blob: c892583d5aebb5d66121e8f6aa87557d179eff5a [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
weijia yuan3aa8d2b2018-03-06 15:35:57 -080022#include <ndn-cxx/util/logger.hpp>
23
Shuo Chen09f09bb2014-03-18 15:37:11 -070024namespace repo {
25
weijia yuan3aa8d2b2018-03-06 15:35:57 -080026NDN_LOG_INIT(repo.DeleteHandle);
27
weijia yuan82cf9142018-10-21 12:25:02 -070028DeleteHandle::DeleteHandle(Face& face, RepoStorage& storageHandle,
29 ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler,
Junxiao Shi047a6fb2017-06-08 16:16:05 +000030 Validator& validator)
weijia yuan82cf9142018-10-21 12:25:02 -070031 : CommandBaseHandle(face, storageHandle, scheduler, validator)
Shuo Chen09f09bb2014-03-18 15:37:11 -070032{
weijia yuan82cf9142018-10-21 12:25:02 -070033 dispatcher.addControlCommand<RepoCommandParameter>(ndn::PartialName("delete"),
34 makeAuthorization(),
35 std::bind(&DeleteHandle::validateParameters<DeleteCommand>, this, _1),
36 std::bind(&DeleteHandle::handleDeleteCommand, this, _1, _2, _3, _4));
37
Shuo Chen09f09bb2014-03-18 15:37:11 -070038}
39
40void
weijia yuan82cf9142018-10-21 12:25:02 -070041DeleteHandle::handleDeleteCommand(const Name& prefix, const Interest& interest,
42 const ndn::mgmt::ControlParameters& parameter,
43 const ndn::mgmt::CommandContinuation& done)
Shuo Chen09f09bb2014-03-18 15:37:11 -070044{
weijia yuan82cf9142018-10-21 12:25:02 -070045 const RepoCommandParameter& repoParameter = dynamic_cast<const RepoCommandParameter&>(parameter);
Shuo Chen09f09bb2014-03-18 15:37:11 -070046
weijia yuan82cf9142018-10-21 12:25:02 -070047 if (!repoParameter.hasStartBlockId() && !repoParameter.hasEndBlockId()) {
48 processSingleDeleteCommand(interest, repoParameter, done);
Shuo Chen09f09bb2014-03-18 15:37:11 -070049 return;
50 }
51
weijia yuan82cf9142018-10-21 12:25:02 -070052 processSegmentDeleteCommand(interest, repoParameter, done);
Shuo Chen09f09bb2014-03-18 15:37:11 -070053}
54
weijia yuan82cf9142018-10-21 12:25:02 -070055RepoCommandResponse
Shuo Chen09f09bb2014-03-18 15:37:11 -070056DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter,
weijia yuan82cf9142018-10-21 12:25:02 -070057 uint64_t statusCode, uint64_t nDeletedData) const
Shuo Chen09f09bb2014-03-18 15:37:11 -070058{
weijia yuan82cf9142018-10-21 12:25:02 -070059 RepoCommandResponse response(statusCode, "Deletion Successful");
60
Shuo Chen09f09bb2014-03-18 15:37:11 -070061 if (parameter.hasProcessId()) {
62 response.setProcessId(parameter.getProcessId());
weijia yuan82cf9142018-10-21 12:25:02 -070063 response.setDeleteNum(nDeletedData);
64 response.setBody(response.wireEncode());
Shuo Chen09f09bb2014-03-18 15:37:11 -070065 }
66 else {
weijia yuan82cf9142018-10-21 12:25:02 -070067 response.setCode(403);
68 response.setText("Malformed Command");
69 response.setBody(response.wireEncode());
Shuo Chen09f09bb2014-03-18 15:37:11 -070070 }
weijia yuan82cf9142018-10-21 12:25:02 -070071 return response;
72}
73
74RepoCommandResponse
75DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode, std::string text) const
76{
77 RepoCommandResponse response(statusCode, text);
78 response.setBody(response.wireEncode());
79 return response;
Shuo Chen09f09bb2014-03-18 15:37:11 -070080}
81
82void
weijia yuan82cf9142018-10-21 12:25:02 -070083DeleteHandle::processSingleDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
84 const ndn::mgmt::CommandContinuation& done) const
Shuo Chen09f09bb2014-03-18 15:37:11 -070085{
weijia yuan82cf9142018-10-21 12:25:02 -070086 int64_t nDeletedData = storageHandle.deleteData(parameter.getName());
87 if (nDeletedData == -1) {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080088 NDN_LOG_DEBUG("Deletion Failed");
weijia yuan82cf9142018-10-21 12:25:02 -070089 done(negativeReply(interest, 405, "Deletion Failed"));
Shuo Chen09f09bb2014-03-18 15:37:11 -070090 }
Weiqi Shif0330d52014-07-09 10:54:27 -070091 else
weijia yuan82cf9142018-10-21 12:25:02 -070092 done(positiveReply(interest, parameter, 200, nDeletedData));
Shuo Chen09f09bb2014-03-18 15:37:11 -070093}
94
95void
weijia yuan82cf9142018-10-21 12:25:02 -070096DeleteHandle::processSegmentDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
97 const ndn::mgmt::CommandContinuation& done) const
Shuo Chen09f09bb2014-03-18 15:37:11 -070098{
weijia yuan82cf9142018-10-21 12:25:02 -070099 SegmentNo startBlockId = parameter.hasStartBlockId() ? parameter.getStartBlockId() : 0;
100 SegmentNo endBlockId = parameter.getEndBlockId();
Shuo Chen09f09bb2014-03-18 15:37:11 -0700101
weijia yuan82cf9142018-10-21 12:25:02 -0700102 Name prefix = parameter.getName();
103 uint64_t nDeletedData = 0;
104 for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
105 Name name = prefix;
106 name.appendSegment(i);
107 if (storageHandle.deleteData(name)) {
108 nDeletedData++;
Shuo Chen09f09bb2014-03-18 15:37:11 -0700109 }
weijia yuan82cf9142018-10-21 12:25:02 -0700110 }
111 //All the data deleted, return 200
112 done(positiveReply(interest, parameter, 200, nDeletedData));
Shuo Chen09f09bb2014-03-18 15:37:11 -0700113
Shuo Chen09f09bb2014-03-18 15:37:11 -0700114}
115
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800116} // namespace repo