Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "manager-base.hpp" |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 23 | |
| 24 | namespace nlsr { |
| 25 | namespace update { |
| 26 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 27 | INIT_LOGGER(update.PrefixCommandProcessor); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 28 | |
| 29 | ManagerBase::ManagerBase(ndn::mgmt::Dispatcher& dispatcher, |
| 30 | const std::string& module) |
| 31 | : m_dispatcher(dispatcher) |
| 32 | , m_module(module) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | ndn::PartialName |
| 37 | ManagerBase::makeRelPrefix(const std::string& verb) const |
| 38 | { |
| 39 | return ndn::PartialName(m_module).append(verb); |
| 40 | } |
| 41 | |
| 42 | CommandManagerBase::CommandManagerBase(ndn::mgmt::Dispatcher& dispatcher, |
| 43 | NamePrefixList& namePrefixList, |
| 44 | Lsdb& lsdb, |
| 45 | const std::string& module) |
| 46 | : ManagerBase(dispatcher, module) |
| 47 | , m_namePrefixList(namePrefixList) |
| 48 | , m_lsdb(lsdb) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | CommandManagerBase::advertiseAndInsertPrefix(const ndn::Name& prefix, |
| 54 | const ndn::Interest& interest, |
| 55 | const ndn::mgmt::ControlParameters& parameters, |
| 56 | const ndn::mgmt::CommandContinuation& done) |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 57 | { |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 58 | const ndn::nfd::ControlParameters& castParams = |
| 59 | static_cast<const ndn::nfd::ControlParameters&>(parameters); |
| 60 | |
| 61 | // Only build a Name LSA if the added name is new |
| 62 | if (m_namePrefixList.insert(castParams.getName())) { |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 63 | NLSR_LOG_INFO("Advertising name: " << castParams.getName() << "\n"); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 64 | m_lsdb.buildAndInstallOwnNameLsa(); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 65 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 66 | NLSR_LOG_INFO("Saving name to the configuration file "); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 67 | if (afterAdvertise(castParams.getName()) == true) { |
| 68 | return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode())); |
| 69 | } |
| 70 | else { |
| 71 | return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.") |
| 72 | .setBody(parameters.wireEncode())); |
| 73 | } |
| 74 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 75 | return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 76 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 77 | else { |
| 78 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
| 79 | // Save an already advertised prefix |
| 80 | NLSR_LOG_INFO("Saving an already advertised name: " << castParams.getName() << "\n"); |
| 81 | if (afterAdvertise(castParams.getName()) == true) { |
| 82 | return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode())); |
| 83 | } |
| 84 | else { |
| 85 | return done(ndn::nfd::ControlResponse(406, "Prefix is already Saved/Failed to open configuration file.") |
| 86 | .setBody(parameters.wireEncode())); |
| 87 | } |
| 88 | } |
| 89 | return done(ndn::nfd::ControlResponse(204, "Prefix is already advertised/inserted.") |
| 90 | .setBody(parameters.wireEncode())); |
| 91 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
| 95 | CommandManagerBase::withdrawAndRemovePrefix(const ndn::Name& prefix, |
| 96 | const ndn::Interest& interest, |
| 97 | const ndn::mgmt::ControlParameters& parameters, |
| 98 | const ndn::mgmt::CommandContinuation& done) |
| 99 | { |
| 100 | const ndn::nfd::ControlParameters& castParams = |
| 101 | static_cast<const ndn::nfd::ControlParameters&>(parameters); |
| 102 | |
| 103 | // Only build a Name LSA if the added name is new |
| 104 | if (m_namePrefixList.remove(castParams.getName())) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 105 | NLSR_LOG_INFO("Withdrawing/Removing name: " << castParams.getName() << "\n"); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 106 | m_lsdb.buildAndInstallOwnNameLsa(); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 107 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
| 108 | if (afterWithdraw(castParams.getName()) == true) { |
| 109 | return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode())); |
| 110 | } |
| 111 | else { |
| 112 | return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.") |
| 113 | .setBody(parameters.wireEncode())); |
| 114 | } |
| 115 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 116 | return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 117 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 118 | else { |
| 119 | if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) { |
| 120 | // Delete an already withdrawn prefix |
| 121 | NLSR_LOG_INFO("Deleting an already withdrawn name: " << castParams.getName() << "\n"); |
| 122 | if (afterWithdraw(castParams.getName()) == true) { |
| 123 | return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode())); |
| 124 | } |
| 125 | else { |
| 126 | return done(ndn::nfd::ControlResponse(406, "Prefix is already deleted/Failed to open configuration file.") |
| 127 | .setBody(parameters.wireEncode())); |
| 128 | } |
| 129 | } |
| 130 | return done(ndn::nfd::ControlResponse(204, "Prefix is already withdrawn/removed.") |
| 131 | .setBody(parameters.wireEncode())); |
| 132 | } |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } // namespace update |
| 136 | } // namespace nlsr |