blob: 33df589f98c201f64672b672940a8ef639bd2180 [file] [log] [blame]
Laqin Fan54a43f02017-03-08 12:31:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, 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 "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
47class ManagerBase : boost::noncopyable
48{
49public:
50 class Error : public std::runtime_error
51 {
52 public:
53 explicit
54 Error(const std::string& what)
55 : std::runtime_error(what)
56 {
57 }
58 };
59
60public:
61 ManagerBase(ndn::mgmt::Dispatcher& m_dispatcher,
62 const std::string& module);
63
64protected:
65 /*! \brief generate the relative prefix for a handler by appending the verb name to the module name
66 */
67 ndn::PartialName
68 makeRelPrefix(const std::string& verb) const;
69
70PUBLIC_WITH_TESTS_ELSE_PROTECTED:
71 /*! \brief validate the parameters for a given command
72 */
73 template<typename T>
74 bool
75 validateParameters(const ndn::mgmt::ControlParameters& parameters)
76 {
77 const ndn::nfd::ControlParameters* castParams =
78 dynamic_cast<const ndn::nfd::ControlParameters*>(&parameters);
79
80 BOOST_ASSERT(castParams != nullptr);
81 T command;
82 try {
83 command.validateRequest(*castParams);
84 }
85 catch (const ndn::nfd::ControlCommand::ArgumentError& ae) {
86 throw ae;
87 }
88 catch (const std::exception& e) {
89 std::cerr << e.what() << std::endl;
90 return false;
91 }
92 return true;
93 }
94
95protected:
96 ndn::mgmt::Dispatcher& m_dispatcher;
97
98private:
99 std::string m_module;
100};
101
102class CommandManagerBase: public ManagerBase
103{
104public:
105 CommandManagerBase(ndn::mgmt::Dispatcher& m_dispatcher,
106 NamePrefixList& m_namePrefixList,
107 Lsdb& lsdb,
108 const std::string& module);
109
110 /*! \brief add desired name prefix to the advertised name prefix list
111 * or insert a prefix into the FIB if parameters is valid.
112 */
113 void
114 advertiseAndInsertPrefix(const ndn::Name& prefix,
115 const ndn::Interest& interest,
116 const ndn::mgmt::ControlParameters& parameters,
117 const ndn::mgmt::CommandContinuation& done);
118
119 /*! \brief remove desired name prefix from the advertised name prefix list
120 * or remove a prefix from the FIB if parameters is valid.
121 */
122 void
123 withdrawAndRemovePrefix(const ndn::Name& prefix,
124 const ndn::Interest& interest,
125 const ndn::mgmt::ControlParameters& parameters,
126 const ndn::mgmt::CommandContinuation& done);
127
128protected:
129 NamePrefixList& m_namePrefixList;
130 Lsdb& m_lsdb;
131};
132
133} // namespace update
134} // namespace nlsr
135
136#endif // NLSR_MANAGER_BASE_HPP