blob: 4d279c7359838264555b940360e82466e92f4e6a [file] [log] [blame]
Laqin Fan54a43f02017-03-08 12:31:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal7526cee2018-01-31 18:14:10 +00003 * Copyright (c) 2014-2019, 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/>.
20 **/
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"
30#include "logger.hpp"
31
32#include <ndn-cxx/face.hpp>
33#include <ndn-cxx/interest.hpp>
34#include <ndn-cxx/mgmt/nfd/control-command.hpp>
35#include <ndn-cxx/mgmt/dispatcher.hpp>
36#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
37#include <ndn-cxx/mgmt/nfd/control-response.hpp>
38
39#include <boost/noncopyable.hpp>
40
41namespace nlsr {
42
43class Lsdb;
44
45namespace update {
46
Saurab Dulal7526cee2018-01-31 18:14:10 +000047enum { PREFIX_FLAG = 1 };
48
Laqin Fan54a43f02017-03-08 12:31:30 -060049class ManagerBase : boost::noncopyable
50{
51public:
52 class Error : public std::runtime_error
53 {
54 public:
55 explicit
56 Error(const std::string& what)
57 : std::runtime_error(what)
58 {
59 }
60 };
61
62public:
63 ManagerBase(ndn::mgmt::Dispatcher& m_dispatcher,
64 const std::string& module);
65
66protected:
67 /*! \brief generate the relative prefix for a handler by appending the verb name to the module name
68 */
69 ndn::PartialName
70 makeRelPrefix(const std::string& verb) const;
71
72PUBLIC_WITH_TESTS_ELSE_PROTECTED:
73 /*! \brief validate the parameters for a given command
74 */
75 template<typename T>
76 bool
77 validateParameters(const ndn::mgmt::ControlParameters& parameters)
78 {
79 const ndn::nfd::ControlParameters* castParams =
80 dynamic_cast<const ndn::nfd::ControlParameters*>(&parameters);
81
82 BOOST_ASSERT(castParams != nullptr);
83 T command;
84 try {
85 command.validateRequest(*castParams);
86 }
87 catch (const ndn::nfd::ControlCommand::ArgumentError& ae) {
88 throw ae;
89 }
90 catch (const std::exception& e) {
91 std::cerr << e.what() << std::endl;
92 return false;
93 }
94 return true;
95 }
96
97protected:
98 ndn::mgmt::Dispatcher& m_dispatcher;
99
100private:
101 std::string m_module;
102};
103
104class CommandManagerBase: public ManagerBase
105{
106public:
107 CommandManagerBase(ndn::mgmt::Dispatcher& m_dispatcher,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500108 NamePrefixList& m_namePrefixList,
109 Lsdb& lsdb,
110 const std::string& module);
Laqin Fan54a43f02017-03-08 12:31:30 -0600111
Saurab Dulal7526cee2018-01-31 18:14:10 +0000112 virtual ~CommandManagerBase() {}
113
Laqin Fan54a43f02017-03-08 12:31:30 -0600114 /*! \brief add desired name prefix to the advertised name prefix list
115 * or insert a prefix into the FIB if parameters is valid.
116 */
117 void
118 advertiseAndInsertPrefix(const ndn::Name& prefix,
119 const ndn::Interest& interest,
120 const ndn::mgmt::ControlParameters& parameters,
121 const ndn::mgmt::CommandContinuation& done);
122
123 /*! \brief remove desired name prefix from the advertised name prefix list
124 * or remove a prefix from the FIB if parameters is valid.
125 */
126 void
127 withdrawAndRemovePrefix(const ndn::Name& prefix,
128 const ndn::Interest& interest,
129 const ndn::mgmt::ControlParameters& parameters,
130 const ndn::mgmt::CommandContinuation& done);
131
Saurab Dulal7526cee2018-01-31 18:14:10 +0000132 /*! \brief save an advertised prefix to the nlsr configuration file
133 * returns bool from the overridden function while nullopt here
134 */
135 virtual ndn::optional<bool>
136 afterAdvertise(const ndn::Name& prefix) { return ndn::nullopt; }
137
138 /*! \brief save an advertised prefix to the nlsr configuration file
139 * returns bool from the overridden function while nullopt here
140 */
141 virtual ndn::optional<bool>
142 afterWithdraw(const ndn::Name& prefix) { return ndn::nullopt; }
143
Laqin Fan54a43f02017-03-08 12:31:30 -0600144protected:
145 NamePrefixList& m_namePrefixList;
146 Lsdb& m_lsdb;
147};
148
149} // namespace update
150} // namespace nlsr
151
152#endif // NLSR_MANAGER_BASE_HPP