blob: d9efe76d16e11a06f4631155cfc76b8aa21c1613 [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 Pesavento11904062022-04-14 22:33:28 -04003 * Copyright (c) 2014-2022, 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,
Davide Pesavento11904062022-04-14 22:33:28 -040030 ndn::security::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
Davide Pesavento11904062022-04-14 22:33:28 -040075DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode,
76 const std::string& text) const
weijia yuan82cf9142018-10-21 12:25:02 -070077{
78 RepoCommandResponse response(statusCode, text);
79 response.setBody(response.wireEncode());
80 return response;
Shuo Chen09f09bb2014-03-18 15:37:11 -070081}
82
83void
weijia yuan82cf9142018-10-21 12:25:02 -070084DeleteHandle::processSingleDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
85 const ndn::mgmt::CommandContinuation& done) const
Shuo Chen09f09bb2014-03-18 15:37:11 -070086{
weijia yuan82cf9142018-10-21 12:25:02 -070087 int64_t nDeletedData = storageHandle.deleteData(parameter.getName());
88 if (nDeletedData == -1) {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080089 NDN_LOG_DEBUG("Deletion Failed");
weijia yuan82cf9142018-10-21 12:25:02 -070090 done(negativeReply(interest, 405, "Deletion Failed"));
Shuo Chen09f09bb2014-03-18 15:37:11 -070091 }
Weiqi Shif0330d52014-07-09 10:54:27 -070092 else
weijia yuan82cf9142018-10-21 12:25:02 -070093 done(positiveReply(interest, parameter, 200, nDeletedData));
Shuo Chen09f09bb2014-03-18 15:37:11 -070094}
95
96void
weijia yuan82cf9142018-10-21 12:25:02 -070097DeleteHandle::processSegmentDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
98 const ndn::mgmt::CommandContinuation& done) const
Shuo Chen09f09bb2014-03-18 15:37:11 -070099{
weijia yuan82cf9142018-10-21 12:25:02 -0700100 SegmentNo startBlockId = parameter.hasStartBlockId() ? parameter.getStartBlockId() : 0;
101 SegmentNo endBlockId = parameter.getEndBlockId();
Shuo Chen09f09bb2014-03-18 15:37:11 -0700102
weijia yuan82cf9142018-10-21 12:25:02 -0700103 Name prefix = parameter.getName();
104 uint64_t nDeletedData = 0;
105 for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
106 Name name = prefix;
107 name.appendSegment(i);
108 if (storageHandle.deleteData(name)) {
109 nDeletedData++;
Shuo Chen09f09bb2014-03-18 15:37:11 -0700110 }
weijia yuan82cf9142018-10-21 12:25:02 -0700111 }
Davide Pesavento11904062022-04-14 22:33:28 -0400112
weijia yuan82cf9142018-10-21 12:25:02 -0700113 //All the data deleted, return 200
114 done(positiveReply(interest, parameter, 200, nDeletedData));
Shuo Chen09f09bb2014-03-18 15:37:11 -0700115}
116
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800117} // namespace repo