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