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 | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -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 | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 20 | */ |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 21 | |
| 22 | #ifndef NLSR_MANAGER_BASE_HPP |
| 23 | #define NLSR_MANAGER_BASE_HPP |
| 24 | |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame^] | 25 | #include "lsdb.hpp" |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 26 | #include "name-prefix-list.hpp" |
| 27 | #include "test-access-control.hpp" |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 28 | |
| 29 | #include <ndn-cxx/face.hpp> |
| 30 | #include <ndn-cxx/interest.hpp> |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 31 | #include <ndn-cxx/mgmt/dispatcher.hpp> |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame^] | 32 | #include <ndn-cxx/mgmt/nfd/control-command.hpp> |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 33 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
| 34 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
| 35 | |
| 36 | #include <boost/noncopyable.hpp> |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 37 | #include <iostream> |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 38 | #include <optional> |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 39 | |
| 40 | namespace nlsr { |
| 41 | |
| 42 | class Lsdb; |
| 43 | |
| 44 | namespace update { |
| 45 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 46 | enum { PREFIX_FLAG = 1 }; |
| 47 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 48 | class ManagerBase : boost::noncopyable |
| 49 | { |
| 50 | public: |
| 51 | class Error : public std::runtime_error |
| 52 | { |
| 53 | public: |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 54 | using std::runtime_error::runtime_error; |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 55 | }; |
| 56 | |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame^] | 57 | protected: |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 58 | ManagerBase(ndn::mgmt::Dispatcher& m_dispatcher, const std::string& module); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 59 | |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame^] | 60 | /*! \brief Generate the relative prefix for a handler by appending the verb name to the module name. |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 61 | */ |
| 62 | ndn::PartialName |
| 63 | makeRelPrefix(const std::string& verb) const; |
| 64 | |
| 65 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame^] | 66 | /*! \brief Validate the parameters for a given command. |
| 67 | */ |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 68 | template<typename T> |
Davide Pesavento | 384327d | 2025-01-02 01:40:23 -0500 | [diff] [blame^] | 69 | static bool |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 70 | validateParameters(const ndn::mgmt::ControlParameters& parameters) |
| 71 | { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 72 | const auto* castParams = dynamic_cast<const ndn::nfd::ControlParameters*>(¶meters); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 73 | BOOST_ASSERT(castParams != nullptr); |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 74 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 75 | T command; |
| 76 | try { |
| 77 | command.validateRequest(*castParams); |
| 78 | } |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 79 | catch (const ndn::nfd::ControlCommand::ArgumentError&) { |
| 80 | throw; |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 81 | } |
| 82 | catch (const std::exception& e) { |
| 83 | std::cerr << e.what() << std::endl; |
| 84 | return false; |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | protected: |
| 90 | ndn::mgmt::Dispatcher& m_dispatcher; |
| 91 | |
| 92 | private: |
| 93 | std::string m_module; |
| 94 | }; |
| 95 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 96 | class CommandManagerBase : public ManagerBase |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 97 | { |
| 98 | public: |
| 99 | CommandManagerBase(ndn::mgmt::Dispatcher& m_dispatcher, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 100 | NamePrefixList& m_namePrefixList, |
| 101 | Lsdb& lsdb, |
| 102 | const std::string& module); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 103 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 104 | virtual |
| 105 | ~CommandManagerBase() = default; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 106 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 107 | /*! \brief add desired name prefix to the advertised name prefix list |
| 108 | * or insert a prefix into the FIB if parameters is valid. |
| 109 | */ |
| 110 | void |
| 111 | advertiseAndInsertPrefix(const ndn::Name& prefix, |
| 112 | const ndn::Interest& interest, |
| 113 | const ndn::mgmt::ControlParameters& parameters, |
| 114 | const ndn::mgmt::CommandContinuation& done); |
| 115 | |
| 116 | /*! \brief remove desired name prefix from the advertised name prefix list |
| 117 | * or remove a prefix from the FIB if parameters is valid. |
| 118 | */ |
| 119 | void |
| 120 | withdrawAndRemovePrefix(const ndn::Name& prefix, |
| 121 | const ndn::Interest& interest, |
| 122 | const ndn::mgmt::ControlParameters& parameters, |
| 123 | const ndn::mgmt::CommandContinuation& done); |
| 124 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 125 | /*! \brief save an advertised prefix to the nlsr configuration file |
| 126 | * returns bool from the overridden function while nullopt here |
| 127 | */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 128 | virtual std::optional<bool> |
| 129 | afterAdvertise(const ndn::Name& prefix) |
| 130 | { |
| 131 | return std::nullopt; |
| 132 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 133 | |
| 134 | /*! \brief save an advertised prefix to the nlsr configuration file |
| 135 | * returns bool from the overridden function while nullopt here |
| 136 | */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 137 | virtual std::optional<bool> |
| 138 | afterWithdraw(const ndn::Name& prefix) |
| 139 | { |
| 140 | return std::nullopt; |
| 141 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 142 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 143 | protected: |
| 144 | NamePrefixList& m_namePrefixList; |
| 145 | Lsdb& m_lsdb; |
| 146 | }; |
| 147 | |
| 148 | } // namespace update |
| 149 | } // namespace nlsr |
| 150 | |
| 151 | #endif // NLSR_MANAGER_BASE_HPP |