blob: 239d3496c728eed20d8adfd8f7d66fd3ee2572c2 [file] [log] [blame]
Nick Gordon4d2c6c02017-01-20 13:18:46 -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 UPDATE_NFD_RIB_COMMAND_PROCESSOR_HPP
23#define UPDATE_NFD_RIB_COMMAND_PROCESSOR_HPP
24
25#include "route/fib.hpp"
26#include "nfd-rib-commands.hpp"
27#include "name-prefix-list.hpp"
28#include "lsdb.hpp"
Nick Gordon4d2c6c02017-01-20 13:18:46 -060029
30#include <ndn-cxx/mgmt/nfd/control-command.hpp>
31#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Ashlesh Gawande6fa00d92017-05-19 19:31:44 -050032#include <ndn-cxx/mgmt/dispatcher.hpp>
Nick Gordon4d2c6c02017-01-20 13:18:46 -060033#include <ndn-cxx/encoding/tlv-nfd.hpp>
34
35#include <boost/noncopyable.hpp>
36#include <typeinfo>
37
38namespace nlsr {
39namespace update {
40
41class NfdRibCommandProcessor : boost::noncopyable
42{
43public:
44 class Error : public std::runtime_error
45 {
46 public:
47 explicit
48 Error(const std::string& what)
49 : std::runtime_error(what)
50 {
51 }
52 };
53
54public:
55 NfdRibCommandProcessor(ndn::mgmt::Dispatcher& dispatcher,
56 NamePrefixList& namePrefixes,
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050057 Lsdb& lsdb);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060058
59 /*! \brief Registers an Interest filter with face
60
61 Registers with face an Interest filter that reacts to any
62 interests on COMMAND_PREFIX.
63 */
64 void
65 startListening();
66
67PUBLIC_WITH_TESTS_ELSE_PRIVATE:
68
69 /*! \brief inserts a prefix into the FIB if parameters is valid.
70
71 We consider parameters to be valid if origin is set to CLIENT and
72 a name is present.
73 */
74 void
75 insertPrefix(const ndn::mgmt::ControlParameters& parameters);
76
77 /*! \brief remove a prefix from the FIB if parameters is valid.
78
79 We consider parameters to be valid if origin is set to CLIENT and
80 a name is present.
81 */
82 void
83 removePrefix(const ndn::mgmt::ControlParameters& parameters);
84
85 /*! \brief uses command's validator to check that parameters is valid.
86
87 The command's validator's behavior is defined per command.
88 */
89 template<typename T>
90 bool
91 validateParameters(const ndn::mgmt::ControlParameters& parameters)
92 {
93 BOOST_ASSERT(dynamic_cast<const ndn::nfd::ControlParameters*>(&parameters) != nullptr);
94 const ndn::nfd::ControlParameters& castParams =
95 static_cast<const ndn::nfd::ControlParameters&>(parameters);
96 T command;
97 try {
98 command.validateRequest(castParams);
99 }
100 catch (const ndn::nfd::ControlCommand::ArgumentError& ae) {
101 throw ae;
102 }
103 catch (...) {
104 return false;
105 }
106 return true;
107 }
108
109private:
110 template<typename T>
111 bool
112 validate(const ndn::mgmt::ControlParameters& parameters, const ndn::PartialName& command);
113
114 ndn::mgmt::Dispatcher& m_dispatcher;
115 NamePrefixList& m_namePrefixList;
116 Lsdb& m_lsdb;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600117};
118
119} // namespace update
120} // namespace nlsr
121
122#endif // UPDATE_NFD_RIB_COMMAND_PROCESSOR_HPP