blob: 12b6f3cb9976f5f585fcc76ff660b51b9699eefa [file] [log] [blame]
Laqin Fan54a43f02017-03-08 12:31:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventod90338d2021-01-07 17:50:05 -05002/*
Davide Pesavento384327d2025-01-02 01:40:23 -05003 * Copyright (c) 2014-2025, The University of Memphis,
Laqin Fan54a43f02017-03-08 12:31:30 -06004 * 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 Pesaventod90338d2021-01-07 17:50:05 -050020 */
Laqin Fan54a43f02017-03-08 12:31:30 -060021
22#ifndef NLSR_MANAGER_BASE_HPP
23#define NLSR_MANAGER_BASE_HPP
24
Davide Pesavento384327d2025-01-02 01:40:23 -050025#include "lsdb.hpp"
Laqin Fan54a43f02017-03-08 12:31:30 -060026#include "name-prefix-list.hpp"
27#include "test-access-control.hpp"
Laqin Fan54a43f02017-03-08 12:31:30 -060028
29#include <ndn-cxx/face.hpp>
30#include <ndn-cxx/interest.hpp>
Laqin Fan54a43f02017-03-08 12:31:30 -060031#include <ndn-cxx/mgmt/dispatcher.hpp>
Davide Pesavento384327d2025-01-02 01:40:23 -050032#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Laqin Fan54a43f02017-03-08 12:31:30 -060033#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
34#include <ndn-cxx/mgmt/nfd/control-response.hpp>
35
36#include <boost/noncopyable.hpp>
Davide Pesaventod90338d2021-01-07 17:50:05 -050037#include <iostream>
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040038#include <optional>
Laqin Fan54a43f02017-03-08 12:31:30 -060039
40namespace nlsr {
41
42class Lsdb;
43
44namespace update {
45
Saurab Dulal7526cee2018-01-31 18:14:10 +000046enum { PREFIX_FLAG = 1 };
47
Laqin Fan54a43f02017-03-08 12:31:30 -060048class ManagerBase : boost::noncopyable
49{
50public:
51 class Error : public std::runtime_error
52 {
53 public:
Davide Pesaventod90338d2021-01-07 17:50:05 -050054 using std::runtime_error::runtime_error;
Laqin Fan54a43f02017-03-08 12:31:30 -060055 };
56
Davide Pesavento384327d2025-01-02 01:40:23 -050057protected:
Davide Pesaventod90338d2021-01-07 17:50:05 -050058 ManagerBase(ndn::mgmt::Dispatcher& m_dispatcher, const std::string& module);
Laqin Fan54a43f02017-03-08 12:31:30 -060059
Davide Pesavento384327d2025-01-02 01:40:23 -050060 /*! \brief Generate the relative prefix for a handler by appending the verb name to the module name.
Laqin Fan54a43f02017-03-08 12:31:30 -060061 */
62 ndn::PartialName
63 makeRelPrefix(const std::string& verb) const;
64
65PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Davide Pesavento384327d2025-01-02 01:40:23 -050066 /*! \brief Validate the parameters for a given command.
67 */
Davide Pesaventod2610dc2025-01-03 13:20:06 -050068 template<typename Command>
Davide Pesavento384327d2025-01-02 01:40:23 -050069 static bool
Davide Pesaventod2610dc2025-01-03 13:20:06 -050070 validateParameters(const ndn::mgmt::ControlParametersBase& parameters)
Laqin Fan54a43f02017-03-08 12:31:30 -060071 {
Laqin Fan54a43f02017-03-08 12:31:30 -060072 try {
Davide Pesaventod2610dc2025-01-03 13:20:06 -050073 Command::validateRequest(dynamic_cast<const ndn::nfd::ControlParameters&>(parameters));
Laqin Fan54a43f02017-03-08 12:31:30 -060074 }
Davide Pesaventod2610dc2025-01-03 13:20:06 -050075 catch (const ndn::nfd::ArgumentError&) {
Davide Pesaventod90338d2021-01-07 17:50:05 -050076 throw;
Laqin Fan54a43f02017-03-08 12:31:30 -060077 }
78 catch (const std::exception& e) {
79 std::cerr << e.what() << std::endl;
80 return false;
81 }
82 return true;
83 }
84
85protected:
86 ndn::mgmt::Dispatcher& m_dispatcher;
87
88private:
89 std::string m_module;
90};
91
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040092class CommandManagerBase : public ManagerBase
Laqin Fan54a43f02017-03-08 12:31:30 -060093{
94public:
95 CommandManagerBase(ndn::mgmt::Dispatcher& m_dispatcher,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050096 NamePrefixList& m_namePrefixList,
97 Lsdb& lsdb,
98 const std::string& module);
Laqin Fan54a43f02017-03-08 12:31:30 -060099
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400100 virtual
101 ~CommandManagerBase() = default;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000102
Laqin Fan54a43f02017-03-08 12:31:30 -0600103 /*! \brief add desired name prefix to the advertised name prefix list
104 * or insert a prefix into the FIB if parameters is valid.
105 */
106 void
107 advertiseAndInsertPrefix(const ndn::Name& prefix,
108 const ndn::Interest& interest,
Davide Pesaventod2610dc2025-01-03 13:20:06 -0500109 const ndn::mgmt::ControlParametersBase& parameters,
Laqin Fan54a43f02017-03-08 12:31:30 -0600110 const ndn::mgmt::CommandContinuation& done);
111
112 /*! \brief remove desired name prefix from the advertised name prefix list
113 * or remove a prefix from the FIB if parameters is valid.
114 */
115 void
116 withdrawAndRemovePrefix(const ndn::Name& prefix,
117 const ndn::Interest& interest,
Davide Pesaventod2610dc2025-01-03 13:20:06 -0500118 const ndn::mgmt::ControlParametersBase& parameters,
Laqin Fan54a43f02017-03-08 12:31:30 -0600119 const ndn::mgmt::CommandContinuation& done);
120
Saurab Dulal7526cee2018-01-31 18:14:10 +0000121 /*! \brief save an advertised prefix to the nlsr configuration file
122 * returns bool from the overridden function while nullopt here
123 */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400124 virtual std::optional<bool>
125 afterAdvertise(const ndn::Name& prefix)
126 {
127 return std::nullopt;
128 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000129
130 /*! \brief save an advertised prefix to the nlsr configuration file
131 * returns bool from the overridden function while nullopt here
132 */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400133 virtual std::optional<bool>
134 afterWithdraw(const ndn::Name& prefix)
135 {
136 return std::nullopt;
137 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000138
Laqin Fan54a43f02017-03-08 12:31:30 -0600139protected:
140 NamePrefixList& m_namePrefixList;
141 Lsdb& m_lsdb;
142};
143
144} // namespace update
145} // namespace nlsr
146
147#endif // NLSR_MANAGER_BASE_HPP