Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | d2610dc | 2025-01-03 13:20:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2025, The University of Memphis, |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 20 | */ |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 21 | |
Davide Pesavento | 20e60a2 | 2025-01-07 01:50:10 -0500 | [diff] [blame] | 22 | #include "command-processor.hpp" |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 23 | #include "logger.hpp" |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 24 | |
Davide Pesavento | 20e60a2 | 2025-01-07 01:50:10 -0500 | [diff] [blame] | 25 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 26 | |
Davide Pesavento | 20e60a2 | 2025-01-07 01:50:10 -0500 | [diff] [blame] | 27 | namespace nlsr::update { |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 28 | |
Davide Pesavento | 20e60a2 | 2025-01-07 01:50:10 -0500 | [diff] [blame] | 29 | INIT_LOGGER(update.CommandProcessor); |
| 30 | |
| 31 | CommandProcessor::CommandProcessor(ndn::mgmt::Dispatcher& dispatcher, |
| 32 | NamePrefixList& namePrefixList, |
| 33 | Lsdb& lsdb) |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 34 | : m_dispatcher(dispatcher) |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 35 | , m_namePrefixList(namePrefixList) |
| 36 | , m_lsdb(lsdb) |
| 37 | { |
| 38 | } |
| 39 | |
Davide Pesavento | 20e60a2 | 2025-01-07 01:50:10 -0500 | [diff] [blame] | 40 | CommandProcessor::~CommandProcessor() = default; |
| 41 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 42 | void |
Davide Pesavento | 20e60a2 | 2025-01-07 01:50:10 -0500 | [diff] [blame] | 43 | CommandProcessor::advertiseAndInsertPrefix(const ndn::mgmt::ControlParametersBase& parameters, |
| 44 | const ndn::mgmt::CommandContinuation& done) |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 45 | { |
Davide Pesavento | d2610dc | 2025-01-03 13:20:06 -0500 | [diff] [blame] | 46 | const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters); |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 47 | // This is a bit of a hack, waiting on the work in #5348 to complete for full refactoring |
| 48 | ndn::nfd::ControlParameters responseParams(castParams.wireEncode()); |
| 49 | uint64_t responseFaceId = (castParams.hasFaceId() && castParams.getFaceId() > 0) |
| 50 | ? responseParams.getFaceId() : m_defaultResponseFaceId; |
| 51 | responseParams.setFaceId(responseFaceId); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 52 | // Only build a Name LSA if the added name is new |
| 53 | if (m_namePrefixList.insert(castParams.getName())) { |
Davide Pesavento | 968eb1a | 2025-01-07 00:46:05 -0500 | [diff] [blame] | 54 | NLSR_LOG_INFO("Advertising name: " << castParams.getName()); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 55 | m_lsdb.buildAndInstallOwnNameLsa(); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 56 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 57 | NLSR_LOG_INFO("Saving name to the configuration file "); |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 58 | auto [afterAdvertiseReturn, afterAdvertiseMessage] = afterAdvertise(castParams.getName()); |
| 59 | if (afterAdvertiseReturn) { |
| 60 | return done(ndn::nfd::ControlResponse(205, afterAdvertiseMessage).setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 61 | } |
| 62 | else { |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 63 | return done(ndn::nfd::ControlResponse(500, afterAdvertiseMessage) |
| 64 | .setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 67 | return done(ndn::nfd::ControlResponse(200, "OK").setBody(responseParams.wireEncode())); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 68 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 69 | else { |
| 70 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
| 71 | // Save an already advertised prefix |
Davide Pesavento | 968eb1a | 2025-01-07 00:46:05 -0500 | [diff] [blame] | 72 | NLSR_LOG_INFO("Saving an already advertised name: " << castParams.getName()); |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 73 | auto [afterAdvertiseReturn, afterAdvertiseMessage] = afterAdvertise(castParams.getName()); |
| 74 | if (afterAdvertiseReturn) { |
| 75 | return done(ndn::nfd::ControlResponse(205, afterAdvertiseMessage).setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 76 | } |
| 77 | else { |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 78 | return done(ndn::nfd::ControlResponse(500, afterAdvertiseMessage) |
| 79 | .setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | return done(ndn::nfd::ControlResponse(204, "Prefix is already advertised/inserted.") |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 83 | .setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 84 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Davide Pesavento | 20e60a2 | 2025-01-07 01:50:10 -0500 | [diff] [blame] | 88 | CommandProcessor::withdrawAndRemovePrefix(const ndn::mgmt::ControlParametersBase& parameters, |
| 89 | const ndn::mgmt::CommandContinuation& done) |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 90 | { |
Davide Pesavento | d2610dc | 2025-01-03 13:20:06 -0500 | [diff] [blame] | 91 | const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters); |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 92 | // This is a bit of a hack, waiting on the work in #5348 to complete for full refactoring |
| 93 | ndn::nfd::ControlParameters responseParams(castParams.wireEncode()); |
| 94 | uint64_t responseFaceId = (castParams.hasFaceId() && castParams.getFaceId() > 0) |
| 95 | ? responseParams.getFaceId() : m_defaultResponseFaceId; |
| 96 | responseParams.setFaceId(responseFaceId); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 97 | // Only build a Name LSA if the added name is new |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 98 | if (m_namePrefixList.erase(castParams.getName())) { |
Davide Pesavento | 968eb1a | 2025-01-07 00:46:05 -0500 | [diff] [blame] | 99 | NLSR_LOG_INFO("Withdrawing/Removing name: " << castParams.getName()); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 100 | m_lsdb.buildAndInstallOwnNameLsa(); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 101 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 102 | auto [afterWithdrawReturn, afterWithdrawMessage] = afterWithdraw(castParams.getName()); |
| 103 | if (afterWithdrawReturn) { |
| 104 | return done(ndn::nfd::ControlResponse(205, afterWithdrawMessage).setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 105 | } |
| 106 | else { |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 107 | return done(ndn::nfd::ControlResponse(500, afterWithdrawMessage) |
| 108 | .setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 111 | return done(ndn::nfd::ControlResponse(200, "OK").setBody(responseParams.wireEncode())); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 112 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 113 | else { |
| 114 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
| 115 | // Delete an already withdrawn prefix |
Davide Pesavento | 968eb1a | 2025-01-07 00:46:05 -0500 | [diff] [blame] | 116 | NLSR_LOG_INFO("Deleting an already withdrawn name: " << castParams.getName()); |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 117 | auto [afterWithdrawReturn, afterWithdrawMessage] = afterWithdraw(castParams.getName()); |
| 118 | if (afterWithdrawReturn) { |
| 119 | return done(ndn::nfd::ControlResponse(205, afterWithdrawMessage).setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 120 | } |
| 121 | else { |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 122 | return done(ndn::nfd::ControlResponse(500, afterWithdrawMessage) |
| 123 | .setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | return done(ndn::nfd::ControlResponse(204, "Prefix is already withdrawn/removed.") |
awlane | 697a0ad | 2025-04-21 13:09:17 -0500 | [diff] [blame^] | 127 | .setBody(responseParams.wireEncode())); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 128 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 129 | } |
| 130 | |
Davide Pesavento | 20e60a2 | 2025-01-07 01:50:10 -0500 | [diff] [blame] | 131 | } // namespace nlsr::update |