blob: 8d9cba64ba1d06ad927cbacdeca5cd93ea001a68 [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"
29#include "communication/sync-logic-handler.hpp"
30
31#include <ndn-cxx/mgmt/nfd/control-command.hpp>
32#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
33#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,
57 Lsdb& lsdb,
58 SyncLogicHandler& sync);
59
60 /*! \brief Registers an Interest filter with face
61
62 Registers with face an Interest filter that reacts to any
63 interests on COMMAND_PREFIX.
64 */
65 void
66 startListening();
67
68PUBLIC_WITH_TESTS_ELSE_PRIVATE:
69
70 /*! \brief inserts a prefix into the FIB if parameters is valid.
71
72 We consider parameters to be valid if origin is set to CLIENT and
73 a name is present.
74 */
75 void
76 insertPrefix(const ndn::mgmt::ControlParameters& parameters);
77
78 /*! \brief remove a prefix from the FIB if parameters is valid.
79
80 We consider parameters to be valid if origin is set to CLIENT and
81 a name is present.
82 */
83 void
84 removePrefix(const ndn::mgmt::ControlParameters& parameters);
85
86 /*! \brief uses command's validator to check that parameters is valid.
87
88 The command's validator's behavior is defined per command.
89 */
90 template<typename T>
91 bool
92 validateParameters(const ndn::mgmt::ControlParameters& parameters)
93 {
94 BOOST_ASSERT(dynamic_cast<const ndn::nfd::ControlParameters*>(&parameters) != nullptr);
95 const ndn::nfd::ControlParameters& castParams =
96 static_cast<const ndn::nfd::ControlParameters&>(parameters);
97 T command;
98 try {
99 command.validateRequest(castParams);
100 }
101 catch (const ndn::nfd::ControlCommand::ArgumentError& ae) {
102 throw ae;
103 }
104 catch (...) {
105 return false;
106 }
107 return true;
108 }
109
110private:
111 template<typename T>
112 bool
113 validate(const ndn::mgmt::ControlParameters& parameters, const ndn::PartialName& command);
114
115 ndn::mgmt::Dispatcher& m_dispatcher;
116 NamePrefixList& m_namePrefixList;
117 Lsdb& m_lsdb;
118 SyncLogicHandler& m_sync;
119};
120
121} // namespace update
122} // namespace nlsr
123
124#endif // UPDATE_NFD_RIB_COMMAND_PROCESSOR_HPP