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