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 | |
| 22 | #include "manager-base.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 | |
| 25 | namespace nlsr { |
| 26 | namespace update { |
| 27 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 28 | INIT_LOGGER(update.PrefixCommandProcessor); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 29 | |
| 30 | ManagerBase::ManagerBase(ndn::mgmt::Dispatcher& dispatcher, |
| 31 | const std::string& module) |
| 32 | : m_dispatcher(dispatcher) |
| 33 | , m_module(module) |
| 34 | { |
| 35 | } |
| 36 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 37 | CommandManagerBase::CommandManagerBase(ndn::mgmt::Dispatcher& dispatcher, |
| 38 | NamePrefixList& namePrefixList, |
| 39 | Lsdb& lsdb, |
| 40 | const std::string& module) |
| 41 | : ManagerBase(dispatcher, module) |
| 42 | , m_namePrefixList(namePrefixList) |
| 43 | , m_lsdb(lsdb) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void |
Davide Pesavento | 968eb1a | 2025-01-07 00:46:05 -0500 | [diff] [blame^] | 48 | CommandManagerBase::advertiseAndInsertPrefix(const ndn::mgmt::ControlParametersBase& parameters, |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 49 | const ndn::mgmt::CommandContinuation& done) |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 50 | { |
Davide Pesavento | d2610dc | 2025-01-03 13:20:06 -0500 | [diff] [blame] | 51 | const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 52 | |
| 53 | // Only build a Name LSA if the added name is new |
| 54 | if (m_namePrefixList.insert(castParams.getName())) { |
Davide Pesavento | 968eb1a | 2025-01-07 00:46:05 -0500 | [diff] [blame^] | 55 | NLSR_LOG_INFO("Advertising name: " << castParams.getName()); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 56 | m_lsdb.buildAndInstallOwnNameLsa(); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 57 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 58 | NLSR_LOG_INFO("Saving name to the configuration file "); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 59 | if (afterAdvertise(castParams.getName()) == true) { |
| 60 | return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode())); |
| 61 | } |
| 62 | else { |
| 63 | return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.") |
| 64 | .setBody(parameters.wireEncode())); |
| 65 | } |
| 66 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 67 | return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 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()); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 73 | if (afterAdvertise(castParams.getName()) == true) { |
| 74 | return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode())); |
| 75 | } |
| 76 | else { |
| 77 | return done(ndn::nfd::ControlResponse(406, "Prefix is already Saved/Failed to open configuration file.") |
| 78 | .setBody(parameters.wireEncode())); |
| 79 | } |
| 80 | } |
| 81 | return done(ndn::nfd::ControlResponse(204, "Prefix is already advertised/inserted.") |
| 82 | .setBody(parameters.wireEncode())); |
| 83 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void |
Davide Pesavento | 968eb1a | 2025-01-07 00:46:05 -0500 | [diff] [blame^] | 87 | CommandManagerBase::withdrawAndRemovePrefix(const ndn::mgmt::ControlParametersBase& parameters, |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 88 | const ndn::mgmt::CommandContinuation& done) |
| 89 | { |
Davide Pesavento | d2610dc | 2025-01-03 13:20:06 -0500 | [diff] [blame] | 90 | const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 91 | |
| 92 | // Only build a Name LSA if the added name is new |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame] | 93 | if (m_namePrefixList.erase(castParams.getName())) { |
Davide Pesavento | 968eb1a | 2025-01-07 00:46:05 -0500 | [diff] [blame^] | 94 | NLSR_LOG_INFO("Withdrawing/Removing name: " << castParams.getName()); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 95 | m_lsdb.buildAndInstallOwnNameLsa(); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 96 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
| 97 | if (afterWithdraw(castParams.getName()) == true) { |
| 98 | return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode())); |
| 99 | } |
| 100 | else { |
| 101 | return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.") |
| 102 | .setBody(parameters.wireEncode())); |
| 103 | } |
| 104 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 105 | return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 106 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 107 | else { |
| 108 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
| 109 | // Delete an already withdrawn prefix |
Davide Pesavento | 968eb1a | 2025-01-07 00:46:05 -0500 | [diff] [blame^] | 110 | NLSR_LOG_INFO("Deleting an already withdrawn name: " << castParams.getName()); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 111 | if (afterWithdraw(castParams.getName()) == true) { |
| 112 | return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode())); |
| 113 | } |
| 114 | else { |
| 115 | return done(ndn::nfd::ControlResponse(406, "Prefix is already deleted/Failed to open configuration file.") |
| 116 | .setBody(parameters.wireEncode())); |
| 117 | } |
| 118 | } |
| 119 | return done(ndn::nfd::ControlResponse(204, "Prefix is already withdrawn/removed.") |
| 120 | .setBody(parameters.wireEncode())); |
| 121 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | } // namespace update |
| 125 | } // namespace nlsr |