manager-base.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2025, The University of Memphis,
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 #ifndef NLSR_MANAGER_BASE_HPP
23 #define NLSR_MANAGER_BASE_HPP
24 
25 #include "lsdb.hpp"
26 #include "name-prefix-list.hpp"
27 #include "test-access-control.hpp"
28 
29 #include <ndn-cxx/face.hpp>
30 #include <ndn-cxx/interest.hpp>
31 #include <ndn-cxx/mgmt/dispatcher.hpp>
32 #include <ndn-cxx/mgmt/nfd/control-command.hpp>
33 #include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
34 #include <ndn-cxx/mgmt/nfd/control-response.hpp>
35 
36 #include <boost/noncopyable.hpp>
37 #include <iostream>
38 #include <optional>
39 
40 namespace nlsr {
41 
42 class Lsdb;
43 
44 namespace update {
45 
46 enum { PREFIX_FLAG = 1 };
47 
48 class ManagerBase : boost::noncopyable
49 {
50 public:
51  class Error : public std::runtime_error
52  {
53  public:
54  using std::runtime_error::runtime_error;
55  };
56 
57 protected:
58  ManagerBase(ndn::mgmt::Dispatcher& m_dispatcher, const std::string& module);
59 
62  ndn::PartialName
63  makeRelPrefix(const std::string& verb) const;
64 
68  template<typename Command>
69  static bool
70  validateParameters(const ndn::mgmt::ControlParametersBase& parameters)
71  {
72  try {
73  Command::validateRequest(dynamic_cast<const ndn::nfd::ControlParameters&>(parameters));
74  }
75  catch (const ndn::nfd::ArgumentError&) {
76  throw;
77  }
78  catch (const std::exception& e) {
79  std::cerr << e.what() << std::endl;
80  return false;
81  }
82  return true;
83  }
84 
85 protected:
86  ndn::mgmt::Dispatcher& m_dispatcher;
87 
88 private:
89  std::string m_module;
90 };
91 
93 {
94 public:
95  CommandManagerBase(ndn::mgmt::Dispatcher& m_dispatcher,
97  Lsdb& lsdb,
98  const std::string& module);
99 
100  virtual
101  ~CommandManagerBase() = default;
102 
106  void
107  advertiseAndInsertPrefix(const ndn::Name& prefix,
108  const ndn::Interest& interest,
109  const ndn::mgmt::ControlParametersBase& parameters,
110  const ndn::mgmt::CommandContinuation& done);
111 
115  void
116  withdrawAndRemovePrefix(const ndn::Name& prefix,
117  const ndn::Interest& interest,
118  const ndn::mgmt::ControlParametersBase& parameters,
119  const ndn::mgmt::CommandContinuation& done);
120 
124  virtual std::optional<bool>
125  afterAdvertise(const ndn::Name& prefix)
126  {
127  return std::nullopt;
128  }
129 
133  virtual std::optional<bool>
134  afterWithdraw(const ndn::Name& prefix)
135  {
136  return std::nullopt;
137  }
138 
139 protected:
142 };
143 
144 } // namespace update
145 } // namespace nlsr
146 
147 #endif // NLSR_MANAGER_BASE_HPP
virtual std::optional< bool > afterWithdraw(const ndn::Name &prefix)
save an advertised prefix to the nlsr configuration file returns bool from the overridden function wh...
void withdrawAndRemovePrefix(const ndn::Name &prefix, const ndn::Interest &interest, const ndn::mgmt::ControlParametersBase &parameters, const ndn::mgmt::CommandContinuation &done)
remove desired name prefix from the advertised name prefix list or remove a prefix from the FIB if pa...
virtual ~CommandManagerBase()=default
void advertiseAndInsertPrefix(const ndn::Name &prefix, const ndn::Interest &interest, const ndn::mgmt::ControlParametersBase &parameters, const ndn::mgmt::CommandContinuation &done)
add desired name prefix to the advertised name prefix list or insert a prefix into the FIB if paramet...
virtual std::optional< bool > afterAdvertise(const ndn::Name &prefix)
save an advertised prefix to the nlsr configuration file returns bool from the overridden function wh...
CommandManagerBase(ndn::mgmt::Dispatcher &m_dispatcher, NamePrefixList &m_namePrefixList, Lsdb &lsdb, const std::string &module)
ManagerBase(ndn::mgmt::Dispatcher &m_dispatcher, const std::string &module)
ndn::PartialName makeRelPrefix(const std::string &verb) const
Generate the relative prefix for a handler by appending the verb name to the module name.
ndn::mgmt::Dispatcher & m_dispatcher
static bool validateParameters(const ndn::mgmt::ControlParametersBase &parameters)
Validate the parameters for a given command.
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
#define PUBLIC_WITH_TESTS_ELSE_PROTECTED